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 leave_all_communities(
157 for_person_id: PersonId,
158 ) -> Result<usize, Error> {
159 use crate::schema::community_moderator::dsl::{community_moderator, person_id};
160 let conn = &mut get_conn(pool).await?;
161 diesel::delete(community_moderator.filter(person_id.eq(for_person_id)))
166 pub async fn get_person_moderated_communities(
168 for_person_id: PersonId,
169 ) -> Result<Vec<CommunityId>, Error> {
170 use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
171 let conn = &mut get_conn(pool).await?;
173 .filter(person_id.eq(for_person_id))
174 .select(community_id)
175 .load::<CommunityId>(conn)
181 impl Bannable for CommunityPersonBan {
182 type Form = CommunityPersonBanForm;
185 community_person_ban_form: &CommunityPersonBanForm,
186 ) -> Result<Self, Error> {
187 use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
188 let conn = &mut get_conn(pool).await?;
189 insert_into(community_person_ban)
190 .values(community_person_ban_form)
191 .on_conflict((community_id, person_id))
193 .set(community_person_ban_form)
194 .get_result::<Self>(conn)
200 community_person_ban_form: &CommunityPersonBanForm,
201 ) -> Result<usize, Error> {
202 use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
203 let conn = &mut get_conn(pool).await?;
206 .filter(community_id.eq(community_person_ban_form.community_id))
207 .filter(person_id.eq(community_person_ban_form.person_id)),
214 impl CommunityFollower {
215 pub fn to_subscribed_type(follower: &Option<Self>) -> SubscribedType {
219 SubscribedType::Pending
221 SubscribedType::Subscribed
224 // If the row doesn't exist, the person isn't a follower.
225 None => SubscribedType::NotSubscribed,
231 impl Followable for CommunityFollower {
232 type Form = CommunityFollowerForm;
233 async fn follow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<Self, Error> {
234 use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
235 let conn = &mut get_conn(pool).await?;
236 insert_into(community_follower)
238 .on_conflict((community_id, person_id))
241 .get_result::<Self>(conn)
244 async fn follow_accepted(
246 community_id_: CommunityId,
247 person_id_: PersonId,
248 ) -> Result<Self, Error> {
249 use crate::schema::community_follower::dsl::{
255 let conn = &mut get_conn(pool).await?;
258 .filter(community_id.eq(community_id_))
259 .filter(person_id.eq(person_id_)),
261 .set(pending.eq(false))
262 .get_result::<Self>(conn)
265 async fn unfollow(pool: &DbPool, form: &CommunityFollowerForm) -> Result<usize, Error> {
266 use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
267 let conn = &mut get_conn(pool).await?;
270 .filter(community_id.eq(&form.community_id))
271 .filter(person_id.eq(&form.person_id)),
279 impl ApubActor for Community {
280 async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
281 let conn = &mut get_conn(pool).await?;
284 .filter(actor_id.eq(object_id))
285 .first::<Community>(conn)
292 async fn read_from_name(
294 community_name: &str,
295 include_deleted: bool,
296 ) -> Result<Community, Error> {
297 let conn = &mut get_conn(pool).await?;
298 let mut q = community
300 .filter(local.eq(true))
301 .filter(lower(name).eq(lower(community_name)));
302 if !include_deleted {
303 q = q.filter(deleted.eq(false)).filter(removed.eq(false));
305 q.first::<Self>(conn).await
308 async fn read_from_name_and_domain(
310 community_name: &str,
311 protocol_domain: &str,
312 ) -> Result<Community, Error> {
313 let conn = &mut get_conn(pool).await?;
315 .filter(lower(name).eq(lower(community_name)))
316 .filter(actor_id.like(format!("{protocol_domain}%")))
329 CommunityFollowerForm,
332 CommunityModeratorForm,
334 CommunityPersonBanForm,
338 person::{Person, PersonInsertForm},
340 traits::{Bannable, Crud, Followable, Joinable},
341 utils::build_db_pool_for_tests,
343 use serial_test::serial;
347 async fn test_crud() {
348 let pool = &build_db_pool_for_tests().await;
350 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
354 let new_person = PersonInsertForm::builder()
355 .name("bobbee".into())
356 .public_key("pubkey".to_string())
357 .instance_id(inserted_instance.id)
360 let inserted_person = Person::create(pool, &new_person).await.unwrap();
362 let new_community = CommunityInsertForm::builder()
364 .title("nada".to_owned())
365 .public_key("pubkey".to_string())
366 .instance_id(inserted_instance.id)
369 let inserted_community = Community::create(pool, &new_community).await.unwrap();
371 let expected_community = Community {
372 id: inserted_community.id,
374 title: "nada".to_owned(),
379 published: inserted_community.published,
381 actor_id: inserted_community.actor_id.clone(),
384 public_key: "pubkey".to_owned(),
385 last_refreshed_at: inserted_community.published,
388 followers_url: inserted_community.followers_url.clone(),
389 inbox_url: inserted_community.inbox_url.clone(),
390 shared_inbox_url: None,
391 moderators_url: None,
394 posting_restricted_to_mods: false,
395 instance_id: inserted_instance.id,
398 let community_follower_form = CommunityFollowerForm {
399 community_id: inserted_community.id,
400 person_id: inserted_person.id,
404 let inserted_community_follower = CommunityFollower::follow(pool, &community_follower_form)
408 let expected_community_follower = CommunityFollower {
409 id: inserted_community_follower.id,
410 community_id: inserted_community.id,
411 person_id: inserted_person.id,
413 published: inserted_community_follower.published,
416 let community_moderator_form = CommunityModeratorForm {
417 community_id: inserted_community.id,
418 person_id: inserted_person.id,
421 let inserted_community_moderator = CommunityModerator::join(pool, &community_moderator_form)
425 let expected_community_moderator = CommunityModerator {
426 id: inserted_community_moderator.id,
427 community_id: inserted_community.id,
428 person_id: inserted_person.id,
429 published: inserted_community_moderator.published,
432 let community_person_ban_form = CommunityPersonBanForm {
433 community_id: inserted_community.id,
434 person_id: inserted_person.id,
438 let inserted_community_person_ban = CommunityPersonBan::ban(pool, &community_person_ban_form)
442 let expected_community_person_ban = CommunityPersonBan {
443 id: inserted_community_person_ban.id,
444 community_id: inserted_community.id,
445 person_id: inserted_person.id,
446 published: inserted_community_person_ban.published,
450 let read_community = Community::read(pool, inserted_community.id).await.unwrap();
452 let update_community_form = CommunityUpdateForm::builder()
453 .title(Some("nada".to_owned()))
455 let updated_community = Community::update(pool, inserted_community.id, &update_community_form)
459 let ignored_community = CommunityFollower::unfollow(pool, &community_follower_form)
462 let left_community = CommunityModerator::leave(pool, &community_moderator_form)
465 let unban = CommunityPersonBan::unban(pool, &community_person_ban_form)
468 let num_deleted = Community::delete(pool, inserted_community.id)
471 Person::delete(pool, inserted_person.id).await.unwrap();
472 Instance::delete(pool, inserted_instance.id).await.unwrap();
474 assert_eq!(expected_community, read_community);
475 assert_eq!(expected_community, inserted_community);
476 assert_eq!(expected_community, updated_community);
477 assert_eq!(expected_community_follower, inserted_community_follower);
478 assert_eq!(expected_community_moderator, inserted_community_moderator);
479 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
480 assert_eq!(1, ignored_community);
481 assert_eq!(1, left_community);
482 assert_eq!(1, unban);
483 // assert_eq!(2, loaded_count);
484 assert_eq!(1, num_deleted);