4 newtypes::{CommunityId, DbUrl, PersonId},
11 CommunityModeratorForm,
13 CommunityPersonBanForm,
16 traits::{ApubActor, Bannable, Crud, DeleteableOrRemoveable, Followable, Joinable},
25 TextExpressionMethods,
29 use crate::{schema::community::*, source::community::Community, traits::ToSafe};
48 impl ToSafe for Community {
49 type SafeColumns = Columns;
50 fn safe_columns_tuple() -> Self::SafeColumns {
71 impl Crud for Community {
72 type Form = CommunityForm;
73 type IdType = CommunityId;
74 fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
75 use crate::schema::community::dsl::*;
76 community.find(community_id).first::<Self>(conn)
79 fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
80 use crate::schema::community::dsl::*;
81 diesel::delete(community.find(community_id)).execute(conn)
84 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
85 use crate::schema::community::dsl::*;
86 insert_into(community)
87 .values(new_community)
88 .get_result::<Self>(conn)
93 community_id: CommunityId,
94 new_community: &CommunityForm,
95 ) -> Result<Self, Error> {
96 use crate::schema::community::dsl::*;
97 diesel::update(community.find(community_id))
99 .get_result::<Self>(conn)
104 pub fn update_deleted(
106 community_id: CommunityId,
108 ) -> Result<Community, Error> {
109 use crate::schema::community::dsl::*;
110 diesel::update(community.find(community_id))
111 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
112 .get_result::<Self>(conn)
115 pub fn update_removed(
117 community_id: CommunityId,
119 ) -> Result<Community, Error> {
120 use crate::schema::community::dsl::*;
121 diesel::update(community.find(community_id))
122 .set((removed.eq(new_removed), updated.eq(naive_now())))
123 .get_result::<Self>(conn)
126 pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<DbUrl>, Error> {
127 use crate::schema::community::dsl::*;
128 community.select(actor_id).distinct().load::<DbUrl>(conn)
131 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
132 use crate::schema::community::dsl::*;
133 insert_into(community)
134 .values(community_form)
135 .on_conflict(actor_id)
138 .get_result::<Self>(conn)
142 impl Joinable for CommunityModerator {
143 type Form = CommunityModeratorForm;
146 community_moderator_form: &CommunityModeratorForm,
147 ) -> Result<Self, Error> {
148 use crate::schema::community_moderator::dsl::*;
149 insert_into(community_moderator)
150 .values(community_moderator_form)
151 .get_result::<Self>(conn)
156 community_moderator_form: &CommunityModeratorForm,
157 ) -> Result<usize, Error> {
158 use crate::schema::community_moderator::dsl::*;
161 .filter(community_id.eq(community_moderator_form.community_id))
162 .filter(person_id.eq(community_moderator_form.person_id)),
168 impl DeleteableOrRemoveable for CommunitySafe {
169 fn blank_out_deleted_or_removed_info(mut self) -> Self {
170 self.title = "".into();
171 self.description = None;
178 impl DeleteableOrRemoveable for Community {
179 fn blank_out_deleted_or_removed_info(mut self) -> Self {
180 self.title = "".into();
181 self.description = None;
188 impl CommunityModerator {
189 pub fn delete_for_community(
191 for_community_id: CommunityId,
192 ) -> Result<usize, Error> {
193 use crate::schema::community_moderator::dsl::*;
194 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
197 pub fn get_person_moderated_communities(
199 for_person_id: PersonId,
200 ) -> Result<Vec<CommunityId>, Error> {
201 use crate::schema::community_moderator::dsl::*;
203 .filter(person_id.eq(for_person_id))
204 .select(community_id)
205 .load::<CommunityId>(conn)
209 impl Bannable for CommunityPersonBan {
210 type Form = CommunityPersonBanForm;
213 community_person_ban_form: &CommunityPersonBanForm,
214 ) -> Result<Self, Error> {
215 use crate::schema::community_person_ban::dsl::*;
216 insert_into(community_person_ban)
217 .values(community_person_ban_form)
218 .on_conflict((community_id, person_id))
220 .set(community_person_ban_form)
221 .get_result::<Self>(conn)
226 community_person_ban_form: &CommunityPersonBanForm,
227 ) -> Result<usize, Error> {
228 use crate::schema::community_person_ban::dsl::*;
231 .filter(community_id.eq(community_person_ban_form.community_id))
232 .filter(person_id.eq(community_person_ban_form.person_id)),
238 impl Followable for CommunityFollower {
239 type Form = CommunityFollowerForm;
242 community_follower_form: &CommunityFollowerForm,
243 ) -> Result<Self, Error> {
244 use crate::schema::community_follower::dsl::*;
245 insert_into(community_follower)
246 .values(community_follower_form)
247 .on_conflict((community_id, person_id))
249 .set(community_follower_form)
250 .get_result::<Self>(conn)
254 community_id_: CommunityId,
255 person_id_: PersonId,
256 ) -> Result<Self, Error>
260 use crate::schema::community_follower::dsl::*;
263 .filter(community_id.eq(community_id_))
264 .filter(person_id.eq(person_id_)),
266 .set(pending.eq(true))
267 .get_result::<Self>(conn)
271 community_follower_form: &CommunityFollowerForm,
272 ) -> Result<usize, Error> {
273 use crate::schema::community_follower::dsl::*;
276 .filter(community_id.eq(&community_follower_form.community_id))
277 .filter(person_id.eq(&community_follower_form.person_id)),
281 // TODO: this function name only makes sense if you call it with a remote community. for a local
282 // community, it will also return true if only remote followers exist
283 fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
284 use crate::schema::community_follower::dsl::*;
285 diesel::select(exists(
286 community_follower.filter(community_id.eq(community_id_)),
292 impl ApubActor for Community {
293 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error> {
294 use crate::schema::community::dsl::*;
297 .filter(actor_id.eq(object_id))
298 .first::<Community>(conn)
304 fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
305 use crate::schema::community::dsl::*;
307 .filter(local.eq(true))
308 .filter(lower(name).eq(lower(community_name)))
312 fn read_from_name_and_domain(
314 community_name: &str,
315 protocol_domain: &str,
316 ) -> Result<Community, Error> {
317 use crate::schema::community::dsl::*;
319 .filter(lower(name).eq(lower(community_name)))
320 .filter(actor_id.like(format!("{}%", protocol_domain)))
328 establish_unpooled_connection,
329 source::{community::*, person::*},
330 traits::{Bannable, Crud, Followable, Joinable},
332 use serial_test::serial;
337 let conn = establish_unpooled_connection();
339 let new_person = PersonForm {
340 name: "bobbee".into(),
341 ..PersonForm::default()
344 let inserted_person = Person::create(&conn, &new_person).unwrap();
346 let new_community = CommunityForm {
348 title: "nada".to_owned(),
349 public_key: "nada".to_owned(),
350 ..CommunityForm::default()
353 let inserted_community = Community::create(&conn, &new_community).unwrap();
355 let expected_community = Community {
356 id: inserted_community.id,
358 title: "nada".to_owned(),
363 published: inserted_community.published,
365 actor_id: inserted_community.actor_id.to_owned(),
368 public_key: "nada".to_owned(),
369 last_refreshed_at: inserted_community.published,
372 followers_url: inserted_community.followers_url.to_owned(),
373 inbox_url: inserted_community.inbox_url.to_owned(),
374 shared_inbox_url: None,
378 let community_follower_form = CommunityFollowerForm {
379 community_id: inserted_community.id,
380 person_id: inserted_person.id,
384 let inserted_community_follower =
385 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
387 let expected_community_follower = CommunityFollower {
388 id: inserted_community_follower.id,
389 community_id: inserted_community.id,
390 person_id: inserted_person.id,
391 pending: Some(false),
392 published: inserted_community_follower.published,
395 let community_moderator_form = CommunityModeratorForm {
396 community_id: inserted_community.id,
397 person_id: inserted_person.id,
400 let inserted_community_moderator =
401 CommunityModerator::join(&conn, &community_moderator_form).unwrap();
403 let expected_community_moderator = CommunityModerator {
404 id: inserted_community_moderator.id,
405 community_id: inserted_community.id,
406 person_id: inserted_person.id,
407 published: inserted_community_moderator.published,
410 let community_person_ban_form = CommunityPersonBanForm {
411 community_id: inserted_community.id,
412 person_id: inserted_person.id,
416 let inserted_community_person_ban =
417 CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
419 let expected_community_person_ban = CommunityPersonBan {
420 id: inserted_community_person_ban.id,
421 community_id: inserted_community.id,
422 person_id: inserted_person.id,
423 published: inserted_community_person_ban.published,
427 let read_community = Community::read(&conn, inserted_community.id).unwrap();
428 let updated_community =
429 Community::update(&conn, inserted_community.id, &new_community).unwrap();
430 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
431 let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
432 let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
433 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
434 Person::delete(&conn, inserted_person.id).unwrap();
436 assert_eq!(expected_community, read_community);
437 assert_eq!(expected_community, inserted_community);
438 assert_eq!(expected_community, updated_community);
439 assert_eq!(expected_community_follower, inserted_community_follower);
440 assert_eq!(expected_community_moderator, inserted_community_moderator);
441 assert_eq!(expected_community_person_ban, inserted_community_person_ban);
442 assert_eq!(1, ignored_community);
443 assert_eq!(1, left_community);
444 assert_eq!(1, unban);
445 // assert_eq!(2, loaded_count);
446 assert_eq!(1, num_deleted);