OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / keet-core-4.14.4-pretty / lib / core / migrations / 3 / room.js

113 lines · javascript ref:
const e = require("b4a"),
  i = require("keet-identity-key"),
  { mnemonicToEntropy: t } = require("bip39-mnemonic"),
  a = require("compact-encoding"),
  { ConfigSchema: o } = require("@holepunchto/legacy-keet-core-schemas"),
  { CONFIG: r } = require("@holepunchto/keet-core-api"),
  n = 1711547109544,
  c = "keet-seed-phrase"
function s(e) {
  for (const i of e.peers) if (!(i.remoteLength <= e.signedLength)) return !0
  return !1
}
async function l(e, i) {
  await i.core.ready()
  const t = i.core.findingPeers(),
    a = setTimeout(t, e.isTesting ? 75 : 7500)
  try {
    await i.core.update()
    for (let t = 0; t < 5 && s(i.core); t++)
      (t > 0 && (await new Promise((i) => setTimeout(i, e.isTesting ? 10 : 1e3))), await i.update())
  } finally {
    ;(clearTimeout(a), t())
  }
}
async function y(i, n, s, y, f) {
  if (i.debugForceMigrationFailure && e.equals(i.debugForceMigrationFailure, n.key))
    throw new Error("Migration failed")
  if ((await l(i, n.base), !(await n.db.get("@keet/system-digest")))) {
    let e = null
    if (f) {
      const i = []
      for (const e of f.blockedMembers) i.push(e)
      const t = []
      for (const { roomKey: e, v3: i } of y) {
        const { seed: a, encryptionKey: o, initiator: r, fastForwardTo: n } = i,
          c = { roomKey: e, encryptionKey: o, seed: a, initiator: r, fastForwardTo: n }
        t.push(c)
      }
      e = { sharedRooms: t, blockedContacts: i }
    }
    ;(await n.dispatch("@keet/upgrade", { version: 9 }, { dryRun: !1, optimistic: !0 }),
      e && (await n.dispatch("@keet-private-id/run-v3-migration", e, { dryRun: !1 })))
  }
  const m = await i.local.get("@local/rooms", { roomKey: n.key })
  if (
    (m.digest &&
      m.dm &&
      m.dm.length &&
      (m.config.title ||
        (await i.privateProfile.dispatch("@keet-private-id/remove-contact", {
          memberKey: m.dm[0],
        }))),
    !f)
  )
    return void (await n.internalEnsureProfileUpdated())
  const [u, p, w] = await Promise.all([
      n.db.findOne("@keet/members"),
      n.base.view.legacy.bee.get(r.ID_RECOVERY_PHRASE, o.Encoding),
      n.base.getUserData(c),
    ]),
    g = p ? a.decode(a.string, p.value) : w && e.toString(w, "utf-8")
  if (u) {
    const e = { ...u.profile, mailbox: s.mailboxKeyPair.publicKey }
    await i.internalUpdateProfile({ profile: e }, { opening: !0 })
  }
  if (g) {
    const e = await i.internalEnterLocalTransaction()
    try {
      const i = await e.get("@local/identity")
      ;((i.entropy = t(g)), await e.insert("@local/identity", i), await e.flush())
    } finally {
      await i.internalExitLocalTransaction()
    }
    await d(i, n, g, s)
  }
}
async function d(e, t, a, o) {
  const r = await i.from({ mnemonic: a })
  if ((await e.internalCreateDiscoveryCore(r), e.discoveryCore.length > 0)) {
    const i = Math.floor((Date.now() - n) / 1e3)
    await e.discoveryCore.truncate(e.discoveryCore.length, i)
  }
  const c = o.mailboxKeyPair.publicKey
  ;(await e.discoveryCore.append({ version: 1, timestamp: Date.now(), key: t.key, mailbox: c }),
    await t.dispatch("@keet-private-id/update-identity", {
      identity: { publicKey: r.identityPublicKey, discoveryCoreKey: e.discoveryCore.key },
    }))
}
module.exports = async function (
  e,
  i,
  { capabilities: t, encryptionKey: a, privateProfile: o },
  { migratingRooms: r },
) {
  const n = await e.local
    .find("@local/drive-files-by-room", { gte: { roomKey: i }, lte: { roomKey: i } })
    .toArray()
  for (const e of n) e.encryptionKey = a
  const c = await e.internalEnterLocalTransaction()
  try {
    for (const e of n) await c.insert("@local/drive-files", e)
    await c.flush()
  } finally {
    await e.internalExitLocalTransaction()
  }
  const s = e.internalOpenRoom(i, a, { mailbox: t.mailboxKeyPair.publicKey, isPrivateProfile: !!o })
  try {
    ;(await y(e, s, t, r, o), await e.blindPeering.addAutobase(s.base))
  } finally {
    e.internalReleaseRoom(s)
  }
}