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