1 use crate::{schema::activity, Crud};
2 use diesel::{dsl::*, result::Error, *};
4 use serde::{Deserialize, Serialize};
8 io::{Error as IoError, ErrorKind},
11 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
12 #[table_name = "activity"]
18 pub published: chrono::NaiveDateTime,
19 pub updated: Option<chrono::NaiveDateTime>,
22 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
23 #[table_name = "activity"]
24 pub struct ActivityForm {
28 pub updated: Option<chrono::NaiveDateTime>,
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)
37 fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
38 use crate::schema::activity::dsl::*;
39 diesel::delete(activity.find(activity_id)).execute(conn)
42 fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
43 use crate::schema::activity::dsl::*;
46 .get_result::<Self>(conn)
52 new_activity: &ActivityForm,
53 ) -> Result<Self, Error> {
54 use crate::schema::activity::dsl::*;
55 diesel::update(activity.find(activity_id))
57 .get_result::<Self>(conn)
61 pub fn do_insert_activity<T>(
66 ) -> Result<Activity, IoError>
70 debug!("inserting activity for user {}, data {:?}", user_id, &data);
71 let activity_form = ActivityForm {
73 data: serde_json::to_value(&data)?,
77 let result = Activity::create(&conn, &activity_form);
80 Err(e) => Err(IoError::new(
82 format!("Failed to insert activity into database: {}", e),
90 activity::{Activity, ActivityForm},
91 tests::establish_unpooled_connection,
92 user::{UserForm, User_},
97 use serde_json::Value;
101 let conn = establish_unpooled_connection();
103 let creator_form = UserForm {
104 name: "activity_creator_pm".into(),
105 preferred_username: None,
106 password_encrypted: "nope".into(),
108 matrix_user_id: None,
115 theme: "darkly".into(),
116 default_sort_type: SortType::Hot as i16,
117 default_listing_type: ListingType::Subscribed as i16,
118 lang: "browser".into(),
120 send_notifications_to_email: false,
121 actor_id: "changeme_862362".into(),
126 last_refreshed_at: None,
129 let inserted_creator = User_::create(&conn, &creator_form).unwrap();
131 let test_json: Value = serde_json::from_str(
133 "street": "Article Circle Expressway 1",
134 "city": "North Pole",
140 let activity_form = ActivityForm {
141 user_id: inserted_creator.id,
142 data: test_json.to_owned(),
147 let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
149 let expected_activity = Activity {
150 id: inserted_activity.id,
151 user_id: inserted_creator.id,
154 published: inserted_activity.published,
158 let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
159 let num_deleted = Activity::delete(&conn, inserted_activity.id).unwrap();
160 User_::delete(&conn, inserted_creator.id).unwrap();
162 assert_eq!(expected_activity, read_activity);
163 assert_eq!(expected_activity, inserted_activity);
164 assert_eq!(1, num_deleted);