3 schema::{community, community_follower, community_moderator, community_user_ban},
9 use diesel::{dsl::*, result::Error, *};
10 use serde::{Deserialize, Serialize};
12 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
13 #[table_name = "community"]
14 pub struct Community {
18 pub description: Option<String>,
22 pub published: chrono::NaiveDateTime,
23 pub updated: Option<chrono::NaiveDateTime>,
28 pub private_key: Option<String>,
29 pub public_key: Option<String>,
30 pub last_refreshed_at: chrono::NaiveDateTime,
31 pub icon: Option<String>,
32 pub banner: Option<String>,
35 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize, Debug)]
36 #[table_name = "community"]
37 pub struct CommunityForm {
40 pub description: Option<String>,
43 pub removed: Option<bool>,
44 pub published: Option<chrono::NaiveDateTime>,
45 pub updated: Option<chrono::NaiveDateTime>,
46 pub deleted: Option<bool>,
50 pub private_key: Option<String>,
51 pub public_key: Option<String>,
52 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
53 pub icon: Option<Option<String>>,
54 pub banner: Option<Option<String>>,
57 impl Crud<CommunityForm> for Community {
58 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
59 use crate::schema::community::dsl::*;
60 community.find(community_id).first::<Self>(conn)
63 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
64 use crate::schema::community::dsl::*;
65 diesel::delete(community.find(community_id)).execute(conn)
68 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
69 use crate::schema::community::dsl::*;
70 insert_into(community)
71 .values(new_community)
72 .get_result::<Self>(conn)
78 new_community: &CommunityForm,
79 ) -> Result<Self, Error> {
80 use crate::schema::community::dsl::*;
81 diesel::update(community.find(community_id))
83 .get_result::<Self>(conn)
88 pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
89 use crate::schema::community::dsl::*;
91 .filter(name.eq(community_name))
95 pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
96 use crate::schema::community::dsl::*;
98 .filter(actor_id.eq(for_actor_id))
102 pub fn list_local(conn: &PgConnection) -> Result<Vec<Self>, Error> {
103 use crate::schema::community::dsl::*;
104 community.filter(local.eq(true)).load::<Community>(conn)
107 pub fn update_deleted(
111 ) -> Result<Self, Error> {
112 use crate::schema::community::dsl::*;
113 diesel::update(community.find(community_id))
114 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
115 .get_result::<Self>(conn)
118 pub fn update_removed(
122 ) -> Result<Self, Error> {
123 use crate::schema::community::dsl::*;
124 diesel::update(community.find(community_id))
125 .set((removed.eq(new_removed), updated.eq(naive_now())))
126 .get_result::<Self>(conn)
129 pub fn update_creator(
133 ) -> Result<Self, Error> {
134 use crate::schema::community::dsl::*;
135 diesel::update(community.find(community_id))
136 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
137 .get_result::<Self>(conn)
140 fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
141 use crate::{community_view::CommunityModeratorView, user_view::UserView};
142 let mut mods_and_admins: Vec<i32> = Vec::new();
143 mods_and_admins.append(
144 &mut CommunityModeratorView::for_community(conn, community_id)
145 .map(|v| v.into_iter().map(|m| m.user_id).collect())?,
148 .append(&mut UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())?);
152 pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool {
153 Self::community_mods_and_admins(conn, community_id)
159 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
160 #[belongs_to(Community)]
161 #[table_name = "community_moderator"]
162 pub struct CommunityModerator {
164 pub community_id: i32,
166 pub published: chrono::NaiveDateTime,
169 #[derive(Insertable, AsChangeset, Clone)]
170 #[table_name = "community_moderator"]
171 pub struct CommunityModeratorForm {
172 pub community_id: i32,
176 impl Joinable<CommunityModeratorForm> for CommunityModerator {
179 community_user_form: &CommunityModeratorForm,
180 ) -> Result<Self, Error> {
181 use crate::schema::community_moderator::dsl::*;
182 insert_into(community_moderator)
183 .values(community_user_form)
184 .get_result::<Self>(conn)
189 community_user_form: &CommunityModeratorForm,
190 ) -> Result<usize, Error> {
191 use crate::schema::community_moderator::dsl::*;
194 .filter(community_id.eq(community_user_form.community_id))
195 .filter(user_id.eq(community_user_form.user_id)),
201 impl CommunityModerator {
202 pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
203 use crate::schema::community_moderator::dsl::*;
204 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
208 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
209 #[belongs_to(Community)]
210 #[table_name = "community_user_ban"]
211 pub struct CommunityUserBan {
213 pub community_id: i32,
215 pub published: chrono::NaiveDateTime,
218 #[derive(Insertable, AsChangeset, Clone)]
219 #[table_name = "community_user_ban"]
220 pub struct CommunityUserBanForm {
221 pub community_id: i32,
225 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
228 community_user_ban_form: &CommunityUserBanForm,
229 ) -> Result<Self, Error> {
230 use crate::schema::community_user_ban::dsl::*;
231 insert_into(community_user_ban)
232 .values(community_user_ban_form)
233 .get_result::<Self>(conn)
238 community_user_ban_form: &CommunityUserBanForm,
239 ) -> Result<usize, Error> {
240 use crate::schema::community_user_ban::dsl::*;
243 .filter(community_id.eq(community_user_ban_form.community_id))
244 .filter(user_id.eq(community_user_ban_form.user_id)),
250 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
251 #[belongs_to(Community)]
252 #[table_name = "community_follower"]
253 pub struct CommunityFollower {
255 pub community_id: i32,
257 pub published: chrono::NaiveDateTime,
260 #[derive(Insertable, AsChangeset, Clone)]
261 #[table_name = "community_follower"]
262 pub struct CommunityFollowerForm {
263 pub community_id: i32,
267 impl Followable<CommunityFollowerForm> for CommunityFollower {
270 community_follower_form: &CommunityFollowerForm,
271 ) -> Result<Self, Error> {
272 use crate::schema::community_follower::dsl::*;
273 insert_into(community_follower)
274 .values(community_follower_form)
275 .get_result::<Self>(conn)
279 community_follower_form: &CommunityFollowerForm,
280 ) -> Result<usize, Error> {
281 use crate::schema::community_follower::dsl::*;
284 .filter(community_id.eq(&community_follower_form.community_id))
285 .filter(user_id.eq(&community_follower_form.user_id)),
293 use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType};
297 let conn = establish_unpooled_connection();
299 let new_user = UserForm {
300 name: "bobbee".into(),
301 preferred_username: None,
302 password_encrypted: "nope".into(),
304 matrix_user_id: None,
311 theme: "darkly".into(),
312 default_sort_type: SortType::Hot as i16,
313 default_listing_type: ListingType::Subscribed as i16,
314 lang: "browser".into(),
316 send_notifications_to_email: false,
317 actor_id: "changeme_8266238".into(),
322 last_refreshed_at: None,
325 let inserted_user = User_::create(&conn, &new_user).unwrap();
327 let new_community = CommunityForm {
329 creator_id: inserted_user.id,
330 title: "nada".to_owned(),
337 actor_id: "changeme_7625376".into(),
341 last_refreshed_at: None,
347 let inserted_community = Community::create(&conn, &new_community).unwrap();
349 let expected_community = Community {
350 id: inserted_community.id,
351 creator_id: inserted_user.id,
353 title: "nada".to_owned(),
359 published: inserted_community.published,
361 actor_id: inserted_community.actor_id.to_owned(),
365 last_refreshed_at: inserted_community.published,
370 let community_follower_form = CommunityFollowerForm {
371 community_id: inserted_community.id,
372 user_id: inserted_user.id,
375 let inserted_community_follower =
376 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
378 let expected_community_follower = CommunityFollower {
379 id: inserted_community_follower.id,
380 community_id: inserted_community.id,
381 user_id: inserted_user.id,
382 published: inserted_community_follower.published,
385 let community_user_form = CommunityModeratorForm {
386 community_id: inserted_community.id,
387 user_id: inserted_user.id,
390 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
392 let expected_community_user = CommunityModerator {
393 id: inserted_community_user.id,
394 community_id: inserted_community.id,
395 user_id: inserted_user.id,
396 published: inserted_community_user.published,
399 let community_user_ban_form = CommunityUserBanForm {
400 community_id: inserted_community.id,
401 user_id: inserted_user.id,
404 let inserted_community_user_ban =
405 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
407 let expected_community_user_ban = CommunityUserBan {
408 id: inserted_community_user_ban.id,
409 community_id: inserted_community.id,
410 user_id: inserted_user.id,
411 published: inserted_community_user_ban.published,
414 let read_community = Community::read(&conn, inserted_community.id).unwrap();
415 let updated_community =
416 Community::update(&conn, inserted_community.id, &new_community).unwrap();
417 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
418 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
419 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
420 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
421 User_::delete(&conn, inserted_user.id).unwrap();
423 assert_eq!(expected_community, read_community);
424 assert_eq!(expected_community, inserted_community);
425 assert_eq!(expected_community, updated_community);
426 assert_eq!(expected_community_follower, inserted_community_follower);
427 assert_eq!(expected_community_user, inserted_community_user);
428 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
429 assert_eq!(1, ignored_community);
430 assert_eq!(1, left_community);
431 assert_eq!(1, unban);
432 // assert_eq!(2, loaded_count);
433 assert_eq!(1, num_deleted);