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