mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-06-16 04:10:32 +00:00
* Moving settings to Database. - Moves many settings into the database. Fixes #2285 - Adds a local_site and instance table. Fixes #2365 . Fixes #2368 - Separates SQL update an insert forms, to avoid runtime errors. - Adds TypedBuilder to all the SQL forms, instead of default. * Fix weird clippy issue. * Removing extra lines. * Some fixes from suggestions. * Fixing apub tests. * Using instance creation helper function. * Move forms to their own line. * Trying to fix local_site_data, still broken. * Testing out async * Testing out async 2 * Fixing federation tests. * Trying to fix check features 1. * Starting on adding diesel async. 1/4th done. * Added async to views and schema. * Adding some more async * Compiling now. * Added diesel async. Fixes #2465 * Running clippy --fix * Trying to fix cargo test on drone. * Trying new muslrust. * Trying a custom dns * Trying a custom dns 2 * Trying a custom dns 3 * Trying a custom dns 4 * Trying a custom dns 5 * Trying a custom dns 6 * Trying a custom dns 7 * Addressing PR comments. * Adding check_apub to all verify functions. * Reverting back drone. * Fixing merge * Fix docker images. * Adding missing discussion_languages. * Trying to fix federation tests. * Fix site setup user creation. * Fix clippy * Fix clippy 2 * Test api faster * Try to fix 1 * Try to fix 2 * What are these lines about * Trying to fix 3 * Moving federation test back to top. * Remove logging cat.
171 lines
4.6 KiB
Rust
171 lines
4.6 KiB
Rust
use crate::{
|
|
newtypes::DbUrl,
|
|
schema::activity::dsl::*,
|
|
source::activity::*,
|
|
traits::Crud,
|
|
utils::{get_conn, DbPool},
|
|
};
|
|
use diesel::{
|
|
dsl::*,
|
|
result::{DatabaseErrorKind, Error},
|
|
ExpressionMethods,
|
|
QueryDsl,
|
|
};
|
|
use diesel_async::RunQueryDsl;
|
|
use serde_json::Value;
|
|
|
|
#[async_trait]
|
|
impl Crud for Activity {
|
|
type InsertForm = ActivityInsertForm;
|
|
type UpdateForm = ActivityUpdateForm;
|
|
type IdType = i32;
|
|
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
|
|
}
|
|
|
|
async fn create(pool: &DbPool, new_activity: &Self::InsertForm) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
insert_into(activity)
|
|
.values(new_activity)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
|
|
async fn update(
|
|
pool: &DbPool,
|
|
activity_id: i32,
|
|
new_activity: &Self::UpdateForm,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
diesel::update(activity.find(activity_id))
|
|
.set(new_activity)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
pub async fn insert(
|
|
pool: &DbPool,
|
|
ap_id_: DbUrl,
|
|
data_: Value,
|
|
local_: bool,
|
|
sensitive_: Option<bool>,
|
|
) -> Result<bool, Error> {
|
|
let activity_form = ActivityInsertForm {
|
|
ap_id: ap_id_,
|
|
data: data_,
|
|
local: Some(local_),
|
|
sensitive: sensitive_,
|
|
updated: None,
|
|
};
|
|
match Activity::create(pool, &activity_form).await {
|
|
Ok(_) => Ok(true),
|
|
Err(e) => {
|
|
if let Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) = e {
|
|
return Ok(false);
|
|
}
|
|
Err(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{
|
|
newtypes::DbUrl,
|
|
source::{
|
|
activity::{Activity, ActivityInsertForm},
|
|
instance::Instance,
|
|
person::{Person, PersonInsertForm},
|
|
},
|
|
utils::build_db_pool_for_tests,
|
|
};
|
|
use serde_json::Value;
|
|
use serial_test::serial;
|
|
use url::Url;
|
|
|
|
#[tokio::test]
|
|
#[serial]
|
|
async fn test_crud() {
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
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();
|
|
|
|
let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
|
|
|
|
let ap_id_: DbUrl = Url::parse(
|
|
"https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
|
|
)
|
|
.unwrap()
|
|
.into();
|
|
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"
|
|
}"#,
|
|
)
|
|
.unwrap();
|
|
let activity_form = ActivityInsertForm {
|
|
ap_id: ap_id_.clone(),
|
|
data: test_json.to_owned(),
|
|
local: Some(true),
|
|
sensitive: Some(false),
|
|
updated: None,
|
|
};
|
|
|
|
let inserted_activity = Activity::create(pool, &activity_form).await.unwrap();
|
|
|
|
let expected_activity = Activity {
|
|
ap_id: ap_id_.clone(),
|
|
id: inserted_activity.id,
|
|
data: test_json,
|
|
local: true,
|
|
sensitive: Some(false),
|
|
published: inserted_activity.published,
|
|
updated: None,
|
|
};
|
|
|
|
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();
|
|
|
|
assert_eq!(expected_activity, read_activity);
|
|
assert_eq!(expected_activity, read_activity_by_apub_id);
|
|
assert_eq!(expected_activity, inserted_activity);
|
|
}
|
|
}
|