1 use crate::{ApubObject, Bannable, Crud, Followable, Joinable};
2 use diesel::{dsl::*, result::Error, *};
11 CommunityModeratorForm,
18 use crate::{source::community::Community, ToSafe};
19 use lemmy_db_schema::schema::community::*;
39 impl ToSafe for Community {
40 type SafeColumns = Columns;
41 fn safe_columns_tuple() -> Self::SafeColumns {
63 impl Crud<CommunityForm> for Community {
64 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
65 use lemmy_db_schema::schema::community::dsl::*;
66 community.find(community_id).first::<Self>(conn)
69 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
70 use lemmy_db_schema::schema::community::dsl::*;
71 diesel::delete(community.find(community_id)).execute(conn)
74 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
75 use lemmy_db_schema::schema::community::dsl::*;
76 insert_into(community)
77 .values(new_community)
78 .get_result::<Self>(conn)
84 new_community: &CommunityForm,
85 ) -> Result<Self, Error> {
86 use lemmy_db_schema::schema::community::dsl::*;
87 diesel::update(community.find(community_id))
89 .get_result::<Self>(conn)
93 impl ApubObject<CommunityForm> for Community {
94 fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
95 use lemmy_db_schema::schema::community::dsl::*;
97 .filter(actor_id.eq(for_actor_id))
101 fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
102 use lemmy_db_schema::schema::community::dsl::*;
103 insert_into(community)
104 .values(community_form)
105 .on_conflict(actor_id)
108 .get_result::<Self>(conn)
112 pub trait Community_ {
113 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
118 ) -> Result<Community, Error>;
123 ) -> Result<Community, Error>;
124 fn update_removed_for_creator(
128 ) -> Result<Vec<Community>, Error>;
133 ) -> Result<Community, Error>;
134 fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, 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)
196 impl Joinable<CommunityModeratorForm> for CommunityModerator {
199 community_user_form: &CommunityModeratorForm,
200 ) -> Result<Self, Error> {
201 use lemmy_db_schema::schema::community_moderator::dsl::*;
202 insert_into(community_moderator)
203 .values(community_user_form)
204 .get_result::<Self>(conn)
209 community_user_form: &CommunityModeratorForm,
210 ) -> Result<usize, Error> {
211 use lemmy_db_schema::schema::community_moderator::dsl::*;
214 .filter(community_id.eq(community_user_form.community_id))
215 .filter(user_id.eq(community_user_form.user_id)),
221 pub trait CommunityModerator_ {
222 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
223 fn get_user_moderated_communities(
226 ) -> Result<Vec<i32>, Error>;
229 impl CommunityModerator_ for CommunityModerator {
230 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
231 use lemmy_db_schema::schema::community_moderator::dsl::*;
232 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
235 fn get_user_moderated_communities(
238 ) -> Result<Vec<i32>, Error> {
239 use lemmy_db_schema::schema::community_moderator::dsl::*;
241 .filter(user_id.eq(for_user_id))
242 .select(community_id)
247 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
250 community_user_ban_form: &CommunityUserBanForm,
251 ) -> Result<Self, Error> {
252 use lemmy_db_schema::schema::community_user_ban::dsl::*;
253 insert_into(community_user_ban)
254 .values(community_user_ban_form)
255 .get_result::<Self>(conn)
260 community_user_ban_form: &CommunityUserBanForm,
261 ) -> Result<usize, Error> {
262 use lemmy_db_schema::schema::community_user_ban::dsl::*;
265 .filter(community_id.eq(community_user_ban_form.community_id))
266 .filter(user_id.eq(community_user_ban_form.user_id)),
272 impl Followable<CommunityFollowerForm> for CommunityFollower {
275 community_follower_form: &CommunityFollowerForm,
276 ) -> Result<Self, Error> {
277 use lemmy_db_schema::schema::community_follower::dsl::*;
278 insert_into(community_follower)
279 .values(community_follower_form)
280 .on_conflict((community_id, user_id))
282 .set(community_follower_form)
283 .get_result::<Self>(conn)
285 fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
289 use lemmy_db_schema::schema::community_follower::dsl::*;
292 .filter(community_id.eq(community_id_))
293 .filter(user_id.eq(user_id_)),
295 .set(pending.eq(true))
296 .get_result::<Self>(conn)
300 community_follower_form: &CommunityFollowerForm,
301 ) -> Result<usize, Error> {
302 use lemmy_db_schema::schema::community_follower::dsl::*;
305 .filter(community_id.eq(&community_follower_form.community_id))
306 .filter(user_id.eq(&community_follower_form.user_id)),
310 // TODO: this function name only makes sense if you call it with a remote community. for a local
311 // community, it will also return true if only remote followers exist
312 fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> {
313 use lemmy_db_schema::schema::community_follower::dsl::*;
314 diesel::select(exists(
315 community_follower.filter(community_id.eq(community_id_)),
324 establish_unpooled_connection,
332 use lemmy_db_schema::source::{community::*, user::*};
336 let conn = establish_unpooled_connection();
338 let new_user = UserForm {
339 name: "bobbee".into(),
340 preferred_username: None,
341 password_encrypted: "nope".into(),
343 matrix_user_id: None,
351 theme: "browser".into(),
352 default_sort_type: SortType::Hot as i16,
353 default_listing_type: ListingType::Subscribed as i16,
354 lang: "browser".into(),
356 send_notifications_to_email: false,
362 last_refreshed_at: None,
365 let inserted_user = User_::create(&conn, &new_user).unwrap();
367 let new_community = CommunityForm {
369 creator_id: inserted_user.id,
370 title: "nada".to_owned(),
381 last_refreshed_at: None,
387 let inserted_community = Community::create(&conn, &new_community).unwrap();
389 let expected_community = Community {
390 id: inserted_community.id,
391 creator_id: inserted_user.id,
393 title: "nada".to_owned(),
399 published: inserted_community.published,
401 actor_id: inserted_community.actor_id.to_owned(),
405 last_refreshed_at: inserted_community.published,
410 let community_follower_form = CommunityFollowerForm {
411 community_id: inserted_community.id,
412 user_id: inserted_user.id,
416 let inserted_community_follower =
417 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
419 let expected_community_follower = CommunityFollower {
420 id: inserted_community_follower.id,
421 community_id: inserted_community.id,
422 user_id: inserted_user.id,
423 pending: Some(false),
424 published: inserted_community_follower.published,
427 let community_user_form = CommunityModeratorForm {
428 community_id: inserted_community.id,
429 user_id: inserted_user.id,
432 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
434 let expected_community_user = CommunityModerator {
435 id: inserted_community_user.id,
436 community_id: inserted_community.id,
437 user_id: inserted_user.id,
438 published: inserted_community_user.published,
441 let community_user_ban_form = CommunityUserBanForm {
442 community_id: inserted_community.id,
443 user_id: inserted_user.id,
446 let inserted_community_user_ban =
447 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
449 let expected_community_user_ban = CommunityUserBan {
450 id: inserted_community_user_ban.id,
451 community_id: inserted_community.id,
452 user_id: inserted_user.id,
453 published: inserted_community_user_ban.published,
456 let read_community = Community::read(&conn, inserted_community.id).unwrap();
457 let updated_community =
458 Community::update(&conn, inserted_community.id, &new_community).unwrap();
459 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
460 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
461 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
462 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
463 User_::delete(&conn, inserted_user.id).unwrap();
465 assert_eq!(expected_community, read_community);
466 assert_eq!(expected_community, inserted_community);
467 assert_eq!(expected_community, updated_community);
468 assert_eq!(expected_community_follower, inserted_community_follower);
469 assert_eq!(expected_community_user, inserted_community_user);
470 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
471 assert_eq!(1, ignored_community);
472 assert_eq!(1, left_community);
473 assert_eq!(1, unban);
474 // assert_eq!(2, loaded_count);
475 assert_eq!(1, num_deleted);