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},
87 let conn = establish_unpooled_connection();
89 let new_user = UserForm {
90 name: "terrylake".into(),
91 preferred_username: None,
92 password_encrypted: "nope".into(),
102 theme: "browser".into(),
103 default_sort_type: SortType::Hot as i16,
104 default_listing_type: ListingType::Subscribed as i16,
105 lang: "browser".into(),
107 send_notifications_to_email: false,
113 last_refreshed_at: None,
116 let inserted_user = User_::create(&conn, &new_user).unwrap();
118 let recipient_form = UserForm {
119 name: "terrylakes recipient".into(),
120 preferred_username: None,
121 password_encrypted: "nope".into(),
123 matrix_user_id: None,
131 theme: "browser".into(),
132 default_sort_type: SortType::Hot as i16,
133 default_listing_type: ListingType::Subscribed as i16,
134 lang: "browser".into(),
136 send_notifications_to_email: false,
142 last_refreshed_at: None,
145 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
147 let new_community = CommunityForm {
148 name: "test community lake".to_string(),
149 title: "nada".to_owned(),
152 creator_id: inserted_user.id,
161 last_refreshed_at: None,
167 let inserted_community = Community::create(&conn, &new_community).unwrap();
169 let new_post = PostForm {
170 name: "A test post".into(),
171 creator_id: inserted_user.id,
174 community_id: inserted_community.id,
182 embed_description: None,
190 let inserted_post = Post::create(&conn, &new_post).unwrap();
192 let comment_form = CommentForm {
193 content: "A test comment".into(),
194 creator_id: inserted_user.id,
195 post_id: inserted_post.id,
206 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
208 let user_mention_form = UserMentionForm {
209 recipient_id: inserted_recipient.id,
210 comment_id: inserted_comment.id,
214 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
216 let expected_mention = UserMention {
217 id: inserted_mention.id,
218 recipient_id: inserted_mention.recipient_id,
219 comment_id: inserted_mention.comment_id,
221 published: inserted_mention.published,
224 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
225 let updated_mention =
226 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
227 Comment::delete(&conn, inserted_comment.id).unwrap();
228 Post::delete(&conn, inserted_post.id).unwrap();
229 Community::delete(&conn, inserted_community.id).unwrap();
230 User_::delete(&conn, inserted_user.id).unwrap();
231 User_::delete(&conn, inserted_recipient.id).unwrap();
233 assert_eq!(expected_mention, read_mention);
234 assert_eq!(expected_mention, inserted_mention);
235 assert_eq!(expected_mention, updated_mention);