2020-12-18 16:17:21 +00:00
|
|
|
use crate::Crud;
|
2021-01-27 14:02:28 +00:00
|
|
|
use diesel::{dsl::*, result::Error, sql_types::Text, *};
|
2021-03-02 12:41:48 +00:00
|
|
|
use lemmy_db_schema::{source::activity::*, DbUrl};
|
2020-05-30 17:44:50 +00:00
|
|
|
use log::debug;
|
2020-09-14 15:29:50 +00:00
|
|
|
use serde::Serialize;
|
2021-01-27 14:02:28 +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
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
impl Crud<ActivityForm, i32> for Activity {
|
2020-04-27 22:17:02 +00:00
|
|
|
fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::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-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::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-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::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-11-10 16:11:08 +00:00
|
|
|
fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::activity::dsl::*;
|
2020-11-10 16:11:08 +00:00
|
|
|
diesel::delete(activity.find(activity_id)).execute(conn)
|
|
|
|
}
|
2020-04-27 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
pub trait Activity_ {
|
|
|
|
fn insert<T>(
|
|
|
|
conn: &PgConnection,
|
2021-03-02 12:41:48 +00:00
|
|
|
ap_id: DbUrl,
|
2020-12-21 13:38:34 +00:00
|
|
|
data: &T,
|
|
|
|
local: bool,
|
|
|
|
sensitive: bool,
|
|
|
|
) -> Result<Activity, IoError>
|
|
|
|
where
|
|
|
|
T: Serialize + Debug;
|
2021-01-27 14:02:28 +00:00
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error>;
|
2021-01-29 16:38:27 +00:00
|
|
|
fn delete_olds(conn: &PgConnection) -> Result<usize, Error>;
|
2021-01-27 14:02:28 +00:00
|
|
|
|
|
|
|
/// Returns up to 20 activities of type `Announce/Create/Page` from the community
|
|
|
|
fn read_community_outbox(
|
|
|
|
conn: &PgConnection,
|
2021-03-02 12:41:48 +00:00
|
|
|
community_actor_id: &DbUrl,
|
2021-01-27 14:02:28 +00:00
|
|
|
) -> Result<Vec<Value>, Error>;
|
2020-12-21 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Activity_ for Activity {
|
|
|
|
fn insert<T>(
|
2020-10-23 12:29:56 +00:00
|
|
|
conn: &PgConnection,
|
2021-03-02 12:41:48 +00:00
|
|
|
ap_id: DbUrl,
|
2020-10-23 12:29:56 +00:00
|
|
|
data: &T,
|
|
|
|
local: bool,
|
2020-11-06 13:06:47 +00:00
|
|
|
sensitive: bool,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<Activity, IoError>
|
2020-10-23 12:29:56 +00:00
|
|
|
where
|
|
|
|
T: Serialize + Debug,
|
|
|
|
{
|
|
|
|
debug!("{}", serde_json::to_string_pretty(&data)?);
|
|
|
|
let activity_form = ActivityForm {
|
|
|
|
ap_id,
|
|
|
|
data: serde_json::to_value(&data)?,
|
|
|
|
local,
|
2020-11-06 13:06:47 +00:00
|
|
|
sensitive,
|
2020-10-23 12:29:56 +00:00
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
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),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::activity::dsl::*;
|
2020-10-23 12:29:56 +00:00
|
|
|
activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
2020-07-10 18:15:41 +00:00
|
|
|
}
|
2021-01-27 14:02:28 +00:00
|
|
|
|
2021-01-29 16:38:27 +00:00
|
|
|
fn delete_olds(conn: &PgConnection) -> Result<usize, Error> {
|
|
|
|
use lemmy_db_schema::schema::activity::dsl::*;
|
|
|
|
diesel::delete(activity.filter(published.lt(now - 6.months()))).execute(conn)
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
fn read_community_outbox(
|
|
|
|
conn: &PgConnection,
|
2021-03-02 12:41:48 +00:00
|
|
|
community_actor_id: &DbUrl,
|
2021-01-27 14:02:28 +00:00
|
|
|
) -> Result<Vec<Value>, Error> {
|
|
|
|
use lemmy_db_schema::schema::activity::dsl::*;
|
|
|
|
let res: Vec<Value> = activity
|
|
|
|
.select(data)
|
|
|
|
.filter(
|
|
|
|
sql("activity.data ->> 'type' = 'Announce'")
|
|
|
|
.sql(" AND activity.data -> 'object' ->> 'type' = 'Create'")
|
|
|
|
.sql(" AND activity.data -> 'object' -> 'object' ->> 'type' = 'Page'")
|
|
|
|
.sql(" AND activity.data ->> 'actor' = ")
|
2021-02-18 15:15:52 +00:00
|
|
|
.bind::<Text, _>(community_actor_id)
|
2021-02-19 14:59:06 +00:00
|
|
|
.sql(" ORDER BY activity.published DESC"),
|
2021-01-27 14:02:28 +00:00
|
|
|
)
|
|
|
|
.limit(20)
|
|
|
|
.get_results(conn)?;
|
|
|
|
Ok(res)
|
|
|
|
}
|
2020-05-14 12:26:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:17:02 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-02 12:41:48 +00:00
|
|
|
use super::*;
|
2021-03-11 04:43:11 +00:00
|
|
|
use crate::{establish_unpooled_connection, source::activity::Activity_};
|
2020-12-21 13:38:34 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
activity::{Activity, ActivityForm},
|
2021-03-11 04:43:11 +00:00
|
|
|
person::{Person, PersonForm},
|
2020-07-10 18:15:41 +00:00
|
|
|
};
|
|
|
|
use serde_json::Value;
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2021-03-02 12:41:48 +00:00
|
|
|
use url::Url;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2020-04-27 22:17:02 +00:00
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_unpooled_connection();
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let creator_form = PersonForm {
|
2020-04-27 22:17:02 +00:00
|
|
|
name: "activity_creator_pm".into(),
|
|
|
|
preferred_username: None,
|
|
|
|
avatar: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner: None,
|
2021-02-26 13:49:58 +00:00
|
|
|
banned: None,
|
|
|
|
deleted: None,
|
2020-09-18 11:04:12 +00:00
|
|
|
published: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
updated: None,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
bio: None,
|
2021-02-26 13:49:58 +00:00
|
|
|
local: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: None,
|
2021-02-04 16:34:58 +00:00
|
|
|
inbox_url: None,
|
|
|
|
shared_inbox_url: None,
|
2020-04-27 22:17:02 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let inserted_creator = Person::create(&conn, &creator_form).unwrap();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
let ap_id: DbUrl = Url::parse(
|
|
|
|
"https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.into();
|
2020-04-27 22:17:02 +00:00
|
|
|
let test_json: Value = serde_json::from_str(
|
|
|
|
r#"{
|
2020-10-23 12:29:56 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": "https://enterprise.lemmy.ml/u/riker",
|
|
|
|
"to": "https://www.w3.org/ns/activitystreams#Public",
|
|
|
|
"cc": [
|
|
|
|
"https://enterprise.lemmy.ml/c/main/"
|
|
|
|
],
|
|
|
|
"object": "https://enterprise.lemmy.ml/post/32"
|
|
|
|
}"#,
|
2020-04-27 22:17:02 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let activity_form = ActivityForm {
|
2021-03-02 12:41:48 +00:00
|
|
|
ap_id: ap_id.clone(),
|
2020-04-27 22:17:02 +00:00
|
|
|
data: test_json.to_owned(),
|
|
|
|
local: true,
|
2020-11-06 13:06:47 +00:00
|
|
|
sensitive: false,
|
2020-04-27 22:17:02 +00:00
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
|
|
|
|
|
|
|
|
let expected_activity = Activity {
|
2021-03-02 12:41:48 +00:00
|
|
|
ap_id: Some(ap_id.clone()),
|
2020-04-27 22:17:02 +00:00
|
|
|
id: inserted_activity.id,
|
|
|
|
data: test_json,
|
|
|
|
local: true,
|
2020-11-10 16:11:08 +00:00
|
|
|
sensitive: Some(false),
|
2020-04-27 22:17:02 +00:00
|
|
|
published: inserted_activity.published,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
|
2021-03-02 12:41:48 +00:00
|
|
|
let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, &ap_id).unwrap();
|
2021-02-26 13:49:58 +00:00
|
|
|
Person::delete(&conn, inserted_creator.id).unwrap();
|
2020-11-10 16:11:08 +00:00
|
|
|
Activity::delete(&conn, inserted_activity.id).unwrap();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
assert_eq!(expected_activity, read_activity);
|
2020-10-23 12:29:56 +00:00
|
|
|
assert_eq!(expected_activity, read_activity_by_apub_id);
|
2020-04-27 22:17:02 +00:00
|
|
|
assert_eq!(expected_activity, inserted_activity);
|
|
|
|
}
|
|
|
|
}
|