2 use crate::schema::{community, community_follower, community_moderator, community_user_ban, site};
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name = "community"]
10 pub description: Option<String>,
14 pub published: chrono::NaiveDateTime,
15 pub updated: Option<chrono::NaiveDateTime>,
20 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
21 #[table_name = "community"]
22 pub struct CommunityForm {
25 pub description: Option<String>,
28 pub removed: Option<bool>,
29 pub updated: Option<chrono::NaiveDateTime>,
30 pub deleted: Option<bool>,
34 impl Crud<CommunityForm> for Community {
35 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
36 use crate::schema::community::dsl::*;
37 community.find(community_id).first::<Self>(conn)
40 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
41 use crate::schema::community::dsl::*;
42 diesel::delete(community.find(community_id)).execute(conn)
45 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
46 use crate::schema::community::dsl::*;
47 insert_into(community)
48 .values(new_community)
49 .get_result::<Self>(conn)
55 new_community: &CommunityForm,
56 ) -> Result<Self, Error> {
57 use crate::schema::community::dsl::*;
58 diesel::update(community.find(community_id))
60 .get_result::<Self>(conn)
65 pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
66 use crate::schema::community::dsl::*;
68 .filter(name.eq(community_name))
73 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
74 #[belongs_to(Community)]
75 #[table_name = "community_moderator"]
76 pub struct CommunityModerator {
78 pub community_id: i32,
80 pub published: chrono::NaiveDateTime,
83 #[derive(Insertable, AsChangeset, Clone)]
84 #[table_name = "community_moderator"]
85 pub struct CommunityModeratorForm {
86 pub community_id: i32,
90 impl Joinable<CommunityModeratorForm> for CommunityModerator {
93 community_user_form: &CommunityModeratorForm,
94 ) -> Result<Self, Error> {
95 use crate::schema::community_moderator::dsl::*;
96 insert_into(community_moderator)
97 .values(community_user_form)
98 .get_result::<Self>(conn)
103 community_user_form: &CommunityModeratorForm,
104 ) -> Result<usize, Error> {
105 use crate::schema::community_moderator::dsl::*;
108 .filter(community_id.eq(community_user_form.community_id))
109 .filter(user_id.eq(community_user_form.user_id)),
115 impl CommunityModerator {
116 pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
117 use crate::schema::community_moderator::dsl::*;
118 diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
122 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
123 #[belongs_to(Community)]
124 #[table_name = "community_user_ban"]
125 pub struct CommunityUserBan {
127 pub community_id: i32,
129 pub published: chrono::NaiveDateTime,
132 #[derive(Insertable, AsChangeset, Clone)]
133 #[table_name = "community_user_ban"]
134 pub struct CommunityUserBanForm {
135 pub community_id: i32,
139 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
142 community_user_ban_form: &CommunityUserBanForm,
143 ) -> Result<Self, Error> {
144 use crate::schema::community_user_ban::dsl::*;
145 insert_into(community_user_ban)
146 .values(community_user_ban_form)
147 .get_result::<Self>(conn)
152 community_user_ban_form: &CommunityUserBanForm,
153 ) -> Result<usize, Error> {
154 use crate::schema::community_user_ban::dsl::*;
157 .filter(community_id.eq(community_user_ban_form.community_id))
158 .filter(user_id.eq(community_user_ban_form.user_id)),
164 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
165 #[belongs_to(Community)]
166 #[table_name = "community_follower"]
167 pub struct CommunityFollower {
169 pub community_id: i32,
171 pub published: chrono::NaiveDateTime,
174 #[derive(Insertable, AsChangeset, Clone)]
175 #[table_name = "community_follower"]
176 pub struct CommunityFollowerForm {
177 pub community_id: i32,
181 impl Followable<CommunityFollowerForm> for CommunityFollower {
184 community_follower_form: &CommunityFollowerForm,
185 ) -> Result<Self, Error> {
186 use crate::schema::community_follower::dsl::*;
187 insert_into(community_follower)
188 .values(community_follower_form)
189 .get_result::<Self>(conn)
193 community_follower_form: &CommunityFollowerForm,
194 ) -> Result<usize, Error> {
195 use crate::schema::community_follower::dsl::*;
198 .filter(community_id.eq(&community_follower_form.community_id))
199 .filter(user_id.eq(&community_follower_form.user_id)),
205 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
206 #[table_name = "site"]
210 pub description: Option<String>,
212 pub published: chrono::NaiveDateTime,
213 pub updated: Option<chrono::NaiveDateTime>,
216 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
217 #[table_name = "site"]
218 pub struct SiteForm {
220 pub description: Option<String>,
222 pub updated: Option<chrono::NaiveDateTime>,
225 impl Crud<SiteForm> for Site {
226 fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
227 use crate::schema::site::dsl::*;
228 site.first::<Self>(conn)
231 fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
232 use crate::schema::site::dsl::*;
233 diesel::delete(site.find(site_id)).execute(conn)
236 fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
237 use crate::schema::site::dsl::*;
238 insert_into(site).values(new_site).get_result::<Self>(conn)
241 fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
242 use crate::schema::site::dsl::*;
243 diesel::update(site.find(site_id))
245 .get_result::<Self>(conn)
251 use super::super::user::*;
255 let conn = establish_connection();
257 let new_user = UserForm {
258 name: "bobbee".into(),
259 fedi_name: "rrf".into(),
260 preferred_username: None,
261 password_encrypted: "nope".into(),
267 theme: "darkly".into(),
270 let inserted_user = User_::create(&conn, &new_user).unwrap();
272 let new_community = CommunityForm {
274 creator_id: inserted_user.id,
275 title: "nada".to_owned(),
284 let inserted_community = Community::create(&conn, &new_community).unwrap();
286 let expected_community = Community {
287 id: inserted_community.id,
288 creator_id: inserted_user.id,
290 title: "nada".to_owned(),
296 published: inserted_community.published,
300 let community_follower_form = CommunityFollowerForm {
301 community_id: inserted_community.id,
302 user_id: inserted_user.id,
305 let inserted_community_follower =
306 CommunityFollower::follow(&conn, &community_follower_form).unwrap();
308 let expected_community_follower = CommunityFollower {
309 id: inserted_community_follower.id,
310 community_id: inserted_community.id,
311 user_id: inserted_user.id,
312 published: inserted_community_follower.published,
315 let community_user_form = CommunityModeratorForm {
316 community_id: inserted_community.id,
317 user_id: inserted_user.id,
320 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
322 let expected_community_user = CommunityModerator {
323 id: inserted_community_user.id,
324 community_id: inserted_community.id,
325 user_id: inserted_user.id,
326 published: inserted_community_user.published,
329 let community_user_ban_form = CommunityUserBanForm {
330 community_id: inserted_community.id,
331 user_id: inserted_user.id,
334 let inserted_community_user_ban =
335 CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
337 let expected_community_user_ban = CommunityUserBan {
338 id: inserted_community_user_ban.id,
339 community_id: inserted_community.id,
340 user_id: inserted_user.id,
341 published: inserted_community_user_ban.published,
344 let read_community = Community::read(&conn, inserted_community.id).unwrap();
345 let updated_community =
346 Community::update(&conn, inserted_community.id, &new_community).unwrap();
347 let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
348 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
349 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
350 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
351 User_::delete(&conn, inserted_user.id).unwrap();
353 assert_eq!(expected_community, read_community);
354 assert_eq!(expected_community, inserted_community);
355 assert_eq!(expected_community, updated_community);
356 assert_eq!(expected_community_follower, inserted_community_follower);
357 assert_eq!(expected_community_user, inserted_community_user);
358 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
359 assert_eq!(1, ignored_community);
360 assert_eq!(1, left_community);
361 assert_eq!(1, unban);
362 // assert_eq!(2, loaded_count);
363 assert_eq!(1, num_deleted);