async observe()

in mail/components/im/content/chat-messenger.js [1359:1685]


  async observe(aSubject, aTopic, aData) {
    if (aTopic == "chat-core-initialized") {
      this.initAfterChatCore();
      return;
    }

    if (aTopic == "conversation-loaded") {
      const browser = document.getElementById("conv-log-browser");
      if (aSubject != browser) {
        return;
      }

      for (const msg of browser._conv.getMessages()) {
        if (!msg.system) {
          msg.color =
            "color: hsl(" + this._computeColor(msg.who) + ", 100%, 40%);";
        }
        browser.appendMessage(msg);
      }

      if (this._pendingSearchTerm) {
        const findbar = document.getElementById("log-findbar");
        findbar._findField.value = this._pendingSearchTerm;
        findbar.open();
        browser.focus();
        delete this._pendingSearchTerm;
        const eventListener = function () {
          findbar.onFindAgainCommand();
          if (findbar._findFailedString && browser._messageDisplayPending) {
            return;
          }
          // Search result found or all messages added, we're done.
          browser.removeEventListener("MessagesDisplayed", eventListener);
        };
        browser.addEventListener("MessagesDisplayed", eventListener);
      }
      this._pendingLogBrowserLoad = false;
      Services.obs.removeObserver(this, "conversation-loaded");
      return;
    }

    if (
      aTopic == "account-connected" ||
      aTopic == "account-disconnected" ||
      aTopic == "account-added" ||
      aTopic == "account-removed"
    ) {
      this._updateNoConvPlaceHolder();
      return;
    }

    if (aTopic == "contact-signed-on") {
      if (!this._hasConversationForContact(aSubject)) {
        document.getElementById("onlinecontactsGroup").addContact(aSubject);
        document.getElementById("offlinecontactsGroup").removeContact(aSubject);
      }
      return;
    }
    if (aTopic == "contact-signed-off") {
      if (!this._hasConversationForContact(aSubject)) {
        document.getElementById("offlinecontactsGroup").addContact(aSubject);
        document.getElementById("onlinecontactsGroup").removeContact(aSubject);
      }
      return;
    }
    if (aTopic == "contact-added") {
      const groupName = (aSubject.online ? "on" : "off") + "linecontactsGroup";
      document.getElementById(groupName).addContact(aSubject);
      return;
    }
    if (aTopic == "contact-removed") {
      const groupName = (aSubject.online ? "on" : "off") + "linecontactsGroup";
      document.getElementById(groupName).removeContact(aSubject);
      return;
    }
    if (aTopic == "contact-no-longer-dummy") {
      const oldId = parseInt(aData);
      const groupName = (aSubject.online ? "on" : "off") + "linecontactsGroup";
      const group = document.getElementById(groupName);
      if (group.contactsById.hasOwnProperty(oldId)) {
        const contact = group.contactsById[oldId];
        delete group.contactsById[oldId];
        group.contactsById[contact.contact.id] = contact;
      }
      return;
    }
    if (aTopic == "new-text") {
      this.updateChatButtonState();
      return;
    }
    if (aTopic == "new-ui-conversation") {
      if (chatTabType.hasBeenOpened) {
        chatHandler._addConversation(aSubject.wrappedJSObject);
      }
      return;
    }
    if (aTopic == "ui-conversation-closed") {
      this.updateChatButtonState();
      if (!chatTabType.hasBeenOpened) {
        return;
      }
      const conv = document
        .getElementById("conversationsGroup")
        .removeContact(aSubject.wrappedJSObject);
      if (conv.imContact) {
        const contact = conv.imContact;
        const groupName = (contact.online ? "on" : "off") + "linecontactsGroup";
        document.getElementById(groupName).addContact(contact);
      }
      return;
    }

    if (aTopic == "buddy-authorization-request") {
      aSubject.QueryInterface(Ci.prplIBuddyRequest);
      const authLabel = gChatBundle.formatStringFromName(
        "buddy.authRequest.label",
        [aSubject.userName]
      );
      const value =
        "buddy-auth-request-" + aSubject.account.id + aSubject.userName;
      const acceptButton = {
        accessKey: gChatBundle.GetStringFromName(
          "buddy.authRequest.allow.accesskey"
        ),
        label: gChatBundle.GetStringFromName("buddy.authRequest.allow.label"),
        callback() {
          aSubject.grant();
        },
      };
      const denyButton = {
        accessKey: gChatBundle.GetStringFromName(
          "buddy.authRequest.deny.accesskey"
        ),
        label: gChatBundle.GetStringFromName("buddy.authRequest.deny.label"),
        callback() {
          aSubject.deny();
        },
      };
      const box = this.msgNotificationBar;
      const notification = await box.appendNotification(
        value,
        {
          label: authLabel,
          priority: box.PRIORITY_INFO_HIGH,
        },
        [acceptButton, denyButton]
      );
      notification.closeButton?.remove();
      notification.dismissable = false;
      if (!gChatTab) {
        const tabmail = document.getElementById("tabmail");
        tabmail.openTab("chat", { background: true });
      }
      return;
    }
    if (aTopic == "buddy-authorization-request-canceled") {
      aSubject.QueryInterface(Ci.prplIBuddyRequest);
      const value =
        "buddy-auth-request-" + aSubject.account.id + aSubject.userName;
      const box = this.msgNotificationBar;
      const notification = box.getNotificationWithValue(value);
      if (notification) {
        notification.close();
      }
      return;
    }
    if (aTopic == "buddy-verification-request") {
      aSubject.QueryInterface(Ci.imIIncomingSessionVerification);
      const barLabel = gChatBundle.formatStringFromName(
        "buddy.verificationRequest.label",
        [aSubject.subject]
      );
      const value =
        "buddy-verification-request-" +
        aSubject.account.id +
        "-" +
        aSubject.subject;
      const acceptButton = {
        accessKey: gChatBundle.GetStringFromName(
          "buddy.verificationRequest.allow.accesskey"
        ),
        label: gChatBundle.GetStringFromName(
          "buddy.verificationRequest.allow.label"
        ),
        callback() {
          aSubject
            .verify()
            .then(() => {
              window.openDialog(
                "chrome://messenger/content/chat/verify.xhtml",
                "",
                "chrome,modal,titlebar,centerscreen",
                aSubject
              );
            })
            .catch(error => {
              aSubject.account.ERROR(error);
              aSubject.cancel();
            });
        },
      };
      const denyButton = {
        accessKey: gChatBundle.GetStringFromName(
          "buddy.verificationRequest.deny.accesskey"
        ),
        label: gChatBundle.GetStringFromName(
          "buddy.verificationRequest.deny.label"
        ),
        callback() {
          aSubject.cancel();
        },
      };
      const box = this.msgNotificationBar;
      const notification = await box.appendNotification(
        value,
        {
          label: barLabel,
          priority: box.PRIORITY_INFO_HIGH,
        },
        [acceptButton, denyButton]
      );
      notification.closeButton?.remove();
      notification.dismissable = false;
      if (!gChatTab) {
        const tabmail = document.getElementById("tabmail");
        tabmail.openTab("chat", { background: true });
      }
      return;
    }
    if (aTopic == "buddy-verification-request-canceled") {
      aSubject.QueryInterface(Ci.imIIncomingSessionVerification);
      const value =
        "buddy-verification-request-" +
        aSubject.account.id +
        "-" +
        aSubject.subject;
      const box = this.msgNotificationBar;
      const notification = box.getNotificationWithValue(value);
      if (notification) {
        notification.close();
      }
      return;
    }
    if (aTopic == "conv-authorization-request") {
      aSubject.QueryInterface(Ci.prplIChatRequest);
      const value =
        "conv-auth-request-" + aSubject.account.id + aSubject.conversationName;
      const buttons = [
        {
          "l10n-id": "chat-conv-invite-accept",
          callback() {
            aSubject.grant();
          },
        },
      ];
      if (aSubject.canDeny) {
        buttons.push({
          "l10n-id": "chat-conv-invite-deny",
          callback() {
            aSubject.deny();
          },
        });
      }
      const box = this.msgNotificationBar;
      // Remove the notification when the request is cancelled.
      aSubject.completePromise.catch(() => {
        const notification = box.getNotificationWithValue(value);
        if (notification) {
          notification.close();
        }
      });
      const notification = await box.appendNotification(
        value,
        {
          label: "",
          priority: box.PRIORITY_INFO_HIGH,
        },
        buttons
      );
      document.l10n.setAttributes(
        notification.messageText,
        "chat-conv-invite-label",
        {
          conversation: aSubject.conversationName,
        }
      );
      notification.closeButton?.remove();
      notification.dismissable = false;
      if (!gChatTab) {
        const tabmail = document.getElementById("tabmail");
        tabmail.openTab("chat", { background: true });
      }
      return;
    }
    if (aTopic == "conversation-update-type") {
      // Find conversation in conversation list.
      const contactlistbox = document.getElementById("contactlistbox");
      const convs = document.getElementById("conversationsGroup");
      let convItem = convs.nextElementSibling;
      const updatedConv = aSubject.wrappedJSObject;
      while (
        convItem.conv.target.id !== updatedConv.target.id &&
        convItem.id != "searchResultConv"
      ) {
        convItem = convItem.nextElementSibling;
      }
      if (convItem.conv.target.id !== updatedConv.target.id) {
        // Could not find a matching conversation in the front end.
        return;
      }
      // Update UI conversation associated with components
      if (convItem.convView && convItem.convView.conv !== updatedConv) {
        convItem.convView.changeConversation(updatedConv);
      }
      if (convItem.conv !== updatedConv) {
        convItem.changeConversation(updatedConv);
      } else {
        convItem.update();
      }
      // If the changed conversation is the selected item, make sure
      // we update the UI elements to match the conversation type.
      const selectedItem = contactlistbox.selectedItem;
      if (selectedItem === convItem && selectedItem.convView) {
        this.onListItemSelected();
      }
    }
  },