4 newtypes::{CommunityId, DbUrl, PersonId},
11 CommunityModeratorForm,
13 CommunityPersonBanForm,
16 traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
25 TextExpressionMethods,
30 use crate::{schema::community::*, source::community::Community, traits::ToSafe};
48 impl ToSafe for Community {
49 type SafeColumns = Columns;
50 fn safe_columns_tuple() -> Self::SafeColumns {
70 impl Crud for Community {
71 type Form = CommunityForm;
72 type IdType = CommunityId;
73 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
74 use crate::schema::community::dsl::*;
75 community.find(community_id).first::<Self>(conn)
78 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
79 use crate::schema::community::dsl::*;
80 diesel::delete(community.find(community_id)).execute(conn)
83 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
84 use crate::schema::community::dsl::*;
85 insert_into(community)
86 .values(new_community)
87 .get_result::<Self>(conn)
92 community_id: CommunityId,
93 new_community: &CommunityForm,
94 ) -> Result<Self, Error> {
95 use crate::schema::community::dsl::*;
96 diesel::update(community.find(community_id))
98 .get_result::<Self>(conn)
103 pub fn update_deleted(
105 community_id: CommunityId,
107 ) -> Result<Community, Error> {
108 use crate::schema::community::dsl::*;
109 diesel::update(community.find(community_id))
110 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
111 .get_result::<Self>(conn)
114 pub fn update_removed(
116 community_id: CommunityId,
118 ) -> Result<Community, Error> {
119 use crate::schema::community::dsl::*;
120 diesel::update(community.find(community_id))
121 .set((removed.eq(new_removed), updated.eq(naive_now())))
122 .get_result::<Self>(conn)
125 pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<DbUrl>, Error> {
126 use crate::schema::community::dsl::*;
127 community.select(actor_id).distinct().load::<DbUrl>(conn)
130 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
131 use crate::schema::community::dsl::*;
132 insert_into(community)
133 .values(community_form)
134 .on_conflict(actor_id)
137 .get_result::<Self>(conn)
141 impl Joinable for CommunityModerator {
142 type Form = CommunityModeratorForm;
145 community_moderator_form: &CommunityModeratorForm,
146 ) -> Result<Self, Error> {
147 use crate::schema::community_moderator::dsl::*;
148 insert_into(community_moderator)
149 .values(community_moderator_form)
150 .get_result::<Self>(conn)
155 community_moderator_form: &CommunityModeratorForm,
156 ) -> Result<usize, Error> {
157 use crate::schema::community_moderator::dsl::*;
160 .filter(community_id.eq(community_moderator_form.community_id))
161 .filter(person_id.eq(community_moderator_form.person_id)),
167 impl DeleteableOrRemoveable for CommunitySafe {
168 fn blank_out_deleted_or_removed_info(mut self) -> Self {
169 self.title = "".into();
170 self.description = None;
177 impl DeleteableOrRemoveable for Community {
178 fn blank_out_deleted_or_removed_info(mut self) -> Self {
179 self.title = "".into();
180 self.description = None;
187 impl CommunityModerator {
188 pub fn delete_for_community(
190 for_community_id: CommunityId,
191 ) -> Result<usize, Error> {
192 use crate::schema::community_moderator::dsl::*;
193 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
196 pub fn get_person_moderated_communities(
198 for_person_id: PersonId,
199 ) -> Result<Vec<CommunityId>, Error> {
200 use crate::schema::community_moderator::dsl::*;
202 .filter(person_id.eq(for_person_id))
203 .select(community_id)
204 .load::<CommunityId>(conn)
208 impl Bannable for CommunityPersonBan {
209 type Form = CommunityPersonBanForm;
212 community_person_ban_form: &CommunityPersonBanForm,
213 ) -> Result<Self, Error> {
214 use crate::schema::community_person_ban::dsl::*;
215 insert_into(community_person_ban)
216 .values(community_person_ban_form)
217 .on_conflict((community_id, person_id))
219 .set(community_person_ban_form)
220 .get_result::<Self>(conn)
225 community_person_ban_form: &CommunityPersonBanForm,
226 ) -> Result<usize, Error> {
227 use crate::schema::community_person_ban::dsl::*;
230 .filter(community_id.eq(community_person_ban_form.community_id))
231 .filter(person_id.eq(community_person_ban_form.person_id)),
237 impl Followable for CommunityFollower {
238 type Form = CommunityFollowerForm;
241 community_follower_form: &CommunityFollowerForm,
242 ) -> Result<Self, Error> {
243 use crate::schema::community_follower::dsl::*;
244 insert_into(community_follower)
245 .values(community_follower_form)
246 .on_conflict((community_id, person_id))
248 .set(community_follower_form)
249 .get_result::<Self>(conn)
253 community_id_: CommunityId,
254 person_id_: PersonId,
255 ) -> Result<Self, Error>
259 use crate::schema::community_follower::dsl::*;
262 .filter(community_id.eq(community_id_))
263 .filter(person_id.eq(person_id_)),
265 .set(pending.eq(true))
266 .get_result::<Self>(conn)
270 community_follower_form: &CommunityFollowerForm,
271 ) -> Result<usize, Error> {
272 use crate::schema::community_follower::dsl::*;
275 .filter(community_id.eq(&community_follower_form.community_id))
276 .filter(person_id.eq(&community_follower_form.person_id)),
280 // TODO: this function name only makes sense if you call it with a remote community. for a local
281 // community, it will also return true if only remote followers exist
282 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
283 use crate::schema::community_follower::dsl::*;
284 diesel::select(exists(
285 community_follower.filter(community_id.eq(community_id_)),
291 impl ApubActor for Community {
292 fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
293 use crate::schema::community::dsl::*;
294 let object_id: DbUrl = object_id.into();
297 .filter(actor_id.eq(object_id))
298 .first::<Community>(conn)
304 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
305 use crate::schema::community::dsl::*;
307 .filter(local.eq(true))
308 .filter(lower(name).eq(lower(community_name)))
312 fn read_from_name_and_domain(
314 community_name: &str,
315 protocol_domain: &str,
316 ) -> Result<Community, Error> {
317 use crate::schema::community::dsl::*;
319 .filter(lower(name).eq(lower(community_name)))
320 .filter(actor_id.like(format!("{}%", protocol_domain)))
328 establish_unpooled_connection,
329 source::{community::*, person::*},
330 traits::{Bannable, Crud, Followable, Joinable},
332 use serial_test::serial;
337 let conn = establish_unpooled_connection();
339 let new_person = PersonForm {
340 name: "bobbee".into(),
341 ..PersonForm::default()
344 let inserted_person = Person::create(&conn, &new_person).unwrap();
346 let new_community = CommunityForm {
348 title: "nada".to_owned(),
349 public_key: "nada".to_owned(),
350 ..CommunityForm::default()
353 let inserted_community = Community::create(&conn, &new_community).unwrap();
355 let expected_community = Community {
356 id: inserted_community.id,
358 title: "nada".to_owned(),
363 published: inserted_community.published,
365 actor_id: inserted_community.actor_id.to_owned(),
368 public_key: "nada".to_owned(),
369 last_refreshed_at: inserted_community.published,
372 followers_url: inserted_community.followers_url.to_owned(),
373 inbox_url: inserted_community.inbox_url.to_owned(),
374 shared_inbox_url: None,
377 let community_follower_form = CommunityFollowerForm {
378 community_id: inserted_community.id,
379 person_id: inserted_person.id,
383 let inserted_community_follower =
384 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
386 let expected_community_follower = CommunityFollower {
387 id: inserted_community_follower.id,
388 community_id: inserted_community.id,
389 person_id: inserted_person.id,
390 pending: Some(false),
391 published: inserted_community_follower.published,
394 let community_moderator_form = CommunityModeratorForm {
395 community_id: inserted_community.id,
396 person_id: inserted_person.id,
399 let inserted_community_moderator =
400 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
402 let expected_community_moderator = CommunityModerator {
403 id: inserted_community_moderator.id,
404 community_id: inserted_community.id,
405 person_id: inserted_person.id,
406 published: inserted_community_moderator.published,
409 let community_person_ban_form = CommunityPersonBanForm {
410 community_id: inserted_community.id,
411 person_id: inserted_person.id,
415 let inserted_community_person_ban =
416 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
418 let expected_community_person_ban = CommunityPersonBan {
419 id: inserted_community_person_ban.id,
420 community_id: inserted_community.id,
421 person_id: inserted_person.id,
422 published: inserted_community_person_ban.published,
426 let read_community = Community::read(&conn, inserted_community.id).unwrap();
427 let updated_community =
428 Community::update(&conn, inserted_community.id, &new_community).unwrap();
429 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
430 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
431 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
432 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
433 Person::delete(&conn, inserted_person.id).unwrap();
435 assert_eq!(expected_community, read_community);
436 assert_eq!(expected_community, inserted_community);
437 assert_eq!(expected_community, updated_community);
438 assert_eq!(expected_community_follower, inserted_community_follower);
439 assert_eq!(expected_community_moderator, inserted_community_moderator);
440 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
441 assert_eq!(1, ignored_community);
442 assert_eq!(1, left_community);
443 assert_eq!(1, unban);
444 // assert_eq!(2, loaded_count);
445 assert_eq!(1, num_deleted);