OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-indexing / plugins / private-profile / apply.js

61 lines · javascript ref:
const e = require("@holepunchto/hyperindex"),
  t = require("sub-encoder"),
  i = require("compact-encoding"),
  o = require("safety-catch"),
  { PluginSchema: a, PrivateProfileSchema: s } = require("@holepunchto/keet-core-schemas"),
  { PluginOperation: c } = a.Operations,
  {
    AddDeviceAttestationOperation: r,
    AddDeviceKeyOperation: n,
    RemoveRoomOperation: p,
    AddRoomOperation: d,
  } = s.Operations
module.exports = class {
  static PLUGIN_NAME = "@holepunchto/private-profile"
  constructor(i, o) {
    ;((this.view = i),
      (this.prefix = o),
      (this.dex = new e(i.bee, { writable: !0, sub: new t(o) })),
      this.dex.createCollections([
        s.AttestationsCollection,
        s.DeviceKeysCollection,
        s.RoomsCollection,
      ]),
      (this.deviceAttestations = this.dex.collections[0]),
      (this.deviceKeys = this.dex.collections[1]),
      (this.rooms = this.dex.collections[2]))
  }
  async _applyAddDeviceAttestationOperation({ devicePublicKey: e, attestation: t }, i) {
    ;(await this.deviceAttestations.get(e)) ||
      (await this.deviceAttestations.update(e, t, { upsert: !0 }))
  }
  async _applyAddDeviceKeyOperation({ devicePublicKey: e }, t) {
    ;(await this.deviceKeys.get(t.id.key)) ||
      (await this.deviceKeys.update(t.id.key, e, { upsert: !0 }))
  }
  async _applyAddRoomOperation(e) {
    await this.rooms.update(e.key, { key: e.key }, { upsert: !0 })
  }
  async _applyRemoveRoomOperation(e) {
    try {
      await this.rooms.delete(e.key)
    } catch (e) {
      o(e)
    }
  }
  async apply(e, t) {
    if (e.type !== c.TYPE || e.value.name !== this.constructor.PLUGIN_NAME) return
    const { type: o, value: a } = e.value.value
    switch (o) {
      case r.TYPE:
        return this._applyAddDeviceAttestationOperation(i.decode(r, a), t)
      case n.TYPE:
        return this._applyAddDeviceKeyOperation(i.decode(n, a), t)
      case d.TYPE:
        return this._applyAddRoomOperation(i.decode(d, a), t)
      case p.TYPE:
        return this._applyRemoveRoomOperation(i.decode(p, a), t)
    }
  }
}