Ver Fonte

Trigger loading messages when opening an empty conversation (#602)

This is a workaround for Safari, see code comments.
Danilo Bargen há 7 anos atrás
pai
commit
c29a58c821
2 ficheiros alterados com 19 adições e 6 exclusões
  1. 14 5
      src/partials/messenger.ts
  2. 5 1
      src/services/webclient.ts

+ 14 - 5
src/partials/messenger.ts

@@ -24,7 +24,7 @@ import {
 } from '@uirouter/angularjs';
 
 import {ContactControllerModel} from '../controller_model/contact';
-import {bufferToUrl, logAdapter, supportsPassive, throttle, u8aToHex} from '../helpers';
+import {bufferToUrl, hasValue, logAdapter, supportsPassive, throttle, u8aToHex} from '../helpers';
 import {ContactService} from '../services/contact';
 import {ControllerService} from '../services/controller';
 import {ControllerModelService} from '../services/controller_model';
@@ -384,7 +384,7 @@ class ConversationController {
                 return;
             }
 
-            // initial set locked state
+            // Initial set locked state
             this.locked = this.receiver.locked;
 
             this.receiverService.setActive(this.receiver);
@@ -442,14 +442,22 @@ class ConversationController {
                     });
                 }
 
+                // Set initial data
                 this.initialData = {
                     draft: webClientService.getDraft(this.receiver),
                     initialText: $stateParams.initParams ? $stateParams.initParams.text : '',
                 };
 
+                // Set isTyping function for contacts
                 if (isContactReceiver(this.receiver)) {
                     this.isTyping = () => this.webClientService.isTyping(this.receiver as threema.ContactReceiver);
                 }
+
+                // Due to a bug in Safari, sometimes the in-view element does not trigger when initially loading a chat.
+                // As a workaround, manually trigger the initial message loading.
+                if (this.webClientService.messages.getList(this.receiver).length === 0) {
+                    this.requestMessages();
+                }
             }
         } catch (error) {
             $log.error('Could not set receiver and type');
@@ -768,9 +776,10 @@ class ConversationController {
     public requestMessages(): void {
         const refMsgId = this.webClientService.requestMessages(this.$stateParams);
 
-        if (refMsgId !== null
-            && refMsgId !== undefined) {
-            // new message are requested, scroll to refMsgId
+        // TODO: Couldn't this cause a race condition when called twice in parallel?
+        //       Might be related to #277.
+        if (hasValue(refMsgId)) {
+            // New messages are requested, scroll to refMsgId
             this.latestRefMsgId = refMsgId;
         } else {
             this.latestRefMsgId = null;

+ 5 - 1
src/services/webclient.ts

@@ -1384,10 +1384,13 @@ export class WebClientService {
      * New messages are not requested this way, instead they are sent as a
      * message update.
      */
-    public requestMessages(receiver: threema.Receiver): string {
+    public requestMessages(receiver: threema.Receiver): string | null {
+        this.$log.debug(this.logTag, 'requestMessages');
+
         // If there are no more messages available, stop here.
         if (!this.messages.hasMore(receiver)) {
             this.messages.notify(receiver, this.$rootScope);
+            this.$log.debug(this.logTag, 'requestMessages: No more messages available');
             return null;
         }
 
@@ -1395,6 +1398,7 @@ export class WebClientService {
 
         // Check if messages have already been requested
         if (this.messages.isRequested(receiver)) {
+            this.$log.debug(this.logTag, 'requestMessages: Already requested');
             return null;
         }