OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-indexing / lib / private-profile / private-mailbox.js

133 lines · javascript ref:
const { PrivateMailboxSchema: e } = require("@holepunchto/keet-core-schemas"),
  { FEATURES: i } = require("@holepunchto/keet-core-api"),
  { AddPrivateMailboxRecordOperation: t, UpdatePrivateMailboxRecordOperation: a } = e.Operations
module.exports = class {
  static Operations = new Set([t.TYPE, a.TYPE])
  constructor(e) {
    ;((this.profile = e),
      (this.privateMailbox = null),
      (this.sentPrivateMailbox = null),
      (this.privateMailboxCounter = null),
      (this.privateMailByMessageId = null),
      (this.privateMailByPublicMail = null),
      (this.dmByMember = null))
  }
  initialize() {
    ;((this.privateMailbox = this.profile.view.collections.privateMailbox),
      (this.sentPrivateMailbox = this.profile.view.collections.sentPrivateMailbox),
      (this.privateMailboxCounter = this.profile.view.indexes.privateMailboxCounter),
      (this.privateMailByMessageId = this.profile.view.indexes.privateMailByMessageId),
      (this.privateMailByPublicMail = this.profile.view.indexes.privateMailByPublicMail),
      (this.dmByMember = this.profile.view.indexes.dmByMember))
  }
  async _indexCounter(e, i, t) {
    i &&
      !e &&
      (await this.privateMailboxCounter.insert(t[2]),
      await this.privateMailByMessageId.insert([i.id.key, i.id.seq], {
        timestamp: t[0],
        roomKey: t[1],
        seq: t[2],
      }))
  }
  async _indexPublicMail(e, i, t) {
    i &&
      i.mailId &&
      !e &&
      (await this.privateMailByPublicMail.insert([t[1], i.mailId.key, i.mailId.seq], {
        timestamp: t[0],
        roomKey: t[1],
        seq: t[2],
      }))
  }
  async _indexReceiverDm(e, i, t) {
    if (
      (!e &&
        i &&
        i.senderKey &&
        i.invitation &&
        (await this.dmByMember.insert(i.senderKey, { id: i.id, roomKey: null })),
      e && i && i.senderKey)
    ) {
      const { id: e, targetRoomKey: t, status: a } = i
      await this.dmByMember.insert(i.senderKey, { id: e, roomKey: t, status: a })
    }
  }
  async _indexSenderDm(e, i, t) {
    ;(!e &&
      i &&
      i.receiverKey &&
      (await this.dmByMember.insert(i.receiverKey, { id: i.id, roomKey: null })),
      e &&
        i &&
        i.receiverKey &&
        i.targetRoomKey &&
        (await this.dmByMember.insert(i.receiverKey, { id: i.id, roomKey: i.targetRoomKey })))
  }
  async _applyAddPrivateMailboxRecordOperation(e, { id: i }) {
    const { value: t } = e,
      a = [t.timestamp, t.roomKey, await this.getNextPrivateMailboxId()],
      { mailId: r } = t,
      s = !!t.receiverKey,
      l = {
        id: i,
        mailId: t.mailId || null,
        seen: s,
        type: t.type || null,
        status: t.status || null,
        senderKey: t.senderKey || null,
        receiverKey: t.receiverKey || null,
        targetRoomKey: t.targetRoomKey || null,
        message: t.message || null,
        invitation: t.invitation || null,
      }
    if (!s) {
      if (await this.dmByMember.get(t.senderKey)) return
      if (await this.privateMailByPublicMail.get([t.roomKey, r.key, r.seq])) return
      return await this.privateMailbox.update(a, l, { upsert: !0 })
    }
    ;(await this.dmByMember.get(t.receiverKey)) ||
      (await this.sentPrivateMailbox.update(a, l, { upsert: !0 }))
  }
  async _applyUpdatePrivateMailboxRecordOperation(e) {
    const { msgId: i, ...t } = e.value,
      a = await this.privateMailByMessageId.get([i.key, i.seq])
    if (!a || !a.value) return
    const r = a.value,
      s = [r.timestamp, r.roomKey, r.seq],
      [l, o] = await Promise.all([this.privateMailbox.get(s), this.sentPrivateMailbox.get(s)]),
      n = l || o,
      d = t.targetRoomKey || n?.value.targetRoomKey,
      u = n?.value.seen || t.seen
    let p = n?.value.status || 3
    3 !== p || (1 !== t.status && 2 !== t.status) || (p = t.status)
    const y = { ...n.value, status: p, seen: u, targetRoomKey: d }
    return l
      ? this.privateMailbox.update(s, y, { upsert: !0 })
      : o
        ? this.sentPrivateMailbox.update(s, y, { upsert: !0 })
        : void 0
  }
  async getNextPrivateMailboxId() {
    const e = await this.privateMailboxCounter.peek({ reverse: !0 })
    return e ? e.key + 1 : 0
  }
  async autobaseApply(e, r) {
    if (!(await this.profile.view.isFeatureSupported(i.DMS))) return null
    switch (e.type) {
      case t.TYPE:
        return this._applyAddPrivateMailboxRecordOperation(e, r)
      case a.TYPE:
        return this._applyUpdatePrivateMailboxRecordOperation(e, r)
    }
  }
  async autobaseIndex({ prev: e, next: i, id: t }) {
    ;(await this._indexCounter(e, i, t),
      await this._indexPublicMail(e, i, t),
      await this._indexReceiverDm(e, i, t))
  }
  async autobaseIndexSent({ prev: e, next: i, id: t }) {
    ;(await this._indexCounter(e, i, t), await this._indexSenderDm(e, i, t))
  }
}