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: &Url) -> 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(conn: &PgConnection, followers_url: &Url) -> Result<Community, Error>;
137 impl Community_ for Community {
138 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
139 use lemmy_db_schema::schema::community::dsl::*;
141 .filter(local.eq(true))
142 .filter(name.eq(community_name))
150 ) -> Result<Community, Error> {
151 use lemmy_db_schema::schema::community::dsl::*;
152 diesel::update(community.find(community_id))
153 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
154 .get_result::<Self>(conn)
161 ) -> Result<Community, Error> {
162 use lemmy_db_schema::schema::community::dsl::*;
163 diesel::update(community.find(community_id))
164 .set((removed.eq(new_removed), updated.eq(naive_now())))
165 .get_result::<Self>(conn)
168 fn update_removed_for_creator(
172 ) -> Result<Vec<Community>, Error> {
173 use lemmy_db_schema::schema::community::dsl::*;
174 diesel::update(community.filter(creator_id.eq(for_creator_id)))
175 .set((removed.eq(new_removed), updated.eq(naive_now())))
176 .get_results::<Self>(conn)
183 ) -> Result<Community, Error> {
184 use lemmy_db_schema::schema::community::dsl::*;
185 diesel::update(community.find(community_id))
186 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
187 .get_result::<Self>(conn)
190 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
191 use lemmy_db_schema::schema::community::dsl::*;
192 community.select(actor_id).distinct().load::<String>(conn)
195 fn read_from_followers_url(
197 followers_url_: &Url,
198 ) -> Result<Community, Error> {
199 use lemmy_db_schema::schema::community::dsl::*;
201 .filter(followers_url.eq(followers_url_))
206 impl Joinable<CommunityModeratorForm> for CommunityModerator {
209 community_user_form: &CommunityModeratorForm,
210 ) -> Result<Self, Error> {
211 use lemmy_db_schema::schema::community_moderator::dsl::*;
212 insert_into(community_moderator)
213 .values(community_user_form)
214 .get_result::<Self>(conn)
219 community_user_form: &CommunityModeratorForm,
220 ) -> Result<usize, Error> {
221 use lemmy_db_schema::schema::community_moderator::dsl::*;
224 .filter(community_id.eq(community_user_form.community_id))
225 .filter(user_id.eq(community_user_form.user_id)),
231 pub trait CommunityModerator_ {
232 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
233 fn get_user_moderated_communities(
236 ) -> Result<Vec<i32>, Error>;
239 impl CommunityModerator_ for CommunityModerator {
240 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
241 use lemmy_db_schema::schema::community_moderator::dsl::*;
242 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
245 fn get_user_moderated_communities(
248 ) -> Result<Vec<i32>, Error> {
249 use lemmy_db_schema::schema::community_moderator::dsl::*;
251 .filter(user_id.eq(for_user_id))
252 .select(community_id)
257 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
260 community_user_ban_form: &CommunityUserBanForm,
261 ) -> Result<Self, Error> {
262 use lemmy_db_schema::schema::community_user_ban::dsl::*;
263 insert_into(community_user_ban)
264 .values(community_user_ban_form)
265 .get_result::<Self>(conn)
270 community_user_ban_form: &CommunityUserBanForm,
271 ) -> Result<usize, Error> {
272 use lemmy_db_schema::schema::community_user_ban::dsl::*;
275 .filter(community_id.eq(community_user_ban_form.community_id))
276 .filter(user_id.eq(community_user_ban_form.user_id)),
282 impl Followable<CommunityFollowerForm> for CommunityFollower {
285 community_follower_form: &CommunityFollowerForm,
286 ) -> Result<Self, Error> {
287 use lemmy_db_schema::schema::community_follower::dsl::*;
288 insert_into(community_follower)
289 .values(community_follower_form)
290 .on_conflict((community_id, user_id))
292 .set(community_follower_form)
293 .get_result::<Self>(conn)
295 fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
299 use lemmy_db_schema::schema::community_follower::dsl::*;
302 .filter(community_id.eq(community_id_))
303 .filter(user_id.eq(user_id_)),
305 .set(pending.eq(true))
306 .get_result::<Self>(conn)
310 community_follower_form: &CommunityFollowerForm,
311 ) -> Result<usize, Error> {
312 use lemmy_db_schema::schema::community_follower::dsl::*;
315 .filter(community_id.eq(&community_follower_form.community_id))
316 .filter(user_id.eq(&community_follower_form.user_id)),
320 // TODO: this function name only makes sense if you call it with a remote community. for a local
321 // community, it will also return true if only remote followers exist
322 fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> {
323 use lemmy_db_schema::schema::community_follower::dsl::*;
324 diesel::select(exists(
325 community_follower.filter(community_id.eq(community_id_)),
334 establish_unpooled_connection,
342 use lemmy_db_schema::source::{community::*, user::*};
343 use serial_test::serial;
348 let conn = establish_unpooled_connection();
350 let new_user = UserForm {
351 name: "bobbee".into(),
352 preferred_username: None,
353 password_encrypted: "nope".into(),
355 matrix_user_id: None,
363 theme: "browser".into(),
364 default_sort_type: SortType::Hot as i16,
365 default_listing_type: ListingType::Subscribed as i16,
366 lang: "browser".into(),
368 send_notifications_to_email: false,
374 last_refreshed_at: None,
376 shared_inbox_url: None,
379 let inserted_user = User_::create(&conn, &new_user).unwrap();
381 let new_community = CommunityForm {
383 creator_id: inserted_user.id,
384 title: "nada".to_owned(),
394 last_refreshed_at: None,
400 shared_inbox_url: None,
403 let inserted_community = Community::create(&conn, &new_community).unwrap();
405 let expected_community = Community {
406 id: inserted_community.id,
407 creator_id: inserted_user.id,
409 title: "nada".to_owned(),
414 published: inserted_community.published,
416 actor_id: inserted_community.actor_id.to_owned(),
420 last_refreshed_at: inserted_community.published,
423 followers_url: inserted_community.followers_url.to_owned(),
424 inbox_url: inserted_community.inbox_url.to_owned(),
425 shared_inbox_url: None,
428 let community_follower_form = CommunityFollowerForm {
429 community_id: inserted_community.id,
430 user_id: inserted_user.id,
434 let inserted_community_follower =
435 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
437 let expected_community_follower = CommunityFollower {
438 id: inserted_community_follower.id,
439 community_id: inserted_community.id,
440 user_id: inserted_user.id,
441 pending: Some(false),
442 published: inserted_community_follower.published,
445 let community_user_form = CommunityModeratorForm {
446 community_id: inserted_community.id,
447 user_id: inserted_user.id,
450 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
452 let expected_community_user = CommunityModerator {
453 id: inserted_community_user.id,
454 community_id: inserted_community.id,
455 user_id: inserted_user.id,
456 published: inserted_community_user.published,
459 let community_user_ban_form = CommunityUserBanForm {
460 community_id: inserted_community.id,
461 user_id: inserted_user.id,
464 let inserted_community_user_ban =
465 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
467 let expected_community_user_ban = CommunityUserBan {
468 id: inserted_community_user_ban.id,
469 community_id: inserted_community.id,
470 user_id: inserted_user.id,
471 published: inserted_community_user_ban.published,
474 let read_community = Community::read(&conn, inserted_community.id).unwrap();
475 let updated_community =
476 Community::update(&conn, inserted_community.id, &new_community).unwrap();
477 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
478 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
479 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
480 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
481 User_::delete(&conn, inserted_user.id).unwrap();
483 assert_eq!(expected_community, read_community);
484 assert_eq!(expected_community, inserted_community);
485 assert_eq!(expected_community, updated_community);
486 assert_eq!(expected_community_follower, inserted_community_follower);
487 assert_eq!(expected_community_user, inserted_community_user);
488 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
489 assert_eq!(1, ignored_community);
490 assert_eq!(1, left_community);
491 assert_eq!(1, unban);
492 // assert_eq!(2, loaded_count);
493 assert_eq!(1, num_deleted);