2020-07-10 18:15:41 +00:00
|
|
|
use crate::{schema::activity, Crud};
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2020-05-30 17:44:50 +00:00
|
|
|
use log::debug;
|
2020-09-03 19:45:12 +00:00
|
|
|
use serde::{Serialize};
|
2020-04-27 22:17:02 +00:00
|
|
|
use serde_json::Value;
|
2020-07-10 18:15:41 +00:00
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
io::{Error as IoError, ErrorKind},
|
|
|
|
};
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug)]
|
2020-04-27 22:17:02 +00:00
|
|
|
#[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>,
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Insertable, AsChangeset)]
|
2020-04-27 22:17:02 +00:00
|
|
|
#[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> {
|
2020-05-14 12:26:44 +00:00
|
|
|
use crate::schema::activity::dsl::*;
|
2020-04-27 22:17:02 +00:00
|
|
|
activity.find(activity_id).first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
|
2020-05-14 12:26:44 +00:00
|
|
|
use crate::schema::activity::dsl::*;
|
2020-04-27 22:17:02 +00:00
|
|
|
insert_into(activity)
|
|
|
|
.values(new_activity)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(
|
|
|
|
conn: &PgConnection,
|
|
|
|
activity_id: i32,
|
|
|
|
new_activity: &ActivityForm,
|
|
|
|
) -> Result<Self, Error> {
|
2020-05-14 12:26:44 +00:00
|
|
|
use crate::schema::activity::dsl::*;
|
2020-04-27 22:17:02 +00:00
|
|
|
diesel::update(activity.find(activity_id))
|
|
|
|
.set(new_activity)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub fn do_insert_activity<T>(
|
2020-05-14 12:26:44 +00:00
|
|
|
conn: &PgConnection,
|
|
|
|
user_id: i32,
|
|
|
|
data: &T,
|
|
|
|
local: bool,
|
2020-07-10 18:15:41 +00:00
|
|
|
) -> Result<Activity, IoError>
|
2020-05-14 12:26:44 +00:00
|
|
|
where
|
2020-05-30 17:44:50 +00:00
|
|
|
T: Serialize + Debug,
|
2020-05-14 12:26:44 +00:00
|
|
|
{
|
2020-07-10 18:15:41 +00:00
|
|
|
debug!("inserting activity for user {}, data {:?}", user_id, &data);
|
2020-05-14 12:26:44 +00:00
|
|
|
let activity_form = ActivityForm {
|
|
|
|
user_id,
|
|
|
|
data: serde_json::to_value(&data)?,
|
|
|
|
local,
|
|
|
|
updated: None,
|
|
|
|
};
|
2020-07-10 18:15:41 +00:00
|
|
|
let result = Activity::create(&conn, &activity_form);
|
|
|
|
match result {
|
|
|
|
Ok(s) => Ok(s),
|
|
|
|
Err(e) => Err(IoError::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Failed to insert activity into database: {}", e),
|
|
|
|
)),
|
|
|
|
}
|
2020-05-14 12:26:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:17:02 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-10 18:15:41 +00:00
|
|
|
use crate::{
|
|
|
|
activity::{Activity, ActivityForm},
|
|
|
|
tests::establish_unpooled_connection,
|
|
|
|
user::{UserForm, User_},
|
|
|
|
Crud,
|
|
|
|
ListingType,
|
|
|
|
SortType,
|
|
|
|
};
|
|
|
|
use serde_json::Value;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
#[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,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
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,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
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();
|
|
|
|
User_::delete(&conn, inserted_creator.id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(expected_activity, read_activity);
|
|
|
|
assert_eq!(expected_activity, inserted_activity);
|
|
|
|
}
|
|
|
|
}
|