]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/community.rs
Add both (De)Serialize to all models (#1851)
[lemmy.git] / crates / db_schema / src / source / community.rs
1 use crate::{
2   schema::{community, community_follower, community_moderator, community_person_ban},
3   CommunityId,
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 = "community"]
16 pub struct Community {
17   pub id: CommunityId,
18   pub name: String,
19   pub title: String,
20   pub description: Option<String>,
21   pub removed: bool,
22   pub published: chrono::NaiveDateTime,
23   pub updated: Option<chrono::NaiveDateTime>,
24   pub deleted: bool,
25   pub nsfw: bool,
26   pub actor_id: DbUrl,
27   pub local: bool,
28   pub private_key: Option<String>,
29   pub public_key: Option<String>,
30   pub last_refreshed_at: chrono::NaiveDateTime,
31   pub icon: Option<DbUrl>,
32   pub banner: Option<DbUrl>,
33   pub followers_url: DbUrl,
34   pub inbox_url: DbUrl,
35   pub shared_inbox_url: Option<DbUrl>,
36 }
37
38 /// A safe representation of community, without the sensitive info
39 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
40 #[table_name = "community"]
41 pub struct CommunitySafe {
42   pub id: CommunityId,
43   pub name: String,
44   pub title: String,
45   pub description: Option<String>,
46   pub removed: bool,
47   pub published: chrono::NaiveDateTime,
48   pub updated: Option<chrono::NaiveDateTime>,
49   pub deleted: bool,
50   pub nsfw: bool,
51   pub actor_id: DbUrl,
52   pub local: bool,
53   pub icon: Option<DbUrl>,
54   pub banner: Option<DbUrl>,
55 }
56
57 #[derive(Insertable, AsChangeset, Debug, Default)]
58 #[table_name = "community"]
59 pub struct CommunityForm {
60   pub name: String,
61   pub title: String,
62   pub description: Option<String>,
63   pub removed: Option<bool>,
64   pub published: Option<chrono::NaiveDateTime>,
65   pub updated: Option<chrono::NaiveDateTime>,
66   pub deleted: Option<bool>,
67   pub nsfw: Option<bool>,
68   pub actor_id: Option<DbUrl>,
69   pub local: Option<bool>,
70   pub private_key: Option<String>,
71   pub public_key: Option<String>,
72   pub last_refreshed_at: Option<chrono::NaiveDateTime>,
73   pub icon: Option<Option<DbUrl>>,
74   pub banner: Option<Option<DbUrl>>,
75   pub followers_url: Option<DbUrl>,
76   pub inbox_url: Option<DbUrl>,
77   pub shared_inbox_url: Option<Option<DbUrl>>,
78 }
79
80 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
81 #[belongs_to(Community)]
82 #[table_name = "community_moderator"]
83 pub struct CommunityModerator {
84   pub id: i32,
85   pub community_id: CommunityId,
86   pub person_id: PersonId,
87   pub published: chrono::NaiveDateTime,
88 }
89
90 #[derive(Insertable, AsChangeset, Clone)]
91 #[table_name = "community_moderator"]
92 pub struct CommunityModeratorForm {
93   pub community_id: CommunityId,
94   pub person_id: PersonId,
95 }
96
97 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
98 #[belongs_to(Community)]
99 #[table_name = "community_person_ban"]
100 pub struct CommunityPersonBan {
101   pub id: i32,
102   pub community_id: CommunityId,
103   pub person_id: PersonId,
104   pub published: chrono::NaiveDateTime,
105 }
106
107 #[derive(Insertable, AsChangeset, Clone)]
108 #[table_name = "community_person_ban"]
109 pub struct CommunityPersonBanForm {
110   pub community_id: CommunityId,
111   pub person_id: PersonId,
112 }
113
114 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
115 #[belongs_to(Community)]
116 #[table_name = "community_follower"]
117 pub struct CommunityFollower {
118   pub id: i32,
119   pub community_id: CommunityId,
120   pub person_id: PersonId,
121   pub published: chrono::NaiveDateTime,
122   pub pending: Option<bool>,
123 }
124
125 #[derive(Insertable, AsChangeset, Clone)]
126 #[table_name = "community_follower"]
127 pub struct CommunityFollowerForm {
128   pub community_id: CommunityId,
129   pub person_id: PersonId,
130   pub pending: bool,
131 }
132
133 impl ApubObject for Community {
134   type DataType = PgConnection;
135
136   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
137     Some(self.last_refreshed_at)
138   }
139
140   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
141     use crate::schema::community::dsl::*;
142     let object_id: DbUrl = object_id.into();
143     Ok(
144       community
145         .filter(actor_id.eq(object_id))
146         .first::<Self>(conn)
147         .ok(),
148     )
149   }
150 }
151
152 impl ActorType for Community {
153   fn is_local(&self) -> bool {
154     self.local
155   }
156   fn actor_id(&self) -> Url {
157     self.actor_id.to_owned().into()
158   }
159   fn name(&self) -> String {
160     self.name.clone()
161   }
162   fn public_key(&self) -> Option<String> {
163     self.public_key.to_owned()
164   }
165   fn private_key(&self) -> Option<String> {
166     self.private_key.to_owned()
167   }
168
169   fn inbox_url(&self) -> Url {
170     self.inbox_url.clone().into()
171   }
172
173   fn shared_inbox_url(&self) -> Option<Url> {
174     self.shared_inbox_url.clone().map(|s| s.into_inner())
175   }
176 }