]> Untitled Git - lemmy.git/blob - crates/db_schema/src/traits.rs
Sanitize html (#3708)
[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: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>
13   where
14     Self: Sized;
15   async fn read(pool: &mut 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(
20     pool: &mut DbPool<'_>,
21     id: Self::IdType,
22     form: &Self::UpdateForm,
23   ) -> Result<Self, Error>
24   where
25     Self: Sized;
26   async fn delete(_pool: &mut DbPool<'_>, _id: Self::IdType) -> Result<usize, Error>
27   where
28     Self: Sized,
29     Self::IdType: Send,
30   {
31     async { Err(Error::NotFound) }.await
32   }
33 }
34
35 #[async_trait]
36 pub trait Followable {
37   type Form;
38   async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
39   where
40     Self: Sized;
41   async fn follow_accepted(
42     pool: &mut DbPool<'_>,
43     community_id: CommunityId,
44     person_id: PersonId,
45   ) -> Result<Self, Error>
46   where
47     Self: Sized;
48   async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
49   where
50     Self: Sized;
51 }
52
53 #[async_trait]
54 pub trait Joinable {
55   type Form;
56   async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
57   where
58     Self: Sized;
59   async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
60   where
61     Self: Sized;
62 }
63
64 #[async_trait]
65 pub trait Likeable {
66   type Form;
67   type IdType;
68   async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
69   where
70     Self: Sized;
71   async fn remove(
72     pool: &mut DbPool<'_>,
73     person_id: PersonId,
74     item_id: Self::IdType,
75   ) -> Result<usize, Error>
76   where
77     Self: Sized;
78 }
79
80 #[async_trait]
81 pub trait Bannable {
82   type Form;
83   async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
84   where
85     Self: Sized;
86   async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
87   where
88     Self: Sized;
89 }
90
91 #[async_trait]
92 pub trait Saveable {
93   type Form;
94   async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
95   where
96     Self: Sized;
97   async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
98   where
99     Self: Sized;
100 }
101
102 #[async_trait]
103 pub trait Blockable {
104   type Form;
105   async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
106   where
107     Self: Sized;
108   async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
109   where
110     Self: Sized;
111 }
112
113 #[async_trait]
114 pub trait Readable {
115   type Form;
116   async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
117   where
118     Self: Sized;
119   async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
120   where
121     Self: Sized;
122 }
123
124 #[async_trait]
125 pub trait Reportable {
126   type Form;
127   type IdType;
128   async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
129   where
130     Self: Sized;
131   async fn resolve(
132     pool: &mut DbPool<'_>,
133     report_id: Self::IdType,
134     resolver_id: PersonId,
135   ) -> Result<usize, Error>
136   where
137     Self: Sized;
138   async fn unresolve(
139     pool: &mut DbPool<'_>,
140     report_id: Self::IdType,
141     resolver_id: PersonId,
142   ) -> Result<usize, Error>
143   where
144     Self: Sized;
145 }
146
147 pub trait JoinView {
148   type JoinTuple;
149   fn from_tuple(tuple: Self::JoinTuple) -> Self
150   where
151     Self: Sized;
152 }
153
154 #[async_trait]
155 pub trait ApubActor {
156   async fn read_from_apub_id(
157     pool: &mut DbPool<'_>,
158     object_id: &DbUrl,
159   ) -> 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   async fn read_from_name(
165     pool: &mut DbPool<'_>,
166     actor_name: &str,
167     include_deleted: bool,
168   ) -> Result<Self, Error>
169   where
170     Self: Sized;
171   async fn read_from_name_and_domain(
172     pool: &mut DbPool<'_>,
173     actor_name: &str,
174     protocol_domain: &str,
175   ) -> Result<Self, Error>
176   where
177     Self: Sized;
178 }