-use crate::{ApubObject, Crud};
+use crate::{ApubObject, Crud, DeleteableOrRemoveable};
+use chrono::NaiveDateTime;
use diesel::{dsl::*, result::Error, *};
-use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl};
+use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
-impl Crud<PrivateMessageForm> for PrivateMessage {
- fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
+impl Crud for PrivateMessage {
+ type Form = PrivateMessageForm;
+ type IdType = PrivateMessageId;
+ fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result<Self, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
private_message.find(private_message_id).first::<Self>(conn)
}
fn update(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
}
}
-impl ApubObject<PrivateMessageForm> for PrivateMessage {
+impl ApubObject for PrivateMessage {
+ fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
+ None
+ }
+
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
where
Self: Sized,
.filter(ap_id.eq(object_id))
.first::<Self>(conn)
}
-
- fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, 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)
- }
}
pub trait PrivateMessage_ {
fn update_ap_id(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
apub_id: DbUrl,
) -> Result<PrivateMessage, Error>;
fn update_content(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_content: &str,
) -> Result<PrivateMessage, Error>;
fn update_deleted(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_deleted: bool,
) -> Result<PrivateMessage, Error>;
fn update_read(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_read: bool,
) -> Result<PrivateMessage, Error>;
fn mark_all_as_read(
conn: &PgConnection,
- for_recipient_id: i32,
+ for_recipient_id: PersonId,
) -> Result<Vec<PrivateMessage>, Error>;
+ fn upsert(
+ conn: &PgConnection,
+ private_message_form: &PrivateMessageForm,
+ ) -> Result<PrivateMessage, Error>;
}
impl PrivateMessage_ for PrivateMessage {
fn update_ap_id(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
apub_id: DbUrl,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
fn update_content(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_content: &str,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
fn update_deleted(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_deleted: bool,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
fn update_read(
conn: &PgConnection,
- private_message_id: i32,
+ private_message_id: PrivateMessageId,
new_read: bool,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
fn mark_all_as_read(
conn: &PgConnection,
- for_recipient_id: i32,
+ for_recipient_id: PersonId,
) -> Result<Vec<PrivateMessage>, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(
.set(read.eq(true))
.get_results::<Self>(conn)
}
+
+ 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)
+ }
+}
+
+impl DeleteableOrRemoveable for PrivateMessage {
+ fn blank_out_deleted_or_removed_info(mut self) -> Self {
+ self.content = "".into();
+ self
+ }
}
#[cfg(test)]
let creator_form = PersonForm {
name: "creator_pm".into(),
- preferred_username: None,
- avatar: None,
- banner: None,
- banned: None,
- deleted: None,
- published: None,
- updated: None,
- actor_id: None,
- bio: None,
- local: None,
- private_key: None,
- public_key: None,
- last_refreshed_at: None,
- inbox_url: None,
- shared_inbox_url: None,
+ ..PersonForm::default()
};
let inserted_creator = Person::create(&conn, &creator_form).unwrap();
let recipient_form = PersonForm {
name: "recipient_pm".into(),
- preferred_username: None,
- avatar: None,
- banner: None,
- banned: None,
- deleted: None,
- published: None,
- updated: None,
- actor_id: None,
- bio: None,
- local: None,
- private_key: None,
- public_key: None,
- last_refreshed_at: None,
- inbox_url: None,
- shared_inbox_url: None,
+ ..PersonForm::default()
};
let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
content: "A test private message".into(),
creator_id: inserted_creator.id,
recipient_id: inserted_recipient.id,
- deleted: None,
- read: None,
- published: None,
- updated: None,
- ap_id: None,
- local: true,
+ ..PrivateMessageForm::default()
};
let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();