OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / keet-core-4.14.4-pretty / lib / core / tasks / rooms / leave.js

68 lines · javascript ref:
const { Task: i } = require("../task"),
  t = require("b4a")
module.exports = class extends i {
  constructor(i) {
    super(i)
    const t = this.description.leaveRoom
    ;((this._roomKey = t.roomKey),
      (this._isGossiped = t.isGossiped),
      (this._roomInfo = null),
      (this._identityInfo = null),
      (this._room = null),
      (this._privateProfile = null))
  }
  async _close() {
    ;(this._room && this.keet.internalReleaseRoom(this._room),
      this._privateProfile && this.keet.internalReleaseRoom(this._privateProfile))
  }
  async _load() {
    this.keet.onlineBoot.opened || (await this.keet.onlineBoot.ready())
    const [i, e] = await Promise.all([
      this.local.get("@local/rooms", { roomKey: this._roomKey }),
      this.local.get("@local/identity"),
    ])
    if (this.closing || !i || !e) return !1
    if (
      ((this._roomInfo = i),
      (this._identityInfo = e),
      (this._privateProfile = this.keet.internalOpenExistingRoom(
        this._identityInfo.privateProfileKey,
      )),
      await this._privateProfile.ready(),
      this.closing)
    )
      return !1
    if (this.discovery && this._roomInfo) {
      const i = this._roomInfo.capabilities.discoveryKeyPair.publicKey
      this.discovery.unsubscribe(i)
    }
    if (
      ((this._room = this.keet.internalOpenExistingRoom(this._roomKey)),
      await this._room.ready(),
      this.closing)
    )
      return !1
    let o = !1
    const s = await this._privateProfile.db.find("@keet-private-id/pending-invitations").toArray()
    for (const i of s)
      t.equals(i.mailbox, this._roomInfo.capabilities.mailboxKeyPair.publicKey) && (o = !0)
    return !this.closing && !o
  }
  async _save() {
    const i = await this.keet.internalEnterLocalTransaction()
    try {
      ;(await i.delete("@local/writers", {
        mailbox: this._roomInfo.capabilities.mailboxKeyPair.publicKey,
      }),
        await i.flush())
    } finally {
      await this.keet.internalExitLocalTransaction()
    }
    await this.files.clearAllFiles(this._roomKey, { leaving: !0 })
  }
  async _run() {
    const i = await this._load()
    !this.closing && i && (await this._save())
  }
}