1 use crate::schema::{community, community_moderator, community_follower, 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)
41 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
42 use crate::schema::community::dsl::*;
43 diesel::delete(community.find(community_id))
47 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
48 use crate::schema::community::dsl::*;
49 insert_into(community)
50 .values(new_community)
51 .get_result::<Self>(conn)
54 fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
55 use crate::schema::community::dsl::*;
56 diesel::update(community.find(community_id))
58 .get_result::<Self>(conn)
63 pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
64 use crate::schema::community::dsl::*;
65 community.filter(name.eq(community_name))
70 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
71 #[belongs_to(Community)]
72 #[table_name = "community_moderator"]
73 pub struct CommunityModerator {
75 pub community_id: i32,
77 pub published: chrono::NaiveDateTime,
80 #[derive(Insertable, AsChangeset, Clone)]
81 #[table_name="community_moderator"]
82 pub struct CommunityModeratorForm {
83 pub community_id: i32,
87 impl Joinable<CommunityModeratorForm> for CommunityModerator {
88 fn join(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<Self, Error> {
89 use crate::schema::community_moderator::dsl::*;
90 insert_into(community_moderator)
91 .values(community_user_form)
92 .get_result::<Self>(conn)
95 fn leave(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<usize, Error> {
96 use crate::schema::community_moderator::dsl::*;
97 diesel::delete(community_moderator
98 .filter(community_id.eq(community_user_form.community_id))
99 .filter(user_id.eq(community_user_form.user_id)))
104 impl CommunityModerator {
105 pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
106 use crate::schema::community_moderator::dsl::*;
109 .filter(community_id.eq(for_community_id)))
114 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
115 #[belongs_to(Community)]
116 #[table_name = "community_user_ban"]
117 pub struct CommunityUserBan {
119 pub community_id: i32,
121 pub published: chrono::NaiveDateTime,
124 #[derive(Insertable, AsChangeset, Clone)]
125 #[table_name="community_user_ban"]
126 pub struct CommunityUserBanForm {
127 pub community_id: i32,
131 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
132 fn ban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<Self, Error> {
133 use crate::schema::community_user_ban::dsl::*;
134 insert_into(community_user_ban)
135 .values(community_user_ban_form)
136 .get_result::<Self>(conn)
139 fn unban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<usize, Error> {
140 use crate::schema::community_user_ban::dsl::*;
141 diesel::delete(community_user_ban
142 .filter(community_id.eq(community_user_ban_form.community_id))
143 .filter(user_id.eq(community_user_ban_form.user_id)))
148 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
149 #[belongs_to(Community)]
150 #[table_name = "community_follower"]
151 pub struct CommunityFollower {
153 pub community_id: i32,
155 pub published: chrono::NaiveDateTime,
158 #[derive(Insertable, AsChangeset, Clone)]
159 #[table_name="community_follower"]
160 pub struct CommunityFollowerForm {
161 pub community_id: i32,
165 impl Followable<CommunityFollowerForm> for CommunityFollower {
166 fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
167 use crate::schema::community_follower::dsl::*;
168 insert_into(community_follower)
169 .values(community_follower_form)
170 .get_result::<Self>(conn)
172 fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
173 use crate::schema::community_follower::dsl::*;
174 diesel::delete(community_follower
175 .filter(community_id.eq(&community_follower_form.community_id))
176 .filter(user_id.eq(&community_follower_form.user_id)))
181 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
186 pub description: Option<String>,
188 pub published: chrono::NaiveDateTime,
189 pub updated: Option<chrono::NaiveDateTime>
192 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
194 pub struct SiteForm {
196 pub description: Option<String>,
198 pub updated: Option<chrono::NaiveDateTime>
201 impl Crud<SiteForm> for Site {
202 fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
203 use crate::schema::site::dsl::*;
204 site.first::<Self>(conn)
207 fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
208 use crate::schema::site::dsl::*;
209 diesel::delete(site.find(site_id))
213 fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
214 use crate::schema::site::dsl::*;
217 .get_result::<Self>(conn)
220 fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
221 use crate::schema::site::dsl::*;
222 diesel::update(site.find(site_id))
224 .get_result::<Self>(conn)
231 use super::super::user::*;
234 let conn = establish_connection();
236 let new_user = UserForm {
237 name: "bobbee".into(),
238 fedi_name: "rrf".into(),
239 preferred_username: None,
240 password_encrypted: "nope".into(),
248 let inserted_user = User_::create(&conn, &new_user).unwrap();
250 let new_community = CommunityForm {
252 creator_id: inserted_user.id,
253 title: "nada".to_owned(),
262 let inserted_community = Community::create(&conn, &new_community).unwrap();
264 let expected_community = Community {
265 id: inserted_community.id,
266 creator_id: inserted_user.id,
268 title: "nada".to_owned(),
274 published: inserted_community.published,
278 let community_follower_form = CommunityFollowerForm {
279 community_id: inserted_community.id,
280 user_id: inserted_user.id
283 let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();
286 let expected_community_follower = CommunityFollower {
287 id: inserted_community_follower.id,
288 community_id: inserted_community.id,
289 user_id: inserted_user.id,
290 published: inserted_community_follower.published
293 let community_user_form = CommunityModeratorForm {
294 community_id: inserted_community.id,
295 user_id: inserted_user.id
298 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
300 let expected_community_user = CommunityModerator {
301 id: inserted_community_user.id,
302 community_id: inserted_community.id,
303 user_id: inserted_user.id,
304 published: inserted_community_user.published
307 let community_user_ban_form = CommunityUserBanForm {
308 community_id: inserted_community.id,
309 user_id: inserted_user.id
312 let inserted_community_user_ban = CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
314 let expected_community_user_ban = CommunityUserBan {
315 id: inserted_community_user_ban.id,
316 community_id: inserted_community.id,
317 user_id: inserted_user.id,
318 published: inserted_community_user_ban.published
321 let read_community = Community::read(&conn, inserted_community.id).unwrap();
322 let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
323 let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
324 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
325 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
326 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
327 User_::delete(&conn, inserted_user.id).unwrap();
329 assert_eq!(expected_community, read_community);
330 assert_eq!(expected_community, inserted_community);
331 assert_eq!(expected_community, updated_community);
332 assert_eq!(expected_community_follower, inserted_community_follower);
333 assert_eq!(expected_community_user, inserted_community_user);
334 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
335 assert_eq!(1, ignored_community);
336 assert_eq!(1, left_community);
337 assert_eq!(1, unban);
338 // assert_eq!(2, loaded_count);
339 assert_eq!(1, num_deleted);