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