1 use crate::{ApubObject, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{naive_now, source::private_message::*};
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: &str) -> 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::*};
153 let conn = establish_unpooled_connection();
155 let creator_form = UserForm {
156 name: "creator_pm".into(),
157 preferred_username: None,
158 password_encrypted: "nope".into(),
160 matrix_user_id: None,
168 theme: "browser".into(),
169 default_sort_type: SortType::Hot as i16,
170 default_listing_type: ListingType::Subscribed as i16,
171 lang: "browser".into(),
173 send_notifications_to_email: false,
179 last_refreshed_at: None,
182 let inserted_creator = User_::create(&conn, &creator_form).unwrap();
184 let recipient_form = UserForm {
185 name: "recipient_pm".into(),
186 preferred_username: None,
187 password_encrypted: "nope".into(),
189 matrix_user_id: None,
197 theme: "browser".into(),
198 default_sort_type: SortType::Hot as i16,
199 default_listing_type: ListingType::Subscribed as i16,
200 lang: "browser".into(),
202 send_notifications_to_email: false,
208 last_refreshed_at: None,
211 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
213 let private_message_form = PrivateMessageForm {
214 content: "A test private message".into(),
215 creator_id: inserted_creator.id,
216 recipient_id: inserted_recipient.id,
225 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
227 let expected_private_message = PrivateMessage {
228 id: inserted_private_message.id,
229 content: "A test private message".into(),
230 creator_id: inserted_creator.id,
231 recipient_id: inserted_recipient.id,
235 published: inserted_private_message.published,
236 ap_id: inserted_private_message.ap_id.to_owned(),
240 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
241 let updated_private_message =
242 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
243 let deleted_private_message =
244 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
245 let marked_read_private_message =
246 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
247 User_::delete(&conn, inserted_creator.id).unwrap();
248 User_::delete(&conn, inserted_recipient.id).unwrap();
250 assert_eq!(expected_private_message, read_private_message);
251 assert_eq!(expected_private_message, updated_private_message);
252 assert_eq!(expected_private_message, inserted_private_message);
253 assert!(deleted_private_message.deleted);
254 assert!(marked_read_private_message.read);