2021-07-30 18:44:15 +00:00
|
|
|
use crate::{ApubObject, Crud, DeleteableOrRemoveable};
|
2021-09-25 15:44:52 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2021-03-18 20:25:21 +00:00
|
|
|
use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for PrivateMessage {
|
|
|
|
type Form = PrivateMessageForm;
|
|
|
|
type IdType = PrivateMessageId;
|
2021-03-18 20:25:21 +00:00
|
|
|
fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-01-22 21:35:29 +00:00
|
|
|
private_message.find(private_message_id).first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-01-22 21:35:29 +00:00
|
|
|
insert_into(private_message)
|
|
|
|
.values(private_message_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-01-22 21:35:29 +00:00
|
|
|
private_message_form: &PrivateMessageForm,
|
|
|
|
) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-01-22 21:35:29 +00:00
|
|
|
diesel::update(private_message.find(private_message_id))
|
|
|
|
.set(private_message_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
impl ApubObject for PrivateMessage {
|
2021-09-25 15:44:52 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
|
2020-12-08 17:38:48 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-12-08 17:38:48 +00:00
|
|
|
private_message
|
|
|
|
.filter(ap_id.eq(object_id))
|
|
|
|
.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
pub trait PrivateMessage_ {
|
|
|
|
fn update_ap_id(
|
2020-07-10 18:15:41 +00:00
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2021-03-02 12:41:48 +00:00
|
|
|
apub_id: DbUrl,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PrivateMessage, Error>;
|
|
|
|
fn update_content(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-12-21 13:38:34 +00:00
|
|
|
new_content: &str,
|
|
|
|
) -> Result<PrivateMessage, Error>;
|
|
|
|
fn update_deleted(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-12-21 13:38:34 +00:00
|
|
|
new_deleted: bool,
|
|
|
|
) -> Result<PrivateMessage, Error>;
|
|
|
|
fn update_read(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-12-21 13:38:34 +00:00
|
|
|
new_read: bool,
|
|
|
|
) -> Result<PrivateMessage, Error>;
|
|
|
|
fn mark_all_as_read(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_recipient_id: PersonId,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<Vec<PrivateMessage>, Error>;
|
2021-09-25 15:44:52 +00:00
|
|
|
fn upsert(
|
|
|
|
conn: &PgConnection,
|
|
|
|
private_message_form: &PrivateMessageForm,
|
|
|
|
) -> Result<PrivateMessage, Error>;
|
2020-12-21 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrivateMessage_ for PrivateMessage {
|
|
|
|
fn update_ap_id(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2021-03-02 12:41:48 +00:00
|
|
|
apub_id: DbUrl,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PrivateMessage, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
diesel::update(private_message.find(private_message_id))
|
2020-07-10 18:15:41 +00:00
|
|
|
.set(ap_id.eq(apub_id))
|
2020-05-06 02:06:24 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
fn update_content(
|
2020-07-20 04:29:44 +00:00
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-07-20 04:29:44 +00:00
|
|
|
new_content: &str,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PrivateMessage, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-07-20 04:29:44 +00:00
|
|
|
diesel::update(private_message.find(private_message_id))
|
2020-07-21 01:37:44 +00:00
|
|
|
.set((content.eq(new_content), updated.eq(naive_now())))
|
2020-07-20 04:29:44 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
fn update_deleted(
|
2020-07-20 04:29:44 +00:00
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-07-20 04:29:44 +00:00
|
|
|
new_deleted: bool,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PrivateMessage, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-07-20 04:29:44 +00:00
|
|
|
diesel::update(private_message.find(private_message_id))
|
|
|
|
.set(deleted.eq(new_deleted))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
fn update_read(
|
2020-07-20 04:29:44 +00:00
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
private_message_id: PrivateMessageId,
|
2020-07-20 04:29:44 +00:00
|
|
|
new_read: bool,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PrivateMessage, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-07-20 04:29:44 +00:00
|
|
|
diesel::update(private_message.find(private_message_id))
|
|
|
|
.set(read.eq(new_read))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
fn mark_all_as_read(
|
|
|
|
conn: &PgConnection,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_recipient_id: PersonId,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<Vec<PrivateMessage>, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-07-20 04:29:44 +00:00
|
|
|
diesel::update(
|
|
|
|
private_message
|
|
|
|
.filter(recipient_id.eq(for_recipient_id))
|
|
|
|
.filter(read.eq(false)),
|
|
|
|
)
|
|
|
|
.set(read.eq(true))
|
|
|
|
.get_results::<Self>(conn)
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
|
|
|
fn upsert(
|
|
|
|
conn: &PgConnection,
|
|
|
|
private_message_form: &PrivateMessageForm,
|
|
|
|
) -> Result<PrivateMessage, Error> {
|
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
|
|
|
insert_into(private_message)
|
|
|
|
.values(private_message_form)
|
|
|
|
.on_conflict(ap_id)
|
|
|
|
.do_update()
|
|
|
|
.set(private_message_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
impl DeleteableOrRemoveable for PrivateMessage {
|
|
|
|
fn blank_out_deleted_or_removed_info(mut self) -> Self {
|
|
|
|
self.content = "".into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:35:29 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-11 04:43:11 +00:00
|
|
|
use crate::{establish_unpooled_connection, source::private_message::PrivateMessage_, Crud};
|
|
|
|
use lemmy_db_schema::source::{person::*, private_message::*};
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2020-05-16 14:04:08 +00:00
|
|
|
|
2020-01-22 21:35:29 +00:00
|
|
|
#[test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2020-01-22 21:35:29 +00:00
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_unpooled_connection();
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let creator_form = PersonForm {
|
2020-01-22 21:35:29 +00:00
|
|
|
name: "creator_pm".into(),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PersonForm::default()
|
2020-01-22 21:35:29 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let inserted_creator = Person::create(&conn, &creator_form).unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let recipient_form = PersonForm {
|
2020-01-22 21:35:29 +00:00
|
|
|
name: "recipient_pm".into(),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PersonForm::default()
|
2020-01-22 21:35:29 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
let private_message_form = PrivateMessageForm {
|
2020-05-06 02:06:24 +00:00
|
|
|
content: "A test private message".into(),
|
2020-01-22 21:35:29 +00:00
|
|
|
creator_id: inserted_creator.id,
|
|
|
|
recipient_id: inserted_recipient.id,
|
2021-03-20 20:59:07 +00:00
|
|
|
..PrivateMessageForm::default()
|
2020-01-22 21:35:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).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,
|
2020-08-31 13:48:02 +00:00
|
|
|
ap_id: inserted_private_message.ap_id.to_owned(),
|
2020-05-06 02:06:24 +00:00
|
|
|
local: true,
|
2020-01-22 21:35:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
|
|
|
|
let updated_private_message =
|
|
|
|
PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
|
2020-07-20 04:29:44 +00:00
|
|
|
let deleted_private_message =
|
|
|
|
PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
|
|
|
|
let marked_read_private_message =
|
|
|
|
PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
|
2021-02-26 13:49:58 +00:00
|
|
|
Person::delete(&conn, inserted_creator.id).unwrap();
|
|
|
|
Person::delete(&conn, inserted_recipient.id).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
|
|
|
}
|
|
|
|
}
|