2 newtypes::{CommunityId, DbUrl, PersonId},
3 schema::community::dsl::{actor_id, community, deleted, local, name, removed},
5 actor_language::CommunityLanguage,
12 CommunityModeratorForm,
14 CommunityPersonBanForm,
18 traits::{ApubActor, Bannable, Crud, Followable, Joinable},
19 utils::{functions::lower, get_conn, DbPool},
22 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
23 use diesel_async::RunQueryDsl;
26 impl Crud for Community {
27 type InsertForm = CommunityInsertForm;
28 type UpdateForm = CommunityUpdateForm;
29 type IdType = CommunityId;
30 async fn read(pool: &DbPool, community_id: CommunityId) -> Result<Self, Error> {
31 let conn = &mut get_conn(pool).await?;
32 community.find(community_id).first::<Self>(conn).await
35 async fn delete(pool: &DbPool, community_id: CommunityId) -> Result<usize, Error> {
36 let conn = &mut get_conn(pool).await?;
37 diesel::delete(community.find(community_id))
42 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
43 let conn = &mut get_conn(pool).await?;
44 let is_new_community = match &form.actor_id {
45 Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(),
49 // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
50 let community_ = insert_into(community)
52 .on_conflict(actor_id)
55 .get_result::<Self>(conn)
58 // Initialize languages for new community
60 CommunityLanguage::update(pool, vec![], community_.id).await?;
68 community_id: CommunityId,
69 form: &Self::UpdateForm,
70 ) -> Result<Self, Error> {
71 let conn = &mut get_conn(pool).await?;
72 diesel::update(community.find(community_id))
74 .get_result::<Self>(conn)
80 impl Joinable for CommunityModerator {
81 type Form = CommunityModeratorForm;
84 community_moderator_form: &CommunityModeratorForm,
85 ) -> Result<Self, Error> {
86 use crate::schema::community_moderator::dsl::community_moderator;
87 let conn = &mut get_conn(pool).await?;
88 insert_into(community_moderator)
89 .values(community_moderator_form)
90 .get_result::<Self>(conn)
96 community_moderator_form: &CommunityModeratorForm,
97 ) -> Result<usize, Error> {
98 use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
99 let conn = &mut get_conn(pool).await?;
102 .filter(community_id.eq(community_moderator_form.community_id))
103 .filter(person_id.eq(community_moderator_form.person_id)),
110 pub enum CollectionType {
116 /// Get the community which has a given moderators or featured url, also return the collection type
117 pub async fn get_by_collection_url(
120 ) -> Result<(Community, CollectionType), Error> {
121 use crate::schema::community::dsl::{featured_url, moderators_url};
122 use CollectionType::*;
123 let conn = &mut get_conn(pool).await?;
125 .filter(moderators_url.eq(url))
129 return Ok((c, Moderators));
132 .filter(featured_url.eq(url))
136 return Ok((c, Featured));
138 Err(diesel::NotFound)
142 impl CommunityModerator {
143 pub async fn delete_for_community(
145 for_community_id: CommunityId,
146 ) -> Result<usize, Error> {
147 use crate::schema::community_moderator::dsl::{community_id, community_moderator};
148 let conn = &mut get_conn(pool).await?;
150 diesel::delete(community_moderator.filter(community_id.eq(for_community_id)))
155 pub async fn get_person_moderated_communities(
157 for_person_id: PersonId,
158 ) -> Result<Vec<CommunityId>, Error> {
159 use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
160 let conn = &mut get_conn(pool).await?;
162 .filter(person_id.eq(for_person_id))
163 .select(community_id)
164 .load::<CommunityId>(conn)
170 impl Bannable for CommunityPersonBan {
171 type Form = CommunityPersonBanForm;
174 community_person_ban_form: &CommunityPersonBanForm,
175 ) -> Result<Self, Error> {
176 use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
177 let conn = &mut get_conn(pool).await?;
178 insert_into(community_person_ban)
179 .values(community_person_ban_form)
180 .on_conflict((community_id, person_id))
182 .set(community_person_ban_form)
183 .get_result::<Self>(conn)
189 community_person_ban_form: &CommunityPersonBanForm,
190 ) -> Result<usize, Error> {
191 use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
192 let conn = &mut get_conn(pool).await?;
195 .filter(community_id.eq(community_person_ban_form.community_id))
196 .filter(person_id.eq(community_person_ban_form.person_id)),
203 impl CommunityFollower {
204 pub fn to_subscribed_type(follower: &Option<Self>) -> SubscribedType {
208 SubscribedType::Pending
210 SubscribedType::Subscribed
213 // If the row doesn't exist, the person isn't a follower.
214 None => SubscribedType::NotSubscribed,
220 impl Followable for CommunityFollower {
221 type Form = CommunityFollowerForm;
222 async fn follow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<Self, Error> {
223 use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
224 let conn = &mut get_conn(pool).await?;
225 insert_into(community_follower)
227 .on_conflict((community_id, person_id))
230 .get_result::<Self>(conn)
233 async fn follow_accepted(
235 community_id_: CommunityId,
236 person_id_: PersonId,
237 ) -> Result<Self, Error> {
238 use crate::schema::community_follower::dsl::{
244 let conn = &mut get_conn(pool).await?;
247 .filter(community_id.eq(community_id_))
248 .filter(person_id.eq(person_id_)),
250 .set(pending.eq(false))
251 .get_result::<Self>(conn)
254 async fn unfollow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<usize, Error> {
255 use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
256 let conn = &mut get_conn(pool).await?;
259 .filter(community_id.eq(&form.community_id))
260 .filter(person_id.eq(&form.person_id)),
268 impl ApubActor for Community {
269 async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
270 let conn = &mut get_conn(pool).await?;
273 .filter(actor_id.eq(object_id))
274 .first::<Community>(conn)
281 async fn read_from_name(
283 community_name: &str,
284 include_deleted: bool,
285 ) -> Result<Community, Error> {
286 let conn = &mut get_conn(pool).await?;
287 let mut q = community
289 .filter(local.eq(true))
290 .filter(lower(name).eq(lower(community_name)));
291 if !include_deleted {
292 q = q.filter(deleted.eq(false)).filter(removed.eq(false));
294 q.first::<Self>(conn).await
297 async fn read_from_name_and_domain(
299 community_name: &str,
300 protocol_domain: &str,
301 ) -> Result<Community, Error> {
302 let conn = &mut get_conn(pool).await?;
304 .filter(lower(name).eq(lower(community_name)))
305 .filter(actor_id.like(format!("{protocol_domain}%")))
318 CommunityFollowerForm,
321 CommunityModeratorForm,
323 CommunityPersonBanForm,
327 person::{Person, PersonInsertForm},
329 traits::{Bannable, Crud, Followable, Joinable},
330 utils::build_db_pool_for_tests,
332 use serial_test::serial;
336 async fn test_crud() {
337 let pool = &build_db_pool_for_tests().await;
339 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
343 let new_person = PersonInsertForm::builder()
344 .name("bobbee".into())
345 .public_key("pubkey".to_string())
346 .instance_id(inserted_instance.id)
349 let inserted_person = Person::create(pool, &new_person).await.unwrap();
351 let new_community = CommunityInsertForm::builder()
353 .title("nada".to_owned())
354 .public_key("pubkey".to_string())
355 .instance_id(inserted_instance.id)
358 let inserted_community = Community::create(pool, &new_community).await.unwrap();
360 let expected_community = Community {
361 id: inserted_community.id,
363 title: "nada".to_owned(),
368 published: inserted_community.published,
370 actor_id: inserted_community.actor_id.clone(),
373 public_key: "pubkey".to_owned(),
374 last_refreshed_at: inserted_community.published,
377 followers_url: inserted_community.followers_url.clone(),
378 inbox_url: inserted_community.inbox_url.clone(),
379 shared_inbox_url: None,
380 moderators_url: None,
383 posting_restricted_to_mods: false,
384 instance_id: inserted_instance.id,
387 let community_follower_form = CommunityFollowerForm {
388 community_id: inserted_community.id,
389 person_id: inserted_person.id,
393 let inserted_community_follower = CommunityFollower::follow(pool, &community_follower_form)
397 let expected_community_follower = CommunityFollower {
398 id: inserted_community_follower.id,
399 community_id: inserted_community.id,
400 person_id: inserted_person.id,
402 published: inserted_community_follower.published,
405 let community_moderator_form = CommunityModeratorForm {
406 community_id: inserted_community.id,
407 person_id: inserted_person.id,
410 let inserted_community_moderator = CommunityModerator::join(pool, &community_moderator_form)
414 let expected_community_moderator = CommunityModerator {
415 id: inserted_community_moderator.id,
416 community_id: inserted_community.id,
417 person_id: inserted_person.id,
418 published: inserted_community_moderator.published,
421 let community_person_ban_form = CommunityPersonBanForm {
422 community_id: inserted_community.id,
423 person_id: inserted_person.id,
427 let inserted_community_person_ban = CommunityPersonBan::ban(pool, &community_person_ban_form)
431 let expected_community_person_ban = CommunityPersonBan {
432 id: inserted_community_person_ban.id,
433 community_id: inserted_community.id,
434 person_id: inserted_person.id,
435 published: inserted_community_person_ban.published,
439 let read_community = Community::read(pool, inserted_community.id).await.unwrap();
441 let update_community_form = CommunityUpdateForm::builder()
442 .title(Some("nada".to_owned()))
444 let updated_community = Community::update(pool, inserted_community.id, &update_community_form)
448 let ignored_community = CommunityFollower::unfollow(pool, &community_follower_form)
451 let left_community = CommunityModerator::leave(pool, &community_moderator_form)
454 let unban = CommunityPersonBan::unban(pool, &community_person_ban_form)
457 let num_deleted = Community::delete(pool, inserted_community.id)
460 Person::delete(pool, inserted_person.id).await.unwrap();
461 Instance::delete(pool, inserted_instance.id).await.unwrap();
463 assert_eq!(expected_community, read_community);
464 assert_eq!(expected_community, inserted_community);
465 assert_eq!(expected_community, updated_community);
466 assert_eq!(expected_community_follower, inserted_community_follower);
467 assert_eq!(expected_community_moderator, inserted_community_moderator);
468 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
469 assert_eq!(1, ignored_community);
470 assert_eq!(1, left_community);
471 assert_eq!(1, unban);
472 // assert_eq!(2, loaded_count);
473 assert_eq!(1, num_deleted);