2 use crate::schema::private_message;
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name = "private_message"]
6 pub struct PrivateMessage {
13 pub published: chrono::NaiveDateTime,
14 pub updated: Option<chrono::NaiveDateTime>,
17 #[derive(Insertable, AsChangeset, Clone)]
18 #[table_name = "private_message"]
19 pub struct PrivateMessageForm {
21 pub recipient_id: i32,
22 pub content: Option<String>,
23 pub deleted: Option<bool>,
24 pub read: Option<bool>,
25 pub updated: Option<chrono::NaiveDateTime>,
28 impl Crud<PrivateMessageForm> for PrivateMessage {
29 fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
30 use crate::schema::private_message::dsl::*;
31 private_message.find(private_message_id).first::<Self>(conn)
34 fn delete(conn: &PgConnection, private_message_id: i32) -> Result<usize, Error> {
35 use crate::schema::private_message::dsl::*;
36 diesel::delete(private_message.find(private_message_id)).execute(conn)
39 fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
40 use crate::schema::private_message::dsl::*;
41 insert_into(private_message)
42 .values(private_message_form)
43 .get_result::<Self>(conn)
48 private_message_id: i32,
49 private_message_form: &PrivateMessageForm,
50 ) -> Result<Self, Error> {
51 use crate::schema::private_message::dsl::*;
52 diesel::update(private_message.find(private_message_id))
53 .set(private_message_form)
54 .get_result::<Self>(conn)
60 use super::super::user::*;
64 let conn = establish_unpooled_connection();
66 let creator_form = UserForm {
67 name: "creator_pm".into(),
68 fedi_name: "rrf".into(),
69 preferred_username: None,
70 password_encrypted: "nope".into(),
78 theme: "darkly".into(),
79 default_sort_type: SortType::Hot as i16,
80 default_listing_type: ListingType::Subscribed as i16,
81 lang: "browser".into(),
83 send_notifications_to_email: false,
86 let inserted_creator = User_::create(&conn, &creator_form).unwrap();
88 let recipient_form = UserForm {
89 name: "recipient_pm".into(),
90 fedi_name: "rrf".into(),
91 preferred_username: None,
92 password_encrypted: "nope".into(),
100 theme: "darkly".into(),
101 default_sort_type: SortType::Hot as i16,
102 default_listing_type: ListingType::Subscribed as i16,
103 lang: "browser".into(),
105 send_notifications_to_email: false,
108 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
110 let private_message_form = PrivateMessageForm {
111 content: Some("A test private message".into()),
112 creator_id: inserted_creator.id,
113 recipient_id: inserted_recipient.id,
119 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
121 let expected_private_message = PrivateMessage {
122 id: inserted_private_message.id,
123 content: "A test private message".into(),
124 creator_id: inserted_creator.id,
125 recipient_id: inserted_recipient.id,
129 published: inserted_private_message.published,
132 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
133 let updated_private_message =
134 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
135 let num_deleted = PrivateMessage::delete(&conn, inserted_private_message.id).unwrap();
136 User_::delete(&conn, inserted_creator.id).unwrap();
137 User_::delete(&conn, inserted_recipient.id).unwrap();
139 assert_eq!(expected_private_message, read_private_message);
140 assert_eq!(expected_private_message, updated_private_message);
141 assert_eq!(expected_private_message, inserted_private_message);
142 assert_eq!(1, num_deleted);