1 use crate::{ApubObject, Bannable, Crud, Followable, Joinable};
2 use diesel::{dsl::*, result::Error, *};
11 CommunityModeratorForm,
13 CommunityPersonBanForm,
21 use crate::{source::community::Community, ToSafe};
22 use lemmy_db_schema::schema::community::*;
41 impl ToSafe for Community {
42 type SafeColumns = Columns;
43 fn safe_columns_tuple() -> Self::SafeColumns {
64 impl Crud<CommunityForm, CommunityId> for Community {
65 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
66 use lemmy_db_schema::schema::community::dsl::*;
67 community.find(community_id).first::<Self>(conn)
70 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
71 use lemmy_db_schema::schema::community::dsl::*;
72 diesel::delete(community.find(community_id)).execute(conn)
75 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
76 use lemmy_db_schema::schema::community::dsl::*;
77 insert_into(community)
78 .values(new_community)
79 .get_result::<Self>(conn)
84 community_id: CommunityId,
85 new_community: &CommunityForm,
86 ) -> Result<Self, Error> {
87 use lemmy_db_schema::schema::community::dsl::*;
88 diesel::update(community.find(community_id))
90 .get_result::<Self>(conn)
94 impl ApubObject<CommunityForm> for Community {
95 fn read_from_apub_id(conn: &PgConnection, for_actor_id: &DbUrl) -> Result<Self, Error> {
96 use lemmy_db_schema::schema::community::dsl::*;
98 .filter(actor_id.eq(for_actor_id))
102 fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
103 use lemmy_db_schema::schema::community::dsl::*;
104 insert_into(community)
105 .values(community_form)
106 .on_conflict(actor_id)
109 .get_result::<Self>(conn)
113 pub trait Community_ {
114 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
117 community_id: CommunityId,
119 ) -> Result<Community, Error>;
122 community_id: CommunityId,
124 ) -> Result<Community, Error>;
125 fn update_removed_for_creator(
127 for_creator_id: PersonId,
129 ) -> Result<Vec<Community>, Error>;
132 community_id: CommunityId,
133 new_creator_id: PersonId,
134 ) -> Result<Community, Error>;
135 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error>;
136 fn read_from_followers_url(
138 followers_url: &DbUrl,
139 ) -> Result<Community, Error>;
142 impl Community_ for Community {
143 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
144 use lemmy_db_schema::schema::community::dsl::*;
146 .filter(local.eq(true))
147 .filter(name.eq(community_name))
153 community_id: CommunityId,
155 ) -> Result<Community, Error> {
156 use lemmy_db_schema::schema::community::dsl::*;
157 diesel::update(community.find(community_id))
158 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
159 .get_result::<Self>(conn)
164 community_id: CommunityId,
166 ) -> Result<Community, Error> {
167 use lemmy_db_schema::schema::community::dsl::*;
168 diesel::update(community.find(community_id))
169 .set((removed.eq(new_removed), updated.eq(naive_now())))
170 .get_result::<Self>(conn)
173 fn update_removed_for_creator(
175 for_creator_id: PersonId,
177 ) -> Result<Vec<Community>, Error> {
178 use lemmy_db_schema::schema::community::dsl::*;
179 diesel::update(community.filter(creator_id.eq(for_creator_id)))
180 .set((removed.eq(new_removed), updated.eq(naive_now())))
181 .get_results::<Self>(conn)
186 community_id: CommunityId,
187 new_creator_id: PersonId,
188 ) -> Result<Community, Error> {
189 use lemmy_db_schema::schema::community::dsl::*;
190 diesel::update(community.find(community_id))
191 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
192 .get_result::<Self>(conn)
195 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
196 use lemmy_db_schema::schema::community::dsl::*;
197 community.select(actor_id).distinct().load::<String>(conn)
200 fn read_from_followers_url(
202 followers_url_: &DbUrl,
203 ) -> Result<Community, Error> {
204 use lemmy_db_schema::schema::community::dsl::*;
206 .filter(followers_url.eq(followers_url_))
211 impl Joinable<CommunityModeratorForm> for CommunityModerator {
214 community_moderator_form: &CommunityModeratorForm,
215 ) -> Result<Self, Error> {
216 use lemmy_db_schema::schema::community_moderator::dsl::*;
217 insert_into(community_moderator)
218 .values(community_moderator_form)
219 .get_result::<Self>(conn)
224 community_moderator_form: &CommunityModeratorForm,
225 ) -> Result<usize, Error> {
226 use lemmy_db_schema::schema::community_moderator::dsl::*;
229 .filter(community_id.eq(community_moderator_form.community_id))
230 .filter(person_id.eq(community_moderator_form.person_id)),
236 pub trait CommunityModerator_ {
237 fn delete_for_community(
239 for_community_id: CommunityId,
240 ) -> Result<usize, Error>;
241 fn get_person_moderated_communities(
243 for_person_id: PersonId,
244 ) -> Result<Vec<CommunityId>, Error>;
247 impl CommunityModerator_ for CommunityModerator {
248 fn delete_for_community(
250 for_community_id: CommunityId,
251 ) -> Result<usize, Error> {
252 use lemmy_db_schema::schema::community_moderator::dsl::*;
253 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
256 fn get_person_moderated_communities(
258 for_person_id: PersonId,
259 ) -> Result<Vec<CommunityId>, Error> {
260 use lemmy_db_schema::schema::community_moderator::dsl::*;
262 .filter(person_id.eq(for_person_id))
263 .select(community_id)
264 .load::<CommunityId>(conn)
268 impl Bannable<CommunityPersonBanForm> for CommunityPersonBan {
271 community_person_ban_form: &CommunityPersonBanForm,
272 ) -> Result<Self, Error> {
273 use lemmy_db_schema::schema::community_person_ban::dsl::*;
274 insert_into(community_person_ban)
275 .values(community_person_ban_form)
276 .get_result::<Self>(conn)
281 community_person_ban_form: &CommunityPersonBanForm,
282 ) -> Result<usize, Error> {
283 use lemmy_db_schema::schema::community_person_ban::dsl::*;
286 .filter(community_id.eq(community_person_ban_form.community_id))
287 .filter(person_id.eq(community_person_ban_form.person_id)),
293 impl Followable<CommunityFollowerForm> for CommunityFollower {
296 community_follower_form: &CommunityFollowerForm,
297 ) -> Result<Self, Error> {
298 use lemmy_db_schema::schema::community_follower::dsl::*;
299 insert_into(community_follower)
300 .values(community_follower_form)
301 .on_conflict((community_id, person_id))
303 .set(community_follower_form)
304 .get_result::<Self>(conn)
308 community_id_: CommunityId,
309 person_id_: PersonId,
310 ) -> Result<Self, Error>
314 use lemmy_db_schema::schema::community_follower::dsl::*;
317 .filter(community_id.eq(community_id_))
318 .filter(person_id.eq(person_id_)),
320 .set(pending.eq(true))
321 .get_result::<Self>(conn)
325 community_follower_form: &CommunityFollowerForm,
326 ) -> Result<usize, Error> {
327 use lemmy_db_schema::schema::community_follower::dsl::*;
330 .filter(community_id.eq(&community_follower_form.community_id))
331 .filter(person_id.eq(&community_follower_form.person_id)),
335 // TODO: this function name only makes sense if you call it with a remote community. for a local
336 // community, it will also return true if only remote followers exist
337 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
338 use lemmy_db_schema::schema::community_follower::dsl::*;
339 diesel::select(exists(
340 community_follower.filter(community_id.eq(community_id_)),
348 use crate::{establish_unpooled_connection, Bannable, Crud, Followable, Joinable};
349 use lemmy_db_schema::source::{community::*, person::*};
350 use serial_test::serial;
355 let conn = establish_unpooled_connection();
357 let new_person = PersonForm {
358 name: "bobbee".into(),
359 preferred_username: None,
371 last_refreshed_at: None,
373 shared_inbox_url: None,
374 matrix_user_id: None,
377 let inserted_person = Person::create(&conn, &new_person).unwrap();
379 let new_community = CommunityForm {
381 creator_id: inserted_person.id,
382 title: "nada".to_owned(),
392 last_refreshed_at: None,
398 shared_inbox_url: None,
401 let inserted_community = Community::create(&conn, &new_community).unwrap();
403 let expected_community = Community {
404 id: inserted_community.id,
405 creator_id: inserted_person.id,
407 title: "nada".to_owned(),
412 published: inserted_community.published,
414 actor_id: inserted_community.actor_id.to_owned(),
418 last_refreshed_at: inserted_community.published,
421 followers_url: inserted_community.followers_url.to_owned(),
422 inbox_url: inserted_community.inbox_url.to_owned(),
423 shared_inbox_url: None,
426 let community_follower_form = CommunityFollowerForm {
427 community_id: inserted_community.id,
428 person_id: inserted_person.id,
432 let inserted_community_follower =
433 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
435 let expected_community_follower = CommunityFollower {
436 id: inserted_community_follower.id,
437 community_id: inserted_community.id,
438 person_id: inserted_person.id,
439 pending: Some(false),
440 published: inserted_community_follower.published,
443 let community_moderator_form = CommunityModeratorForm {
444 community_id: inserted_community.id,
445 person_id: inserted_person.id,
448 let inserted_community_moderator =
449 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
451 let expected_community_moderator = CommunityModerator {
452 id: inserted_community_moderator.id,
453 community_id: inserted_community.id,
454 person_id: inserted_person.id,
455 published: inserted_community_moderator.published,
458 let community_person_ban_form = CommunityPersonBanForm {
459 community_id: inserted_community.id,
460 person_id: inserted_person.id,
463 let inserted_community_person_ban =
464 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
466 let expected_community_person_ban = CommunityPersonBan {
467 id: inserted_community_person_ban.id,
468 community_id: inserted_community.id,
469 person_id: inserted_person.id,
470 published: inserted_community_person_ban.published,
473 let read_community = Community::read(&conn, inserted_community.id).unwrap();
474 let updated_community =
475 Community::update(&conn, inserted_community.id, &new_community).unwrap();
476 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
477 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
478 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
479 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
480 Person::delete(&conn, inserted_person.id).unwrap();
482 assert_eq!(expected_community, read_community);
483 assert_eq!(expected_community, inserted_community);
484 assert_eq!(expected_community, updated_community);
485 assert_eq!(expected_community_follower, inserted_community_follower);
486 assert_eq!(expected_community_moderator, inserted_community_moderator);
487 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
488 assert_eq!(1, ignored_community);
489 assert_eq!(1, left_community);
490 assert_eq!(1, unban);
491 // assert_eq!(2, loaded_count);
492 assert_eq!(1, num_deleted);