summaryrefslogtreecommitdiffstats
path: root/server/src/db/activity.rs
blob: 12137887add86a76ca19f971396e61bbff444098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{db::Crud, schema::activity};
use diesel::{dsl::*, result::Error, *};
use failure::_core::fmt::Debug;
use log::debug;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "activity"]
pub struct Activity {
  pub id: i32,
  pub user_id: i32,
  pub data: Value,
  pub local: bool,
  pub published: chrono::NaiveDateTime,
  pub updated: Option<chrono::NaiveDateTime>,
}

#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
#[table_name = "activity"]
pub struct ActivityForm {
  pub user_id: i32,
  pub data: Value,
  pub local: bool,
  pub updated: Option<chrono::NaiveDateTime>,
}

impl Crud<ActivityForm> for Activity {
  fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
    use crate::schema::activity::dsl::*;
    activity.find(activity_id).first::<Self>(conn)
  }

  fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
    use crate::schema::activity::dsl::*;
    diesel::delete(activity.find(activity_id)).execute(conn)
  }

  fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
    use crate::schema::activity::dsl::*;
    insert_into(activity)
      .values(new_activity)
      .get_result::<Self>(conn)
  }

  fn update(
    conn: &PgConnection,
    activity_id: i32,
    new_activity: &ActivityForm,
  ) -> Result<Self, Error> {
    use crate::schema::activity::dsl::*;
    diesel::update(activity.find(activity_id))
      .set(new_activity)
      .get_result::<Self>(conn)
  }
}

pub fn insert_activity<T>(
  conn: &PgConnection,
  user_id: i32,
  data: &T,
  local: bool,
) -> Result<(), failure::Error>
where
  T: Serialize + Debug,
{
  let activity_form = ActivityForm {
    user_id,
    data: serde_json::to_value(&data)?,
    local,
    updated: None,
  };
  debug!("inserting activity for user {}, data {:?}", user_id, data);
  // TODO: this is broken
  //Activity::create(&conn, &activity_form)?;
  Ok(())
}

#[cfg(test)]
mod tests {
  use super::{super::user::*, *};
  use crate::db::{establish_unpooled_connection, Crud, ListingType, SortType};

  #[test]
  fn test_crud() {
    let conn = establish_unpooled_connection();

    let creator_form = UserForm {
      name: "activity_creator_pm".into(),
      preferred_username: None,
      password_encrypted: "nope".into(),
      email: None,
      matrix_user_id: None,
      avatar: None,
      admin: false,
      banned: false,
      updated: None,
      show_nsfw: false,
      theme: "darkly".into(),
      default_sort_type: SortType::Hot as i16,
      default_listing_type: ListingType::Subscribed as i16,
      lang: "browser".into(),
      show_avatars: true,
      send_notifications_to_email: false,
      actor_id: "changeme".into(),
      bio: None,
      local: true,
      private_key: None,
      public_key: None,
      last_refreshed_at: None,
    };

    let inserted_creator = User_::create(&conn, &creator_form).unwrap();

    let test_json: Value = serde_json::from_str(
      r#"{
    "street": "Article Circle Expressway 1",
    "city": "North Pole",
    "postcode": "99705",
    "state": "Alaska"
}"#,
    )
    .unwrap();
    let activity_form = ActivityForm {
      user_id: inserted_creator.id,
      data: test_json.to_owned(),
      local: true,
      updated: None,
    };

    let inserted_activity = Activity::create(&conn, &activity_form).unwrap();

    let expected_activity = Activity {
      id: inserted_activity.id,
      user_id: inserted_creator.id,
      data: test_json,
      local: true,
      published: inserted_activity.published,
      updated: None,
    };

    let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
    let num_deleted = Activity::delete(&conn, inserted_activity.id).unwrap();
    User_::delete(&conn, inserted_creator.id).unwrap();

    assert_eq!(expected_activity, read_activity);
    assert_eq!(expected_activity, inserted_activity);
    assert_eq!(1, num_deleted);
  }
}