1 use crate::{ApubObject, Crud, DeleteableOrRemoveable};
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
5 impl Crud for PrivateMessage {
6 type Form = PrivateMessageForm;
7 type IdType = PrivateMessageId;
8 fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result<Self, Error> {
9 use lemmy_db_schema::schema::private_message::dsl::*;
10 private_message.find(private_message_id).first::<Self>(conn)
13 fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
14 use lemmy_db_schema::schema::private_message::dsl::*;
15 insert_into(private_message)
16 .values(private_message_form)
17 .get_result::<Self>(conn)
22 private_message_id: PrivateMessageId,
23 private_message_form: &PrivateMessageForm,
24 ) -> Result<Self, Error> {
25 use lemmy_db_schema::schema::private_message::dsl::*;
26 diesel::update(private_message.find(private_message_id))
27 .set(private_message_form)
28 .get_result::<Self>(conn)
32 impl ApubObject for PrivateMessage {
33 type Form = PrivateMessageForm;
34 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
38 use lemmy_db_schema::schema::private_message::dsl::*;
40 .filter(ap_id.eq(object_id))
44 fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
45 use lemmy_db_schema::schema::private_message::dsl::*;
46 insert_into(private_message)
47 .values(private_message_form)
50 .set(private_message_form)
51 .get_result::<Self>(conn)
55 pub trait PrivateMessage_ {
58 private_message_id: PrivateMessageId,
60 ) -> Result<PrivateMessage, Error>;
63 private_message_id: PrivateMessageId,
65 ) -> Result<PrivateMessage, Error>;
68 private_message_id: PrivateMessageId,
70 ) -> Result<PrivateMessage, Error>;
73 private_message_id: PrivateMessageId,
75 ) -> Result<PrivateMessage, Error>;
78 for_recipient_id: PersonId,
79 ) -> Result<Vec<PrivateMessage>, Error>;
82 impl PrivateMessage_ for PrivateMessage {
85 private_message_id: PrivateMessageId,
87 ) -> Result<PrivateMessage, Error> {
88 use lemmy_db_schema::schema::private_message::dsl::*;
90 diesel::update(private_message.find(private_message_id))
91 .set(ap_id.eq(apub_id))
92 .get_result::<Self>(conn)
97 private_message_id: PrivateMessageId,
99 ) -> Result<PrivateMessage, Error> {
100 use lemmy_db_schema::schema::private_message::dsl::*;
101 diesel::update(private_message.find(private_message_id))
102 .set((content.eq(new_content), updated.eq(naive_now())))
103 .get_result::<Self>(conn)
108 private_message_id: PrivateMessageId,
110 ) -> Result<PrivateMessage, Error> {
111 use lemmy_db_schema::schema::private_message::dsl::*;
112 diesel::update(private_message.find(private_message_id))
113 .set(deleted.eq(new_deleted))
114 .get_result::<Self>(conn)
119 private_message_id: PrivateMessageId,
121 ) -> Result<PrivateMessage, Error> {
122 use lemmy_db_schema::schema::private_message::dsl::*;
123 diesel::update(private_message.find(private_message_id))
124 .set(read.eq(new_read))
125 .get_result::<Self>(conn)
130 for_recipient_id: PersonId,
131 ) -> Result<Vec<PrivateMessage>, Error> {
132 use lemmy_db_schema::schema::private_message::dsl::*;
135 .filter(recipient_id.eq(for_recipient_id))
136 .filter(read.eq(false)),
139 .get_results::<Self>(conn)
143 impl DeleteableOrRemoveable for PrivateMessage {
144 fn blank_out_deleted_or_removed_info(mut self) -> Self {
145 self.content = "".into();
152 use crate::{establish_unpooled_connection, source::private_message::PrivateMessage_, Crud};
153 use lemmy_db_schema::source::{person::*, private_message::*};
154 use serial_test::serial;
159 let conn = establish_unpooled_connection();
161 let creator_form = PersonForm {
162 name: "creator_pm".into(),
163 ..PersonForm::default()
166 let inserted_creator = Person::create(&conn, &creator_form).unwrap();
168 let recipient_form = PersonForm {
169 name: "recipient_pm".into(),
170 ..PersonForm::default()
173 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
175 let private_message_form = PrivateMessageForm {
176 content: "A test private message".into(),
177 creator_id: inserted_creator.id,
178 recipient_id: inserted_recipient.id,
179 ..PrivateMessageForm::default()
182 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
184 let expected_private_message = PrivateMessage {
185 id: inserted_private_message.id,
186 content: "A test private message".into(),
187 creator_id: inserted_creator.id,
188 recipient_id: inserted_recipient.id,
192 published: inserted_private_message.published,
193 ap_id: inserted_private_message.ap_id.to_owned(),
197 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
198 let updated_private_message =
199 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
200 let deleted_private_message =
201 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
202 let marked_read_private_message =
203 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
204 Person::delete(&conn, inserted_creator.id).unwrap();
205 Person::delete(&conn, inserted_recipient.id).unwrap();
207 assert_eq!(expected_private_message, read_private_message);
208 assert_eq!(expected_private_message, updated_private_message);
209 assert_eq!(expected_private_message, inserted_private_message);
210 assert!(deleted_private_message.deleted);
211 assert!(marked_read_private_message.read);