3 schema::{community, community_follower, community_moderator, community_user_ban},
9 use diesel::{dsl::*, result::Error, *};
11 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug)]
12 #[table_name = "community"]
13 pub struct Community {
17 pub description: Option<String>,
21 pub published: chrono::NaiveDateTime,
22 pub updated: Option<chrono::NaiveDateTime>,
27 pub private_key: Option<String>,
28 pub public_key: Option<String>,
29 pub last_refreshed_at: chrono::NaiveDateTime,
30 pub icon: Option<String>,
31 pub banner: Option<String>,
34 #[derive(Insertable, AsChangeset, Debug)]
35 #[table_name = "community"]
36 pub struct CommunityForm {
39 pub description: Option<String>,
42 pub removed: Option<bool>,
43 pub published: Option<chrono::NaiveDateTime>,
44 pub updated: Option<chrono::NaiveDateTime>,
45 pub deleted: Option<bool>,
47 pub actor_id: Option<String>,
49 pub private_key: Option<String>,
50 pub public_key: Option<String>,
51 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
52 pub icon: Option<Option<String>>,
53 pub banner: Option<Option<String>>,
56 impl Crud<CommunityForm> for Community {
57 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
58 use crate::schema::community::dsl::*;
59 community.find(community_id).first::<Self>(conn)
62 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
63 use crate::schema::community::dsl::*;
64 diesel::delete(community.find(community_id)).execute(conn)
67 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
68 use crate::schema::community::dsl::*;
69 insert_into(community)
70 .values(new_community)
71 .get_result::<Self>(conn)
77 new_community: &CommunityForm,
78 ) -> Result<Self, Error> {
79 use crate::schema::community::dsl::*;
80 diesel::update(community.find(community_id))
82 .get_result::<Self>(conn)
87 pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
88 use crate::schema::community::dsl::*;
90 .filter(name.eq(community_name))
94 pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
95 use crate::schema::community::dsl::*;
97 .filter(actor_id.eq(for_actor_id))
101 pub fn update_deleted(
105 ) -> Result<Self, Error> {
106 use crate::schema::community::dsl::*;
107 diesel::update(community.find(community_id))
108 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
109 .get_result::<Self>(conn)
112 pub fn update_removed(
116 ) -> Result<Self, Error> {
117 use crate::schema::community::dsl::*;
118 diesel::update(community.find(community_id))
119 .set((removed.eq(new_removed), updated.eq(naive_now())))
120 .get_result::<Self>(conn)
123 pub fn update_removed_for_creator(
127 ) -> Result<Vec<Self>, Error> {
128 use crate::schema::community::dsl::*;
129 diesel::update(community.filter(creator_id.eq(for_creator_id)))
130 .set((removed.eq(new_removed), updated.eq(naive_now())))
131 .get_results::<Self>(conn)
134 pub fn update_creator(
138 ) -> Result<Self, Error> {
139 use crate::schema::community::dsl::*;
140 diesel::update(community.find(community_id))
141 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
142 .get_result::<Self>(conn)
145 fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
146 use crate::{community_view::CommunityModeratorView, user_view::UserView};
147 let mut mods_and_admins: Vec<i32> = Vec::new();
148 mods_and_admins.append(
149 &mut CommunityModeratorView::for_community(conn, community_id)
150 .map(|v| v.into_iter().map(|m| m.user_id).collect())?,
153 .append(&mut UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())?);
157 pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool {
158 Self::community_mods_and_admins(conn, community_id)
163 pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
164 use crate::schema::community::dsl::*;
165 insert_into(community)
166 .values(community_form)
167 .on_conflict(actor_id)
170 .get_result::<Self>(conn)
174 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
175 #[belongs_to(Community)]
176 #[table_name = "community_moderator"]
177 pub struct CommunityModerator {
179 pub community_id: i32,
181 pub published: chrono::NaiveDateTime,
184 #[derive(Insertable, AsChangeset, Clone)]
185 #[table_name = "community_moderator"]
186 pub struct CommunityModeratorForm {
187 pub community_id: i32,
191 impl Joinable<CommunityModeratorForm> for CommunityModerator {
194 community_user_form: &CommunityModeratorForm,
195 ) -> Result<Self, Error> {
196 use crate::schema::community_moderator::dsl::*;
197 insert_into(community_moderator)
198 .values(community_user_form)
199 .get_result::<Self>(conn)
204 community_user_form: &CommunityModeratorForm,
205 ) -> Result<usize, Error> {
206 use crate::schema::community_moderator::dsl::*;
209 .filter(community_id.eq(community_user_form.community_id))
210 .filter(user_id.eq(community_user_form.user_id)),
216 impl CommunityModerator {
217 pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
218 use crate::schema::community_moderator::dsl::*;
219 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
223 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
224 #[belongs_to(Community)]
225 #[table_name = "community_user_ban"]
226 pub struct CommunityUserBan {
228 pub community_id: i32,
230 pub published: chrono::NaiveDateTime,
233 #[derive(Insertable, AsChangeset, Clone)]
234 #[table_name = "community_user_ban"]
235 pub struct CommunityUserBanForm {
236 pub community_id: i32,
240 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
243 community_user_ban_form: &CommunityUserBanForm,
244 ) -> Result<Self, Error> {
245 use crate::schema::community_user_ban::dsl::*;
246 insert_into(community_user_ban)
247 .values(community_user_ban_form)
248 .get_result::<Self>(conn)
253 community_user_ban_form: &CommunityUserBanForm,
254 ) -> Result<usize, Error> {
255 use crate::schema::community_user_ban::dsl::*;
258 .filter(community_id.eq(community_user_ban_form.community_id))
259 .filter(user_id.eq(community_user_ban_form.user_id)),
265 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
266 #[belongs_to(Community)]
267 #[table_name = "community_follower"]
268 pub struct CommunityFollower {
270 pub community_id: i32,
272 pub published: chrono::NaiveDateTime,
275 #[derive(Insertable, AsChangeset, Clone)]
276 #[table_name = "community_follower"]
277 pub struct CommunityFollowerForm {
278 pub community_id: i32,
282 impl Followable<CommunityFollowerForm> for CommunityFollower {
285 community_follower_form: &CommunityFollowerForm,
286 ) -> Result<Self, Error> {
287 use crate::schema::community_follower::dsl::*;
288 insert_into(community_follower)
289 .values(community_follower_form)
290 .get_result::<Self>(conn)
294 community_follower_form: &CommunityFollowerForm,
295 ) -> Result<usize, Error> {
296 use crate::schema::community_follower::dsl::*;
299 .filter(community_id.eq(&community_follower_form.community_id))
300 .filter(user_id.eq(&community_follower_form.user_id)),
308 use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType};
312 let conn = establish_unpooled_connection();
314 let new_user = UserForm {
315 name: "bobbee".into(),
316 preferred_username: None,
317 password_encrypted: "nope".into(),
319 matrix_user_id: None,
326 theme: "darkly".into(),
327 default_sort_type: SortType::Hot as i16,
328 default_listing_type: ListingType::Subscribed as i16,
329 lang: "browser".into(),
331 send_notifications_to_email: false,
337 last_refreshed_at: None,
340 let inserted_user = User_::create(&conn, &new_user).unwrap();
342 let new_community = CommunityForm {
344 creator_id: inserted_user.id,
345 title: "nada".to_owned(),
356 last_refreshed_at: None,
362 let inserted_community = Community::create(&conn, &new_community).unwrap();
364 let expected_community = Community {
365 id: inserted_community.id,
366 creator_id: inserted_user.id,
368 title: "nada".to_owned(),
374 published: inserted_community.published,
376 actor_id: inserted_community.actor_id.to_owned(),
380 last_refreshed_at: inserted_community.published,
385 let community_follower_form = CommunityFollowerForm {
386 community_id: inserted_community.id,
387 user_id: inserted_user.id,
390 let inserted_community_follower =
391 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
393 let expected_community_follower = CommunityFollower {
394 id: inserted_community_follower.id,
395 community_id: inserted_community.id,
396 user_id: inserted_user.id,
397 published: inserted_community_follower.published,
400 let community_user_form = CommunityModeratorForm {
401 community_id: inserted_community.id,
402 user_id: inserted_user.id,
405 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
407 let expected_community_user = CommunityModerator {
408 id: inserted_community_user.id,
409 community_id: inserted_community.id,
410 user_id: inserted_user.id,
411 published: inserted_community_user.published,
414 let community_user_ban_form = CommunityUserBanForm {
415 community_id: inserted_community.id,
416 user_id: inserted_user.id,
419 let inserted_community_user_ban =
420 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
422 let expected_community_user_ban = CommunityUserBan {
423 id: inserted_community_user_ban.id,
424 community_id: inserted_community.id,
425 user_id: inserted_user.id,
426 published: inserted_community_user_ban.published,
429 let read_community = Community::read(&conn, inserted_community.id).unwrap();
430 let updated_community =
431 Community::update(&conn, inserted_community.id, &new_community).unwrap();
432 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
433 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
434 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
435 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
436 User_::delete(&conn, inserted_user.id).unwrap();
438 assert_eq!(expected_community, read_community);
439 assert_eq!(expected_community, inserted_community);
440 assert_eq!(expected_community, updated_community);
441 assert_eq!(expected_community_follower, inserted_community_follower);
442 assert_eq!(expected_community_user, inserted_community_user);
443 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
444 assert_eq!(1, ignored_community);
445 assert_eq!(1, left_community);
446 assert_eq!(1, unban);
447 // assert_eq!(2, loaded_count);
448 assert_eq!(1, num_deleted);