From c302735cc76757fe089424a5d973356348671cc9 Mon Sep 17 00:00:00 2001 From: MMelchor Date: Thu, 28 Sep 2023 00:01:20 +0200 Subject: [PATCH] feat: [#303] store in the database the torrent fields creation_date created_by encoding API responses now contain the three field in the - Torrent details endpoint - Torrent list endpoint --- ...ust_add_creation_date_field_to_torrent.sql | 1 + ...1551_torrust_add_created_by_to_torrent.sql | 1 + ...211559_torrust_add_encoding_to_torrent.sql | 1 + ...ust_add_creation_date_field_to_torrent.sql | 1 + ...1551_torrust_add_created_by_to_torrent.sql | 1 + ...211559_torrust_add_encoding_to_torrent.sql | 1 + src/databases/mysql.rs | 16 ++++++++++++++-- src/databases/sqlite.rs | 19 +++++++++++++++++-- src/models/response.rs | 6 ++++++ src/models/torrent.rs | 3 +++ src/models/torrent_file.rs | 9 ++++++--- src/services/torrent_file.rs | 12 +++++++++--- tests/common/contexts/torrent/file.rs | 3 ++- tests/common/contexts/torrent/responses.rs | 6 ++++++ .../web/api/v1/contexts/torrent/contract.rs | 3 +++ 15 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 migrations/mysql/20230921211523_torrust_add_creation_date_field_to_torrent.sql create mode 100644 migrations/mysql/20230921211551_torrust_add_created_by_to_torrent.sql create mode 100644 migrations/mysql/20230921211559_torrust_add_encoding_to_torrent.sql create mode 100644 migrations/sqlite3/20230921211523_torrust_add_creation_date_field_to_torrent.sql create mode 100644 migrations/sqlite3/20230921211551_torrust_add_created_by_to_torrent.sql create mode 100644 migrations/sqlite3/20230921211559_torrust_add_encoding_to_torrent.sql diff --git a/migrations/mysql/20230921211523_torrust_add_creation_date_field_to_torrent.sql b/migrations/mysql/20230921211523_torrust_add_creation_date_field_to_torrent.sql new file mode 100644 index 00000000..adc688e7 --- /dev/null +++ b/migrations/mysql/20230921211523_torrust_add_creation_date_field_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents ADD COLUMN creation_date BIGINT UNSIGNED NULL; diff --git a/migrations/mysql/20230921211551_torrust_add_created_by_to_torrent.sql b/migrations/mysql/20230921211551_torrust_add_created_by_to_torrent.sql new file mode 100644 index 00000000..33cb06d9 --- /dev/null +++ b/migrations/mysql/20230921211551_torrust_add_created_by_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents ADD COLUMN created_by TEXT NULL; diff --git a/migrations/mysql/20230921211559_torrust_add_encoding_to_torrent.sql b/migrations/mysql/20230921211559_torrust_add_encoding_to_torrent.sql new file mode 100644 index 00000000..2250b31c --- /dev/null +++ b/migrations/mysql/20230921211559_torrust_add_encoding_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents ADD COLUMN "encoding" TEXT NULL; diff --git a/migrations/sqlite3/20230921211523_torrust_add_creation_date_field_to_torrent.sql b/migrations/sqlite3/20230921211523_torrust_add_creation_date_field_to_torrent.sql new file mode 100644 index 00000000..d413f4b0 --- /dev/null +++ b/migrations/sqlite3/20230921211523_torrust_add_creation_date_field_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE "torrust_torrents" ADD COLUMN "creation_date" BIGINT NULL; diff --git a/migrations/sqlite3/20230921211551_torrust_add_created_by_to_torrent.sql b/migrations/sqlite3/20230921211551_torrust_add_created_by_to_torrent.sql new file mode 100644 index 00000000..8f34bce2 --- /dev/null +++ b/migrations/sqlite3/20230921211551_torrust_add_created_by_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE "torrust_torrents" ADD COLUMN "created_by" TEXT NULL; diff --git a/migrations/sqlite3/20230921211559_torrust_add_encoding_to_torrent.sql b/migrations/sqlite3/20230921211559_torrust_add_encoding_to_torrent.sql new file mode 100644 index 00000000..1bebfd91 --- /dev/null +++ b/migrations/sqlite3/20230921211559_torrust_add_encoding_to_torrent.sql @@ -0,0 +1 @@ +ALTER TABLE "torrust_torrents" ADD COLUMN "encoding" TEXT NULL; \ No newline at end of file diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index b57ad077..417b0153 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -387,6 +387,9 @@ impl Database for Mysql { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt @@ -465,8 +468,11 @@ impl Database for Mysql { root_hash, `source`, comment, - date_uploaded - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())", + date_uploaded, + creation_date, + created_by, + encoding + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP(), ?, ?, ?)", ) .bind(uploader_id) .bind(metadata.category_id) @@ -754,6 +760,9 @@ impl Database for Mysql { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt @@ -782,6 +791,9 @@ impl Database for Mysql { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 8e12f9be..99a2e1ce 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -377,6 +377,9 @@ impl Database for Sqlite { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt @@ -455,8 +458,11 @@ impl Database for Sqlite { root_hash, `source`, comment, - date_uploaded - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')))", + date_uploaded, + creation_date, + created_by, + encoding + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')), ?, ?, ?)", ) .bind(uploader_id) .bind(metadata.category_id) @@ -469,6 +475,9 @@ impl Database for Sqlite { .bind(root_hash) .bind(torrent.info.source.clone()) .bind(torrent.comment.clone()) + .bind(torrent.creation_date.clone()) + .bind(torrent.created_by.clone()) + .bind(torrent.encoding.clone()) .execute(&mut tx) .await .map(|v| v.last_insert_rowid()) @@ -743,6 +752,9 @@ impl Database for Sqlite { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt @@ -770,6 +782,9 @@ impl Database for Sqlite { tt.size AS file_size, tt.name, tt.comment, + tt.creation_date, + tt.created_by, + tt.encoding, CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders, CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers FROM torrust_torrents tt diff --git a/src/models/response.rs b/src/models/response.rs index 7d408b79..e82acb45 100644 --- a/src/models/response.rs +++ b/src/models/response.rs @@ -63,6 +63,9 @@ pub struct TorrentResponse { pub tags: Vec, pub name: String, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } impl TorrentResponse { @@ -85,6 +88,9 @@ impl TorrentResponse { tags: vec![], name: torrent_listing.name, comment: torrent_listing.comment, + creation_date: torrent_listing.creation_date, + created_by: torrent_listing.created_by, + encoding: torrent_listing.encoding, } } } diff --git a/src/models/torrent.rs b/src/models/torrent.rs index 1c2d10cc..40a3f48d 100644 --- a/src/models/torrent.rs +++ b/src/models/torrent.rs @@ -25,6 +25,9 @@ pub struct TorrentListing { pub leechers: i64, pub name: String, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } #[derive(Debug, Display, PartialEq, Eq, Error)] diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 98e57b92..ca6978fb 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -91,12 +91,12 @@ impl Torrent { info: info_dict, announce: None, nodes: None, - encoding: None, + encoding: db_torrent.encoding.clone(), httpseeds: None, announce_list: Some(torrent_announce_urls), - creation_date: None, + creation_date: db_torrent.creation_date.clone(), comment: db_torrent.comment.clone(), - created_by: None, + created_by: db_torrent.created_by.clone(), } } @@ -306,6 +306,9 @@ pub struct DbTorrent { pub private: Option, pub root_hash: i64, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } #[allow(clippy::module_name_repetitions)] diff --git a/src/services/torrent_file.rs b/src/services/torrent_file.rs index 338ba6e6..6c0be1cb 100644 --- a/src/services/torrent_file.rs +++ b/src/services/torrent_file.rs @@ -19,6 +19,9 @@ pub struct CreateTorrentRequest { // Other fields of the root level metainfo dictionary pub announce_urls: Vec>, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } impl CreateTorrentRequest { @@ -35,12 +38,12 @@ impl CreateTorrentRequest { info: info_dict, announce: None, nodes: None, - encoding: None, + encoding: self.encoding.clone(), httpseeds: None, announce_list: Some(self.announce_urls.clone()), - creation_date: None, + creation_date: self.creation_date.clone(), comment: self.comment.clone(), - created_by: None, + created_by: self.created_by.clone(), } } @@ -93,6 +96,9 @@ pub fn generate_random_torrent(id: Uuid) -> Torrent { files: torrent_files, announce_urls: torrent_announce_urls, comment: None, + creation_date: None, + created_by: None, + encoding: None, }; create_torrent_req.build_torrent() diff --git a/tests/common/contexts/torrent/file.rs b/tests/common/contexts/torrent/file.rs index b5f58339..5d8a541a 100644 --- a/tests/common/contexts/torrent/file.rs +++ b/tests/common/contexts/torrent/file.rs @@ -12,8 +12,9 @@ use which::which; pub struct TorrentFileInfo { pub name: String, pub comment: Option, - pub creation_date: Option, + pub creation_date: Option, pub created_by: Option, + pub encoding: Option, pub source: Option, pub info_hash: String, pub torrent_size: u64, diff --git a/tests/common/contexts/torrent/responses.rs b/tests/common/contexts/torrent/responses.rs index f95d67ce..929d31f9 100644 --- a/tests/common/contexts/torrent/responses.rs +++ b/tests/common/contexts/torrent/responses.rs @@ -41,6 +41,9 @@ pub struct ListItem { pub leechers: i64, pub name: String, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } #[derive(Deserialize, PartialEq, Debug)] @@ -66,6 +69,9 @@ pub struct TorrentDetails { pub tags: Vec, pub name: String, pub comment: Option, + pub creation_date: Option, + pub created_by: Option, + pub encoding: Option, } #[derive(Deserialize, PartialEq, Debug)] diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index 601e0478..5bed0730 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -213,6 +213,9 @@ mod for_guests { tags: vec![], name: test_torrent.index_info.name.clone(), comment: test_torrent.file_info.comment.clone(), + creation_date: test_torrent.file_info.creation_date.clone(), + created_by: test_torrent.file_info.created_by.clone(), + encoding: test_torrent.file_info.encoding.clone(), }; assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);