ソースを参照

Fix notifications

Danilo Bargen 7 年 前
コミット
6c420f3ff5
2 ファイル変更20 行追加22 行削除
  1. 0 12
      src/services/notification.ts
  2. 20 10
      src/services/webclient.ts

+ 0 - 12
src/services/notification.ts

@@ -174,7 +174,6 @@ export class NotificationService {
 
     /**
      * Returns if the user has granted the notification permission
-     * @returns {boolean}
      */
     public getNotificationPermission(): boolean {
         return this.notificationPermission;
@@ -182,7 +181,6 @@ export class NotificationService {
 
     /**
      * Returns if the user wants to receive notifications
-     * @returns {boolean}
      */
     public getWantsNotifications(): boolean {
         return this.desktopNotifications;
@@ -190,7 +188,6 @@ export class NotificationService {
 
     /**
      * Returns if the user wants a message preview in the notification
-     * @returns {boolean}
      */
     public getWantsPreview(): boolean {
         return this.notificationPreview;
@@ -198,7 +195,6 @@ export class NotificationService {
 
     /**
      * Returns if the user wants sound when a new message arrives
-     * @returns {boolean}
      */
     public getWantsSound(): boolean {
         return this.notificationSound;
@@ -206,7 +202,6 @@ export class NotificationService {
 
     /**
      * Returns if the notification api is available
-     * @returns {boolean}
      */
     public isNotificationApiAvailable(): boolean {
         return this.notificationAPIAvailable;
@@ -214,7 +209,6 @@ export class NotificationService {
 
     /**
      * Sets if the user wants desktop notifications
-     * @param wantsNotifications
      */
     public setWantsNotifications(wantsNotifications: boolean): void {
         this.$log.debug(this.logTag, 'Requesting notification preference change to', wantsNotifications);
@@ -228,7 +222,6 @@ export class NotificationService {
 
     /**
      * Sets if the user wants a message preview
-     * @param wantsPreview
      */
     public setWantsPreview(wantsPreview: boolean): void {
         this.$log.debug(this.logTag, 'Requesting preview preference change to', wantsPreview);
@@ -238,7 +231,6 @@ export class NotificationService {
 
     /**
      * Sets if the user wants sound when a new message arrives
-     * @param wantsSound
      */
     public setWantsSound(wantsSound: boolean): void {
         this.$log.debug(this.logTag, 'Requesting sound preference change to', wantsSound);
@@ -248,8 +240,6 @@ export class NotificationService {
 
     /**
      * Stores the given key/value pair in local storage
-     * @param key
-     * @param value
      */
     private storeSetting(key: string, value: string): void {
         this.settingsService.storeUntrustedKeyValuePair(key, value);
@@ -257,8 +247,6 @@ export class NotificationService {
 
     /**
      * Retrieves the value for the given key from local storage
-     * @param key
-     * @returns {string}
      */
     private retrieveSetting(key: string): string {
         return this.settingsService.retrieveUntrustedKeyValuePair(key);

+ 20 - 10
src/services/webclient.ts

@@ -2196,6 +2196,9 @@ export class WebClientService {
             return;
         }
 
+        // Get receiver
+        const receiver = this.receivers.getData({type: data.type, id: data.id});
+
         // Unpack required argument fields
         const type: string = args[WebClientService.ARGUMENT_MODE];
         switch (type) {
@@ -2209,7 +2212,7 @@ export class WebClientService {
                 if (data.unreadCount > 0) {
                     const oldConversation = this.conversations.updateOrAdd(data);
                     if (oldConversation === null) {
-                        this.onNewMessage(data.latestMessage, data);
+                        this.onNewMessage(data.latestMessage, data, receiver);
                     } else {
                         // Check for unread count changes
                         const unreadCountIncreased = data.unreadCount > oldConversation.unreadCount;
@@ -2218,7 +2221,7 @@ export class WebClientService {
                         // If the unreadcount has increased, we received a new message.
                         // Otherwise, if it has decreased, hide the notification.
                         if (unreadCountIncreased) {
-                            this.onNewMessage(data.latestMessage, data);
+                            this.onNewMessage(data.latestMessage, data, receiver);
                         } else if (unreadCountDecreased) {
                             this.notificationService.hideNotification(data.type + '-' + data.id);
                         }
@@ -2231,16 +2234,20 @@ export class WebClientService {
 
                 break;
             case WebClientService.ARGUMENT_MODE_REMOVED:
+                // Remove conversation
                 this.conversations.remove(data);
-                // Remove all cached messages
-                this.messages.clearReceiverMessages(data.receiver);
+
+                // Remove all cached messages for the receiver
+                this.messages.clearReceiverMessages(receiver);
+
+                // Call on-removed listener
                 this.receiverListener.forEach((listener: threema.ReceiverListener) => {
-                    this.$log.debug('call on removed listener');
+                    this.$log.debug(this.logTag, 'Call on removed listener');
                     listener.onRemoved(data.receiver);
                 });
                 break;
             default:
-                this.$log.warn('Received conversation without a mode');
+                this.$log.warn(this.logTag, 'Received conversation without a mode');
                 return;
         }
 
@@ -2402,16 +2409,19 @@ export class WebClientService {
     /**
      * Called when a new message arrives.
      */
-    private onNewMessage(message: threema.Message, conversation: threema.Conversation): void {
+    private onNewMessage(
+        message: threema.Message,
+        conversation: threema.Conversation,
+        sender: threema.Receiver,
+    ): void {
         // Ignore message from active receivers (and if the browser tab is visible)
         if (this.browserService.isVisible()
                 && this.receiverService.compare(conversation, this.receiverService.getActive())) {
             return;
         }
-        const sender: threema.Receiver = conversation.receiver;
 
         // Do not show any notifications on private chats
-        if (sender === undefined || sender.locked) {
+        if (sender.locked === true) {
             return;
         }
 
@@ -2434,7 +2444,7 @@ export class WebClientService {
         const partnerName = partner.displayName || ('~' + partner.publicNickname);
 
         // Show notification
-        this.$translate('messenger.MESSAGE_NOTIFICATION_SUBJECT', {messageCount: 1 + conversation.unreadCount})
+        this.$translate('messenger.MESSAGE_NOTIFICATION_SUBJECT', {messageCount: conversation.unreadCount})
             .then((titlePrefix) =>  {
                 const title = `${titlePrefix} ${senderName}`;
                 let body = '';