Explorar o código

Fix `sortKey` for temporary messages (#872)

Resolves #871
Lennart Grahl %!s(int64=6) %!d(string=hai) anos
pai
achega
d33ef03a56
Modificáronse 3 ficheiros con 21 adicións e 11 borrados
  1. 1 1
      src/partials/messenger.ts
  2. 1 1
      src/services/message.ts
  3. 19 9
      src/threema/container.ts

+ 1 - 1
src/partials/messenger.ts

@@ -793,7 +793,7 @@ class ConversationController {
     public requestMessages(): void {
         const refMsgId = this.webClientService.requestMessages(this.$stateParams);
 
-        // TODO: Couldn't this cause a race condition when called twice in parallel?
+        // TODO: Couldn't this cause a race condition when called twice asynchronously?
         //       Might be related to #277.
         if (hasValue(refMsgId)) {
             // New messages are requested, scroll to refMsgId

+ 1 - 1
src/services/message.ts

@@ -180,7 +180,7 @@ export class MessageService {
             type: type,
             id: undefined, // Note: Hack, violates the interface
             date: timestampS,
-            sortKey: Number.MAX_SAFE_INTEGER, // Note: Ugly hack
+            sortKey: undefined, // Note: Hack, violates the interface
             partnerId: receiver.id,
             isOutbox: true,
             isStatus: false,

+ 19 - 9
src/threema/container.ts

@@ -567,16 +567,31 @@ class Messages implements threema.Container.Messages {
      * Messages must be sorted ascending by date.
      */
     public addNewer(receiver: threema.BaseReceiver, messages: threema.Message[]): void {
+        // Ignore empty list
         if (messages.length === 0) {
-            // do nothing
             return;
         }
         const receiverMessages = this.getReceiverMessages(receiver);
-        // if the list is empty, add the current message as ref
+
+        // Set oldest message if none previously available
         if (receiverMessages.list.length === 0) {
             receiverMessages.referenceMsgId = messages[0].id;
         }
-        receiverMessages.list.push.apply(receiverMessages.list, messages);
+
+        // Append new messages
+        for (const message of messages) {
+            // Determine sort key (if not set)
+            // Note: This resolves an ugly interface violation of temporary
+            //       messages.
+            if (message.sortKey === undefined) {
+                message.sortKey = receiverMessages.list.length > 0
+                    ? receiverMessages.list[receiverMessages.list.length - 1].sortKey
+                    : 0;
+            }
+
+            // Append message
+            receiverMessages.list.push(message);
+        }
     }
 
     /**
@@ -618,18 +633,13 @@ class Messages implements threema.Container.Messages {
         // Get reference to message list for the specified receiver
         const receiverMessages = this.getReceiverMessages(receiver);
 
-        // Determine sort key
-        const sortKey = receiverMessages.list.length > 0
-            ? receiverMessages.list[receiverMessages.list.length - 1].sortKey
-            : 0;
-
         // Create fake status message
         receiverMessages.list.push({
             type: 'status',
             id: randomString(6),
             body: '',
             caption: text,
-            sortKey: sortKey,
+            sortKey: undefined, // Note: Hack, violates the interface
             partnerId: receiver.id,
             isOutbox: false,
             isStatus: true,