]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/person.rs
Remove DeletableApubObject trait
[lemmy.git] / crates / db_schema / src / source / person.rs
1 use crate::{
2   naive_now,
3   schema::{person, person_alias_1, person_alias_2},
4   DbUrl,
5   PersonId,
6 };
7 use chrono::NaiveDateTime;
8 use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
9 use lemmy_apub_lib::traits::{ActorType, ApubObject};
10 use lemmy_utils::LemmyError;
11 use serde::{Deserialize, Serialize};
12 use url::Url;
13
14 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
15 #[table_name = "person"]
16 pub struct Person {
17   pub id: PersonId,
18   pub name: String,
19   pub display_name: Option<String>,
20   pub avatar: Option<DbUrl>,
21   pub banned: bool,
22   pub published: chrono::NaiveDateTime,
23   pub updated: Option<chrono::NaiveDateTime>,
24   pub actor_id: DbUrl,
25   pub bio: Option<String>,
26   pub local: bool,
27   pub private_key: Option<String>,
28   pub public_key: Option<String>,
29   pub last_refreshed_at: chrono::NaiveDateTime,
30   pub banner: Option<DbUrl>,
31   pub deleted: bool,
32   pub inbox_url: DbUrl,
33   pub shared_inbox_url: Option<DbUrl>,
34   pub matrix_user_id: Option<String>,
35   pub admin: bool,
36   pub bot_account: bool,
37 }
38
39 /// A safe representation of person, without the sensitive info
40 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
41 #[table_name = "person"]
42 pub struct PersonSafe {
43   pub id: PersonId,
44   pub name: String,
45   pub display_name: Option<String>,
46   pub avatar: Option<DbUrl>,
47   pub banned: bool,
48   pub published: chrono::NaiveDateTime,
49   pub updated: Option<chrono::NaiveDateTime>,
50   pub actor_id: DbUrl,
51   pub bio: Option<String>,
52   pub local: bool,
53   pub banner: Option<DbUrl>,
54   pub deleted: bool,
55   pub inbox_url: DbUrl,
56   pub shared_inbox_url: Option<DbUrl>,
57   pub matrix_user_id: Option<String>,
58   pub admin: bool,
59   pub bot_account: bool,
60 }
61
62 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
63 #[table_name = "person_alias_1"]
64 pub struct PersonAlias1 {
65   pub id: PersonId,
66   pub name: String,
67   pub display_name: Option<String>,
68   pub avatar: Option<DbUrl>,
69   pub banned: bool,
70   pub published: chrono::NaiveDateTime,
71   pub updated: Option<chrono::NaiveDateTime>,
72   pub actor_id: DbUrl,
73   pub bio: Option<String>,
74   pub local: bool,
75   pub private_key: Option<String>,
76   pub public_key: Option<String>,
77   pub last_refreshed_at: chrono::NaiveDateTime,
78   pub banner: Option<DbUrl>,
79   pub deleted: bool,
80   pub inbox_url: DbUrl,
81   pub shared_inbox_url: Option<DbUrl>,
82   pub matrix_user_id: Option<String>,
83   pub admin: bool,
84   pub bot_account: bool,
85 }
86
87 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
88 #[table_name = "person_alias_1"]
89 pub struct PersonSafeAlias1 {
90   pub id: PersonId,
91   pub name: String,
92   pub display_name: Option<String>,
93   pub avatar: Option<DbUrl>,
94   pub banned: bool,
95   pub published: chrono::NaiveDateTime,
96   pub updated: Option<chrono::NaiveDateTime>,
97   pub actor_id: DbUrl,
98   pub bio: Option<String>,
99   pub local: bool,
100   pub banner: Option<DbUrl>,
101   pub deleted: bool,
102   pub inbox_url: DbUrl,
103   pub shared_inbox_url: Option<DbUrl>,
104   pub matrix_user_id: Option<String>,
105   pub admin: bool,
106   pub bot_account: bool,
107 }
108
109 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
110 #[table_name = "person_alias_2"]
111 pub struct PersonAlias2 {
112   pub id: PersonId,
113   pub name: String,
114   pub display_name: Option<String>,
115   pub avatar: Option<DbUrl>,
116   pub banned: bool,
117   pub published: chrono::NaiveDateTime,
118   pub updated: Option<chrono::NaiveDateTime>,
119   pub actor_id: DbUrl,
120   pub bio: Option<String>,
121   pub local: bool,
122   pub private_key: Option<String>,
123   pub public_key: Option<String>,
124   pub last_refreshed_at: chrono::NaiveDateTime,
125   pub banner: Option<DbUrl>,
126   pub deleted: bool,
127   pub inbox_url: DbUrl,
128   pub shared_inbox_url: Option<DbUrl>,
129   pub matrix_user_id: Option<String>,
130   pub admin: bool,
131   pub bot_account: bool,
132 }
133
134 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
135 #[table_name = "person_alias_1"]
136 pub struct PersonSafeAlias2 {
137   pub id: PersonId,
138   pub name: String,
139   pub display_name: Option<String>,
140   pub avatar: Option<DbUrl>,
141   pub banned: bool,
142   pub published: chrono::NaiveDateTime,
143   pub updated: Option<chrono::NaiveDateTime>,
144   pub actor_id: DbUrl,
145   pub bio: Option<String>,
146   pub local: bool,
147   pub banner: Option<DbUrl>,
148   pub deleted: bool,
149   pub inbox_url: DbUrl,
150   pub shared_inbox_url: Option<DbUrl>,
151   pub matrix_user_id: Option<String>,
152   pub admin: bool,
153   pub bot_account: bool,
154 }
155
156 #[derive(Insertable, AsChangeset, Clone, Default)]
157 #[table_name = "person"]
158 pub struct PersonForm {
159   pub name: String,
160   pub display_name: Option<Option<String>>,
161   pub avatar: Option<Option<DbUrl>>,
162   pub banned: Option<bool>,
163   pub published: Option<chrono::NaiveDateTime>,
164   pub updated: Option<chrono::NaiveDateTime>,
165   pub actor_id: Option<DbUrl>,
166   pub bio: Option<Option<String>>,
167   pub local: Option<bool>,
168   pub private_key: Option<Option<String>>,
169   pub public_key: Option<Option<String>>,
170   pub last_refreshed_at: Option<chrono::NaiveDateTime>,
171   pub banner: Option<Option<DbUrl>>,
172   pub deleted: Option<bool>,
173   pub inbox_url: Option<DbUrl>,
174   pub shared_inbox_url: Option<Option<DbUrl>>,
175   pub matrix_user_id: Option<Option<String>>,
176   pub admin: Option<bool>,
177   pub bot_account: Option<bool>,
178 }
179
180 impl ApubObject for Person {
181   type DataType = PgConnection;
182
183   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
184     Some(self.last_refreshed_at)
185   }
186
187   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
188     use crate::schema::person::dsl::*;
189     let object_id: DbUrl = object_id.into();
190     Ok(
191       person
192         .filter(deleted.eq(false))
193         .filter(actor_id.eq(object_id))
194         .first::<Self>(conn)
195         .ok(),
196     )
197   }
198
199   fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
200     use crate::schema::person::dsl::*;
201     diesel::update(person.find(self.id))
202       .set((deleted.eq(true), updated.eq(naive_now())))
203       .get_result::<Self>(conn)?;
204     Ok(())
205   }
206 }
207
208 impl ActorType for Person {
209   fn is_local(&self) -> bool {
210     self.local
211   }
212   fn actor_id(&self) -> Url {
213     self.actor_id.to_owned().into_inner()
214   }
215   fn name(&self) -> String {
216     self.name.clone()
217   }
218
219   fn public_key(&self) -> Option<String> {
220     self.public_key.to_owned()
221   }
222
223   fn private_key(&self) -> Option<String> {
224     self.private_key.to_owned()
225   }
226
227   fn inbox_url(&self) -> Url {
228     self.inbox_url.clone().into()
229   }
230
231   fn shared_inbox_url(&self) -> Option<Url> {
232     self.shared_inbox_url.clone().map(|s| s.into_inner())
233   }
234 }