]> Untitled Git - lemmy.git/commitdiff
EditUserMention changed to MarkUserMentionAsRead.
authorDessalines <tyhou13@gmx.com>
Mon, 20 Jul 2020 14:56:40 +0000 (10:56 -0400)
committerDessalines <tyhou13@gmx.com>
Mon, 20 Jul 2020 14:56:40 +0000 (10:56 -0400)
docs/src/contributing_websocket_http_api.md
server/lemmy_db/src/user_mention.rs
server/src/api/user.rs
server/src/routes/api.rs
server/src/websocket/mod.rs
server/src/websocket/server.rs
ui/src/components/comment-node.tsx
ui/src/components/inbox.tsx
ui/src/interfaces.ts
ui/src/services/WebSocketService.ts

index 5628c3287766b9d964fed78f96a7c34646abc0f6..63d8f994bd5c34c2eea010b529d3d68504feaa55 100644 (file)
@@ -464,14 +464,14 @@ Only the first user will be able to be the admin.
 
 `GET /user/mentions`
 
-#### Edit User Mention
+#### Mark User Mention as read
 ##### Request
 ```rust
 {
-  op: "EditUserMention",
+  op: "MarkUserMentionAsRead",
   data: {
     user_mention_id: i32,
-    read: Option<bool>,
+    read: bool,
     auth: String,
   }
 }
@@ -479,7 +479,7 @@ Only the first user will be able to be the admin.
 ##### Response
 ```rust
 {
-  op: "EditUserMention",
+  op: "MarkUserMentionAsRead",
   data: {
     mention: UserMentionView,
   }
@@ -487,7 +487,7 @@ Only the first user will be able to be the admin.
 ```
 ##### HTTP
 
-`PUT /user/mention`
+`POST /user/mention/mark_as_read`
 
 #### Get Private Messages
 ##### Request
index 5dc899b29ce8623016cb3adbdf1404b806ffb441..f32318e0acf648a41877b802a62a27f4517fc441 100644 (file)
@@ -52,6 +52,30 @@ impl Crud<UserMentionForm> for UserMention {
   }
 }
 
+impl UserMention {
+  pub fn update_read(
+    conn: &PgConnection,
+    user_mention_id: i32,
+    new_read: bool,
+  ) -> Result<Self, Error> {
+    use crate::schema::user_mention::dsl::*;
+    diesel::update(user_mention.find(user_mention_id))
+      .set(read.eq(new_read))
+      .get_result::<Self>(conn)
+  }
+
+  pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
+    use crate::schema::user_mention::dsl::*;
+    diesel::update(
+      user_mention
+        .filter(recipient_id.eq(for_recipient_id))
+        .filter(read.eq(false)),
+    )
+    .set(read.eq(true))
+    .get_results::<Self>(conn)
+  }
+}
+
 #[cfg(test)]
 mod tests {
   use crate::{
index a83b794a9b02e5159c3db84c651e6efd19ce9c06..2e62182396eccc444e5eb0f901c9ba4f22037206 100644 (file)
@@ -174,9 +174,9 @@ pub struct GetUserMentions {
 }
 
 #[derive(Serialize, Deserialize)]
-pub struct EditUserMention {
+pub struct MarkUserMentionAsRead {
   user_mention_id: i32,
-  read: Option<bool>,
+  read: bool,
   auth: String,
 }
 
@@ -874,7 +874,7 @@ impl Perform for Oper<GetUserMentions> {
 }
 
 #[async_trait::async_trait(?Send)]
-impl Perform for Oper<EditUserMention> {
+impl Perform for Oper<MarkUserMentionAsRead> {
   type Response = UserMentionResponse;
 
   async fn perform(
@@ -882,7 +882,7 @@ impl Perform for Oper<EditUserMention> {
     pool: &DbPool,
     _websocket_info: Option<WebsocketInfo>,
   ) -> Result<UserMentionResponse, LemmyError> {
-    let data: &EditUserMention = &self.data;
+    let data: &MarkUserMentionAsRead = &self.data;
 
     let claims = match Claims::decode(&data.auth) {
       Ok(claims) => claims.claims,
@@ -899,15 +899,9 @@ impl Perform for Oper<EditUserMention> {
       return Err(APIError::err("couldnt_update_comment").into());
     }
 
-    let user_mention_form = UserMentionForm {
-      recipient_id: read_user_mention.recipient_id,
-      comment_id: read_user_mention.comment_id,
-      read: data.read.to_owned(),
-    };
-
     let user_mention_id = read_user_mention.id;
-    let update_mention =
-      move |conn: &'_ _| UserMention::update(conn, user_mention_id, &user_mention_form);
+    let read = data.read;
+    let update_mention = move |conn: &'_ _| UserMention::update_read(conn, user_mention_id, read);
     if blocking(pool, update_mention).await?.is_err() {
       return Err(APIError::err("couldnt_update_comment").into());
     };
@@ -960,30 +954,10 @@ impl Perform for Oper<MarkAllAsRead> {
       }
     }
 
-    // Mentions
-    let mentions = blocking(pool, move |conn| {
-      UserMentionQueryBuilder::create(conn, user_id)
-        .unread_only(true)
-        .page(1)
-        .limit(999)
-        .list()
-    })
-    .await??;
-
-    // TODO: this should probably be a bulk operation
-    for mention in &mentions {
-      let mention_form = UserMentionForm {
-        recipient_id: mention.to_owned().recipient_id,
-        comment_id: mention.to_owned().id,
-        read: Some(true),
-      };
-
-      let user_mention_id = mention.user_mention_id;
-      let update_mention =
-        move |conn: &'_ _| UserMention::update(conn, user_mention_id, &mention_form);
-      if blocking(pool, update_mention).await?.is_err() {
-        return Err(APIError::err("couldnt_update_comment").into());
-      }
+    // Mark all user mentions as read
+    let update_user_mentions = move |conn: &'_ _| UserMention::mark_all_as_read(conn, user_id);
+    if blocking(pool, update_user_mentions).await?.is_err() {
+      return Err(APIError::err("couldnt_update_comment").into());
     }
 
     // Mark all private_messages as read
index 69ecbc8fd96a578e5a890929260d0cabcbd2dd1f..a02f1d3b56529ab004d319eb9b95ef3a7928e51a 100644 (file)
@@ -115,7 +115,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
           .wrap(rate_limit.message())
           .route("", web::get().to(route_get::<GetUserDetails>))
           .route("/mention", web::get().to(route_get::<GetUserMentions>))
-          .route("/mention", web::put().to(route_post::<EditUserMention>))
+          .route(
+            "/mention/mark_as_read",
+            web::post().to(route_post::<MarkUserMentionAsRead>),
+          )
           .route("/replies", web::get().to(route_get::<GetReplies>))
           .route(
             "/followed_communities",
index 0e938c7ef36bdace242968a11e9a93042cb1400e..431273d2564d85808372a3936bd6a153fe4c2956 100644 (file)
@@ -40,7 +40,7 @@ pub enum UserOperation {
   GetUserDetails,
   GetReplies,
   GetUserMentions,
-  EditUserMention,
+  MarkUserMentionAsRead,
   GetModlog,
   BanFromCommunity,
   AddModToCommunity,
index 4543781a3c70a2f7b064d6aa1fb2bc47617e8ba7..4909908443929b720a0d5099194db409274a8045 100644 (file)
@@ -443,7 +443,9 @@ impl ChatServer {
         UserOperation::AddAdmin => do_user_operation::<AddAdmin>(args).await,
         UserOperation::BanUser => do_user_operation::<BanUser>(args).await,
         UserOperation::GetUserMentions => do_user_operation::<GetUserMentions>(args).await,
-        UserOperation::EditUserMention => do_user_operation::<EditUserMention>(args).await,
+        UserOperation::MarkUserMentionAsRead => {
+          do_user_operation::<MarkUserMentionAsRead>(args).await
+        }
         UserOperation::MarkAllAsRead => do_user_operation::<MarkAllAsRead>(args).await,
         UserOperation::DeleteAccount => do_user_operation::<DeleteAccount>(args).await,
         UserOperation::PasswordReset => do_user_operation::<PasswordReset>(args).await,
index a6b9b7bac1921d888575d0be21fecf65ea9383f0..dfe52ec1cc64cc8298c47a6029335c4eb6c92385 100644 (file)
@@ -4,7 +4,7 @@ import {
   CommentNode as CommentNodeI,
   CommentLikeForm,
   CommentForm as CommentFormI,
-  EditUserMentionForm,
+  MarkUserMentionAsReadForm,
   SaveCommentForm,
   BanFromCommunityForm,
   BanUserForm,
@@ -969,11 +969,11 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
   handleMarkRead(i: CommentNode) {
     // if it has a user_mention_id field, then its a mention
     if (i.props.node.comment.user_mention_id) {
-      let form: EditUserMentionForm = {
+      let form: MarkUserMentionAsReadForm = {
         user_mention_id: i.props.node.comment.user_mention_id,
         read: !i.props.node.comment.read,
       };
-      WebSocketService.Instance.editUserMention(form);
+      WebSocketService.Instance.markUserMentionAsRead(form);
     } else {
       let form: CommentFormI = {
         content: i.props.node.comment.content,
index faedb4b13aba4238ac636ca35114ffb6c74b0ab4..5609879cc0348a1d77c5a58c5e176a050ebc1b95 100644 (file)
@@ -500,7 +500,7 @@ export class Inbox extends Component<any, InboxState> {
       this.sendUnreadCount();
       this.setState(this.state);
       setupTippy();
-    } else if (res.op == UserOperation.EditUserMention) {
+    } else if (res.op == UserOperation.MarkUserMentionAsRead) {
       let data = res.data as UserMentionResponse;
 
       let found = this.state.mentions.find(c => c.id == data.mention.id);
index 17b5f694a9ce3e0cd5d924fd4d120af6ada43ac0..0beb0ff92bf5b4fad820773b9499eea58d45a78c 100644 (file)
@@ -21,7 +21,7 @@ export enum UserOperation {
   GetUserDetails,
   GetReplies,
   GetUserMentions,
-  EditUserMention,
+  MarkUserMentionAsRead,
   GetModlog,
   BanFromCommunity,
   AddModToCommunity,
@@ -357,9 +357,9 @@ export interface GetUserMentionsResponse {
   mentions: Array<Comment>;
 }
 
-export interface EditUserMentionForm {
+export interface MarkUserMentionAsReadForm {
   user_mention_id: number;
-  read?: boolean;
+  read: boolean;
   auth?: string;
 }
 
@@ -901,7 +901,7 @@ export type MessageType =
   | GetUserDetailsForm
   | GetRepliesForm
   | GetUserMentionsForm
-  | EditUserMentionForm
+  | MarkUserMentionAsReadForm
   | GetModlogForm
   | SiteForm
   | SearchForm
index f0fb6fc771a5e14ed9a605899db80cf6888bfca6..54dc96359d020595e3c5925babf5279d7d628303 100644 (file)
@@ -28,7 +28,7 @@ import {
   UserView,
   GetRepliesForm,
   GetUserMentionsForm,
-  EditUserMentionForm,
+  MarkUserMentionAsReadForm,
   SearchForm,
   UserSettingsForm,
   DeleteAccountForm,
@@ -247,9 +247,9 @@ export class WebSocketService {
     this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
   }
 
-  public editUserMention(form: EditUserMentionForm) {
+  public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
     this.setAuth(form);
-    this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
+    this.ws.send(this.wsSendWrapper(UserOperation.MarkUserMentionAsRead, form));
   }
 
   public getModlog(form: GetModlogForm) {