]> Untitled Git - lemmy.git/commitdiff
Adding browser notifications.
authorDessalines <tyhou13@gmx.com>
Sat, 18 May 2019 23:41:22 +0000 (16:41 -0700)
committerDessalines <tyhou13@gmx.com>
Sat, 18 May 2019 23:41:22 +0000 (16:41 -0700)
- Fixes #153

ui/src/components/navbar.tsx
ui/src/components/post.tsx
ui/src/css/main.css

index 844711457f68aca68a82856ea043768b402500b3..c19c208481d46cd10d14fe54bf2ce01ec4e8da35 100644 (file)
@@ -3,7 +3,7 @@ import { Link } from 'inferno-router';
 import { Subscription } from "rxjs";
 import { retryWhen, delay, take } from 'rxjs/operators';
 import { WebSocketService, UserService } from '../services';
-import { UserOperation, GetRepliesForm, GetRepliesResponse, SortType, GetSiteResponse } from '../interfaces';
+import { UserOperation, GetRepliesForm, GetRepliesResponse, SortType, GetSiteResponse, Comment} from '../interfaces';
 import { msgOp } from '../utils';
 import { version } from '../version';
 
@@ -11,6 +11,7 @@ interface NavbarState {
   isLoggedIn: boolean;
   expanded: boolean;
   expandUserDropdown: boolean;
+  replies: Array<Comment>,
   unreadCount: number;
   siteName: string;
 }
@@ -21,6 +22,7 @@ export class Navbar extends Component<any, NavbarState> {
   emptyState: NavbarState = {
     isLoggedIn: (UserService.Instance.user !== undefined),
     unreadCount: 0,
+    replies: [],
     expanded: false,
     expandUserDropdown: false,
     siteName: undefined
@@ -37,6 +39,7 @@ export class Navbar extends Component<any, NavbarState> {
     this.userSub = UserService.Instance.sub.subscribe(user => {
       this.state.isLoggedIn = user.user !== undefined;
       this.state.unreadCount = user.unreadCount;
+      this.requestNotificationPermission();
       this.setState(this.state);
     });
 
@@ -48,6 +51,10 @@ export class Navbar extends Component<any, NavbarState> {
         () => console.log('complete')
     );
 
+    if (this.state.isLoggedIn) {
+      this.requestNotificationPermission();
+    }
+
     WebSocketService.Instance.getSite();
   }
 
@@ -151,6 +158,12 @@ export class Navbar extends Component<any, NavbarState> {
       return;
     } else if (op == UserOperation.GetReplies) {
       let res: GetRepliesResponse = msg;
+      if (res.replies.length > 0 && this.state.replies.length > 0 && 
+          (JSON.stringify(this.state.replies) !== JSON.stringify(res.replies))) {
+        this.notify(res.replies);
+      }
+
+      this.state.replies = res.replies;
       this.sendRepliesCount(res);
     } else if (op == UserOperation.GetSite) {
       let res: GetSiteResponse = msg;
@@ -187,5 +200,35 @@ export class Navbar extends Component<any, NavbarState> {
   sendRepliesCount(res: GetRepliesResponse) {
     UserService.Instance.sub.next({user: UserService.Instance.user, unreadCount: res.replies.filter(r => !r.read).length});
   }
-}
 
+  requestNotificationPermission() {
+    if (UserService.Instance.user) {
+    document.addEventListener('DOMContentLoaded', function () {
+      if (!Notification) {
+        alert('Desktop notifications not available in your browser. Try Chromium.'); 
+        return;
+      }
+
+      if (Notification.permission !== 'granted')
+        Notification.requestPermission();
+    });
+    }
+  }
+
+  notify(replies: Array<Comment>) {
+    let recentReply = replies[0];
+    if (Notification.permission !== 'granted')
+      Notification.requestPermission();
+    else {
+      var notification = new Notification(`${replies.length} Unread Messages`, {
+        icon: `${window.location.protocol}//${window.location.host}/static/assets/apple-touch-icon.png`,
+        body: recentReply.content
+      });
+
+      notification.onclick = () => {
+        this.context.router.history.push(`/post/${recentReply.post_id}/comment/${recentReply.id}`);
+      };
+
+    }
+  }
+}
index 7b2f790ebbf4a617f46ff046c3f127433fc3efde..8fa18fbf89d25c8ab17a3d60254c61889934a8f0 100644 (file)
@@ -69,7 +69,7 @@ export class Post extends Component<any, PostState> {
     if (this.state.scrolled_comment_id && !this.state.scrolled && lastState.comments.length > 0) {
       var elmnt = document.getElementById(`comment-${this.state.scrolled_comment_id}`);
       elmnt.scrollIntoView(); 
-      elmnt.classList.add("mark");
+      elmnt.classList.add("mark-two");
       this.state.scrolled = true;
     }
   }
index c3ff3bdab4d1a9d380d62e554f07dd5e73b3fca5..2a65ca19f545efb2c5cb4631f5e081bd9aa50f66 100644 (file)
@@ -38,6 +38,10 @@ body, .text-white, .navbar-brand, .badge-light, .btn-secondary {
   background-color: #333;
 }
 
+.mark-two {
+  background-color: #444 !important;
+}
+
 .md-div p {
   margin-bottom: 0px;
 }