const {
contract: {
hyperschema: { getEnum: e },
},
} = require("../../../../schema/spec"),
{ Task: t } = require("../task"),
i = require("b4a"),
s = e("@keet/room-type"),
o = require("../../sync/updater"),
{ roomsStats: a } = require("../../contract/stats/local")
module.exports = class extends t {
constructor(e) {
super(e)
const t = this.description.createRoom
;((this._key = t.roomKey),
(this._mailbox = t.mailbox),
(this._keyPair = t.keyPair),
(this._genesis = t.genesis),
(this._fastForwardTo = t.fastForwardTo),
(this._encryptionKey = t.encryptionKey),
(this._isPrivateProfile = t.isPrivateProfile),
(this._room = null),
(this._updater = null),
(this._privateProfile = null))
}
async _close() {
;(this._updater && (await this._updater.close()),
this._room && this.keet.internalReleaseRoom(this._room),
this._privateProfile && this.keet.internalReleaseRoom(this._privateProfile))
}
async _save() {
const e = await this.keet.internalEnterLocalTransaction()
try {
const t = await e.get("@local/rooms", this._updater.roomInfo)
;(await e.insert("@local/rooms", this._updater.roomInfo),
await a(e, this._updater.roomInfo, t),
await e.flush())
} finally {
await this.keet.internalExitLocalTransaction()
}
}
_getDmMemberKey() {
if (this._genesis.digest.settings.roomType !== s.DirectMessage) return null
for (const { attestation: e } of this._genesis.initialMembers)
if (!i.equals(e.memberKey, this._genesis.bootstrapDevice.attestation.memberKey))
return e.memberKey
}
_maybeEmitEvents() {
this.keet.emit("room-changed", this._room.id)
}
async _create() {
if ((await this._room.ready(), this.closing)) return
let e = this._genesis.bootstrapDevice,
t = this._genesis.initialMembers
if (!e) {
const i = await this.keet.internalGetIdentityInfo()
if (this.closing) return
;((e = this.keet.internalCreateDeviceUpdate(i, this._room.deviceKey)),
(t = [this.keet.internalGetMemberUpdate(i)]))
}
;(await this._room.dispatch("@keet/genesis", {
...this._genesis,
bootstrapDevice: e,
initialMembers: t,
}),
(this._updater = new o(this.keet, this._room.base.view, this._room.base, null)))
const i = await this._updater.run()
if (!this.closing && i && (await this._save(), !this.closing)) {
if (this._privateProfile) {
const e = this._room.base.getSystemKey(),
t = { key: this._room.base.local.key, length: this._room.base.local.length }
await this._privateProfile.dispatch("@keet-private-id/add-room", {
roomKey: this._room.key,
encryptionKey: this._room.encryptionKey,
mailbox: this._mailbox,
fastForwardTo: e,
initiator: t,
dm: this._getDmMemberKey(),
})
}
this._maybeEmitEvents()
}
}
async _run() {
if (
((this._room = this.keet.internalOpenRoom(this._key, this._encryptionKey, {
fastForwardTo: this._fastForwardTo,
keyPair: this._keyPair,
mailbox: this._mailbox,
})),
!this._isPrivateProfile)
) {
this.keet.onlineBoot.opened || (await this.keet.onlineBoot.ready())
const e = await this.local.get("@local/identity")
this._privateProfile = this.keet.internalOpenExistingRoom(e.privateProfileKey)
}
await this._create()
}
}