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::*;
40 impl ToSafe for Community {
41 type SafeColumns = Columns;
42 fn safe_columns_tuple() -> Self::SafeColumns {
64 impl Crud<CommunityForm> for Community {
65 fn read(conn: &PgConnection, community_id: i32) -> 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: i32) -> 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)
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: &Url) -> 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>;
119 ) -> Result<Community, Error>;
124 ) -> Result<Community, Error>;
125 fn update_removed_for_creator(
129 ) -> Result<Vec<Community>, Error>;
134 ) -> Result<Community, Error>;
135 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error>;
138 impl Community_ for Community {
139 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
140 use lemmy_db_schema::schema::community::dsl::*;
142 .filter(local.eq(true))
143 .filter(name.eq(community_name))
151 ) -> Result<Community, Error> {
152 use lemmy_db_schema::schema::community::dsl::*;
153 diesel::update(community.find(community_id))
154 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
155 .get_result::<Self>(conn)
162 ) -> Result<Community, Error> {
163 use lemmy_db_schema::schema::community::dsl::*;
164 diesel::update(community.find(community_id))
165 .set((removed.eq(new_removed), updated.eq(naive_now())))
166 .get_result::<Self>(conn)
169 fn update_removed_for_creator(
173 ) -> Result<Vec<Community>, Error> {
174 use lemmy_db_schema::schema::community::dsl::*;
175 diesel::update(community.filter(creator_id.eq(for_creator_id)))
176 .set((removed.eq(new_removed), updated.eq(naive_now())))
177 .get_results::<Self>(conn)
184 ) -> Result<Community, Error> {
185 use lemmy_db_schema::schema::community::dsl::*;
186 diesel::update(community.find(community_id))
187 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
188 .get_result::<Self>(conn)
191 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
192 use lemmy_db_schema::schema::community::dsl::*;
193 community.select(actor_id).distinct().load::<String>(conn)
197 impl Joinable<CommunityModeratorForm> for CommunityModerator {
200 community_user_form: &CommunityModeratorForm,
201 ) -> Result<Self, Error> {
202 use lemmy_db_schema::schema::community_moderator::dsl::*;
203 insert_into(community_moderator)
204 .values(community_user_form)
205 .get_result::<Self>(conn)
210 community_user_form: &CommunityModeratorForm,
211 ) -> Result<usize, Error> {
212 use lemmy_db_schema::schema::community_moderator::dsl::*;
215 .filter(community_id.eq(community_user_form.community_id))
216 .filter(user_id.eq(community_user_form.user_id)),
222 pub trait CommunityModerator_ {
223 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
224 fn get_user_moderated_communities(
227 ) -> Result<Vec<i32>, Error>;
230 impl CommunityModerator_ for CommunityModerator {
231 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
232 use lemmy_db_schema::schema::community_moderator::dsl::*;
233 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
236 fn get_user_moderated_communities(
239 ) -> Result<Vec<i32>, Error> {
240 use lemmy_db_schema::schema::community_moderator::dsl::*;
242 .filter(user_id.eq(for_user_id))
243 .select(community_id)
248 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
251 community_user_ban_form: &CommunityUserBanForm,
252 ) -> Result<Self, Error> {
253 use lemmy_db_schema::schema::community_user_ban::dsl::*;
254 insert_into(community_user_ban)
255 .values(community_user_ban_form)
256 .get_result::<Self>(conn)
261 community_user_ban_form: &CommunityUserBanForm,
262 ) -> Result<usize, Error> {
263 use lemmy_db_schema::schema::community_user_ban::dsl::*;
266 .filter(community_id.eq(community_user_ban_form.community_id))
267 .filter(user_id.eq(community_user_ban_form.user_id)),
273 impl Followable<CommunityFollowerForm> for CommunityFollower {
276 community_follower_form: &CommunityFollowerForm,
277 ) -> Result<Self, Error> {
278 use lemmy_db_schema::schema::community_follower::dsl::*;
279 insert_into(community_follower)
280 .values(community_follower_form)
281 .on_conflict((community_id, user_id))
283 .set(community_follower_form)
284 .get_result::<Self>(conn)
286 fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
290 use lemmy_db_schema::schema::community_follower::dsl::*;
293 .filter(community_id.eq(community_id_))
294 .filter(user_id.eq(user_id_)),
296 .set(pending.eq(true))
297 .get_result::<Self>(conn)
301 community_follower_form: &CommunityFollowerForm,
302 ) -> Result<usize, Error> {
303 use lemmy_db_schema::schema::community_follower::dsl::*;
306 .filter(community_id.eq(&community_follower_form.community_id))
307 .filter(user_id.eq(&community_follower_form.user_id)),
311 // TODO: this function name only makes sense if you call it with a remote community. for a local
312 // community, it will also return true if only remote followers exist
313 fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> {
314 use lemmy_db_schema::schema::community_follower::dsl::*;
315 diesel::select(exists(
316 community_follower.filter(community_id.eq(community_id_)),
325 establish_unpooled_connection,
333 use lemmy_db_schema::source::{community::*, user::*};
337 let conn = establish_unpooled_connection();
339 let new_user = UserForm {
340 name: "bobbee".into(),
341 preferred_username: None,
342 password_encrypted: "nope".into(),
344 matrix_user_id: None,
352 theme: "browser".into(),
353 default_sort_type: SortType::Hot as i16,
354 default_listing_type: ListingType::Subscribed as i16,
355 lang: "browser".into(),
357 send_notifications_to_email: false,
363 last_refreshed_at: None,
366 let inserted_user = User_::create(&conn, &new_user).unwrap();
368 let new_community = CommunityForm {
370 creator_id: inserted_user.id,
371 title: "nada".to_owned(),
382 last_refreshed_at: None,
388 let inserted_community = Community::create(&conn, &new_community).unwrap();
390 let expected_community = Community {
391 id: inserted_community.id,
392 creator_id: inserted_user.id,
394 title: "nada".to_owned(),
400 published: inserted_community.published,
402 actor_id: inserted_community.actor_id.to_owned(),
406 last_refreshed_at: inserted_community.published,
411 let community_follower_form = CommunityFollowerForm {
412 community_id: inserted_community.id,
413 user_id: inserted_user.id,
417 let inserted_community_follower =
418 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
420 let expected_community_follower = CommunityFollower {
421 id: inserted_community_follower.id,
422 community_id: inserted_community.id,
423 user_id: inserted_user.id,
424 pending: Some(false),
425 published: inserted_community_follower.published,
428 let community_user_form = CommunityModeratorForm {
429 community_id: inserted_community.id,
430 user_id: inserted_user.id,
433 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
435 let expected_community_user = CommunityModerator {
436 id: inserted_community_user.id,
437 community_id: inserted_community.id,
438 user_id: inserted_user.id,
439 published: inserted_community_user.published,
442 let community_user_ban_form = CommunityUserBanForm {
443 community_id: inserted_community.id,
444 user_id: inserted_user.id,
447 let inserted_community_user_ban =
448 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
450 let expected_community_user_ban = CommunityUserBan {
451 id: inserted_community_user_ban.id,
452 community_id: inserted_community.id,
453 user_id: inserted_user.id,
454 published: inserted_community_user_ban.published,
457 let read_community = Community::read(&conn, inserted_community.id).unwrap();
458 let updated_community =
459 Community::update(&conn, inserted_community.id, &new_community).unwrap();
460 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
461 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
462 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
463 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
464 User_::delete(&conn, inserted_user.id).unwrap();
466 assert_eq!(expected_community, read_community);
467 assert_eq!(expected_community, inserted_community);
468 assert_eq!(expected_community, updated_community);
469 assert_eq!(expected_community_follower, inserted_community_follower);
470 assert_eq!(expected_community_user, inserted_community_user);
471 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
472 assert_eq!(1, ignored_community);
473 assert_eq!(1, left_community);
474 assert_eq!(1, unban);
475 // assert_eq!(2, loaded_count);
476 assert_eq!(1, num_deleted);