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::*};
346 let conn = establish_unpooled_connection();
348 let new_user = UserForm {
349 name: "bobbee".into(),
350 preferred_username: None,
351 password_encrypted: "nope".into(),
353 matrix_user_id: None,
361 theme: "browser".into(),
362 default_sort_type: SortType::Hot as i16,
363 default_listing_type: ListingType::Subscribed as i16,
364 lang: "browser".into(),
366 send_notifications_to_email: false,
372 last_refreshed_at: None,
374 shared_inbox_url: None,
377 let inserted_user = User_::create(&conn, &new_user).unwrap();
379 let new_community = CommunityForm {
381 creator_id: inserted_user.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_user.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 user_id: inserted_user.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 user_id: inserted_user.id,
439 pending: Some(false),
440 published: inserted_community_follower.published,
443 let community_user_form = CommunityModeratorForm {
444 community_id: inserted_community.id,
445 user_id: inserted_user.id,
448 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
450 let expected_community_user = CommunityModerator {
451 id: inserted_community_user.id,
452 community_id: inserted_community.id,
453 user_id: inserted_user.id,
454 published: inserted_community_user.published,
457 let community_user_ban_form = CommunityUserBanForm {
458 community_id: inserted_community.id,
459 user_id: inserted_user.id,
462 let inserted_community_user_ban =
463 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
465 let expected_community_user_ban = CommunityUserBan {
466 id: inserted_community_user_ban.id,
467 community_id: inserted_community.id,
468 user_id: inserted_user.id,
469 published: inserted_community_user_ban.published,
472 let read_community = Community::read(&conn, inserted_community.id).unwrap();
473 let updated_community =
474 Community::update(&conn, inserted_community.id, &new_community).unwrap();
475 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
476 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
477 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
478 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
479 User_::delete(&conn, inserted_user.id).unwrap();
481 assert_eq!(expected_community, read_community);
482 assert_eq!(expected_community, inserted_community);
483 assert_eq!(expected_community, updated_community);
484 assert_eq!(expected_community_follower, inserted_community_follower);
485 assert_eq!(expected_community_user, inserted_community_user);
486 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
487 assert_eq!(1, ignored_community);
488 assert_eq!(1, left_community);
489 assert_eq!(1, unban);
490 // assert_eq!(2, loaded_count);
491 assert_eq!(1, num_deleted);