Selaa lähdekoodia

Conversations: Make position optional

Danilo Bargen 7 vuotta sitten
vanhempi
commit
cdd60cb931
4 muutettua tiedostoa jossa 40 lisäystä ja 8 poistoa
  1. 4 4
      src/services/webclient.ts
  2. 9 1
      src/threema.d.ts
  3. 2 2
      src/threema/container.ts
  4. 25 1
      tests/ts/containers.ts

+ 4 - 4
src/services/webclient.ts

@@ -1677,7 +1677,7 @@ export class WebClientService {
 
     private _receiveResponseConversations(message: threema.WireMessage) {
         this.$log.debug('Received conversations response');
-        const data = message.data;
+        const data = message.data as threema.Conversation[];
         if (data === undefined) {
             this.$log.warn('Invalid conversation response, data missing');
         } else {
@@ -2039,7 +2039,7 @@ export class WebClientService {
     private _receiveUpdateConversation(message: threema.WireMessage) {
         this.$log.debug('Received conversation update');
         const args = message.args;
-        const data = message.data;
+        const data = message.data as threema.ConversationWithPosition;
         if (args === undefined || data === undefined) {
             this.$log.warn('Invalid conversation update, data or arguments missing');
             return;
@@ -2089,10 +2089,10 @@ export class WebClientService {
             case WebClientService.ARGUMENT_MODE_REMOVED:
                 this.conversations.remove(data);
                 // Remove all cached messages
-                this.messages.clearReceiverMessages((data as threema.Receiver));
+                this.messages.clearReceiverMessages(data.receiver);
                 this.receiverListener.forEach((listener: threema.ReceiverListener) => {
                     this.$log.debug('call on removed listener');
-                    listener.onRemoved(data);
+                    listener.onRemoved(data.receiver);
                 });
                 break;
             default:

+ 9 - 1
src/threema.d.ts

@@ -303,13 +303,14 @@ declare namespace threema {
         label: string;
         number: string;
     }
+
     /**
      * A conversation.
      */
     interface Conversation {
         type: ReceiverType;
         id: string;
-        position: number;
+        position?: number;
         messageCount: number;
         unreadCount: number;
         latestMessage: Message | null;
@@ -319,6 +320,13 @@ declare namespace threema {
         isStarred?: boolean;
     }
 
+    /**
+     * A conversation with a position field, used for updating a conversation.
+     */
+    interface ConversationWithPosition extends Conversation {
+        position: number;
+    }
+
     /**
      * Connection state in the welcome dialog.
      *

+ 2 - 2
src/threema/container.ts

@@ -269,11 +269,11 @@ export class Conversations implements threema.Container.Conversations {
         return null;
     }
 
-    public add(conversation: threema.Conversation): void {
+    public add(conversation: threema.ConversationWithPosition): void {
         this.conversations.splice(conversation.position, 0, conversation);
     }
 
-    public updateOrAdd(conversation: threema.Conversation): void {
+    public updateOrAdd(conversation: threema.ConversationWithPosition): void {
         let moveDirection = 0;
         let updated = false;
         for (const p of this.conversations.keys()) {

+ 25 - 1
tests/ts/containers.ts

@@ -17,6 +17,9 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
+// tslint:disable:no-reference
+/// <reference path="../../src/threema.d.ts" />
+
 import {ReceiverService} from '../../src/services/receiver';
 import {Conversations} from '../../src/threema/container';
 
@@ -25,7 +28,7 @@ function getConversations(): Conversations {
     return new Conversations(receiverService);
 }
 
-function makeContactConversation(id: string, position: number): threema.Conversation {
+function makeContactConversation(id: string, position: number): threema.ConversationWithPosition {
     return {
         type: 'contact',
         id: id,
@@ -73,6 +76,27 @@ describe('Container', () => {
                 expect(conversations.get().map(simplifyConversation)).toEqual([['1', 1]]);
             });
         });
+
+        describe('add', function() {
+            it('adds a conversation at the correct location', function() {
+                const conversations = getConversations();
+                expect(conversations.get()).toEqual([]);
+
+                conversations.add(makeContactConversation('0', 0));
+                conversations.add(makeContactConversation('1', 1));
+                expect(conversations.get().map(simplifyConversation)).toEqual([
+                    ['0', 0],
+                    ['1', 1],
+                ]);
+
+                conversations.add(makeContactConversation('2', 1));
+                expect(conversations.get().map(simplifyConversation)).toEqual([
+                    ['0', 0],
+                    ['2', 1],
+                    ['1', 1],
+                ]);
+            });
+        });
     });
 
 });