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,
33 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize, Debug)]
34 #[table_name = "community"]
35 pub struct CommunityForm {
38 pub description: Option<String>,
41 pub removed: Option<bool>,
42 pub published: Option<chrono::NaiveDateTime>,
43 pub updated: Option<chrono::NaiveDateTime>,
44 pub deleted: Option<bool>,
48 pub private_key: Option<String>,
49 pub public_key: Option<String>,
50 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
53 impl Crud<CommunityForm> for Community {
54 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
55 use crate::schema::community::dsl::*;
56 community.find(community_id).first::<Self>(conn)
59 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
60 use crate::schema::community::dsl::*;
61 diesel::delete(community.find(community_id)).execute(conn)
64 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
65 use crate::schema::community::dsl::*;
66 insert_into(community)
67 .values(new_community)
68 .get_result::<Self>(conn)
74 new_community: &CommunityForm,
75 ) -> Result<Self, Error> {
76 use crate::schema::community::dsl::*;
77 diesel::update(community.find(community_id))
79 .get_result::<Self>(conn)
84 pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
85 use crate::schema::community::dsl::*;
87 .filter(name.eq(community_name))
91 pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
92 use crate::schema::community::dsl::*;
94 .filter(actor_id.eq(for_actor_id))
98 pub fn list_local(conn: &PgConnection) -> Result<Vec<Self>, Error> {
99 use crate::schema::community::dsl::*;
100 community.filter(local.eq(true)).load::<Community>(conn)
103 pub fn update_deleted(
107 ) -> Result<Self, Error> {
108 use crate::schema::community::dsl::*;
109 diesel::update(community.find(community_id))
111 deleted.eq(new_deleted),
112 updated.eq(naive_now())
114 .get_result::<Self>(conn)
117 pub fn update_removed(
121 ) -> Result<Self, Error> {
122 use crate::schema::community::dsl::*;
123 diesel::update(community.find(community_id))
125 removed.eq(new_removed),
126 updated.eq(naive_now())
128 .get_result::<Self>(conn)
131 pub fn update_creator(
135 ) -> Result<Self, Error> {
136 use crate::schema::community::dsl::*;
137 diesel::update(community.find(community_id))
138 .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
139 .get_result::<Self>(conn)
142 fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
143 use crate::{community_view::CommunityModeratorView, user_view::UserView};
144 let mut mods_and_admins: Vec<i32> = Vec::new();
145 mods_and_admins.append(
146 &mut CommunityModeratorView::for_community(conn, community_id)
147 .map(|v| v.into_iter().map(|m| m.user_id).collect())?,
150 .append(&mut UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())?);
154 pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool {
155 Self::community_mods_and_admins(conn, community_id)
161 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
162 #[belongs_to(Community)]
163 #[table_name = "community_moderator"]
164 pub struct CommunityModerator {
166 pub community_id: i32,
168 pub published: chrono::NaiveDateTime,
171 #[derive(Insertable, AsChangeset, Clone)]
172 #[table_name = "community_moderator"]
173 pub struct CommunityModeratorForm {
174 pub community_id: i32,
178 impl Joinable<CommunityModeratorForm> for CommunityModerator {
181 community_user_form: &CommunityModeratorForm,
182 ) -> Result<Self, Error> {
183 use crate::schema::community_moderator::dsl::*;
184 insert_into(community_moderator)
185 .values(community_user_form)
186 .get_result::<Self>(conn)
191 community_user_form: &CommunityModeratorForm,
192 ) -> Result<usize, Error> {
193 use crate::schema::community_moderator::dsl::*;
196 .filter(community_id.eq(community_user_form.community_id))
197 .filter(user_id.eq(community_user_form.user_id)),
203 impl CommunityModerator {
204 pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
205 use crate::schema::community_moderator::dsl::*;
206 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
210 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
211 #[belongs_to(Community)]
212 #[table_name = "community_user_ban"]
213 pub struct CommunityUserBan {
215 pub community_id: i32,
217 pub published: chrono::NaiveDateTime,
220 #[derive(Insertable, AsChangeset, Clone)]
221 #[table_name = "community_user_ban"]
222 pub struct CommunityUserBanForm {
223 pub community_id: i32,
227 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
230 community_user_ban_form: &CommunityUserBanForm,
231 ) -> Result<Self, Error> {
232 use crate::schema::community_user_ban::dsl::*;
233 insert_into(community_user_ban)
234 .values(community_user_ban_form)
235 .get_result::<Self>(conn)
240 community_user_ban_form: &CommunityUserBanForm,
241 ) -> Result<usize, Error> {
242 use crate::schema::community_user_ban::dsl::*;
245 .filter(community_id.eq(community_user_ban_form.community_id))
246 .filter(user_id.eq(community_user_ban_form.user_id)),
252 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
253 #[belongs_to(Community)]
254 #[table_name = "community_follower"]
255 pub struct CommunityFollower {
257 pub community_id: i32,
259 pub published: chrono::NaiveDateTime,
262 #[derive(Insertable, AsChangeset, Clone)]
263 #[table_name = "community_follower"]
264 pub struct CommunityFollowerForm {
265 pub community_id: i32,
269 impl Followable<CommunityFollowerForm> for CommunityFollower {
272 community_follower_form: &CommunityFollowerForm,
273 ) -> Result<Self, Error> {
274 use crate::schema::community_follower::dsl::*;
275 insert_into(community_follower)
276 .values(community_follower_form)
277 .get_result::<Self>(conn)
281 community_follower_form: &CommunityFollowerForm,
282 ) -> Result<usize, Error> {
283 use crate::schema::community_follower::dsl::*;
286 .filter(community_id.eq(&community_follower_form.community_id))
287 .filter(user_id.eq(&community_follower_form.user_id)),
295 use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType};
299 let conn = establish_unpooled_connection();
301 let new_user = UserForm {
302 name: "bobbee".into(),
303 preferred_username: None,
304 password_encrypted: "nope".into(),
306 matrix_user_id: None,
312 theme: "darkly".into(),
313 default_sort_type: SortType::Hot as i16,
314 default_listing_type: ListingType::Subscribed as i16,
315 lang: "browser".into(),
317 send_notifications_to_email: false,
318 actor_id: "changeme_8266238".into(),
323 last_refreshed_at: None,
326 let inserted_user = User_::create(&conn, &new_user).unwrap();
328 let new_community = CommunityForm {
330 creator_id: inserted_user.id,
331 title: "nada".to_owned(),
338 actor_id: "changeme_7625376".into(),
342 last_refreshed_at: None,
346 let inserted_community = Community::create(&conn, &new_community).unwrap();
348 let expected_community = Community {
349 id: inserted_community.id,
350 creator_id: inserted_user.id,
352 title: "nada".to_owned(),
358 published: inserted_community.published,
360 actor_id: inserted_community.actor_id.to_owned(),
364 last_refreshed_at: inserted_community.published,
367 let community_follower_form = CommunityFollowerForm {
368 community_id: inserted_community.id,
369 user_id: inserted_user.id,
372 let inserted_community_follower =
373 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
375 let expected_community_follower = CommunityFollower {
376 id: inserted_community_follower.id,
377 community_id: inserted_community.id,
378 user_id: inserted_user.id,
379 published: inserted_community_follower.published,
382 let community_user_form = CommunityModeratorForm {
383 community_id: inserted_community.id,
384 user_id: inserted_user.id,
387 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
389 let expected_community_user = CommunityModerator {
390 id: inserted_community_user.id,
391 community_id: inserted_community.id,
392 user_id: inserted_user.id,
393 published: inserted_community_user.published,
396 let community_user_ban_form = CommunityUserBanForm {
397 community_id: inserted_community.id,
398 user_id: inserted_user.id,
401 let inserted_community_user_ban =
402 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
404 let expected_community_user_ban = CommunityUserBan {
405 id: inserted_community_user_ban.id,
406 community_id: inserted_community.id,
407 user_id: inserted_user.id,
408 published: inserted_community_user_ban.published,
411 let read_community = Community::read(&conn, inserted_community.id).unwrap();
412 let updated_community =
413 Community::update(&conn, inserted_community.id, &new_community).unwrap();
414 let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
415 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
416 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
417 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
418 User_::delete(&conn, inserted_user.id).unwrap();
420 assert_eq!(expected_community, read_community);
421 assert_eq!(expected_community, inserted_community);
422 assert_eq!(expected_community, updated_community);
423 assert_eq!(expected_community_follower, inserted_community_follower);
424 assert_eq!(expected_community_user, inserted_community_user);
425 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
426 assert_eq!(1, ignored_community);
427 assert_eq!(1, left_community);
428 assert_eq!(1, unban);
429 // assert_eq!(2, loaded_count);
430 assert_eq!(1, num_deleted);