3 newtypes::{CommunityId, DbUrl, PersonId},
10 CommunityModeratorForm,
12 CommunityPersonBanForm,
15 traits::{Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
17 use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
21 use crate::{schema::community::*, source::community::Community, traits::ToSafe};
39 impl ToSafe for Community {
40 type SafeColumns = Columns;
41 fn safe_columns_tuple() -> Self::SafeColumns {
61 impl Crud for Community {
62 type Form = CommunityForm;
63 type IdType = CommunityId;
64 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
65 use crate::schema::community::dsl::*;
66 community.find(community_id).first::<Self>(conn)
69 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
70 use crate::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 crate::schema::community::dsl::*;
76 insert_into(community)
77 .values(new_community)
78 .get_result::<Self>(conn)
83 community_id: CommunityId,
84 new_community: &CommunityForm,
85 ) -> Result<Self, Error> {
86 use crate::schema::community::dsl::*;
87 diesel::update(community.find(community_id))
89 .get_result::<Self>(conn)
94 pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
95 use crate::schema::community::dsl::*;
97 .filter(local.eq(true))
98 .filter(name.eq(community_name))
102 pub fn update_deleted(
104 community_id: CommunityId,
106 ) -> Result<Community, Error> {
107 use crate::schema::community::dsl::*;
108 diesel::update(community.find(community_id))
109 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
110 .get_result::<Self>(conn)
113 pub fn update_removed(
115 community_id: CommunityId,
117 ) -> Result<Community, Error> {
118 use crate::schema::community::dsl::*;
119 diesel::update(community.find(community_id))
120 .set((removed.eq(new_removed), updated.eq(naive_now())))
121 .get_result::<Self>(conn)
124 pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
125 use crate::schema::community::dsl::*;
126 community.select(actor_id).distinct().load::<String>(conn)
129 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
130 use crate::schema::community::dsl::*;
131 insert_into(community)
132 .values(community_form)
133 .on_conflict(actor_id)
136 .get_result::<Self>(conn)
138 pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
139 use crate::schema::community::dsl::*;
140 let object_id: DbUrl = object_id.into();
143 .filter(actor_id.eq(object_id))
144 .first::<Community>(conn)
151 impl Joinable for CommunityModerator {
152 type Form = CommunityModeratorForm;
155 community_moderator_form: &CommunityModeratorForm,
156 ) -> Result<Self, Error> {
157 use crate::schema::community_moderator::dsl::*;
158 insert_into(community_moderator)
159 .values(community_moderator_form)
160 .get_result::<Self>(conn)
165 community_moderator_form: &CommunityModeratorForm,
166 ) -> Result<usize, Error> {
167 use crate::schema::community_moderator::dsl::*;
170 .filter(community_id.eq(community_moderator_form.community_id))
171 .filter(person_id.eq(community_moderator_form.person_id)),
177 impl DeleteableOrRemoveable for CommunitySafe {
178 fn blank_out_deleted_or_removed_info(mut self) -> Self {
179 self.title = "".into();
180 self.description = None;
187 impl DeleteableOrRemoveable for Community {
188 fn blank_out_deleted_or_removed_info(mut self) -> Self {
189 self.title = "".into();
190 self.description = None;
197 impl CommunityModerator {
198 pub fn delete_for_community(
200 for_community_id: CommunityId,
201 ) -> Result<usize, Error> {
202 use crate::schema::community_moderator::dsl::*;
203 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
206 pub fn get_person_moderated_communities(
208 for_person_id: PersonId,
209 ) -> Result<Vec<CommunityId>, Error> {
210 use crate::schema::community_moderator::dsl::*;
212 .filter(person_id.eq(for_person_id))
213 .select(community_id)
214 .load::<CommunityId>(conn)
218 impl Bannable for CommunityPersonBan {
219 type Form = CommunityPersonBanForm;
222 community_person_ban_form: &CommunityPersonBanForm,
223 ) -> Result<Self, Error> {
224 use crate::schema::community_person_ban::dsl::*;
225 insert_into(community_person_ban)
226 .values(community_person_ban_form)
227 .get_result::<Self>(conn)
232 community_person_ban_form: &CommunityPersonBanForm,
233 ) -> Result<usize, Error> {
234 use crate::schema::community_person_ban::dsl::*;
237 .filter(community_id.eq(community_person_ban_form.community_id))
238 .filter(person_id.eq(community_person_ban_form.person_id)),
244 impl Followable for CommunityFollower {
245 type Form = CommunityFollowerForm;
248 community_follower_form: &CommunityFollowerForm,
249 ) -> Result<Self, Error> {
250 use crate::schema::community_follower::dsl::*;
251 insert_into(community_follower)
252 .values(community_follower_form)
253 .on_conflict((community_id, person_id))
255 .set(community_follower_form)
256 .get_result::<Self>(conn)
260 community_id_: CommunityId,
261 person_id_: PersonId,
262 ) -> Result<Self, Error>
266 use crate::schema::community_follower::dsl::*;
269 .filter(community_id.eq(community_id_))
270 .filter(person_id.eq(person_id_)),
272 .set(pending.eq(true))
273 .get_result::<Self>(conn)
277 community_follower_form: &CommunityFollowerForm,
278 ) -> Result<usize, Error> {
279 use crate::schema::community_follower::dsl::*;
282 .filter(community_id.eq(&community_follower_form.community_id))
283 .filter(person_id.eq(&community_follower_form.person_id)),
287 // TODO: this function name only makes sense if you call it with a remote community. for a local
288 // community, it will also return true if only remote followers exist
289 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
290 use crate::schema::community_follower::dsl::*;
291 diesel::select(exists(
292 community_follower.filter(community_id.eq(community_id_)),
301 establish_unpooled_connection,
302 source::{community::*, person::*},
303 traits::{Bannable, Crud, Followable, Joinable},
305 use serial_test::serial;
310 let conn = establish_unpooled_connection();
312 let new_person = PersonForm {
313 name: "bobbee".into(),
314 ..PersonForm::default()
317 let inserted_person = Person::create(&conn, &new_person).unwrap();
319 let new_community = CommunityForm {
321 title: "nada".to_owned(),
322 ..CommunityForm::default()
325 let inserted_community = Community::create(&conn, &new_community).unwrap();
327 let expected_community = Community {
328 id: inserted_community.id,
330 title: "nada".to_owned(),
335 published: inserted_community.published,
337 actor_id: inserted_community.actor_id.to_owned(),
341 last_refreshed_at: inserted_community.published,
344 followers_url: inserted_community.followers_url.to_owned(),
345 inbox_url: inserted_community.inbox_url.to_owned(),
346 shared_inbox_url: None,
349 let community_follower_form = CommunityFollowerForm {
350 community_id: inserted_community.id,
351 person_id: inserted_person.id,
355 let inserted_community_follower =
356 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
358 let expected_community_follower = CommunityFollower {
359 id: inserted_community_follower.id,
360 community_id: inserted_community.id,
361 person_id: inserted_person.id,
362 pending: Some(false),
363 published: inserted_community_follower.published,
366 let community_moderator_form = CommunityModeratorForm {
367 community_id: inserted_community.id,
368 person_id: inserted_person.id,
371 let inserted_community_moderator =
372 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
374 let expected_community_moderator = CommunityModerator {
375 id: inserted_community_moderator.id,
376 community_id: inserted_community.id,
377 person_id: inserted_person.id,
378 published: inserted_community_moderator.published,
381 let community_person_ban_form = CommunityPersonBanForm {
382 community_id: inserted_community.id,
383 person_id: inserted_person.id,
386 let inserted_community_person_ban =
387 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
389 let expected_community_person_ban = CommunityPersonBan {
390 id: inserted_community_person_ban.id,
391 community_id: inserted_community.id,
392 person_id: inserted_person.id,
393 published: inserted_community_person_ban.published,
396 let read_community = Community::read(&conn, inserted_community.id).unwrap();
397 let updated_community =
398 Community::update(&conn, inserted_community.id, &new_community).unwrap();
399 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
400 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
401 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
402 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
403 Person::delete(&conn, inserted_person.id).unwrap();
405 assert_eq!(expected_community, read_community);
406 assert_eq!(expected_community, inserted_community);
407 assert_eq!(expected_community, updated_community);
408 assert_eq!(expected_community_follower, inserted_community_follower);
409 assert_eq!(expected_community_moderator, inserted_community_moderator);
410 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
411 assert_eq!(1, ignored_community);
412 assert_eq!(1, left_community);
413 assert_eq!(1, unban);
414 // assert_eq!(2, loaded_count);
415 assert_eq!(1, num_deleted);