1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use async_trait::async_trait;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

use crate::databases::mysql::Mysql;
use crate::databases::sqlite::Sqlite;
use crate::models::info_hash::InfoHash;
use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentInfo, Torrent, TorrentFile};
use crate::models::tracker_key::TrackerKey;
use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};

/// Database drivers.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub enum Driver {
    Sqlite3,
    Mysql,
}

/// Compact representation of torrent.
#[derive(Debug, Serialize, sqlx::FromRow)]
pub struct TorrentCompact {
    pub torrent_id: i64,
    pub info_hash: String,
}

/// Torrent category.
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct Category {
    pub category_id: i64,
    pub name: String,
    pub num_torrents: i64,
}

/// Sorting options for torrents.
#[derive(Clone, Copy, Debug, Deserialize)]
pub enum Sorting {
    UploadedAsc,
    UploadedDesc,
    SeedersAsc,
    SeedersDesc,
    LeechersAsc,
    LeechersDesc,
    NameAsc,
    NameDesc,
    SizeAsc,
    SizeDesc,
}

/// Database errors.
#[derive(Debug)]
pub enum Error {
    Error,
    UnrecognizedDatabaseDriver, // when the db path does not start with sqlite or mysql
    UsernameTaken,
    EmailTaken,
    UserNotFound,
    CategoryAlreadyExists,
    CategoryNotFound,
    TorrentNotFound,
    TorrentAlreadyExists, // when uploading an already uploaded info_hash
    TorrentTitleAlreadyExists,
}

/// Get the Driver of the Database from the Connection String
///
/// # Errors
///
/// This function will return an `Error::UnrecognizedDatabaseDriver` if unable to match database type.
pub fn get_driver(db_path: &str) -> Result<Driver, Error> {
    match &db_path.chars().collect::<Vec<char>>() as &[char] {
        ['s', 'q', 'l', 'i', 't', 'e', ..] => Ok(Driver::Sqlite3),
        ['m', 'y', 's', 'q', 'l', ..] => Ok(Driver::Mysql),
        _ => Err(Error::UnrecognizedDatabaseDriver),
    }
}

/// Connect to a database.
///
/// # Errors
///
/// This function will return an `Error::UnrecognizedDatabaseDriver` if unable to match database type.
pub async fn connect(db_path: &str) -> Result<Box<dyn Database>, Error> {
    let db_driver = self::get_driver(db_path)?;

    Ok(match db_driver {
        self::Driver::Sqlite3 => Box::new(Sqlite::new(db_path).await),
        self::Driver::Mysql => Box::new(Mysql::new(db_path).await),
    })
}

/// Trait for database implementations.
#[async_trait]
pub trait Database: Sync + Send {
    /// Return current database driver.
    fn get_database_driver(&self) -> Driver;

    async fn new(db_path: &str) -> Self
    where
        Self: Sized;

    /// Add new user and return the newly inserted `user_id`.
    async fn insert_user_and_get_id(&self, username: &str, email: &str, password: &str) -> Result<i64, Error>;

    /// Get `User` from `user_id`.
    async fn get_user_from_id(&self, user_id: i64) -> Result<User, Error>;

    /// Get `UserAuthentication` from `user_id`.
    async fn get_user_authentication_from_id(&self, user_id: i64) -> Result<UserAuthentication, Error>;

    /// Get `UserProfile` from `username`.
    async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, Error>;

    /// Get `UserCompact` from `user_id`.
    async fn get_user_compact_from_id(&self, user_id: i64) -> Result<UserCompact, Error>;

    /// Get a user's `TrackerKey`.
    async fn get_user_tracker_key(&self, user_id: i64) -> Option<TrackerKey>;

    /// Get total user count.
    async fn count_users(&self) -> Result<i64, Error>;

    /// Ban user with `user_id`, `reason` and `date_expiry`.
    async fn ban_user(&self, user_id: i64, reason: &str, date_expiry: NaiveDateTime) -> Result<(), Error>;

    /// Grant a user the administrator role.
    async fn grant_admin_role(&self, user_id: i64) -> Result<(), Error>;

    /// Verify a user's email with `user_id`.
    async fn verify_email(&self, user_id: i64) -> Result<(), Error>;

    /// Link a `TrackerKey` to a certain user with `user_id`.
    async fn add_tracker_key(&self, user_id: i64, tracker_key: &TrackerKey) -> Result<(), Error>;

