@towns-protocol/sdk
Version:
For more details, visit the following resources:
49 lines • 2.34 kB
JavaScript
import { removeCommon } from './utils';
import { check } from '@towns-protocol/dlog';
import { isDefined } from './check';
export class StreamStateView_Members_Solicitations {
streamId;
snapshotSigBundle;
snapshotEventId;
constructor(streamId) {
this.streamId = streamId;
}
initSolicitations(eventHashStr, members, sigBundle, encryptionEmitter) {
this.snapshotSigBundle = sigBundle;
this.snapshotEventId = eventHashStr;
encryptionEmitter?.emit('initKeySolicitations', this.streamId, eventHashStr, members.map((member) => ({
userId: member.userId,
userAddress: member.userAddress,
solicitations: member.solicitations,
})), sigBundle);
}
applySolicitation(user, eventId, solicitation, sigBundle, encryptionEmitter) {
user.solicitations = user.solicitations.filter((x) => x.deviceKey !== solicitation.deviceKey);
const newSolicitation = {
deviceKey: solicitation.deviceKey,
fallbackKey: solicitation.fallbackKey,
isNewDevice: solicitation.isNewDevice,
sessionIds: solicitation.sessionIds.toSorted(),
};
user.solicitations.push(newSolicitation);
encryptionEmitter?.emit('newKeySolicitation', this.streamId, eventId, user.userId, user.userAddress, newSolicitation, sigBundle);
}
applyFulfillment(user, fulfillment, encryptionEmitter) {
check(isDefined(this.snapshotSigBundle), 'snapshotSigBundle not set');
check(isDefined(this.snapshotEventId), 'snapshotEventId not set');
const index = user.solicitations.findIndex((x) => x.deviceKey === fulfillment.deviceKey);
if (index === undefined || index === -1) {
return;
}
const prev = user.solicitations[index];
const newEvent = {
deviceKey: prev.deviceKey,
fallbackKey: prev.fallbackKey,
isNewDevice: false,
sessionIds: [...removeCommon(prev.sessionIds, fulfillment.sessionIds.toSorted())],
};
user.solicitations[index] = newEvent;
encryptionEmitter?.emit('updatedKeySolicitation', this.streamId, this.snapshotEventId, user.userId, user.userAddress, newEvent, this.snapshotSigBundle);
}
}
//# sourceMappingURL=streamStateView_Members_Solicitations.js.map