79e6dbf0de
* Update session_middleware.rs * Update private_message_report_view.rs * Update session_middleware.rs * Update private_message_view.rs * Update private_message.rs * Update registration_application_view.rs * Update actor_language.rs * Update vote_view.rs * Update code_migrations.rs * Update comment_aggregates.rs * Update person_view.rs * Update user_settings_backup.rs * Update person.rs * Update create.rs * Update comment_view.rs * Update moderator.rs * Update site_aggregates.rs * Update claims.rs * Update community_aggregates.rs * Update post_report.rs * Update person_mention_view.rs * Update community_view.rs * Update comment_report_view.rs * Update post_report_view.rs * Update community_moderators.rs * Update comment.rs * Update person_aggregates.rs * Update comment_reply_view.rs * Update password_reset_request.rs * Update post_aggregates.rs * Update community.rs * Update main.rs * Update post.rs * Update person.rs * Update person.rs * Update claims.rs * Update person.rs * Update create.rs * Update user_settings_backup.rs * Update community_moderators.rs * Update main.rs * Update comment_aggregates.rs * Update community_aggregates.rs * Update person.rs * Update Cargo.toml * Update Cargo.toml * Update person.rs * fix * Update code_migrations.rs * fix submodule * Update person.rs
192 lines
5.5 KiB
Rust
192 lines
5.5 KiB
Rust
use crate::{
|
|
diesel::{DecoratableTarget, OptionalExtension},
|
|
newtypes::{DbUrl, PersonId, PrivateMessageId},
|
|
schema::private_message,
|
|
source::private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
|
traits::Crud,
|
|
utils::{functions::coalesce, get_conn, DbPool},
|
|
};
|
|
use chrono::{DateTime, Utc};
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
|
use diesel_async::RunQueryDsl;
|
|
use url::Url;
|
|
|
|
#[async_trait]
|
|
impl Crud for PrivateMessage {
|
|
type InsertForm = PrivateMessageInsertForm;
|
|
type UpdateForm = PrivateMessageUpdateForm;
|
|
type IdType = PrivateMessageId;
|
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
insert_into(private_message::table)
|
|
.values(form)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
|
|
async fn update(
|
|
pool: &mut DbPool<'_>,
|
|
private_message_id: PrivateMessageId,
|
|
form: &Self::UpdateForm,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
diesel::update(private_message::table.find(private_message_id))
|
|
.set(form)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
}
|
|
|
|
impl PrivateMessage {
|
|
pub async fn insert_apub(
|
|
pool: &mut DbPool<'_>,
|
|
timestamp: DateTime<Utc>,
|
|
form: &PrivateMessageInsertForm,
|
|
) -> Result<Self, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
insert_into(private_message::table)
|
|
.values(form)
|
|
.on_conflict(private_message::ap_id)
|
|
.filter_target(coalesce(private_message::updated, private_message::published).lt(timestamp))
|
|
.do_update()
|
|
.set(form)
|
|
.get_result::<Self>(conn)
|
|
.await
|
|
}
|
|
|
|
pub async fn mark_all_as_read(
|
|
pool: &mut DbPool<'_>,
|
|
for_recipient_id: PersonId,
|
|
) -> Result<Vec<PrivateMessage>, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
diesel::update(
|
|
private_message::table
|
|
.filter(private_message::recipient_id.eq(for_recipient_id))
|
|
.filter(private_message::read.eq(false)),
|
|
)
|
|
.set(private_message::read.eq(true))
|
|
.get_results::<Self>(conn)
|
|
.await
|
|
}
|
|
|
|
pub async fn read_from_apub_id(
|
|
pool: &mut DbPool<'_>,
|
|
object_id: Url,
|
|
) -> Result<Option<Self>, Error> {
|
|
let conn = &mut get_conn(pool).await?;
|
|
let object_id: DbUrl = object_id.into();
|
|
private_message::table
|
|
.filter(private_message::ap_id.eq(object_id))
|
|
.first(conn)
|
|
.await
|
|
.optional()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::unwrap_used)]
|
|
#[allow(clippy::indexing_slicing)]
|
|
mod tests {
|
|
|
|
use crate::{
|
|
source::{
|
|
instance::Instance,
|
|
person::{Person, PersonInsertForm},
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
|
},
|
|
traits::Crud,
|
|
utils::build_db_pool_for_tests,
|
|
};
|
|
use pretty_assertions::assert_eq;
|
|
use serial_test::serial;
|
|
|
|
#[tokio::test]
|
|
#[serial]
|
|
async fn test_crud() {
|
|
let pool = &build_db_pool_for_tests().await;
|
|
let pool = &mut pool.into();
|
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
|
.await
|
|
.unwrap();
|
|
|
|
let creator_form = PersonInsertForm::test_form(inserted_instance.id, "creator_pm");
|
|
|
|
let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
|
|
|
|
let recipient_form = PersonInsertForm::test_form(inserted_instance.id, "recipient_pm");
|
|
|
|
let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
|
|
|
|
let private_message_form = PrivateMessageInsertForm::builder()
|
|
.content("A test private message".into())
|
|
.creator_id(inserted_creator.id)
|
|
.recipient_id(inserted_recipient.id)
|
|
.build();
|
|
|
|
let inserted_private_message = PrivateMessage::create(pool, &private_message_form)
|
|
.await
|
|
.unwrap();
|
|
|
|
let expected_private_message = PrivateMessage {
|
|
id: inserted_private_message.id,
|
|
content: "A test private message".into(),
|
|
creator_id: inserted_creator.id,
|
|
recipient_id: inserted_recipient.id,
|
|
deleted: false,
|
|
read: false,
|
|
updated: None,
|
|
published: inserted_private_message.published,
|
|
ap_id: inserted_private_message.ap_id.clone(),
|
|
local: true,
|
|
};
|
|
|
|
let read_private_message = PrivateMessage::read(pool, inserted_private_message.id)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
|
|
let private_message_update_form = PrivateMessageUpdateForm {
|
|
content: Some("A test private message".into()),
|
|
..Default::default()
|
|
};
|
|
let updated_private_message = PrivateMessage::update(
|
|
pool,
|
|
inserted_private_message.id,
|
|
&private_message_update_form,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let deleted_private_message = PrivateMessage::update(
|
|
pool,
|
|
inserted_private_message.id,
|
|
&PrivateMessageUpdateForm {
|
|
deleted: Some(true),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let marked_read_private_message = PrivateMessage::update(
|
|
pool,
|
|
inserted_private_message.id,
|
|
&PrivateMessageUpdateForm {
|
|
read: Some(true),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
Person::delete(pool, inserted_creator.id).await.unwrap();
|
|
Person::delete(pool, inserted_recipient.id).await.unwrap();
|
|
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
|
|
|
assert_eq!(expected_private_message, read_private_message);
|
|
assert_eq!(expected_private_message, updated_private_message);
|
|
assert_eq!(expected_private_message, inserted_private_message);
|
|
assert!(deleted_private_message.deleted);
|
|
assert!(marked_read_private_message.read);
|
|
}
|
|
}
|