]> Untitled Git - lemmy.git/blob - crates/db_schema/src/lib.rs
User / community blocking. Fixes #426 (#1604)
[lemmy.git] / crates / db_schema / src / lib.rs
1 #[macro_use]
2 extern crate diesel;
3
4 #[macro_use]
5 extern crate diesel_derive_newtype;
6
7 use chrono::NaiveDateTime;
8 use diesel::{
9   backend::Backend,
10   deserialize::FromSql,
11   serialize::{Output, ToSql},
12   sql_types::Text,
13 };
14 use serde::{Deserialize, Serialize};
15 use std::{
16   fmt,
17   fmt::{Display, Formatter},
18   io::Write,
19 };
20 use url::Url;
21
22 pub mod schema;
23 pub mod source;
24
25 #[derive(
26   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
27 )]
28 pub struct PostId(pub i32);
29
30 impl fmt::Display for PostId {
31   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32     write!(f, "{}", self.0)
33   }
34 }
35
36 #[derive(
37   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
38 )]
39 pub struct PersonId(pub i32);
40
41 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
42 pub struct CommentId(pub i32);
43
44 impl fmt::Display for CommentId {
45   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46     write!(f, "{}", self.0)
47   }
48 }
49
50 #[derive(
51   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
52 )]
53 pub struct CommunityId(pub i32);
54
55 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
56 pub struct LocalUserId(pub i32);
57
58 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
59 pub struct PrivateMessageId(i32);
60
61 impl fmt::Display for PrivateMessageId {
62   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63     write!(f, "{}", self.0)
64   }
65 }
66
67 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
68 pub struct PersonMentionId(i32);
69
70 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
71 pub struct PersonBlockId(i32);
72
73 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
74 pub struct CommunityBlockId(i32);
75
76 #[repr(transparent)]
77 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
78 #[sql_type = "Text"]
79 pub struct DbUrl(Url);
80
81 impl<DB: Backend> ToSql<Text, DB> for DbUrl
82 where
83   String: ToSql<Text, DB>,
84 {
85   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
86     self.0.to_string().to_sql(out)
87   }
88 }
89
90 impl<DB: Backend> FromSql<Text, DB> for DbUrl
91 where
92   String: FromSql<Text, DB>,
93 {
94   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
95     let str = String::from_sql(bytes)?;
96     Ok(DbUrl(Url::parse(&str)?))
97   }
98 }
99
100 impl DbUrl {
101   // TODO: remove this method and just use into()
102   pub fn into_inner(self) -> Url {
103     self.0
104   }
105 }
106
107 impl Display for DbUrl {
108   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
109     self.to_owned().0.fmt(f)
110   }
111 }
112
113 impl From<DbUrl> for Url {
114   fn from(url: DbUrl) -> Self {
115     url.0
116   }
117 }
118
119 impl From<Url> for DbUrl {
120   fn from(url: Url) -> Self {
121     DbUrl(url)
122   }
123 }
124
125 // TODO: can probably move this back to lemmy_db_queries
126 pub fn naive_now() -> NaiveDateTime {
127   chrono::prelude::Utc::now().naive_utc()
128 }