1 use 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>,
19 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
20 #[table_name="community"]
21 pub struct CommunityForm {
24 pub description: Option<String>,
27 pub removed: Option<bool>,
28 pub updated: Option<chrono::NaiveDateTime>,
29 pub deleted: Option<bool>,
32 impl Crud<CommunityForm> for Community {
33 fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
34 use schema::community::dsl::*;
35 community.find(community_id)
39 fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
40 use schema::community::dsl::*;
41 diesel::delete(community.find(community_id))
45 fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
46 use schema::community::dsl::*;
47 insert_into(community)
48 .values(new_community)
49 .get_result::<Self>(conn)
52 fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
53 use schema::community::dsl::*;
54 diesel::update(community.find(community_id))
56 .get_result::<Self>(conn)
61 pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
62 use schema::community::dsl::*;
63 community.filter(name.eq(community_name))
68 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
69 #[belongs_to(Community)]
70 #[table_name = "community_moderator"]
71 pub struct CommunityModerator {
73 pub community_id: i32,
75 pub published: chrono::NaiveDateTime,
78 #[derive(Insertable, AsChangeset, Clone)]
79 #[table_name="community_moderator"]
80 pub struct CommunityModeratorForm {
81 pub community_id: i32,
85 impl Joinable<CommunityModeratorForm> for CommunityModerator {
86 fn join(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<Self, Error> {
87 use schema::community_moderator::dsl::*;
88 insert_into(community_moderator)
89 .values(community_user_form)
90 .get_result::<Self>(conn)
93 fn leave(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<usize, Error> {
94 use schema::community_moderator::dsl::*;
95 diesel::delete(community_moderator
96 .filter(community_id.eq(community_user_form.community_id))
97 .filter(user_id.eq(community_user_form.user_id)))
102 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
103 #[belongs_to(Community)]
104 #[table_name = "community_user_ban"]
105 pub struct CommunityUserBan {
107 pub community_id: i32,
109 pub published: chrono::NaiveDateTime,
112 #[derive(Insertable, AsChangeset, Clone)]
113 #[table_name="community_user_ban"]
114 pub struct CommunityUserBanForm {
115 pub community_id: i32,
119 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
120 fn ban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<Self, Error> {
121 use schema::community_user_ban::dsl::*;
122 insert_into(community_user_ban)
123 .values(community_user_ban_form)
124 .get_result::<Self>(conn)
127 fn unban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<usize, Error> {
128 use schema::community_user_ban::dsl::*;
129 diesel::delete(community_user_ban
130 .filter(community_id.eq(community_user_ban_form.community_id))
131 .filter(user_id.eq(community_user_ban_form.user_id)))
136 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
137 #[belongs_to(Community)]
138 #[table_name = "community_follower"]
139 pub struct CommunityFollower {
141 pub community_id: i32,
143 pub published: chrono::NaiveDateTime,
146 #[derive(Insertable, AsChangeset, Clone)]
147 #[table_name="community_follower"]
148 pub struct CommunityFollowerForm {
149 pub community_id: i32,
153 impl Followable<CommunityFollowerForm> for CommunityFollower {
154 fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
155 use schema::community_follower::dsl::*;
156 insert_into(community_follower)
157 .values(community_follower_form)
158 .get_result::<Self>(conn)
160 fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
161 use schema::community_follower::dsl::*;
162 diesel::delete(community_follower
163 .filter(community_id.eq(&community_follower_form.community_id))
164 .filter(user_id.eq(&community_follower_form.user_id)))
169 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
174 pub description: Option<String>,
176 pub published: chrono::NaiveDateTime,
177 pub updated: Option<chrono::NaiveDateTime>
180 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
182 pub struct SiteForm {
184 pub description: Option<String>,
186 pub updated: Option<chrono::NaiveDateTime>
189 impl Crud<SiteForm> for Site {
190 fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
191 use schema::site::dsl::*;
192 site.first::<Self>(conn)
195 fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
196 use schema::site::dsl::*;
197 diesel::delete(site.find(site_id))
201 fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
202 use schema::site::dsl::*;
205 .get_result::<Self>(conn)
208 fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
209 use schema::site::dsl::*;
210 diesel::update(site.find(site_id))
212 .get_result::<Self>(conn)
219 use super::super::user::*;
222 let conn = establish_connection();
224 let new_user = UserForm {
225 name: "bobbee".into(),
226 fedi_name: "rrf".into(),
227 preferred_username: None,
228 password_encrypted: "nope".into(),
235 let inserted_user = User_::create(&conn, &new_user).unwrap();
237 let new_community = CommunityForm {
239 creator_id: inserted_user.id,
240 title: "nada".to_owned(),
248 let inserted_community = Community::create(&conn, &new_community).unwrap();
250 let expected_community = Community {
251 id: inserted_community.id,
252 creator_id: inserted_user.id,
254 title: "nada".to_owned(),
259 published: inserted_community.published,
263 let community_follower_form = CommunityFollowerForm {
264 community_id: inserted_community.id,
265 user_id: inserted_user.id
268 let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();
271 let expected_community_follower = CommunityFollower {
272 id: inserted_community_follower.id,
273 community_id: inserted_community.id,
274 user_id: inserted_user.id,
275 published: inserted_community_follower.published
278 let community_user_form = CommunityModeratorForm {
279 community_id: inserted_community.id,
280 user_id: inserted_user.id
283 let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
285 let expected_community_user = CommunityModerator {
286 id: inserted_community_user.id,
287 community_id: inserted_community.id,
288 user_id: inserted_user.id,
289 published: inserted_community_user.published
292 let community_user_ban_form = CommunityUserBanForm {
293 community_id: inserted_community.id,
294 user_id: inserted_user.id
297 let inserted_community_user_ban = CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
299 let expected_community_user_ban = CommunityUserBan {
300 id: inserted_community_user_ban.id,
301 community_id: inserted_community.id,
302 user_id: inserted_user.id,
303 published: inserted_community_user_ban.published
306 let read_community = Community::read(&conn, inserted_community.id).unwrap();
307 let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
308 let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
309 let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
310 let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
311 let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
312 User_::delete(&conn, inserted_user.id).unwrap();
314 assert_eq!(expected_community, read_community);
315 assert_eq!(expected_community, inserted_community);
316 assert_eq!(expected_community, updated_community);
317 assert_eq!(expected_community_follower, inserted_community_follower);
318 assert_eq!(expected_community_user, inserted_community_user);
319 assert_eq!(expected_community_user_ban, inserted_community_user_ban);
320 assert_eq!(1, ignored_community);
321 assert_eq!(1, left_community);
322 assert_eq!(1, unban);
323 // assert_eq!(2, loaded_count);
324 assert_eq!(1, num_deleted);