2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::DbUrl,
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::activity::dsl::{activity, ap_id},
|
|
|
|
source::activity::{Activity, ActivityInsertForm, ActivityUpdateForm},
|
2022-11-09 10:05:00 +00:00
|
|
|
traits::Crud,
|
|
|
|
utils::{get_conn, DbPool},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Activity {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = ActivityInsertForm;
|
|
|
|
type UpdateForm = ActivityUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = i32;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn read(pool: &mut DbPool<'_>, activity_id: i32) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
activity.find(activity_id).first::<Self>(conn).await
|
2020-04-27 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn create(pool: &mut DbPool<'_>, new_activity: &Self::InsertForm) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-04-27 22:17:02 +00:00
|
|
|
insert_into(activity)
|
|
|
|
.values(new_activity)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-04-27 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2020-04-27 22:17:02 +00:00
|
|
|
activity_id: i32,
|
2022-10-27 09:24:07 +00:00
|
|
|
new_activity: &Self::UpdateForm,
|
2020-04-27 22:17:02 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-04-27 22:17:02 +00:00
|
|
|
diesel::update(activity.find(activity_id))
|
|
|
|
.set(new_activity)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-04-27 22:17:02 +00:00
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn delete(pool: &mut DbPool<'_>, activity_id: i32) -> Result<usize, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(activity.find(activity_id))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2020-11-10 16:11:08 +00:00
|
|
|
}
|
2020-04-27 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Activity {
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_from_apub_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
object_id: &DbUrl,
|
|
|
|
) -> Result<Activity, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
activity
|
|
|
|
.filter(ap_id.eq(object_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
.await
|
2021-01-29 16:38:27 +00:00
|
|
|
}
|
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-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::DbUrl,
|
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
activity::{Activity, ActivityInsertForm},
|
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
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
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_crud() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2023-03-01 02:36:57 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let creator_form = PersonInsertForm::builder()
|
|
|
|
.name("activity_creator_ pm".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let ap_id_: DbUrl = Url::parse(
|
2021-03-02 12:41:48 +00:00
|
|
|
"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();
|
2022-10-27 09:24:07 +00:00
|
|
|
let activity_form = ActivityInsertForm {
|
2022-11-09 10:05:00 +00:00
|
|
|
ap_id: ap_id_.clone(),
|
2022-11-19 04:33:54 +00:00
|
|
|
data: test_json.clone(),
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(true),
|
2022-10-27 09:24:07 +00:00
|
|
|
sensitive: Some(false),
|
2020-04-27 22:17:02 +00:00
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_activity = Activity::create(pool, &activity_form).await.unwrap();
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
let expected_activity = Activity {
|
2022-11-09 10:05:00 +00:00
|
|
|
ap_id: ap_id_.clone(),
|
2020-04-27 22:17:02 +00:00
|
|
|
id: inserted_activity.id,
|
|
|
|
data: test_json,
|
|
|
|
local: true,
|
2023-04-17 19:19:51 +00:00
|
|
|
sensitive: false,
|
2020-04-27 22:17:02 +00:00
|
|
|
published: inserted_activity.published,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let read_activity = Activity::read(pool, inserted_activity.id).await.unwrap();
|
|
|
|
let read_activity_by_apub_id = Activity::read_from_apub_id(pool, &ap_id_).await.unwrap();
|
|
|
|
Person::delete(pool, inserted_creator.id).await.unwrap();
|
|
|
|
Activity::delete(pool, inserted_activity.id).await.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);
|
|
|
|
}
|
|
|
|
}
|