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