2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::user_mention::*;
5 impl Crud<UserMentionForm> for UserMention {
6 fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
7 use lemmy_db_schema::schema::user_mention::dsl::*;
8 user_mention.find(user_mention_id).first::<Self>(conn)
11 fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
12 use lemmy_db_schema::schema::user_mention::dsl::*;
13 // since the return here isnt utilized, we dont need to do an update
14 // but get_result doesnt return the existing row here
15 insert_into(user_mention)
16 .values(user_mention_form)
17 .on_conflict((recipient_id, comment_id))
19 .set(user_mention_form)
20 .get_result::<Self>(conn)
26 user_mention_form: &UserMentionForm,
27 ) -> Result<Self, Error> {
28 use lemmy_db_schema::schema::user_mention::dsl::*;
29 diesel::update(user_mention.find(user_mention_id))
30 .set(user_mention_form)
31 .get_result::<Self>(conn)
35 pub trait UserMention_ {
40 ) -> Result<UserMention, Error>;
43 for_recipient_id: i32,
44 ) -> Result<Vec<UserMention>, Error>;
47 impl UserMention_ for UserMention {
52 ) -> Result<UserMention, Error> {
53 use lemmy_db_schema::schema::user_mention::dsl::*;
54 diesel::update(user_mention.find(user_mention_id))
55 .set(read.eq(new_read))
56 .get_result::<Self>(conn)
61 for_recipient_id: i32,
62 ) -> Result<Vec<UserMention>, Error> {
63 use lemmy_db_schema::schema::user_mention::dsl::*;
66 .filter(recipient_id.eq(for_recipient_id))
67 .filter(read.eq(false)),
70 .get_results::<Self>(conn)
76 use crate::{establish_unpooled_connection, Crud, ListingType, SortType};
77 use lemmy_db_schema::source::{
79 community::{Community, CommunityForm},
84 use serial_test::serial;
89 let conn = establish_unpooled_connection();
91 let new_user = UserForm {
92 name: "terrylake".into(),
93 preferred_username: None,
94 password_encrypted: "nope".into(),
104 theme: "browser".into(),
105 default_sort_type: SortType::Hot as i16,
106 default_listing_type: ListingType::Subscribed as i16,
107 lang: "browser".into(),
109 send_notifications_to_email: false,
115 last_refreshed_at: None,
117 shared_inbox_url: None,
120 let inserted_user = User_::create(&conn, &new_user).unwrap();
122 let recipient_form = UserForm {
123 name: "terrylakes recipient".into(),
124 preferred_username: None,
125 password_encrypted: "nope".into(),
127 matrix_user_id: None,
135 theme: "browser".into(),
136 default_sort_type: SortType::Hot as i16,
137 default_listing_type: ListingType::Subscribed as i16,
138 lang: "browser".into(),
140 send_notifications_to_email: false,
146 last_refreshed_at: None,
148 shared_inbox_url: None,
151 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
153 let new_community = CommunityForm {
154 name: "test community lake".to_string(),
155 title: "nada".to_owned(),
157 creator_id: inserted_user.id,
166 last_refreshed_at: None,
172 shared_inbox_url: None,
175 let inserted_community = Community::create(&conn, &new_community).unwrap();
177 let new_post = PostForm {
178 name: "A test post".into(),
179 creator_id: inserted_user.id,
182 community_id: inserted_community.id,
190 embed_description: None,
198 let inserted_post = Post::create(&conn, &new_post).unwrap();
200 let comment_form = CommentForm {
201 content: "A test comment".into(),
202 creator_id: inserted_user.id,
203 post_id: inserted_post.id,
214 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
216 let user_mention_form = UserMentionForm {
217 recipient_id: inserted_recipient.id,
218 comment_id: inserted_comment.id,
222 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
224 let expected_mention = UserMention {
225 id: inserted_mention.id,
226 recipient_id: inserted_mention.recipient_id,
227 comment_id: inserted_mention.comment_id,
229 published: inserted_mention.published,
232 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
233 let updated_mention =
234 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
235 Comment::delete(&conn, inserted_comment.id).unwrap();
236 Post::delete(&conn, inserted_post.id).unwrap();
237 Community::delete(&conn, inserted_community.id).unwrap();
238 User_::delete(&conn, inserted_user.id).unwrap();
239 User_::delete(&conn, inserted_recipient.id).unwrap();
241 assert_eq!(expected_mention, read_mention);
242 assert_eq!(expected_mention, inserted_mention);
243 assert_eq!(expected_mention, updated_mention);