1 use crate::{ApubObject, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
5 impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
6 fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> 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: PrivateMessageId,
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: PrivateMessageId,
57 ) -> Result<PrivateMessage, Error>;
60 private_message_id: PrivateMessageId,
62 ) -> Result<PrivateMessage, Error>;
65 private_message_id: PrivateMessageId,
67 ) -> Result<PrivateMessage, Error>;
70 private_message_id: PrivateMessageId,
72 ) -> Result<PrivateMessage, Error>;
75 for_recipient_id: PersonId,
76 ) -> Result<Vec<PrivateMessage>, Error>;
79 impl PrivateMessage_ for PrivateMessage {
82 private_message_id: PrivateMessageId,
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: PrivateMessageId,
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: PrivateMessageId,
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: PrivateMessageId,
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: PersonId,
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 ..PersonForm::default()
156 let inserted_creator = Person::create(&conn, &creator_form).unwrap();
158 let recipient_form = PersonForm {
159 name: "recipient_pm".into(),
160 ..PersonForm::default()
163 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
165 let private_message_form = PrivateMessageForm {
166 content: "A test private message".into(),
167 creator_id: inserted_creator.id,
168 recipient_id: inserted_recipient.id,
169 ..PrivateMessageForm::default()
172 let inserted_private_message = PrivateMessage::create(&conn, &private_message_form).unwrap();
174 let expected_private_message = PrivateMessage {
175 id: inserted_private_message.id,
176 content: "A test private message".into(),
177 creator_id: inserted_creator.id,
178 recipient_id: inserted_recipient.id,
182 published: inserted_private_message.published,
183 ap_id: inserted_private_message.ap_id.to_owned(),
187 let read_private_message = PrivateMessage::read(&conn, inserted_private_message.id).unwrap();
188 let updated_private_message =
189 PrivateMessage::update(&conn, inserted_private_message.id, &private_message_form).unwrap();
190 let deleted_private_message =
191 PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
192 let marked_read_private_message =
193 PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
194 Person::delete(&conn, inserted_creator.id).unwrap();
195 Person::delete(&conn, inserted_recipient.id).unwrap();
197 assert_eq!(expected_private_message, read_private_message);
198 assert_eq!(expected_private_message, updated_private_message);
199 assert_eq!(expected_private_message, inserted_private_message);
200 assert!(deleted_private_message.deleted);
201 assert!(marked_read_private_message.read);