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