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)
143 establish_unpooled_connection,
144 source::private_message::PrivateMessage_,
149 use lemmy_db_schema::source::{private_message::*, user::*};
150 use serial_test::serial;
155 let conn = establish_unpooled_connection();
157 let creator_form = UserForm {
158 name: "creator_pm".into(),
159 preferred_username: None,
160 password_encrypted: "nope".into(),
162 matrix_user_id: None,
170 theme: "browser".into(),
171 default_sort_type: SortType::Hot as i16,
172 default_listing_type: ListingType::Subscribed as i16,
173 lang: "browser".into(),
175 send_notifications_to_email: false,
181 last_refreshed_at: None,
183 shared_inbox_url: None,
186 let inserted_creator = User_::create(&conn, &creator_form).unwrap();
188 let recipient_form = UserForm {
189 name: "recipient_pm".into(),
190 preferred_username: None,
191 password_encrypted: "nope".into(),
193 matrix_user_id: None,
201 theme: "browser".into(),
202 default_sort_type: SortType::Hot as i16,
203 default_listing_type: ListingType::Subscribed as i16,
204 lang: "browser".into(),
206 send_notifications_to_email: false,
212 last_refreshed_at: None,
214 shared_inbox_url: None,
217 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
219 let private_message_form = PrivateMessageForm {
220 content: "A test private message".into(),
221 creator_id: inserted_creator.id,
222 recipient_id: inserted_recipient.id,
231 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
233 let expected_private_message = PrivateMessage {
234 id: inserted_private_message.id,
235 content: "A test private message".into(),
236 creator_id: inserted_creator.id,
237 recipient_id: inserted_recipient.id,
241 published: inserted_private_message.published,
242 ap_id: inserted_private_message.ap_id.to_owned(),
246 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
247 let updated_private_message =
248 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
249 let deleted_private_message =
250 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
251 let marked_read_private_message =
252 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
253 User_::delete(&conn, inserted_creator.id).unwrap();
254 User_::delete(&conn, inserted_recipient.id).unwrap();
256 assert_eq!(expected_private_message, read_private_message);
257 assert_eq!(expected_private_message, updated_private_message);
258 assert_eq!(expected_private_message, inserted_private_message);
259 assert!(deleted_private_message.deleted);
260 assert!(marked_read_private_message.read);