1 use crate::{ApubObject, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl};
5 impl Crud<PrivateMessageForm> for PrivateMessage {
6 fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
7 use lemmy_db_schema::schema::private_message::dsl::*;
8 private_message.find(private_message_id).first::<Self>(conn)
11 fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
12 use lemmy_db_schema::schema::private_message::dsl::*;
13 insert_into(private_message)
14 .values(private_message_form)
15 .get_result::<Self>(conn)
20 private_message_id: i32,
21 private_message_form: &PrivateMessageForm,
22 ) -> Result<Self, Error> {
23 use lemmy_db_schema::schema::private_message::dsl::*;
24 diesel::update(private_message.find(private_message_id))
25 .set(private_message_form)
26 .get_result::<Self>(conn)
30 impl ApubObject<PrivateMessageForm> for PrivateMessage {
31 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
35 use lemmy_db_schema::schema::private_message::dsl::*;
37 .filter(ap_id.eq(object_id))
41 fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
42 use lemmy_db_schema::schema::private_message::dsl::*;
43 insert_into(private_message)
44 .values(private_message_form)
47 .set(private_message_form)
48 .get_result::<Self>(conn)
52 pub trait PrivateMessage_ {
55 private_message_id: i32,
57 ) -> Result<PrivateMessage, Error>;
60 private_message_id: i32,
62 ) -> Result<PrivateMessage, Error>;
65 private_message_id: i32,
67 ) -> Result<PrivateMessage, Error>;
70 private_message_id: i32,
72 ) -> Result<PrivateMessage, Error>;
75 for_recipient_id: i32,
76 ) -> Result<Vec<PrivateMessage>, Error>;
79 impl PrivateMessage_ for PrivateMessage {
82 private_message_id: i32,
84 ) -> Result<PrivateMessage, Error> {
85 use lemmy_db_schema::schema::private_message::dsl::*;
87 diesel::update(private_message.find(private_message_id))
88 .set(ap_id.eq(apub_id))
89 .get_result::<Self>(conn)
94 private_message_id: i32,
96 ) -> Result<PrivateMessage, Error> {
97 use lemmy_db_schema::schema::private_message::dsl::*;
98 diesel::update(private_message.find(private_message_id))
99 .set((content.eq(new_content), updated.eq(naive_now())))
100 .get_result::<Self>(conn)
105 private_message_id: i32,
107 ) -> Result<PrivateMessage, Error> {
108 use lemmy_db_schema::schema::private_message::dsl::*;
109 diesel::update(private_message.find(private_message_id))
110 .set(deleted.eq(new_deleted))
111 .get_result::<Self>(conn)
116 private_message_id: i32,
118 ) -> Result<PrivateMessage, Error> {
119 use lemmy_db_schema::schema::private_message::dsl::*;
120 diesel::update(private_message.find(private_message_id))
121 .set(read.eq(new_read))
122 .get_result::<Self>(conn)
127 for_recipient_id: i32,
128 ) -> Result<Vec<PrivateMessage>, Error> {
129 use lemmy_db_schema::schema::private_message::dsl::*;
132 .filter(recipient_id.eq(for_recipient_id))
133 .filter(read.eq(false)),
136 .get_results::<Self>(conn)
142 use crate::{establish_unpooled_connection, source::private_message::PrivateMessage_, Crud};
143 use lemmy_db_schema::source::{person::*, private_message::*};
144 use serial_test::serial;
149 let conn = establish_unpooled_connection();
151 let creator_form = PersonForm {
152 name: "creator_pm".into(),
153 preferred_username: None,
165 last_refreshed_at: None,
167 shared_inbox_url: None,
170 let inserted_creator = Person::create(&conn, &creator_form).unwrap();
172 let recipient_form = PersonForm {
173 name: "recipient_pm".into(),
174 preferred_username: None,
186 last_refreshed_at: None,
188 shared_inbox_url: None,
191 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
193 let private_message_form = PrivateMessageForm {
194 content: "A test private message".into(),
195 creator_id: inserted_creator.id,
196 recipient_id: inserted_recipient.id,
205 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
207 let expected_private_message = PrivateMessage {
208 id: inserted_private_message.id,
209 content: "A test private message".into(),
210 creator_id: inserted_creator.id,
211 recipient_id: inserted_recipient.id,
215 published: inserted_private_message.published,
216 ap_id: inserted_private_message.ap_id.to_owned(),
220 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
221 let updated_private_message =
222 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
223 let deleted_private_message =
224 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
225 let marked_read_private_message =
226 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
227 Person::delete(&conn, inserted_creator.id).unwrap();
228 Person::delete(&conn, inserted_recipient.id).unwrap();
230 assert_eq!(expected_private_message, read_private_message);
231 assert_eq!(expected_private_message, updated_private_message);
232 assert_eq!(expected_private_message, inserted_private_message);
233 assert!(deleted_private_message.deleted);
234 assert!(marked_read_private_message.read);