torrust_index/models/
response.rs

1use serde::{Deserialize, Serialize};
2use url::Url;
3
4use super::category::Category;
5use super::torrent::TorrentId;
6use crate::databases::database::Category as DatabaseCategory;
7use crate::models::torrent::TorrentListing;
8use crate::models::torrent_file::TorrentFile;
9use crate::models::torrent_tag::TorrentTag;
10use crate::services::torrent::CanonicalInfoHashGroup;
11
12pub enum OkResponses {
13    TokenResponse(TokenResponse),
14}
15
16#[allow(clippy::module_name_repetitions)]
17#[derive(Serialize, Deserialize, Debug)]
18pub struct OkResponse<T> {
19    pub data: T,
20}
21
22#[allow(clippy::module_name_repetitions)]
23#[derive(Serialize, Deserialize, Debug)]
24pub struct ErrorResponse<T> {
25    pub errors: Vec<T>,
26}
27
28#[allow(clippy::module_name_repetitions)]
29#[derive(Serialize, Deserialize, Debug)]
30pub struct TokenResponse {
31    pub token: String,
32    pub username: String,
33    pub admin: bool,
34}
35
36#[allow(clippy::module_name_repetitions)]
37#[derive(Serialize, Deserialize, Debug)]
38pub struct NewTorrentResponse {
39    pub torrent_id: TorrentId,
40    pub info_hash: String,
41}
42
43#[allow(clippy::module_name_repetitions)]
44#[derive(Serialize, Deserialize, Debug)]
45pub struct DeletedTorrentResponse {
46    pub torrent_id: TorrentId,
47    pub info_hash: String,
48}
49
50#[allow(clippy::module_name_repetitions)]
51#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
52pub struct TorrentResponse {
53    pub torrent_id: i64,
54    pub uploader: String,
55    pub info_hash: String,
56    pub title: String,
57    pub description: Option<String>,
58    pub category: Option<Category>,
59    pub upload_date: String,
60    pub file_size: i64,
61    pub seeders: i64,
62    pub leechers: i64,
63    pub files: Vec<TorrentFile>,
64    pub trackers: Vec<String>,
65    pub magnet_link: String,
66    pub tags: Vec<TorrentTag>,
67    pub name: String,
68    pub comment: Option<String>,
69    pub creation_date: Option<i64>,
70    pub created_by: Option<String>,
71    pub encoding: Option<String>,
72    pub canonical_info_hash_group: Vec<String>,
73}
74
75impl TorrentResponse {
76    #[must_use]
77    pub fn from_listing(
78        torrent_listing: TorrentListing,
79        category: Option<DatabaseCategory>,
80        canonical_info_hash_group: &CanonicalInfoHashGroup,
81    ) -> TorrentResponse {
82        TorrentResponse {
83            torrent_id: torrent_listing.torrent_id,
84            uploader: torrent_listing.uploader,
85            info_hash: torrent_listing.info_hash,
86            title: torrent_listing.title,
87            description: torrent_listing.description,
88            category: category.map(std::convert::Into::into),
89            upload_date: torrent_listing.date_uploaded,
90            file_size: torrent_listing.file_size,
91            seeders: torrent_listing.seeders,
92            leechers: torrent_listing.leechers,
93            files: vec![],
94            trackers: vec![],
95            magnet_link: String::new(),
96            tags: vec![],
97            name: torrent_listing.name,
98            comment: torrent_listing.comment,
99            creation_date: torrent_listing.creation_date,
100            created_by: torrent_listing.created_by,
101            encoding: torrent_listing.encoding,
102            canonical_info_hash_group: canonical_info_hash_group
103                .original_info_hashes
104                .iter()
105                .map(super::info_hash::InfoHash::to_hex_string)
106                .collect(),
107        }
108    }
109
110    /// It adds the tracker URL in the first position of the tracker list.
111    pub fn include_url_as_main_tracker(&mut self, tracker_url: &Url) {
112        // Remove any existing instances of tracker_url
113        self.trackers.retain(|tracker| *tracker != tracker_url.to_string());
114
115        // Insert tracker_url at the first position
116        self.trackers.insert(0, tracker_url.to_owned().to_string());
117    }
118}
119
120#[allow(clippy::module_name_repetitions)]
121#[derive(Serialize, Deserialize, Debug, sqlx::FromRow)]
122pub struct TorrentsResponse {
123    pub total: u32,
124    pub results: Vec<TorrentListing>,
125}