    /// Delete user and all related user data with `user_id`.
    async fn delete_user(&self, user_id: i64) -> Result<(), Error>;

    /// Add a new category and return `category_id`.
    async fn insert_category_and_get_id(&self, category_name: &str) -> Result<i64, Error>;

    /// Get `Category` from `category_id`.
    async fn get_category_from_id(&self, category_id: i64) -> Result<Category, Error>;

    /// Get `Category` from `category_name`.
    async fn get_category_from_name(&self, category_name: &str) -> Result<Category, Error>;

    /// Get all categories as `Vec<Category>`.
    async fn get_categories(&self) -> Result<Vec<Category>, Error>;

    /// Delete category with `category_name`.
    async fn delete_category(&self, category_name: &str) -> Result<(), Error>;

    /// Get results of a torrent search in a paginated and sorted form as `TorrentsResponse` from `search`, `categories`, `sort`, `offset` and `page_size`.
    async fn get_torrents_search_sorted_paginated(
        &self,
        search: &Option<String>,
        categories: &Option<Vec<String>>,
        sort: &Sorting,
        offset: u64,
        page_size: u8,
    ) -> Result<TorrentsResponse, Error>;

    /// Add new torrent and return the newly inserted `torrent_id` with `torrent`, `uploader_id`, `category_id`, `title` and `description`.
    async fn insert_torrent_and_get_id(
        &self,
        torrent: &Torrent,
        uploader_id: i64,
        category_id: i64,
        title: &str,
        description: &str,
    ) -> Result<i64, Error>;

    /// Get `Torrent` from `InfoHash`.
    async fn get_torrent_from_info_hash(&self, info_hash: &InfoHash) -> Result<Torrent, Error> {
        let torrent_info = self.get_torrent_info_from_info_hash(info_hash).await?;

        let torrent_files = self.get_torrent_files_from_id(torrent_info.torrent_id).await?;

        let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_info.torrent_id).await?;

        Ok(Torrent::from_db_info_files_and_announce_urls(
            torrent_info,
            torrent_files,
            torrent_announce_urls,
        ))
    }

    /// Get `Torrent` from `torrent_id`.
    async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, Error> {
        let torrent_info = self.get_torrent_info_from_id(torrent_id).await?;

        let torrent_files = self.get_torrent_files_from_id(torrent_id).await?;

        let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_id).await?;

        Ok(Torrent::from_db_info_files_and_announce_urls(
            torrent_info,
            torrent_files,
            torrent_announce_urls,
        ))
    }

    /// Get torrent's info as `DbTorrentInfo` from `torrent_id`.
    async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, Error>;

    /// Get torrent's info as `DbTorrentInfo` from torrent `InfoHash`.
    async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result<DbTorrentInfo, Error>;

    /// Get all torrent's files as `Vec<TorrentFile>` from `torrent_id`.
    async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, Error>;

    /// Get all torrent's announce urls as `Vec<Vec<String>>` from `torrent_id`.
    async fn get_torrent_announce_urls_from_id(&self, torrent_id: i64) -> Result<Vec<Vec<String>>, Error>;

    /// Get `TorrentListing` from `torrent_id`.
    async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, Error>;

    /// Get `TorrentListing` from `InfoHash`.
    async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result<TorrentListing, Error>;

    /// Get all torrents as `Vec<TorrentCompact>`.
    async fn get_all_torrents_compact(&self) -> Result<Vec<TorrentCompact>, Error>;

    /// Update a torrent's title with `torrent_id` and `title`.
    async fn update_torrent_title(&self, torrent_id: i64, title: &str) -> Result<(), Error>;

    /// Update a torrent's description with `torrent_id` and `description`.
    async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), Error>;

    /// Update the seeders and leechers info for a torrent with `torrent_id`, `tracker_url`, `seeders` and `leechers`.
    async fn update_tracker_info(&self, torrent_id: i64, tracker_url: &str, seeders: i64, leechers: i64) -> Result<(), Error>;

    /// Delete a torrent with `torrent_id`.
    async fn delete_torrent(&self, torrent_id: i64) -> Result<(), Error>;

    /// DELETES ALL DATABASE ROWS, ONLY CALL THIS IF YOU KNOW WHAT YOU'RE DOING!
    async fn delete_all_database_rows(&self) -> Result<(), Error>;
}