]> Untitled Git - lemmy.git/blob - crates/db_schema/src/traits.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / traits.rs
1 use crate::newtypes::{CommunityId, DbUrl, PersonId};
2 use diesel::{result::Error, PgConnection};
3
4 pub trait Crud {
5   type Form;
6   type IdType;
7   fn create(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
8   where
9     Self: Sized;
10   fn read(conn: &mut PgConnection, id: Self::IdType) -> Result<Self, Error>
11   where
12     Self: Sized;
13   fn update(conn: &mut PgConnection, id: Self::IdType, form: &Self::Form) -> Result<Self, Error>
14   where
15     Self: Sized;
16   fn delete(_conn: &mut PgConnection, _id: Self::IdType) -> Result<usize, Error>
17   where
18     Self: Sized,
19   {
20     unimplemented!()
21   }
22 }
23
24 pub trait Followable {
25   type Form;
26   fn follow(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
27   where
28     Self: Sized;
29   fn follow_accepted(
30     conn: &mut PgConnection,
31     community_id: CommunityId,
32     person_id: PersonId,
33   ) -> Result<Self, Error>
34   where
35     Self: Sized;
36   fn unfollow(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
37   where
38     Self: Sized;
39   fn has_local_followers(conn: &mut PgConnection, community_id: CommunityId)
40     -> Result<bool, Error>;
41 }
42
43 pub trait Joinable {
44   type Form;
45   fn join(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
46   where
47     Self: Sized;
48   fn leave(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
49   where
50     Self: Sized;
51 }
52
53 pub trait Likeable {
54   type Form;
55   type IdType;
56   fn like(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
57   where
58     Self: Sized;
59   fn remove(
60     conn: &mut PgConnection,
61     person_id: PersonId,
62     item_id: Self::IdType,
63   ) -> Result<usize, Error>
64   where
65     Self: Sized;
66 }
67
68 pub trait Bannable {
69   type Form;
70   fn ban(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
71   where
72     Self: Sized;
73   fn unban(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
74   where
75     Self: Sized;
76 }
77
78 pub trait Saveable {
79   type Form;
80   fn save(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
81   where
82     Self: Sized;
83   fn unsave(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
84   where
85     Self: Sized;
86 }
87
88 pub trait Blockable {
89   type Form;
90   fn block(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
91   where
92     Self: Sized;
93   fn unblock(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
94   where
95     Self: Sized;
96 }
97
98 pub trait Readable {
99   type Form;
100   fn mark_as_read(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
101   where
102     Self: Sized;
103   fn mark_as_unread(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
104   where
105     Self: Sized;
106 }
107
108 pub trait Reportable {
109   type Form;
110   type IdType;
111   fn report(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
112   where
113     Self: Sized;
114   fn resolve(
115     conn: &mut PgConnection,
116     report_id: Self::IdType,
117     resolver_id: PersonId,
118   ) -> Result<usize, Error>
119   where
120     Self: Sized;
121   fn unresolve(
122     conn: &mut PgConnection,
123     report_id: Self::IdType,
124     resolver_id: PersonId,
125   ) -> Result<usize, Error>
126   where
127     Self: Sized;
128 }
129
130 pub trait DeleteableOrRemoveable {
131   fn blank_out_deleted_or_removed_info(self) -> Self;
132 }
133
134 pub trait ToSafe {
135   type SafeColumns;
136   fn safe_columns_tuple() -> Self::SafeColumns;
137 }
138
139 pub trait ToSafeSettings {
140   type SafeSettingsColumns;
141   fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
142 }
143
144 pub trait ViewToVec {
145   type DbTuple;
146   fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
147   where
148     Self: Sized;
149 }
150
151 pub trait ApubActor {
152   // TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well)
153   fn read_from_apub_id(conn: &mut PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error>
154   where
155     Self: Sized;
156   /// - actor_name is the name of the community or user to read.
157   /// - include_deleted, if true, will return communities or users that were deleted/removed
158   fn read_from_name(
159     conn: &mut PgConnection,
160     actor_name: &str,
161     include_deleted: bool,
162   ) -> Result<Self, Error>
163   where
164     Self: Sized;
165   fn read_from_name_and_domain(
166     conn: &mut PgConnection,
167     actor_name: &str,
168     protocol_domain: &str,
169   ) -> Result<Self, Error>
170   where
171     Self: Sized;
172 }