1 use crate::{ApubObject, Bannable, Crud, Followable, Joinable};
2 use diesel::{dsl::*, result::Error, *};
11 CommunityModeratorForm,
13 CommunityPersonBanForm,
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_moderator_form: &CommunityModeratorForm,
210 ) -> Result<Self, Error> {
211 use lemmy_db_schema::schema::community_moderator::dsl::*;
212 insert_into(community_moderator)
213 .values(community_moderator_form)
214 .get_result::<Self>(conn)
219 community_moderator_form: &CommunityModeratorForm,
220 ) -> Result<usize, Error> {
221 use lemmy_db_schema::schema::community_moderator::dsl::*;
224 .filter(community_id.eq(community_moderator_form.community_id))
225 .filter(person_id.eq(community_moderator_form.person_id)),
231 pub trait CommunityModerator_ {
232 fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
233 fn get_person_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_person_moderated_communities(
248 ) -> Result<Vec<i32>, Error> {
249 use lemmy_db_schema::schema::community_moderator::dsl::*;
251 .filter(person_id.eq(for_person_id))
252 .select(community_id)
257 impl Bannable<CommunityPersonBanForm> for CommunityPersonBan {
260 community_person_ban_form: &CommunityPersonBanForm,
261 ) -> Result<Self, Error> {
262 use lemmy_db_schema::schema::community_person_ban::dsl::*;
263 insert_into(community_person_ban)
264 .values(community_person_ban_form)
265 .get_result::<Self>(conn)
270 community_person_ban_form: &CommunityPersonBanForm,
271 ) -> Result<usize, Error> {
272 use lemmy_db_schema::schema::community_person_ban::dsl::*;
275 .filter(community_id.eq(community_person_ban_form.community_id))
276 .filter(person_id.eq(community_person_ban_form.person_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.person_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::*, person::*};
346 let conn = establish_unpooled_connection();
348 let new_person = PersonForm {
349 name: "bobbee".into(),
350 preferred_username: None,
362 last_refreshed_at: None,
364 shared_inbox_url: None,
367 let inserted_person = Person::create(&conn, &new_person).unwrap();
369 let new_community = CommunityForm {
371 creator_id: inserted_person.id,
372 title: "nada".to_owned(),
382 last_refreshed_at: None,
388 shared_inbox_url: None,
391 let inserted_community = Community::create(&conn, &new_community).unwrap();
393 let expected_community = Community {
394 id: inserted_community.id,
395 creator_id: inserted_person.id,
397 title: "nada".to_owned(),
402 published: inserted_community.published,
404 actor_id: inserted_community.actor_id.to_owned(),
408 last_refreshed_at: inserted_community.published,
411 followers_url: inserted_community.followers_url.to_owned(),
412 inbox_url: inserted_community.inbox_url.to_owned(),
413 shared_inbox_url: None,
416 let community_follower_form = CommunityFollowerForm {
417 community_id: inserted_community.id,
418 person_id: inserted_person.id,
422 let inserted_community_follower =
423 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
425 let expected_community_follower = CommunityFollower {
426 id: inserted_community_follower.id,
427 community_id: inserted_community.id,
428 person_id: inserted_person.id,
429 pending: Some(false),
430 published: inserted_community_follower.published,
433 let community_moderator_form = CommunityModeratorForm {
434 community_id: inserted_community.id,
435 person_id: inserted_person.id,
438 let inserted_community_moderator = CommunityModerator::join(&conn, &community_moderator_form).unwrap();
440 let expected_community_moderator = CommunityModerator {
441 id: inserted_community_moderator.id,
442 community_id: inserted_community.id,
443 person_id: inserted_person.id,
444 published: inserted_community_moderator.published,
447 let community_person_ban_form = CommunityPersonBanForm {
448 community_id: inserted_community.id,
449 person_id: inserted_person.id,
452 let inserted_community_person_ban =
453 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
455 let expected_community_person_ban = CommunityPersonBan {
456 id: inserted_community_person_ban.id,
457 community_id: inserted_community.id,
458 person_id: inserted_person.id,
459 published: inserted_community_person_ban.published,
462 let read_community = Community::read(&conn, inserted_community.id).unwrap();
463 let updated_community =
464 Community::update(&conn, inserted_community.id, &new_community).unwrap();
465 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
466 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
467 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
468 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
469 Person::delete(&conn, inserted_person.id).unwrap();
471 assert_eq!(expected_community, read_community);
472 assert_eq!(expected_community, inserted_community);
473 assert_eq!(expected_community, updated_community);
474 assert_eq!(expected_community_follower, inserted_community_follower);
475 assert_eq!(expected_community_moderator, inserted_community_moderator);
476 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
477 assert_eq!(1, ignored_community);
478 assert_eq!(1, left_community);
479 assert_eq!(1, unban);
480 // assert_eq!(2, loaded_count);
481 assert_eq!(1, num_deleted);