]> Untitled Git - lemmy.git/blob - crates/db_schema/src/traits.rs
Add diesel_async, get rid of blocking function (#2510)
[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   async fn has_local_followers(pool: &DbPool, community_id: CommunityId) -> Result<bool, Error>;
48 }
49
50 #[async_trait]
51 pub trait Joinable {
52   type Form;
53   async fn join(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
54   where
55     Self: Sized;
56   async fn leave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
57   where
58     Self: Sized;
59 }
60
61 #[async_trait]
62 pub trait Likeable {
63   type Form;
64   type IdType;
65   async fn like(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
66   where
67     Self: Sized;
68   async fn remove(
69     pool: &DbPool,
70     person_id: PersonId,
71     item_id: Self::IdType,
72   ) -> Result<usize, Error>
73   where
74     Self: Sized;
75 }
76
77 #[async_trait]
78 pub trait Bannable {
79   type Form;
80   async fn ban(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
81   where
82     Self: Sized;
83   async fn unban(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
84   where
85     Self: Sized;
86 }
87
88 #[async_trait]
89 pub trait Saveable {
90   type Form;
91   async fn save(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
92   where
93     Self: Sized;
94   async fn unsave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
95   where
96     Self: Sized;
97 }
98
99 #[async_trait]
100 pub trait Blockable {
101   type Form;
102   async fn block(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
103   where
104     Self: Sized;
105   async fn unblock(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
106   where
107     Self: Sized;
108 }
109
110 #[async_trait]
111 pub trait Readable {
112   type Form;
113   async fn mark_as_read(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
114   where
115     Self: Sized;
116   async fn mark_as_unread(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
117   where
118     Self: Sized;
119 }
120
121 #[async_trait]
122 pub trait Reportable {
123   type Form;
124   type IdType;
125   async fn report(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
126   where
127     Self: Sized;
128   async fn resolve(
129     pool: &DbPool,
130     report_id: Self::IdType,
131     resolver_id: PersonId,
132   ) -> Result<usize, Error>
133   where
134     Self: Sized;
135   async fn unresolve(
136     pool: &DbPool,
137     report_id: Self::IdType,
138     resolver_id: PersonId,
139   ) -> Result<usize, Error>
140   where
141     Self: Sized;
142 }
143
144 // TODO these should be removed, there should be another way to do this
145 pub trait DeleteableOrRemoveable {
146   fn blank_out_deleted_or_removed_info(self) -> Self;
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 #[async_trait]
167 pub trait ApubActor {
168   // TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well)
169   async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error>
170   where
171     Self: Sized;
172   /// - actor_name is the name of the community or user to read.
173   /// - include_deleted, if true, will return communities or users that were deleted/removed
174   async fn read_from_name(
175     pool: &DbPool,
176     actor_name: &str,
177     include_deleted: bool,
178   ) -> Result<Self, Error>
179   where
180     Self: Sized;
181   async fn read_from_name_and_domain(
182     pool: &DbPool,
183     actor_name: &str,
184     protocol_domain: &str,
185   ) -> Result<Self, Error>
186   where
187     Self: Sized;
188 }