3 schema::{community, community_follower, community_moderator, community_person_ban},
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};
15 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
16 #[table_name = "community"]
17 pub struct Community {
21 pub description: Option<String>,
23 pub published: chrono::NaiveDateTime,
24 pub updated: Option<chrono::NaiveDateTime>,
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,
36 pub shared_inbox_url: Option<DbUrl>,
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 {
46 pub description: Option<String>,
48 pub published: chrono::NaiveDateTime,
49 pub updated: Option<chrono::NaiveDateTime>,
54 pub icon: Option<DbUrl>,
55 pub banner: Option<DbUrl>,
58 #[derive(Insertable, AsChangeset, Debug, Default)]
59 #[table_name = "community"]
60 pub struct CommunityForm {
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>>,
81 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
82 #[belongs_to(Community)]
83 #[table_name = "community_moderator"]
84 pub struct CommunityModerator {
86 pub community_id: CommunityId,
87 pub person_id: PersonId,
88 pub published: chrono::NaiveDateTime,
91 #[derive(Insertable, AsChangeset, Clone)]
92 #[table_name = "community_moderator"]
93 pub struct CommunityModeratorForm {
94 pub community_id: CommunityId,
95 pub person_id: PersonId,
98 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
99 #[belongs_to(Community)]
100 #[table_name = "community_person_ban"]
101 pub struct CommunityPersonBan {
103 pub community_id: CommunityId,
104 pub person_id: PersonId,
105 pub published: chrono::NaiveDateTime,
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,
115 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
116 #[belongs_to(Community)]
117 #[table_name = "community_follower"]
118 pub struct CommunityFollower {
120 pub community_id: CommunityId,
121 pub person_id: PersonId,
122 pub published: chrono::NaiveDateTime,
123 pub pending: Option<bool>,
126 #[derive(Insertable, AsChangeset, Clone)]
127 #[table_name = "community_follower"]
128 pub struct CommunityFollowerForm {
129 pub community_id: CommunityId,
130 pub person_id: PersonId,
134 impl ApubObject for Community {
135 type DataType = PgConnection;
137 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
138 Some(self.last_refreshed_at)
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();
146 .filter(actor_id.eq(object_id))
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)?;
161 impl ActorType for Community {
162 fn is_local(&self) -> bool {
165 fn actor_id(&self) -> Url {
166 self.actor_id.to_owned().into()
168 fn name(&self) -> String {
171 fn public_key(&self) -> Option<String> {
172 self.public_key.to_owned()
174 fn private_key(&self) -> Option<String> {
175 self.private_key.to_owned()
178 fn inbox_url(&self) -> Url {
179 self.inbox_url.clone().into()
182 fn shared_inbox_url(&self) -> Option<Url> {
183 self.shared_inbox_url.clone().map(|s| s.into_inner())