Browse Source

Always do receiver type checking using typeguards

This is cleaner and more useful than using the ReceiverService methods,
since it also helps typechecking without casts.
Danilo Bargen 7 năm trước cách đây
mục cha
commit
21197c8bba

+ 2 - 2
src/partials/messenger.ts

@@ -1139,8 +1139,8 @@ class ReceiverDetailController {
         this.me = webClientService.me;
 
         // Append group membership
-        if (this.receiver.type === 'contact') {
-            const contactReceiver = this.receiver as threema.ContactReceiver;
+        if (isContactReceiver(this.receiver)) {
+            const contactReceiver = this.receiver;
 
             this.contactService.requiredDetails(contactReceiver)
                 .then(() => {

+ 2 - 1
src/services/message.ts

@@ -15,6 +15,7 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
+import {isContactReceiver} from '../typeguards';
 import {ReceiverService} from './receiver';
 
 export class MessageAccess {
@@ -48,7 +49,7 @@ export class MessageService {
 
             if (receiver !== undefined && message.temporaryId === undefined) {
                 if (message.isOutbox === false
-                    && this.receiverService.isContact(receiver)
+                    && isContactReceiver(receiver)
                     && message.type !== 'voipStatus') {
                     access.ack = message.state !== 'user-ack';
                     access.dec = message.state !== 'user-dec';

+ 3 - 16
src/services/receiver.ts

@@ -15,6 +15,8 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
+import {isContactReceiver} from '../typeguards';
+
 export class ReceiverService {
     private activeReceiver: threema.Receiver;
 
@@ -51,23 +53,8 @@ export class ReceiverService {
             && a.id === b.id;
     }
 
-    public isContact(receiver: threema.Receiver): boolean {
-        return receiver !== undefined
-            && receiver.type === 'contact';
-    }
-
-    public isGroup(receiver: threema.Receiver): boolean {
-        return receiver !== undefined
-            && receiver.type === 'group';
-    }
-
-    public isDistributionList(receiver: threema.Receiver): boolean {
-        return receiver !== undefined
-            && receiver.type === 'distributionList';
-    }
-
     public isBusinessContact(receiver: threema.Receiver): boolean {
-        return this.isContact(receiver)
+        return isContactReceiver(receiver)
             && receiver.id.substr(0, 1) === '*';
 
     }

+ 3 - 4
src/services/webclient.ts

@@ -1112,8 +1112,7 @@ export class WebClientService {
                 // Try to load receiver object
                 const receiverObject = this.receivers.getData(receiver);
                 // Check blocked flag
-                if (receiverObject.type === 'contact'
-                    && (receiverObject as threema.ContactReceiver).isBlocked) {
+                if (isContactReceiver(receiverObject) && receiverObject.isBlocked) {
                     return reject(this.$translate.instant('error.CONTACT_BLOCKED'));
                 }
                 // Decide on subtype
@@ -2547,8 +2546,8 @@ export class WebClientService {
         let senderName = sender.id;
         if (sender.displayName) {
             senderName = sender.displayName;
-        } else if (sender.type === 'contact') {
-            senderName = '~' + (sender as threema.ContactReceiver).publicNickname;
+        } else if (isContactReceiver(sender)) {
+            senderName = '~' + sender.publicNickname;
         }
         const partner = this.receivers.getData({
             id: message.partnerId,

+ 9 - 3
src/typeguards.ts

@@ -22,20 +22,26 @@
 /**
  * Contact receiver type guard
  */
-export function isContactReceiver(receiver: threema.Receiver): receiver is threema.ContactReceiver {
+export function isContactReceiver(
+    receiver: threema.BaseReceiver,
+): receiver is threema.ContactReceiver {
     return receiver.type === 'contact';
 }
 
 /**
  * Group receiver type guard
  */
-export function isGroupReceiver(receiver: threema.Receiver): receiver is threema.GroupReceiver {
+export function isGroupReceiver(
+    receiver: threema.BaseReceiver,
+): receiver is threema.GroupReceiver {
     return receiver.type === 'group';
 }
 
 /**
  * Distribution list receiver type guard
  */
-export function isDistributionListReceiver(receiver: threema.Receiver): receiver is threema.DistributionListReceiver {
+export function isDistributionListReceiver(
+    receiver: threema.BaseReceiver,
+): receiver is threema.DistributionListReceiver {
     return receiver.type === 'distributionList';
 }