]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/activity.rs
79b185fb74fa225aaf259faac255854a7cfcdd5b
[lemmy.git] / server / lemmy_db / src / activity.rs
1 use crate::{schema::activity, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use log::debug;
4 use serde::Serialize;
5 use serde_json::Value;
6 use std::{
7   fmt::Debug,
8   io::{Error as IoError, ErrorKind},
9 };
10
11 #[derive(Queryable, Identifiable, PartialEq, Debug)]
12 #[table_name = "activity"]
13 pub struct Activity {
14   pub id: i32,
15   pub user_id: i32,
16   pub data: Value,
17   pub local: bool,
18   pub published: chrono::NaiveDateTime,
19   pub updated: Option<chrono::NaiveDateTime>,
20 }
21
22 #[derive(Insertable, AsChangeset)]
23 #[table_name = "activity"]
24 pub struct ActivityForm {
25   pub user_id: i32,
26   pub data: Value,
27   pub local: bool,
28   pub updated: Option<chrono::NaiveDateTime>,
29 }
30
31 impl Crud<ActivityForm> for Activity {
32   fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
33     use crate::schema::activity::dsl::*;
34     activity.find(activity_id).first::<Self>(conn)
35   }
36
37   fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
38     use crate::schema::activity::dsl::*;
39     insert_into(activity)
40       .values(new_activity)
41       .get_result::<Self>(conn)
42   }
43
44   fn update(
45     conn: &PgConnection,
46     activity_id: i32,
47     new_activity: &ActivityForm,
48   ) -> Result<Self, Error> {
49     use crate::schema::activity::dsl::*;
50     diesel::update(activity.find(activity_id))
51       .set(new_activity)
52       .get_result::<Self>(conn)
53   }
54 }
55
56 pub fn do_insert_activity<T>(
57   conn: &PgConnection,
58   user_id: i32,
59   data: &T,
60   local: bool,
61 ) -> Result<Activity, IoError>
62 where
63   T: Serialize + Debug,
64 {
65   debug!("inserting activity for user {}, data {:?}", user_id, &data);
66   let activity_form = ActivityForm {
67     user_id,
68     data: serde_json::to_value(&data)?,
69     local,
70     updated: None,
71   };
72   let result = Activity::create(&conn, &activity_form);
73   match result {
74     Ok(s) => Ok(s),
75     Err(e) => Err(IoError::new(
76       ErrorKind::Other,
77       format!("Failed to insert activity into database: {}", e),
78     )),
79   }
80 }
81
82 #[cfg(test)]
83 mod tests {
84   use crate::{
85     activity::{Activity, ActivityForm},
86     tests::establish_unpooled_connection,
87     user::{UserForm, User_},
88     Crud,
89     ListingType,
90     SortType,
91   };
92   use serde_json::Value;
93
94   #[test]
95   fn test_crud() {
96     let conn = establish_unpooled_connection();
97
98     let creator_form = UserForm {
99       name: "activity_creator_pm".into(),
100       preferred_username: None,
101       password_encrypted: "nope".into(),
102       email: None,
103       matrix_user_id: None,
104       avatar: None,
105       banner: None,
106       admin: false,
107       banned: false,
108       updated: None,
109       show_nsfw: false,
110       theme: "darkly".into(),
111       default_sort_type: SortType::Hot as i16,
112       default_listing_type: ListingType::Subscribed as i16,
113       lang: "browser".into(),
114       show_avatars: true,
115       send_notifications_to_email: false,
116       actor_id: None,
117       bio: None,
118       local: true,
119       private_key: None,
120       public_key: None,
121       last_refreshed_at: None,
122     };
123
124     let inserted_creator = User_::create(&conn, &creator_form).unwrap();
125
126     let test_json: Value = serde_json::from_str(
127       r#"{
128     "street": "Article Circle Expressway 1",
129     "city": "North Pole",
130     "postcode": "99705",
131     "state": "Alaska"
132 }"#,
133     )
134     .unwrap();
135     let activity_form = ActivityForm {
136       user_id: inserted_creator.id,
137       data: test_json.to_owned(),
138       local: true,
139       updated: None,
140     };
141
142     let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
143
144     let expected_activity = Activity {
145       id: inserted_activity.id,
146       user_id: inserted_creator.id,
147       data: test_json,
148       local: true,
149       published: inserted_activity.published,
150       updated: None,
151     };
152
153     let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
154     User_::delete(&conn, inserted_creator.id).unwrap();
155
156     assert_eq!(expected_activity, read_activity);
157     assert_eq!(expected_activity, inserted_activity);
158   }
159 }