]> Untitled Git - lemmy.git/commitdiff
Adding mark all as read
authorDessalines <tyhou13@gmx.com>
Mon, 29 Apr 2019 16:51:13 +0000 (09:51 -0700)
committerDessalines <tyhou13@gmx.com>
Mon, 29 Apr 2019 16:51:13 +0000 (09:51 -0700)
- Fixes #107

server/src/websocket_server/server.rs
ui/src/components/inbox.tsx
ui/src/interfaces.ts
ui/src/services/WebSocketService.ts

index 9c609a47000fea9dc0eed0675095de5727c2e501..3e361f691432e17d2cae4c3531035a684082f87c 100644 (file)
@@ -27,7 +27,7 @@ use actions::moderator::*;
 
 #[derive(EnumString,ToString,Debug)]
 pub enum UserOperation {
-  Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search
+  Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead
 }
 
 #[derive(Fail, Debug)]
@@ -478,6 +478,11 @@ pub struct SearchResponse {
   posts: Vec<PostView>,
 }
 
+#[derive(Serialize, Deserialize)]
+pub struct MarkAllAsRead {
+  auth: String
+}
+
 /// `ChatServer` manages chat rooms and responsible for coordinating chat
 /// session. implementation is super primitive
 pub struct ChatServer {
@@ -728,6 +733,10 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
       let search: Search = serde_json::from_str(data)?;
       search.perform(chat, msg.id)
     },
+    UserOperation::MarkAllAsRead => {
+      let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?;
+      mark_all_as_read.perform(chat, msg.id)
+    },
   }
 }
 
@@ -2709,3 +2718,56 @@ impl Perform for Search {
       )
   }
 }
+
+
+impl Perform for MarkAllAsRead {
+  fn op_type(&self) -> UserOperation {
+    UserOperation::MarkAllAsRead
+  }
+
+  fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
+
+    let conn = establish_connection();
+
+    let claims = match Claims::decode(&self.auth) {
+      Ok(claims) => claims.claims,
+      Err(_e) => {
+        return Err(self.error("Not logged in."))?
+      }
+    };
+
+    let user_id = claims.id;
+
+    let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
+
+    for reply in &replies {
+      let comment_form = CommentForm {
+        content: reply.to_owned().content,
+        parent_id: reply.to_owned().parent_id,
+        post_id: reply.to_owned().post_id,
+        creator_id: reply.to_owned().creator_id,
+        removed: None,
+        read: Some(true),
+        updated: reply.to_owned().updated 
+      };
+
+      let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
+        Ok(comment) => comment,
+        Err(_e) => {
+          return Err(self.error("Couldn't update Comment"))?
+        }
+      };
+    }
+
+    let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
+
+    Ok(
+      serde_json::to_string(
+        &GetRepliesResponse {
+          op: self.op_type().to_string(),
+          replies: replies,
+        }
+        )?
+      )
+  }
+}
index 02d813f3e2269bd84a0e1d3fdf045184ca86040a..f4ef2ecdcefbe5b080b8dc9e65a0bce2ce94f3eb 100644 (file)
@@ -58,7 +58,16 @@ export class Inbox extends Component<any, InboxState> {
       <div class="container">
         <div class="row">
           <div class="col-12">
-            <h5>Inbox for <Link to={`/u/${user.username}`}>{user.username}</Link></h5>
+            <h5 class="mb-0">
+              <span>Inbox for <Link to={`/u/${user.username}`}>{user.username}</Link></span>
+            </h5>
+            {this.state.replies.length > 0 && this.state.unreadType == UnreadType.Unread &&
+              <ul class="list-inline mb-1 text-muted small font-weight-bold">
+                <li className="list-inline-item">
+                  <span class="pointer" onClick={this.markAllAsRead}>mark all as read</span>
+                </li>
+              </ul>
+            }
             {this.selects()}
             {this.replies()}
             {this.paginator()}
@@ -147,13 +156,17 @@ export class Inbox extends Component<any, InboxState> {
     i.refetch();
   }
 
+  markAllAsRead() {
+    WebSocketService.Instance.markAllAsRead();
+  }
+
   parseMessage(msg: any) {
     console.log(msg);
     let op: UserOperation = msgOp(msg);
     if (msg.error) {
       alert(msg.error);
       return;
-    } else if (op == UserOperation.GetReplies) {
+    } else if (op == UserOperation.GetReplies || op == UserOperation.MarkAllAsRead) {
       let res: GetRepliesResponse = msg;
       this.state.replies = res.replies;
       this.sendRepliesCount();
index 51582f3a1ef80ca9c1319c30067fbecdfd1b7b00..8a16de4325a73d62c0b35e071d80b3033ca0d219 100644 (file)
@@ -1,5 +1,5 @@
 export enum UserOperation {
-  Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search
+  Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead
 }
 
 export enum CommentSortType {
index 2389ab842f61bc9453db1820df7e637a26bf57eb..06e604e95fb4326dcbd397e1642889194d40b50b 100644 (file)
@@ -177,6 +177,12 @@ export class WebSocketService {
     this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
   }
 
+  public markAllAsRead() {
+    let form = {};
+    this.setAuth(form);
+    this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
+  }
+
   private wsSendWrapper(op: UserOperation, data: any) {
     let send = { op: UserOperation[op], data: data };
     console.log(send);