4 newtypes::{CommunityId, DbUrl, PersonId},
11 CommunityModeratorForm,
13 CommunityPersonBanForm,
16 traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
25 TextExpressionMethods,
29 use crate::{schema::community::*, source::community::Community, traits::ToSafe};
46 posting_restricted_to_mods,
49 impl ToSafe for Community {
50 type SafeColumns = Columns;
51 fn safe_columns_tuple() -> Self::SafeColumns {
67 posting_restricted_to_mods,
73 impl Crud for Community {
74 type Form = CommunityForm;
75 type IdType = CommunityId;
76 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
77 use crate::schema::community::dsl::*;
78 community.find(community_id).first::<Self>(conn)
81 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
82 use crate::schema::community::dsl::*;
83 diesel::delete(community.find(community_id)).execute(conn)
86 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
87 use crate::schema::community::dsl::*;
88 insert_into(community)
89 .values(new_community)
90 .get_result::<Self>(conn)
95 community_id: CommunityId,
96 new_community: &CommunityForm,
97 ) -> Result<Self, Error> {
98 use crate::schema::community::dsl::*;
99 diesel::update(community.find(community_id))
101 .get_result::<Self>(conn)
106 pub fn update_deleted(
108 community_id: CommunityId,
110 ) -> Result<Community, Error> {
111 use crate::schema::community::dsl::*;
112 diesel::update(community.find(community_id))
113 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
114 .get_result::<Self>(conn)
117 pub fn update_removed(
119 community_id: CommunityId,
121 ) -> Result<Community, Error> {
122 use crate::schema::community::dsl::*;
123 diesel::update(community.find(community_id))
124 .set((removed.eq(new_removed), updated.eq(naive_now())))
125 .get_result::<Self>(conn)
128 pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<DbUrl>, Error> {
129 use crate::schema::community::dsl::*;
130 community.select(actor_id).distinct().load::<DbUrl>(conn)
133 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
134 use crate::schema::community::dsl::*;
135 insert_into(community)
136 .values(community_form)
137 .on_conflict(actor_id)
140 .get_result::<Self>(conn)
144 impl Joinable for CommunityModerator {
145 type Form = CommunityModeratorForm;
148 community_moderator_form: &CommunityModeratorForm,
149 ) -> Result<Self, Error> {
150 use crate::schema::community_moderator::dsl::*;
151 insert_into(community_moderator)
152 .values(community_moderator_form)
153 .get_result::<Self>(conn)
158 community_moderator_form: &CommunityModeratorForm,
159 ) -> Result<usize, Error> {
160 use crate::schema::community_moderator::dsl::*;
163 .filter(community_id.eq(community_moderator_form.community_id))
164 .filter(person_id.eq(community_moderator_form.person_id)),
170 impl DeleteableOrRemoveable for CommunitySafe {
171 fn blank_out_deleted_or_removed_info(mut self) -> Self {
172 self.title = "".into();
173 self.description = None;
180 impl DeleteableOrRemoveable for Community {
181 fn blank_out_deleted_or_removed_info(mut self) -> Self {
182 self.title = "".into();
183 self.description = None;
190 impl CommunityModerator {
191 pub fn delete_for_community(
193 for_community_id: CommunityId,
194 ) -> Result<usize, Error> {
195 use crate::schema::community_moderator::dsl::*;
196 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
199 pub fn get_person_moderated_communities(
201 for_person_id: PersonId,
202 ) -> Result<Vec<CommunityId>, Error> {
203 use crate::schema::community_moderator::dsl::*;
205 .filter(person_id.eq(for_person_id))
206 .select(community_id)
207 .load::<CommunityId>(conn)
211 impl Bannable for CommunityPersonBan {
212 type Form = CommunityPersonBanForm;
215 community_person_ban_form: &CommunityPersonBanForm,
216 ) -> Result<Self, Error> {
217 use crate::schema::community_person_ban::dsl::*;
218 insert_into(community_person_ban)
219 .values(community_person_ban_form)
220 .on_conflict((community_id, person_id))
222 .set(community_person_ban_form)
223 .get_result::<Self>(conn)
228 community_person_ban_form: &CommunityPersonBanForm,
229 ) -> Result<usize, Error> {
230 use crate::schema::community_person_ban::dsl::*;
233 .filter(community_id.eq(community_person_ban_form.community_id))
234 .filter(person_id.eq(community_person_ban_form.person_id)),
240 impl Followable for CommunityFollower {
241 type Form = CommunityFollowerForm;
244 community_follower_form: &CommunityFollowerForm,
245 ) -> Result<Self, Error> {
246 use crate::schema::community_follower::dsl::*;
247 insert_into(community_follower)
248 .values(community_follower_form)
249 .on_conflict((community_id, person_id))
251 .set(community_follower_form)
252 .get_result::<Self>(conn)
256 community_id_: CommunityId,
257 person_id_: PersonId,
258 ) -> Result<Self, Error>
262 use crate::schema::community_follower::dsl::*;
265 .filter(community_id.eq(community_id_))
266 .filter(person_id.eq(person_id_)),
268 .set(pending.eq(true))
269 .get_result::<Self>(conn)
273 community_follower_form: &CommunityFollowerForm,
274 ) -> Result<usize, Error> {
275 use crate::schema::community_follower::dsl::*;
278 .filter(community_id.eq(&community_follower_form.community_id))
279 .filter(person_id.eq(&community_follower_form.person_id)),
283 // TODO: this function name only makes sense if you call it with a remote community. for a local
284 // community, it will also return true if only remote followers exist
285 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
286 use crate::schema::community_follower::dsl::*;
287 diesel::select(exists(
288 community_follower.filter(community_id.eq(community_id_)),
294 impl ApubActor for Community {
295 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
296 use crate::schema::community::dsl::*;
299 .filter(actor_id.eq(object_id))
300 .first::<Community>(conn)
306 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
307 use crate::schema::community::dsl::*;
309 .filter(local.eq(true))
310 .filter(lower(name).eq(lower(community_name)))
314 fn read_from_name_and_domain(
316 community_name: &str,
317 protocol_domain: &str,
318 ) -> Result<Community, Error> {
319 use crate::schema::community::dsl::*;
321 .filter(lower(name).eq(lower(community_name)))
322 .filter(actor_id.like(format!("{}%", protocol_domain)))
330 establish_unpooled_connection,
331 source::{community::*, person::*},
332 traits::{Bannable, Crud, Followable, Joinable},
334 use serial_test::serial;
339 let conn = establish_unpooled_connection();
341 let new_person = PersonForm {
342 name: "bobbee".into(),
343 ..PersonForm::default()
346 let inserted_person = Person::create(&conn, &new_person).unwrap();
348 let new_community = CommunityForm {
350 title: "nada".to_owned(),
351 public_key: "nada".to_owned(),
352 ..CommunityForm::default()
355 let inserted_community = Community::create(&conn, &new_community).unwrap();
357 let expected_community = Community {
358 id: inserted_community.id,
360 title: "nada".to_owned(),
365 published: inserted_community.published,
367 actor_id: inserted_community.actor_id.to_owned(),
370 public_key: "nada".to_owned(),
371 last_refreshed_at: inserted_community.published,
374 followers_url: inserted_community.followers_url.to_owned(),
375 inbox_url: inserted_community.inbox_url.to_owned(),
376 shared_inbox_url: None,
378 posting_restricted_to_mods: false,
381 let community_follower_form = CommunityFollowerForm {
382 community_id: inserted_community.id,
383 person_id: inserted_person.id,
387 let inserted_community_follower =
388 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
390 let expected_community_follower = CommunityFollower {
391 id: inserted_community_follower.id,
392 community_id: inserted_community.id,
393 person_id: inserted_person.id,
394 pending: Some(false),
395 published: inserted_community_follower.published,
398 let community_moderator_form = CommunityModeratorForm {
399 community_id: inserted_community.id,
400 person_id: inserted_person.id,
403 let inserted_community_moderator =
404 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
406 let expected_community_moderator = CommunityModerator {
407 id: inserted_community_moderator.id,
408 community_id: inserted_community.id,
409 person_id: inserted_person.id,
410 published: inserted_community_moderator.published,
413 let community_person_ban_form = CommunityPersonBanForm {
414 community_id: inserted_community.id,
415 person_id: inserted_person.id,
419 let inserted_community_person_ban =
420 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
422 let expected_community_person_ban = CommunityPersonBan {
423 id: inserted_community_person_ban.id,
424 community_id: inserted_community.id,
425 person_id: inserted_person.id,
426 published: inserted_community_person_ban.published,
430 let read_community = Community::read(&conn, inserted_community.id).unwrap();
431 let updated_community =
432 Community::update(&conn, inserted_community.id, &new_community).unwrap();
433 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
434 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
435 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
436 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
437 Person::delete(&conn, inserted_person.id).unwrap();
439 assert_eq!(expected_community, read_community);
440 assert_eq!(expected_community, inserted_community);
441 assert_eq!(expected_community, updated_community);
442 assert_eq!(expected_community_follower, inserted_community_follower);
443 assert_eq!(expected_community_moderator, inserted_community_moderator);
444 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
445 assert_eq!(1, ignored_community);
446 assert_eq!(1, left_community);
447 assert_eq!(1, unban);
448 // assert_eq!(2, loaded_count);
449 assert_eq!(1, num_deleted);