lemmy/crates/db_schema/src/impls/activity.rs

171 lines
4.7 KiB
Rust
Raw Normal View History

2022-11-09 10:05:00 +00:00
use crate::{
newtypes::DbUrl,
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},
};
use diesel::{
dsl::insert_into,
result::{DatabaseErrorKind, Error},
2022-11-09 10:05:00 +00:00
ExpressionMethods,
QueryDsl,
};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
use serde_json::Value;
2020-04-27 22:17:02 +00:00
2022-11-09 10:05:00 +00:00
#[async_trait]
impl Crud for Activity {
type InsertForm = ActivityInsertForm;
type UpdateForm = ActivityUpdateForm;
type IdType = i32;
2022-11-09 10:05:00 +00:00
async fn read(pool: &DbPool, activity_id: i32) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
activity.find(activity_id).first::<Self>(conn).await
2020-04-27 22:17:02 +00:00
}
2022-11-09 10:05:00 +00:00
async fn create(pool: &DbPool, new_activity: &Self::InsertForm) -> Result<Self, Error> {
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(
pool: &DbPool,
2020-04-27 22:17:02 +00:00
activity_id: i32,
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
}
2022-11-09 10:05:00 +00:00
async fn delete(pool: &DbPool, activity_id: i32) -> Result<usize, Error> {
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 {
/// Returns true if the insert was successful
// TODO this should probably just be changed to an upsert on_conflict, rather than an error
2022-11-09 10:05:00 +00:00
pub async fn insert(
pool: &DbPool,
ap_id_: DbUrl,
data_: Value,
local_: bool,
sensitive_: Option<bool>,
) -> Result<bool, Error> {
let activity_form = ActivityInsertForm {
2022-11-09 10:05:00 +00:00
ap_id: ap_id_,
data: data_,
local: Some(local_),
sensitive: sensitive_,
updated: None,
};
2022-11-09 10:05:00 +00:00
match Activity::create(pool, &activity_form).await {
Ok(_) => Ok(true),
Err(e) => {
if let Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) = e {
return Ok(false);
}
Err(e)
}
}
}
2022-11-09 10:05:00 +00:00
pub async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Activity, Error> {
let conn = &mut get_conn(pool).await?;
activity
.filter(ap_id.eq(object_id))
.first::<Self>(conn)
.await
}
}
2020-04-27 22:17:02 +00:00
#[cfg(test)]
mod tests {
use super::*;
2021-10-16 13:33:38 +00:00
use crate::{
newtypes::DbUrl,
source::{
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,
};
use serde_json::Value;
use serial_test::serial;
use url::Url;
2020-04-27 22:17:02 +00:00
2022-11-09 10:05:00 +00:00
#[tokio::test]
#[serial]
2022-11-09 10:05:00 +00:00
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
2020-04-27 22:17:02 +00:00
2022-11-09 10:05:00 +00:00
let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
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(
"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#"{
"@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 = ActivityInsertForm {
2022-11-09 10:05:00 +00:00
ap_id: ap_id_.clone(),
data: test_json.clone(),
2021-03-20 20:59:07 +00:00
local: Some(true),
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,
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,
};
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);
assert_eq!(expected_activity, read_activity_by_apub_id);
2020-04-27 22:17:02 +00:00
assert_eq!(expected_activity, inserted_activity);
}
}