UNPKG

@river-build/sdk

Version:

For more details, visit the following resources:

140 lines 6.12 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { check, dlogger } from '@river-build/dlog'; import { isDefined } from '../../../check'; import { PersistedObservable, persistedObservable } from '../../../observable/persistedObservable'; import { LoadPriority } from '../../../store/store'; import { Members } from '../../members/members'; import { MessageTimeline } from '../../timeline/timeline'; const logger = dlogger('csb:dm'); let Dm = class Dm extends PersistedObservable { riverConnection; timeline; members; constructor(id, riverConnection, store) { super({ id, isJoined: false, initialized: false }, store, LoadPriority.high); this.riverConnection = riverConnection; this.timeline = new MessageTimeline(id, riverConnection.userId, riverConnection); this.members = new Members(id, riverConnection, store); } onLoaded() { this.riverConnection.registerView((client) => { if (client.streams.has(this.data.id) && client.streams.get(this.data.id)?.view.isInitialized) { this.onStreamInitialized(this.data.id); } client.on('streamInitialized', this.onStreamInitialized); client.on('streamNewUserJoined', this.onStreamUserJoined); client.on('streamUserLeft', this.onStreamUserLeft); return () => { client.off('streamInitialized', this.onStreamInitialized); client.off('streamNewUserJoined', this.onStreamUserJoined); client.off('streamUserLeft', this.onStreamUserLeft); }; }); } async sendMessage(message, options) { const channelId = this.data.id; const result = await this.riverConnection.withStream(channelId).call((client) => { return client.sendChannelMessage_Text(channelId, { threadId: options?.threadId, threadPreview: options?.threadId ? '🙉' : undefined, replyId: options?.replyId, replyPreview: options?.replyId ? '🙈' : undefined, content: { body: message, mentions: options?.mentions ?? [], attachments: options?.attachments ?? [], }, }); }); return result; } async pin(eventId) { const channelId = this.data.id; const result = await this.riverConnection .withStream(channelId) .call((client) => client.pin(channelId, eventId)); return result; } async unpin(eventId) { const channelId = this.data.id; const result = await this.riverConnection .withStream(channelId) .call((client) => client.unpin(channelId, eventId)); return result; } async sendReaction(refEventId, reaction) { const channelId = this.data.id; const eventId = await this.riverConnection.call((client) => client.sendChannelMessage_Reaction(channelId, { reaction, refEventId, })); return eventId; } /** Redacts your own event. * @param eventId - The event id of the message to redact * @param reason - The reason for the redaction */ async redact(eventId, reason) { const channelId = this.data.id; const result = await this.riverConnection.withStream(channelId).call((client, stream) => { const event = stream.view.events.get(eventId); if (!event) { throw new Error(`ref event not found: ${eventId}`); } if (event.remoteEvent?.creatorUserId !== this.riverConnection.userId) { throw new Error(`You can only redact your own messages: ${eventId} - userId: ${this.riverConnection.userId}`); } return client.sendChannelMessage_Redaction(channelId, { refEventId: eventId, reason, }); }); return result; } /** Redacts any message as an admin. * @param eventId - The event id of the message to redact */ async adminRedact(eventId) { const channelId = this.data.id; const result = await this.riverConnection .withStream(channelId) .call((client) => client.redactMessage(channelId, eventId)); return result; } onStreamInitialized = (streamId) => { if (this.data.id === streamId) { logger.log('Dm stream initialized', streamId); const stream = this.riverConnection.client?.stream(streamId); check(isDefined(stream), 'stream is not defined'); const view = stream.view.dmChannelContent; const hasJoined = stream.view.getMembers().isMemberJoined(this.riverConnection.userId); this.setData({ initialized: true, isJoined: hasJoined, metadata: view.getChannelMetadata()?.channelProperties, }); this.timeline.initialize(stream); } }; onStreamUserJoined = (streamId, userId) => { if (streamId === this.data.id && userId === this.riverConnection.userId) { this.setData({ isJoined: true }); } }; onStreamUserLeft = (streamId, userId) => { if (streamId === this.data.id && userId === this.riverConnection.userId) { this.setData({ isJoined: false }); } }; }; Dm = __decorate([ persistedObservable({ tableName: 'dm' }) ], Dm); export { Dm }; //# sourceMappingURL=dm.js.map