]> Untitled Git - lemmy.git/blob - crates/db_queries/src/lib.rs
Merge pull request #1785 from LemmyNet/add_cardano_donation_link
[lemmy.git] / crates / db_queries / src / lib.rs
1 #[macro_use]
2 extern crate diesel;
3 #[macro_use]
4 extern crate strum_macros;
5 #[macro_use]
6 extern crate lazy_static;
7 // this is used in tests
8 #[allow(unused_imports)]
9 #[macro_use]
10 extern crate diesel_migrations;
11
12 #[cfg(test)]
13 extern crate serial_test;
14
15 use diesel::{result::Error, *};
16 use lemmy_db_schema::{CommunityId, DbUrl, PersonId};
17 use lemmy_utils::ApiError;
18 use regex::Regex;
19 use serde::{Deserialize, Serialize};
20 use std::{env, env::VarError};
21 use url::Url;
22
23 pub mod aggregates;
24 pub mod source;
25
26 pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
27
28 pub trait Crud {
29   type Form;
30   type IdType;
31   fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
32   where
33     Self: Sized;
34   fn read(conn: &PgConnection, id: Self::IdType) -> Result<Self, Error>
35   where
36     Self: Sized;
37   fn update(conn: &PgConnection, id: Self::IdType, form: &Self::Form) -> Result<Self, Error>
38   where
39     Self: Sized;
40   fn delete(_conn: &PgConnection, _id: Self::IdType) -> Result<usize, Error>
41   where
42     Self: Sized,
43   {
44     unimplemented!()
45   }
46 }
47
48 pub trait Followable {
49   type Form;
50   fn follow(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
51   where
52     Self: Sized;
53   fn follow_accepted(
54     conn: &PgConnection,
55     community_id: CommunityId,
56     person_id: PersonId,
57   ) -> Result<Self, Error>
58   where
59     Self: Sized;
60   fn unfollow(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
61   where
62     Self: Sized;
63   fn has_local_followers(conn: &PgConnection, community_id: CommunityId) -> Result<bool, Error>;
64 }
65
66 pub trait Joinable {
67   type Form;
68   fn join(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
69   where
70     Self: Sized;
71   fn leave(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
72   where
73     Self: Sized;
74 }
75
76 pub trait Likeable {
77   type Form;
78   type IdType;
79   fn like(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
80   where
81     Self: Sized;
82   fn remove(
83     conn: &PgConnection,
84     person_id: PersonId,
85     item_id: Self::IdType,
86   ) -> Result<usize, Error>
87   where
88     Self: Sized;
89 }
90
91 pub trait Bannable {
92   type Form;
93   fn ban(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
94   where
95     Self: Sized;
96   fn unban(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
97   where
98     Self: Sized;
99 }
100
101 pub trait Saveable {
102   type Form;
103   fn save(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
104   where
105     Self: Sized;
106   fn unsave(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
107   where
108     Self: Sized;
109 }
110
111 pub trait Blockable {
112   type Form;
113   fn block(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
114   where
115     Self: Sized;
116   fn unblock(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
117   where
118     Self: Sized;
119 }
120
121 pub trait Readable {
122   type Form;
123   fn mark_as_read(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
124   where
125     Self: Sized;
126   fn mark_as_unread(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
127   where
128     Self: Sized;
129 }
130
131 pub trait Reportable {
132   type Form;
133   fn report(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
134   where
135     Self: Sized;
136   fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
137   where
138     Self: Sized;
139   fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
140   where
141     Self: Sized;
142 }
143
144 pub trait DeleteableOrRemoveable {
145   fn blank_out_deleted_or_removed_info(self) -> Self;
146 }
147
148 pub trait ApubObject {
149   type Form;
150   fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
151   where
152     Self: Sized;
153   fn upsert(conn: &PgConnection, user_form: &Self::Form) -> Result<Self, Error>
154   where
155     Self: Sized;
156 }
157
158 pub trait MaybeOptional<T> {
159   fn get_optional(self) -> Option<T>;
160 }
161
162 impl<T> MaybeOptional<T> for T {
163   fn get_optional(self) -> Option<T> {
164     Some(self)
165   }
166 }
167
168 impl<T> MaybeOptional<T> for Option<T> {
169   fn get_optional(self) -> Option<T> {
170     self
171   }
172 }
173
174 pub trait ToSafe {
175   type SafeColumns;
176   fn safe_columns_tuple() -> Self::SafeColumns;
177 }
178
179 pub trait ToSafeSettings {
180   type SafeSettingsColumns;
181   fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
182 }
183
184 pub trait ViewToVec {
185   type DbTuple;
186   fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
187   where
188     Self: Sized;
189 }
190
191 pub fn get_database_url_from_env() -> Result<String, VarError> {
192   env::var("LEMMY_DATABASE_URL")
193 }
194
195 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
196 pub enum SortType {
197   Active,
198   Hot,
199   New,
200   TopDay,
201   TopWeek,
202   TopMonth,
203   TopYear,
204   TopAll,
205   MostComments,
206   NewComments,
207 }
208
209 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
210 pub enum ListingType {
211   All,
212   Local,
213   Subscribed,
214   Community,
215 }
216
217 #[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)]
218 pub enum SearchType {
219   All,
220   Comments,
221   Posts,
222   Communities,
223   Users,
224   Url,
225 }
226
227 pub fn from_opt_str_to_opt_enum<T: std::str::FromStr>(opt: &Option<String>) -> Option<T> {
228   opt.as_ref().map(|t| T::from_str(t).ok()).flatten()
229 }
230
231 pub fn fuzzy_search(q: &str) -> String {
232   let replaced = q.replace(" ", "%");
233   format!("%{}%", replaced)
234 }
235
236 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
237   let page = page.unwrap_or(1);
238   let limit = limit.unwrap_or(10);
239   let offset = limit * (page - 1);
240   (limit, offset)
241 }
242
243 pub fn is_email_regex(test: &str) -> bool {
244   EMAIL_REGEX.is_match(test)
245 }
246
247 pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
248   match opt {
249     // An empty string is an erase
250     Some(unwrapped) => {
251       if !unwrapped.eq("") {
252         Some(Some(unwrapped.to_owned()))
253       } else {
254         Some(None)
255       }
256     }
257     None => None,
258   }
259 }
260
261 pub fn diesel_option_overwrite_to_url(
262   opt: &Option<String>,
263 ) -> Result<Option<Option<DbUrl>>, ApiError> {
264   match opt.as_ref().map(|s| s.as_str()) {
265     // An empty string is an erase
266     Some("") => Ok(Some(None)),
267     Some(str_url) => match Url::parse(str_url) {
268       Ok(url) => Ok(Some(Some(url.into()))),
269       Err(_) => Err(ApiError::err("invalid_url")),
270     },
271     None => Ok(None),
272   }
273 }
274
275 embed_migrations!();
276
277 pub fn establish_unpooled_connection() -> PgConnection {
278   let db_url = match get_database_url_from_env() {
279     Ok(url) => url,
280     Err(e) => panic!(
281       "Failed to read database URL from env var LEMMY_DATABASE_URL: {}",
282       e
283     ),
284   };
285   let conn =
286     PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
287   embedded_migrations::run(&conn).expect("load migrations");
288   conn
289 }
290
291 lazy_static! {
292   static ref EMAIL_REGEX: Regex =
293     Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
294       .expect("compile email regex");
295 }
296
297 pub mod functions {
298   use diesel::sql_types::*;
299
300   sql_function! {
301     fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
302   }
303 }
304
305 #[cfg(test)]
306 mod tests {
307   use super::{fuzzy_search, *};
308   use crate::is_email_regex;
309
310   #[test]
311   fn test_fuzzy_search() {
312     let test = "This is a fuzzy search";
313     assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
314   }
315
316   #[test]
317   fn test_email() {
318     assert!(is_email_regex("gush@gmail.com"));
319     assert!(!is_email_regex("nada_neutho"));
320   }
321
322   #[test]
323   fn test_diesel_option_overwrite() {
324     assert_eq!(diesel_option_overwrite(&None), None);
325     assert_eq!(diesel_option_overwrite(&Some("".to_string())), Some(None));
326     assert_eq!(
327       diesel_option_overwrite(&Some("test".to_string())),
328       Some(Some("test".to_string()))
329     );
330   }
331
332   #[test]
333   fn test_diesel_option_overwrite_to_url() {
334     assert!(matches!(diesel_option_overwrite_to_url(&None), Ok(None)));
335     assert!(matches!(
336       diesel_option_overwrite_to_url(&Some("".to_string())),
337       Ok(Some(None))
338     ));
339     assert!(matches!(
340       diesel_option_overwrite_to_url(&Some("invalid_url".to_string())),
341       Err(_)
342     ));
343     let example_url = "https://example.com";
344     assert!(matches!(
345       diesel_option_overwrite_to_url(&Some(example_url.to_string())),
346       Ok(Some(Some(url))) if url == Url::parse(example_url).unwrap().into()
347     ));
348   }
349 }