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