2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2024-04-16 12:48:15 +00:00
|
|
|
diesel::{DecoratableTarget, OptionalExtension},
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::{DbUrl, PersonId, PrivateMessageId},
|
2024-04-10 14:03:51 +00:00
|
|
|
schema::private_message,
|
2022-11-19 04:33:54 +00:00
|
|
|
source::private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
2023-03-01 03:46:15 +00:00
|
|
|
traits::Crud,
|
2024-04-10 14:03:51 +00:00
|
|
|
utils::{functions::coalesce, get_conn, DbPool},
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2024-04-10 14:03:51 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-10-16 13:33:38 +00:00
|
|
|
use url::Url;
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for PrivateMessage {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = PrivateMessageInsertForm;
|
|
|
|
type UpdateForm = PrivateMessageUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = PrivateMessageId;
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-04-10 14:03:51 +00:00
|
|
|
insert_into(private_message::table)
|
2022-10-27 09:24:07 +00:00
|
|
|
.values(form)
|
2020-01-22 21:35:29 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2022-10-27 09:24:07 +00:00
|
|
|
form: &Self::UpdateForm,
|
2020-01-22 21:35:29 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-04-10 14:03:51 +00:00
|
|
|
diesel::update(private_message::table.find(private_message_id))
|
2022-10-27 09:24:07 +00:00
|
|
|
.set(form)
|
2020-01-22 21:35:29 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl PrivateMessage {
|
2024-04-10 14:03:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn mark_all_as_read(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_recipient_id: PersonId,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<Vec<PrivateMessage>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-07-20 04:29:44 +00:00
|
|
|
diesel::update(
|
2024-04-10 14:03:51 +00:00
|
|
|
private_message::table
|
|
|
|
.filter(private_message::recipient_id.eq(for_recipient_id))
|
|
|
|
.filter(private_message::read.eq(false)),
|
2020-07-20 04:29:44 +00:00
|
|
|
)
|
2024-04-10 14:03:51 +00:00
|
|
|
.set(private_message::read.eq(true))
|
2020-07-20 04:29:44 +00:00
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-07-20 04:29:44 +00:00
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read_from_apub_id(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2024-04-16 12:48:15 +00:00
|
|
|
) -> Result<Option<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-10-16 13:33:38 +00:00
|
|
|
let object_id: DbUrl = object_id.into();
|
2024-04-16 12:48:15 +00:00
|
|
|
private_message::table
|
|
|
|
.filter(private_message::ap_id.eq(object_id))
|
|
|
|
.first(conn)
|
|
|
|
.await
|
|
|
|
.optional()
|
2021-10-16 13:33:38 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
}
|
2021-10-16 13:33:38 +00:00
|
|
|
|
2020-01-22 21:35:29 +00:00
|
|
|
#[cfg(test)]
|
2024-03-26 09:17:42 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
|
|
|
#[allow(clippy::indexing_slicing)]
|
2020-01-22 21:35:29 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
2022-11-19 04:33:54 +00:00
|
|
|
source::{
|
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2021-10-16 13:33:38 +00:00
|
|
|
};
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2020-05-16 14:04:08 +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-01-22 21:35:29 +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("creator_pm".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let recipient_form = PersonInsertForm::builder()
|
|
|
|
.name("recipient_pm".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let private_message_form = PrivateMessageInsertForm::builder()
|
|
|
|
.content("A test private message".into())
|
|
|
|
.creator_id(inserted_creator.id)
|
|
|
|
.recipient_id(inserted_recipient.id)
|
|
|
|
.build();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_private_message = PrivateMessage::create(pool, &private_message_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
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,
|
2022-11-19 04:33:54 +00:00
|
|
|
ap_id: inserted_private_message.ap_id.clone(),
|
2020-05-06 02:06:24 +00:00
|
|
|
local: true,
|
2020-01-22 21:35:29 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let read_private_message = PrivateMessage::read(pool, inserted_private_message.id)
|
|
|
|
.await
|
2024-04-16 12:48:15 +00:00
|
|
|
.unwrap()
|
2022-11-09 10:05:00 +00:00
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let private_message_update_form = PrivateMessageUpdateForm {
|
|
|
|
content: Some("A test private message".into()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
let updated_private_message = PrivateMessage::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
inserted_private_message.id,
|
|
|
|
&private_message_update_form,
|
|
|
|
)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let deleted_private_message = PrivateMessage::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
inserted_private_message.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PrivateMessageUpdateForm {
|
|
|
|
deleted: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-10-27 09:24:07 +00:00
|
|
|
)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
.unwrap();
|
|
|
|
let marked_read_private_message = PrivateMessage::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
inserted_private_message.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PrivateMessageUpdateForm {
|
|
|
|
read: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-10-27 09:24:07 +00:00
|
|
|
)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
.unwrap();
|
2022-11-09 10:05:00 +00:00
|
|
|
Person::delete(pool, inserted_creator.id).await.unwrap();
|
|
|
|
Person::delete(pool, inserted_recipient.id).await.unwrap();
|
|
|
|
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
assert_eq!(expected_private_message, read_private_message);
|
|
|
|
assert_eq!(expected_private_message, updated_private_message);
|
|
|
|
assert_eq!(expected_private_message, inserted_private_message);
|
2020-07-20 04:29:44 +00:00
|
|
|
assert!(deleted_private_message.deleted);
|
|
|
|
assert!(marked_read_private_message.read);
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
}
|