1 use crate::{ApubObject, Bannable, Crud, Followable, Joinable};
2 use diesel::{dsl::*, result::Error, *};
11 CommunityModeratorForm,
19 use crate::{source::community::Community, ToSafe};
20 use lemmy_db_schema::schema::community::*;
39 impl ToSafe for Community {
40 type SafeColumns = Columns;
41 fn safe_columns_tuple() -> Self::SafeColumns {
62 impl Crud<CommunityForm> for Community {
63 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
64 use lemmy_db_schema::schema::community::dsl::*;
65 community.find(community_id).first::<Self>(conn)
68 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
69 use lemmy_db_schema::schema::community::dsl::*;
70 diesel::delete(community.find(community_id)).execute(conn)
73 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
74 use lemmy_db_schema::schema::community::dsl::*;
75 insert_into(community)
76 .values(new_community)
77 .get_result::<Self>(conn)
83 new_community: &CommunityForm,
84 ) -> Result<Self, Error> {
85 use lemmy_db_schema::schema::community::dsl::*;
86 diesel::update(community.find(community_id))
88 .get_result::<Self>(conn)
92 impl ApubObject<CommunityForm> for Community {
93 fn read_from_apub_id(conn: &PgConnection, for_actor_id: &DbUrl) -> Result<Self, Error> {
94 use lemmy_db_schema::schema::community::dsl::*;
96 .filter(actor_id.eq(for_actor_id))
100 fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
101 use lemmy_db_schema::schema::community::dsl::*;
102 insert_into(community)
103 .values(community_form)
104 .on_conflict(actor_id)
107 .get_result::<Self>(conn)
111 pub trait Community_ {
112 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
117 ) -> Result<Community, Error>;
122 ) -> Result<Community, Error>;
123 fn update_removed_for_creator(
127 ) -> Result<Vec<Community>, Error>;
132 ) -> Result<Community, Error>;
133 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error>;
134 fn read_from_followers_url(
136 followers_url: &DbUrl,
137 ) -> Result<Community, Error>;
140 impl Community_ for Community {
141 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
142 use lemmy_db_schema::schema::community::dsl::*;
144 .filter(local.eq(true))
145 .filter(name.eq(community_name))
153 ) -> Result<Community, Error> {
154 use lemmy_db_schema::schema::community::dsl::*;
155 diesel::update(community.find(community_id))
156 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
157 .get_result::<Self>(conn)
164 ) -> Result<Community, Error> {
165 use lemmy_db_schema::schema::community::dsl::*;
166 diesel::update(community.find(community_id))
167 .set((removed.eq(new_removed), updated.eq(naive_now())))
168 .get_result::<Self>(conn)
171 fn update_removed_for_creator(
175 ) -> Result<Vec<Community>, Error> {
176 use lemmy_db_schema::schema::community::dsl::*;
177 diesel::update(community.filter(creator_id.eq(for_creator_id)))
178 .set((removed.eq(new_removed), updated.eq(naive_now())))
179 .get_results::<Self>(conn)
186 ) -> Result<Community, Error> {
187 use lemmy_db_schema::schema::community::dsl::*;
188 diesel::update(community.find(community_id))
189 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
190 .get_result::<Self>(conn)
193 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
194 use lemmy_db_schema::schema::community::dsl::*;
195 community.select(actor_id).distinct().load::<String>(conn)
198 fn read_from_followers_url(
200 followers_url_: &DbUrl,
201 ) -> Result<Community, Error> {
202 use lemmy_db_schema::schema::community::dsl::*;
204 .filter(followers_url.eq(followers_url_))
209 impl Joinable<CommunityModeratorForm> for CommunityModerator {
212 community_user_form: &CommunityModeratorForm,
213 ) -> Result<Self, Error> {
214 use lemmy_db_schema::schema::community_moderator::dsl::*;
215 insert_into(community_moderator)
216 .values(community_user_form)
217 .get_result::<Self>(conn)
222 community_user_form: &CommunityModeratorForm,
223 ) -> Result<usize, Error> {
224 use lemmy_db_schema::schema::community_moderator::dsl::*;
227 .filter(community_id.eq(community_user_form.community_id))
228 .filter(user_id.eq(community_user_form.user_id)),
234 pub trait CommunityModerator_ {
235 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
236 fn get_user_moderated_communities(
239 ) -> Result<Vec<i32>, Error>;
242 impl CommunityModerator_ for CommunityModerator {
243 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
244 use lemmy_db_schema::schema::community_moderator::dsl::*;
245 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
248 fn get_user_moderated_communities(
251 ) -> Result<Vec<i32>, Error> {
252 use lemmy_db_schema::schema::community_moderator::dsl::*;
254 .filter(user_id.eq(for_user_id))
255 .select(community_id)
260 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
263 community_user_ban_form: &CommunityUserBanForm,
264 ) -> Result<Self, Error> {
265 use lemmy_db_schema::schema::community_user_ban::dsl::*;
266 insert_into(community_user_ban)
267 .values(community_user_ban_form)
268 .get_result::<Self>(conn)
273 community_user_ban_form: &CommunityUserBanForm,
274 ) -> Result<usize, Error> {
275 use lemmy_db_schema::schema::community_user_ban::dsl::*;
278 .filter(community_id.eq(community_user_ban_form.community_id))
279 .filter(user_id.eq(community_user_ban_form.user_id)),
285 impl Followable<CommunityFollowerForm> for CommunityFollower {
288 community_follower_form: &CommunityFollowerForm,
289 ) -> Result<Self, Error> {
290 use lemmy_db_schema::schema::community_follower::dsl::*;
291 insert_into(community_follower)
292 .values(community_follower_form)
293 .on_conflict((community_id, user_id))
295 .set(community_follower_form)
296 .get_result::<Self>(conn)
298 fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
302 use lemmy_db_schema::schema::community_follower::dsl::*;
305 .filter(community_id.eq(community_id_))
306 .filter(user_id.eq(user_id_)),
308 .set(pending.eq(true))
309 .get_result::<Self>(conn)
313 community_follower_form: &CommunityFollowerForm,
314 ) -> Result<usize, Error> {
315 use lemmy_db_schema::schema::community_follower::dsl::*;
318 .filter(community_id.eq(&community_follower_form.community_id))
319 .filter(user_id.eq(&community_follower_form.user_id)),
323 // TODO: this function name only makes sense if you call it with a remote community. for a local
324 // community, it will also return true if only remote followers exist
325 fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> {
326 use lemmy_db_schema::schema::community_follower::dsl::*;
327 diesel::select(exists(
328 community_follower.filter(community_id.eq(community_id_)),
337 establish_unpooled_connection,
345 use lemmy_db_schema::source::{community::*, user::*};
346 use serial_test::serial;
351 let conn = establish_unpooled_connection();
353 let new_user = UserForm {
354 name: "bobbee".into(),
355 preferred_username: None,
356 password_encrypted: "nope".into(),
358 matrix_user_id: None,
366 theme: "browser".into(),
367 default_sort_type: SortType::Hot as i16,
368 default_listing_type: ListingType::Subscribed as i16,
369 lang: "browser".into(),
371 send_notifications_to_email: false,
377 last_refreshed_at: None,
379 shared_inbox_url: None,
382 let inserted_user = User_::create(&conn, &new_user).unwrap();
384 let new_community = CommunityForm {
386 creator_id: inserted_user.id,
387 title: "nada".to_owned(),
397 last_refreshed_at: None,
403 shared_inbox_url: None,
406 let inserted_community = Community::create(&conn, &new_community).unwrap();
408 let expected_community = Community {
409 id: inserted_community.id,
410 creator_id: inserted_user.id,
412 title: "nada".to_owned(),
417 published: inserted_community.published,
419 actor_id: inserted_community.actor_id.to_owned(),
423 last_refreshed_at: inserted_community.published,
426 followers_url: inserted_community.followers_url.to_owned(),
427 inbox_url: inserted_community.inbox_url.to_owned(),
428 shared_inbox_url: None,
431 let community_follower_form = CommunityFollowerForm {
432 community_id: inserted_community.id,
433 user_id: inserted_user.id,
437 let inserted_community_follower =
438 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
440 let expected_community_follower = CommunityFollower {
441 id: inserted_community_follower.id,
442 community_id: inserted_community.id,
443 user_id: inserted_user.id,
444 pending: Some(false),
445 published: inserted_community_follower.published,
448 let community_user_form = CommunityModeratorForm {
449 community_id: inserted_community.id,
450 user_id: inserted_user.id,
453 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
455 let expected_community_user = CommunityModerator {
456 id: inserted_community_user.id,
457 community_id: inserted_community.id,
458 user_id: inserted_user.id,
459 published: inserted_community_user.published,
462 let community_user_ban_form = CommunityUserBanForm {
463 community_id: inserted_community.id,
464 user_id: inserted_user.id,
467 let inserted_community_user_ban =
468 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
470 let expected_community_user_ban = CommunityUserBan {
471 id: inserted_community_user_ban.id,
472 community_id: inserted_community.id,
473 user_id: inserted_user.id,
474 published: inserted_community_user_ban.published,
477 let read_community = Community::read(&conn, inserted_community.id).unwrap();
478 let updated_community =
479 Community::update(&conn, inserted_community.id, &new_community).unwrap();
480 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
481 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
482 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
483 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
484 User_::delete(&conn, inserted_user.id).unwrap();
486 assert_eq!(expected_community, read_community);
487 assert_eq!(expected_community, inserted_community);
488 assert_eq!(expected_community, updated_community);
489 assert_eq!(expected_community_follower, inserted_community_follower);
490 assert_eq!(expected_community_user, inserted_community_user);
491 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
492 assert_eq!(1, ignored_community);
493 assert_eq!(1, left_community);
494 assert_eq!(1, unban);
495 // assert_eq!(2, loaded_count);
496 assert_eq!(1, num_deleted);