@towns-protocol/sdk
Version:
For more details, visit the following resources:
77 lines • 3.27 kB
JavaScript
import { bin_toHexString, dlog, check } from '@towns-protocol/dlog';
import { toDecryptedContent } from './encryptedContentTypes';
// channel metadata is only used by gdms, could be moved back into the _GDMChannel helper
export class StreamStateView_ChannelMetadata {
gdmStreamsView;
log = dlog('csb:streams:channel_metadata');
streamId;
// named channelProperties for backwards compatibility
get channelProperties() {
return this.gdmStreamModel.metadata;
}
get metadataEventId() {
return this.gdmStreamModel.metadataEventId;
}
get gdmStreamModel() {
return this.gdmStreamsView.get(this.streamId);
}
constructor(streamId, gdmStreamsView) {
this.gdmStreamsView = gdmStreamsView;
this.streamId = streamId;
}
applySnapshot(encryptedChannelProperties, cleartexts, encryptionEmitter) {
if (!encryptedChannelProperties.data) {
return;
}
const eventId = bin_toHexString(encryptedChannelProperties.eventHash);
const cleartext = cleartexts?.[eventId];
this.gdmStreamsView.setLatestMetadataEventId(this.streamId, eventId);
this.decryptPayload(encryptedChannelProperties.data, eventId, cleartext, encryptionEmitter);
}
appendEvent(event, cleartext, emitter) {
check(event.remoteEvent.event.payload.case === 'gdmChannelPayload');
check(event.remoteEvent.event.payload.value.content.case === 'channelProperties');
const payload = event.remoteEvent.event.payload.value.content.value;
this.gdmStreamsView.setLatestMetadataEventId(this.streamId, event.hashStr);
this.decryptPayload(payload, event.hashStr, cleartext, emitter);
}
prependEvent(_event, _cleartext, _emitter) {
// conveyed in snapshot
}
onDecryptedContent(eventId, content, stateEmitter) {
this.handleDecryptedContent(eventId, content, stateEmitter);
}
decryptPayload(payload, eventId, cleartext, encryptionEmitter) {
if (cleartext) {
const decryptedContent = toDecryptedContent('channelProperties', payload.version, cleartext);
this.handleDecryptedContent(eventId, decryptedContent, encryptionEmitter);
}
else {
encryptionEmitter?.emit('newEncryptedContent', this.streamId, eventId, {
kind: 'channelProperties',
content: payload,
});
}
}
handleDecryptedContent(eventId, content, emitter) {
if (content.kind === 'channelProperties') {
if (!this.gdmStreamModel.metadataEventId ||
!this.gdmStreamModel.metadata ||
this.gdmStreamModel.latestMetadataEventId === eventId) {
this.gdmStreamsView.setMetadata(this.streamId, content.content, eventId);
emitter?.emit('streamChannelPropertiesUpdated', this.streamId);
}
else {
this.log('channelProperties eventId mismatch', {
eventId,
content,
gdmStreamModel: this.gdmStreamModel,
});
}
}
else {
check(false);
}
}
}
//# sourceMappingURL=streamStateView_ChannelMetadata.js.map