ソースを参照

Conversations container: Overwrite old data with set()

Danilo Bargen 7 年 前
コミット
267a9160d5

+ 0 - 7
src/services/receiver.ts

@@ -16,14 +16,7 @@
  */
 
 export class ReceiverService {
-    private $log: ng.ILogService;
     private activeReceiver: threema.Receiver;
-    public static $inject = ['$log'];
-
-    constructor($log: ng.ILogService) {
-        // Angular services
-        this.$log = $log;
-    }
 
     /**
      * Set the currently active receiver.

+ 1 - 2
src/services/webclient.ts

@@ -1696,9 +1696,8 @@ export class WebClientService {
                     // Remove avatar from conversation
                     delete conversation.avatar;
                 }
-
-                this.conversations.updateOrAdd(conversation);
             }
+            this.conversations.set(data);
         }
         this.updateUnreadCount();
         this.registerInitializationStep(InitializationStep.Conversations);

+ 6 - 5
src/threema/container.ts

@@ -216,7 +216,7 @@ class Receivers implements threema.Container.Receivers {
     }
 }
 
-class Conversations implements threema.Container.Conversations {
+export class Conversations implements threema.Container.Conversations {
 
     private conversations: threema.Conversation[] = [];
 
@@ -245,11 +245,12 @@ class Conversations implements threema.Container.Conversations {
 
     /**
      * Set conversations.
+     *
+     * This will simply overwrite the previous list of conversations with the
+     * one provided.
      */
     public set(data: threema.Conversation[]): void {
-        data.forEach((existingConversation: threema.Conversation) => {
-            this.updateOrAdd(existingConversation);
-        });
+        this.conversations = data;
     }
 
     /**
@@ -257,7 +258,7 @@ class Conversations implements threema.Container.Conversations {
      *
      * Comparison is done by type and id.
      */
-    public find(pattern: threema.Conversation | threema.Receiver): threema.Conversation | null {
+    public find(pattern: threema.Conversation | threema.BaseReceiver): threema.Conversation | null {
         for (const conversation of this.get()) {
             const a = pattern;
             const b = conversation;

+ 78 - 0
tests/ts/containers.ts

@@ -0,0 +1,78 @@
+/**
+ * Copyright © 2016-2018 Threema GmbH (https://threema.ch/).
+ *
+ * This file is part of Threema Web.
+ *
+ * Threema Web is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import {ReceiverService} from '../../src/services/receiver';
+import {Conversations} from '../../src/threema/container';
+
+function getConversations(): Conversations {
+    const receiverService = new ReceiverService();
+    return new Conversations(receiverService);
+}
+
+function makeContactConversation(id: string, position: number): threema.Conversation {
+    return {
+        type: 'contact',
+        id: id,
+        position: position,
+        messageCount: 5,
+        unreadCount: 0,
+        latestMessage: null,
+    };
+}
+
+function simplifyConversation(c: threema.Conversation): Array<string | number> {
+    return [c.id, c.position];
+}
+
+describe('Container', () => {
+
+    describe('Conversations', () => {
+        it('find', function() {
+            const conversations = getConversations();
+            conversations.set([
+                makeContactConversation('1', 0),
+                makeContactConversation('2', 1),
+                makeContactConversation('3', 2),
+                makeContactConversation('4', 3),
+            ]);
+
+            const receiver1: threema.BaseReceiver = { id: '2', type: 'contact' };
+            const receiver2: threema.BaseReceiver = { id: '5', type: 'contact' };
+            const receiver3: threema.BaseReceiver = { id: '2', type: 'me' };
+
+            expect(conversations.find(receiver1)).toEqual(makeContactConversation('2', 1));
+            expect(conversations.find(receiver2)).toEqual(null);
+            expect(conversations.find(receiver3)).toEqual(null);
+        });
+
+        describe('set', function() {
+            it('overwrites previous data', function() {
+                const conversations = getConversations();
+                expect(conversations.get()).toEqual([]);
+
+                conversations.add(makeContactConversation('0', 0));
+                expect(conversations.get().map(simplifyConversation)).toEqual([['0', 0]]);
+
+                conversations.set([makeContactConversation('1', 1)]);
+                expect(conversations.get().map(simplifyConversation)).toEqual([['1', 1]]);
+            });
+        });
+    });
+
+});

+ 27 - 0
tests/ts/helpers.ts

@@ -0,0 +1,27 @@
+/**
+ * Copyright © 2016-2018 Threema GmbH (https://threema.ch/).
+ *
+ * This file is part of Threema Web.
+ *
+ * Threema Web is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import {u8aToHex} from '../../src/helpers';
+
+describe('Helpers', () => {
+    it('u8aToHex', function() {
+        const arr = Uint8Array.of(1, 2, 4, 8, 254, 255);
+        expect(u8aToHex(arr)).toEqual('01020408feff');
+    });
+});

+ 4 - 8
tests/ts/main.ts

@@ -17,12 +17,8 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
-import 'jasmine';
-import {u8aToHex} from '../../src/helpers';
+// tslint:disable:no-reference
+/// <reference path="../../src/threema.d.ts" />
 
-describe('Helpers', () => {
-    it('u8aToHex', function() {
-        const arr = Uint8Array.of(1, 2, 4, 8, 254, 255);
-        expect(u8aToHex(arr)).toEqual('01020408feff');
-    });
-});
+import './containers';
+import './helpers';