1 use crate::{naive_now, schema::private_message, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use serde::{Deserialize, Serialize};
5 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
6 #[table_name = "private_message"]
7 pub struct PrivateMessage {
10 pub recipient_id: i32,
14 pub published: chrono::NaiveDateTime,
15 pub updated: Option<chrono::NaiveDateTime>,
20 #[derive(Insertable, AsChangeset, Clone)]
21 #[table_name = "private_message"]
22 pub struct PrivateMessageForm {
24 pub recipient_id: i32,
26 pub deleted: Option<bool>,
27 pub read: Option<bool>,
28 pub published: Option<chrono::NaiveDateTime>,
29 pub updated: Option<chrono::NaiveDateTime>,
34 impl Crud<PrivateMessageForm> for PrivateMessage {
35 fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
36 use crate::schema::private_message::dsl::*;
37 private_message.find(private_message_id).first::<Self>(conn)
40 fn delete(conn: &PgConnection, private_message_id: i32) -> Result<usize, Error> {
41 use crate::schema::private_message::dsl::*;
42 diesel::delete(private_message.find(private_message_id)).execute(conn)
45 fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
46 use crate::schema::private_message::dsl::*;
47 insert_into(private_message)
48 .values(private_message_form)
49 .get_result::<Self>(conn)
54 private_message_id: i32,
55 private_message_form: &PrivateMessageForm,
56 ) -> Result<Self, Error> {
57 use crate::schema::private_message::dsl::*;
58 diesel::update(private_message.find(private_message_id))
59 .set(private_message_form)
60 .get_result::<Self>(conn)
67 private_message_id: i32,
69 ) -> Result<Self, Error> {
70 use crate::schema::private_message::dsl::*;
72 diesel::update(private_message.find(private_message_id))
73 .set(ap_id.eq(apub_id))
74 .get_result::<Self>(conn)
77 pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
78 use crate::schema::private_message::dsl::*;
80 .filter(ap_id.eq(object_id))
84 pub fn update_content(
86 private_message_id: i32,
88 ) -> Result<Self, Error> {
89 use crate::schema::private_message::dsl::*;
90 diesel::update(private_message.find(private_message_id))
91 .set((content.eq(new_content), updated.eq(naive_now())))
92 .get_result::<Self>(conn)
95 pub fn update_deleted(
97 private_message_id: i32,
99 ) -> Result<Self, Error> {
100 use crate::schema::private_message::dsl::*;
101 diesel::update(private_message.find(private_message_id))
102 .set(deleted.eq(new_deleted))
103 .get_result::<Self>(conn)
108 private_message_id: i32,
110 ) -> Result<Self, Error> {
111 use crate::schema::private_message::dsl::*;
112 diesel::update(private_message.find(private_message_id))
113 .set(read.eq(new_read))
114 .get_result::<Self>(conn)
117 pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
118 use crate::schema::private_message::dsl::*;
121 .filter(recipient_id.eq(for_recipient_id))
122 .filter(read.eq(false)),
125 .get_results::<Self>(conn)
133 tests::establish_unpooled_connection,
141 let conn = establish_unpooled_connection();
143 let creator_form = UserForm {
144 name: "creator_pm".into(),
145 preferred_username: None,
146 password_encrypted: "nope".into(),
148 matrix_user_id: None,
155 theme: "darkly".into(),
156 default_sort_type: SortType::Hot as i16,
157 default_listing_type: ListingType::Subscribed as i16,
158 lang: "browser".into(),
160 send_notifications_to_email: false,
161 actor_id: "changeme_6723878".into(),
166 last_refreshed_at: None,
169 let inserted_creator = User_::create(&conn, &creator_form).unwrap();
171 let recipient_form = UserForm {
172 name: "recipient_pm".into(),
173 preferred_username: None,
174 password_encrypted: "nope".into(),
176 matrix_user_id: None,
183 theme: "darkly".into(),
184 default_sort_type: SortType::Hot as i16,
185 default_listing_type: ListingType::Subscribed as i16,
186 lang: "browser".into(),
188 send_notifications_to_email: false,
189 actor_id: "changeme_287263876".into(),
194 last_refreshed_at: None,
197 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
199 let private_message_form = PrivateMessageForm {
200 content: "A test private message".into(),
201 creator_id: inserted_creator.id,
202 recipient_id: inserted_recipient.id,
207 ap_id: "http://fake.com".into(),
211 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
213 let expected_private_message = PrivateMessage {
214 id: inserted_private_message.id,
215 content: "A test private message".into(),
216 creator_id: inserted_creator.id,
217 recipient_id: inserted_recipient.id,
221 published: inserted_private_message.published,
222 ap_id: "http://fake.com".into(),
226 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
227 let updated_private_message =
228 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
229 let deleted_private_message =
230 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
231 let marked_read_private_message =
232 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
233 let num_deleted = PrivateMessage::delete(&conn, inserted_private_message.id).unwrap();
234 User_::delete(&conn, inserted_creator.id).unwrap();
235 User_::delete(&conn, inserted_recipient.id).unwrap();
237 assert_eq!(expected_private_message, read_private_message);
238 assert_eq!(expected_private_message, updated_private_message);
239 assert_eq!(expected_private_message, inserted_private_message);
240 assert!(deleted_private_message.deleted);
241 assert!(marked_read_private_message.read);
242 assert_eq!(1, num_deleted);