Skip to content

Commit

Permalink
Merge #87: Restore broken behavior: after uploading a new torrent its…
Browse files Browse the repository at this point in the history
… tracker stats must be updated

3c47ffc fix: [#84] SQL query to get list of torrents without stats (Jose Celano)
c291557 fix: [#84] restore behavior. Update torrent stats after upload (Jose Celano)

Pull request description:

  When a new torrent is uploaded, we must update the tracker torrent stats in the `torrust_torrent_tracker_stats` table.

  It worked from the UI but not for the [DB migration script](#77) because the frontend makes a request to get the torrent info after the upload, and that endpoint updates the torrent tracker stats.

  It must update the stats even if that endpoint is not called after uploading a new torrent.

  ### Tasks

  - [x] After uploading a new torrent, update its tracker stats.
  - [x] Change SQL query for torrent list result if we allow torrents without stats.

  ### Potencial refactors

  - Extract `TrackerApiClient` from `TrackerService`
  - Rename `TrackerService::get_torrent_info` to `TrackerService::update_torrent_tracker_stats`

ACKs for top commit:
  da2ce7:
    ACK 3c47ffc

Tree-SHA512: 34f1d8843b86b49336f27a9f78f71ed980c4a747b59b1a2507efd77c5eb59bf27da3e73c57b23e43910de95c7799d5815658be80f49cae3b6630a24c41294f25
  • Loading branch information
da2ce7 committed Nov 30, 2022
2 parents 59b1ea8 + 3c47ffc commit 3e7feb1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl Database for MysqlDatabase {
INNER JOIN torrust_torrent_info ti ON tt.torrent_id = ti.torrent_id
LEFT JOIN torrust_torrent_tracker_stats ts ON tt.torrent_id = ts.torrent_id
WHERE title LIKE ?
GROUP BY torrent_id",
GROUP BY tt.torrent_id",
category_filter_query
);

Expand Down
2 changes: 1 addition & 1 deletion src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl Database for SqliteDatabase {
INNER JOIN torrust_torrent_info ti ON tt.torrent_id = ti.torrent_id
LEFT JOIN torrust_torrent_tracker_stats ts ON tt.torrent_id = ts.torrent_id
WHERE title LIKE ?
GROUP BY ts.torrent_id",
GROUP BY tt.torrent_id",
category_filter_query
);

Expand Down
6 changes: 6 additions & 0 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ pub async fn upload_torrent(req: HttpRequest, payload: Multipart, app_data: WebA
)
.await?;

// update torrent tracker stats
let _ = app_data
.tracker
.update_torrent_tracker_stats(torrent_id, &torrent_request.torrent.info_hash())
.await;

// whitelist info hash on tracker
if let Err(e) = app_data
.tracker
Expand Down
10 changes: 8 additions & 2 deletions src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,19 @@ impl TrackerService {
}

pub async fn update_torrents(&self) -> Result<(), ServiceError> {
println!("Updating torrents..");
println!("Updating torrents ...");
let torrents = self.database.get_all_torrents_compact().await?;

for torrent in torrents {
let _ = self.get_torrent_info(torrent.torrent_id, &torrent.info_hash).await;
let _ = self
.update_torrent_tracker_stats(torrent.torrent_id, &torrent.info_hash)
.await;
}

Ok(())
}

pub async fn update_torrent_tracker_stats(&self, torrent_id: i64, info_hash: &str) -> Result<TorrentInfo, ServiceError> {
self.get_torrent_info(torrent_id, info_hash).await
}
}

0 comments on commit 3e7feb1

Please sign in to comment.