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

197 lines
5.6 KiB
Rust
Raw Normal View History

2021-10-16 13:33:38 +00:00
use crate::{
newtypes::{DbUrl, PersonId, PrivateMessageId},
schema::private_message::dsl::{ap_id, private_message, read, recipient_id},
source::private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
2021-10-16 13:33:38 +00:00
traits::{Crud, DeleteableOrRemoveable},
2022-11-09 10:05:00 +00:00
utils::{get_conn, DbPool},
2021-10-16 13:33:38 +00:00
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
use lemmy_utils::error::LemmyError;
2021-10-16 13:33:38 +00:00
use url::Url;
2022-11-09 10:05:00 +00:00
#[async_trait]
impl Crud for PrivateMessage {
type InsertForm = PrivateMessageInsertForm;
type UpdateForm = PrivateMessageUpdateForm;
type IdType = PrivateMessageId;
2022-11-09 10:05:00 +00:00
async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
private_message
.find(private_message_id)
.first::<Self>(conn)
.await
}
2022-11-09 10:05:00 +00:00
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(private_message)
.values(form)
.on_conflict(ap_id)
.do_update()
.set(form)
.get_result::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
async fn update(
pool: &DbPool,
private_message_id: PrivateMessageId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
diesel::update(private_message.find(private_message_id))
.set(form)
.get_result::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
async fn delete(pool: &DbPool, pm_id: Self::IdType) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(private_message.find(pm_id))
.execute(conn)
.await
}
}
2021-10-16 13:33:38 +00:00
impl PrivateMessage {
2022-11-09 10:05:00 +00:00
pub async fn mark_all_as_read(
pool: &DbPool,
for_recipient_id: PersonId,
) -> Result<Vec<PrivateMessage>, Error> {
2022-11-09 10:05:00 +00:00
let conn = &mut get_conn(pool).await?;
diesel::update(
private_message
.filter(recipient_id.eq(for_recipient_id))
.filter(read.eq(false)),
)
.set(read.eq(true))
.get_results::<Self>(conn)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
pub async fn read_from_apub_id(
pool: &DbPool,
object_id: Url,
) -> Result<Option<Self>, LemmyError> {
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();
Ok(
private_message
.filter(ap_id.eq(object_id))
.first::<PrivateMessage>(conn)
2022-11-09 10:05:00 +00:00
.await
.ok()
.map(Into::into),
2021-10-16 13:33:38 +00:00
)
}
}
2021-10-16 13:33:38 +00:00
impl DeleteableOrRemoveable for PrivateMessage {
fn blank_out_deleted_or_removed_info(mut self) -> Self {
self.content = String::new();
self
2021-10-16 13:33:38 +00:00
}
}
#[cfg(test)]
mod tests {
2021-10-16 13:33:38 +00:00
use crate::{
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
};
use serial_test::serial;
2020-05-16 14:04:08 +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;
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("creator_pm".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
2022-11-09 10:05:00 +00:00
let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("recipient_pm".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
2022-11-09 10:05:00 +00:00
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();
2022-11-09 10:05:00 +00:00
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(),
2020-05-06 02:06:24 +00:00
local: true,
};
2022-11-09 10:05:00 +00:00
let read_private_message = PrivateMessage::read(pool, inserted_private_message.id)
.await
.unwrap();
let private_message_update_form = PrivateMessageUpdateForm::builder()
.content(Some("A test private message".into()))
.build();
let updated_private_message = PrivateMessage::update(
2022-11-09 10:05:00 +00:00
pool,
inserted_private_message.id,
&private_message_update_form,
)
2022-11-09 10:05:00 +00:00
.await
.unwrap();
let deleted_private_message = PrivateMessage::update(
2022-11-09 10:05:00 +00:00
pool,
inserted_private_message.id,
&PrivateMessageUpdateForm::builder()
.deleted(Some(true))
.build(),
)
2022-11-09 10:05:00 +00:00
.await
.unwrap();
let marked_read_private_message = PrivateMessage::update(
2022-11-09 10:05:00 +00:00
pool,
inserted_private_message.id,
&PrivateMessageUpdateForm::builder().read(Some(true)).build(),
)
2022-11-09 10:05:00 +00:00
.await
.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();
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);
}
}