3 newtypes::{CommunityId, DbUrl, PersonId},
10 CommunityModeratorForm,
12 CommunityPersonBanForm,
15 traits::{Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
17 use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
21 use crate::{schema::community::*, source::community::Community, traits::ToSafe};
39 impl ToSafe for Community {
40 type SafeColumns = Columns;
41 fn safe_columns_tuple() -> Self::SafeColumns {
61 impl Crud for Community {
62 type Form = CommunityForm;
63 type IdType = CommunityId;
64 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
65 use crate::schema::community::dsl::*;
66 community.find(community_id).first::<Self>(conn)
69 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
70 use crate::schema::community::dsl::*;
71 diesel::delete(community.find(community_id)).execute(conn)
74 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
75 use crate::schema::community::dsl::*;
76 insert_into(community)
77 .values(new_community)
78 .get_result::<Self>(conn)
83 community_id: CommunityId,
84 new_community: &CommunityForm,
85 ) -> Result<Self, Error> {
86 use crate::schema::community::dsl::*;
87 diesel::update(community.find(community_id))
89 .get_result::<Self>(conn)
94 pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
95 use crate::schema::community::dsl::*;
97 .filter(local.eq(true))
98 .filter(name.eq(community_name))
102 pub fn update_deleted(
104 community_id: CommunityId,
106 ) -> Result<Community, Error> {
107 use crate::schema::community::dsl::*;
108 diesel::update(community.find(community_id))
109 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
110 .get_result::<Self>(conn)
113 pub fn update_removed(
115 community_id: CommunityId,
117 ) -> Result<Community, Error> {
118 use crate::schema::community::dsl::*;
119 diesel::update(community.find(community_id))
120 .set((removed.eq(new_removed), updated.eq(naive_now())))
121 .get_result::<Self>(conn)
124 pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
125 use crate::schema::community::dsl::*;
126 community.select(actor_id).distinct().load::<String>(conn)
129 pub fn read_from_followers_url(
131 followers_url_: &DbUrl,
132 ) -> Result<Community, Error> {
133 use crate::schema::community::dsl::*;
135 .filter(followers_url.eq(followers_url_))
139 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
140 use crate::schema::community::dsl::*;
141 insert_into(community)
142 .values(community_form)
143 .on_conflict(actor_id)
146 .get_result::<Self>(conn)
148 pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
149 use crate::schema::community::dsl::*;
150 let object_id: DbUrl = object_id.into();
153 .filter(actor_id.eq(object_id))
154 .first::<Community>(conn)
161 impl Joinable for CommunityModerator {
162 type Form = CommunityModeratorForm;
165 community_moderator_form: &CommunityModeratorForm,
166 ) -> Result<Self, Error> {
167 use crate::schema::community_moderator::dsl::*;
168 insert_into(community_moderator)
169 .values(community_moderator_form)
170 .get_result::<Self>(conn)
175 community_moderator_form: &CommunityModeratorForm,
176 ) -> Result<usize, Error> {
177 use crate::schema::community_moderator::dsl::*;
180 .filter(community_id.eq(community_moderator_form.community_id))
181 .filter(person_id.eq(community_moderator_form.person_id)),
187 impl DeleteableOrRemoveable for CommunitySafe {
188 fn blank_out_deleted_or_removed_info(mut self) -> Self {
189 self.title = "".into();
190 self.description = None;
197 impl DeleteableOrRemoveable for Community {
198 fn blank_out_deleted_or_removed_info(mut self) -> Self {
199 self.title = "".into();
200 self.description = None;
207 impl CommunityModerator {
208 pub fn delete_for_community(
210 for_community_id: CommunityId,
211 ) -> Result<usize, Error> {
212 use crate::schema::community_moderator::dsl::*;
213 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
216 pub fn get_person_moderated_communities(
218 for_person_id: PersonId,
219 ) -> Result<Vec<CommunityId>, Error> {
220 use crate::schema::community_moderator::dsl::*;
222 .filter(person_id.eq(for_person_id))
223 .select(community_id)
224 .load::<CommunityId>(conn)
228 impl Bannable for CommunityPersonBan {
229 type Form = CommunityPersonBanForm;
232 community_person_ban_form: &CommunityPersonBanForm,
233 ) -> Result<Self, Error> {
234 use crate::schema::community_person_ban::dsl::*;
235 insert_into(community_person_ban)
236 .values(community_person_ban_form)
237 .get_result::<Self>(conn)
242 community_person_ban_form: &CommunityPersonBanForm,
243 ) -> Result<usize, Error> {
244 use crate::schema::community_person_ban::dsl::*;
247 .filter(community_id.eq(community_person_ban_form.community_id))
248 .filter(person_id.eq(community_person_ban_form.person_id)),
254 impl Followable for CommunityFollower {
255 type Form = CommunityFollowerForm;
258 community_follower_form: &CommunityFollowerForm,
259 ) -> Result<Self, Error> {
260 use crate::schema::community_follower::dsl::*;
261 insert_into(community_follower)
262 .values(community_follower_form)
263 .on_conflict((community_id, person_id))
265 .set(community_follower_form)
266 .get_result::<Self>(conn)
270 community_id_: CommunityId,
271 person_id_: PersonId,
272 ) -> Result<Self, Error>
276 use crate::schema::community_follower::dsl::*;
279 .filter(community_id.eq(community_id_))
280 .filter(person_id.eq(person_id_)),
282 .set(pending.eq(true))
283 .get_result::<Self>(conn)
287 community_follower_form: &CommunityFollowerForm,
288 ) -> Result<usize, Error> {
289 use crate::schema::community_follower::dsl::*;
292 .filter(community_id.eq(&community_follower_form.community_id))
293 .filter(person_id.eq(&community_follower_form.person_id)),
297 // TODO: this function name only makes sense if you call it with a remote community. for a local
298 // community, it will also return true if only remote followers exist
299 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
300 use crate::schema::community_follower::dsl::*;
301 diesel::select(exists(
302 community_follower.filter(community_id.eq(community_id_)),
311 establish_unpooled_connection,
312 source::{community::*, person::*},
313 traits::{Bannable, Crud, Followable, Joinable},
315 use serial_test::serial;
320 let conn = establish_unpooled_connection();
322 let new_person = PersonForm {
323 name: "bobbee".into(),
324 ..PersonForm::default()
327 let inserted_person = Person::create(&conn, &new_person).unwrap();
329 let new_community = CommunityForm {
331 title: "nada".to_owned(),
332 ..CommunityForm::default()
335 let inserted_community = Community::create(&conn, &new_community).unwrap();
337 let expected_community = Community {
338 id: inserted_community.id,
340 title: "nada".to_owned(),
345 published: inserted_community.published,
347 actor_id: inserted_community.actor_id.to_owned(),
351 last_refreshed_at: inserted_community.published,
354 followers_url: inserted_community.followers_url.to_owned(),
355 inbox_url: inserted_community.inbox_url.to_owned(),
356 shared_inbox_url: None,
359 let community_follower_form = CommunityFollowerForm {
360 community_id: inserted_community.id,
361 person_id: inserted_person.id,
365 let inserted_community_follower =
366 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
368 let expected_community_follower = CommunityFollower {
369 id: inserted_community_follower.id,
370 community_id: inserted_community.id,
371 person_id: inserted_person.id,
372 pending: Some(false),
373 published: inserted_community_follower.published,
376 let community_moderator_form = CommunityModeratorForm {
377 community_id: inserted_community.id,
378 person_id: inserted_person.id,
381 let inserted_community_moderator =
382 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
384 let expected_community_moderator = CommunityModerator {
385 id: inserted_community_moderator.id,
386 community_id: inserted_community.id,
387 person_id: inserted_person.id,
388 published: inserted_community_moderator.published,
391 let community_person_ban_form = CommunityPersonBanForm {
392 community_id: inserted_community.id,
393 person_id: inserted_person.id,
396 let inserted_community_person_ban =
397 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
399 let expected_community_person_ban = CommunityPersonBan {
400 id: inserted_community_person_ban.id,
401 community_id: inserted_community.id,
402 person_id: inserted_person.id,
403 published: inserted_community_person_ban.published,
406 let read_community = Community::read(&conn, inserted_community.id).unwrap();
407 let updated_community =
408 Community::update(&conn, inserted_community.id, &new_community).unwrap();
409 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
410 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
411 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
412 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
413 Person::delete(&conn, inserted_person.id).unwrap();
415 assert_eq!(expected_community, read_community);
416 assert_eq!(expected_community, inserted_community);
417 assert_eq!(expected_community, updated_community);
418 assert_eq!(expected_community_follower, inserted_community_follower);
419 assert_eq!(expected_community_moderator, inserted_community_moderator);
420 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
421 assert_eq!(1, ignored_community);
422 assert_eq!(1, left_community);
423 assert_eq!(1, unban);
424 // assert_eq!(2, loaded_count);
425 assert_eq!(1, num_deleted);