UNPKG

@river-build/sdk

Version:

For more details, visit the following resources:

147 lines 7.01 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 { isChannelStreamId, makeDefaultChannelStreamId, makeUniqueChannelStreamId, } from '../../../id'; import { PersistedObservable, persistedObservable } from '../../../observable/persistedObservable'; import { LoadPriority } from '../../../store/store'; import { Channel } from './channel'; import { Members } from '../../members/members'; const logger = dlogger('csb:space'); let Space = class Space extends PersistedObservable { riverConnection; spaceDapp; channels; members; constructor(id, riverConnection, store, spaceDapp) { super({ id, channelIds: [], initialized: false }, store, LoadPriority.high); this.riverConnection = riverConnection; this.spaceDapp = spaceDapp; this.channels = { [makeDefaultChannelStreamId(id)]: new Channel(makeDefaultChannelStreamId(id), id, riverConnection, spaceDapp, store), }; 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('spaceChannelCreated', this.onSpaceChannelCreated); client.on('spaceChannelDeleted', this.onSpaceChannelDeleted); client.on('spaceChannelUpdated', this.onSpaceChannelUpdated); return () => { client.off('streamInitialized', this.onStreamInitialized); client.off('spaceChannelCreated', this.onSpaceChannelCreated); client.off('spaceChannelDeleted', this.onSpaceChannelDeleted); client.off('spaceChannelUpdated', this.onSpaceChannelUpdated); }; }); if (!this.data.metadata) { // todo aellis this needs retries and batching this.spaceDapp .getSpaceInfo(this.data.id) .then((spaceInfo) => { this.setData({ metadata: spaceInfo }); }) .catch((error) => { logger.error('getSpaceInfo error', error); }); } } /** Joins the space. * @param signer - The signer to use to join the space. * @param opts - Additional options for the join. */ async join(signer, opts) { const spaceId = this.data.id; if (opts?.skipMintMembership !== true) { const { issued } = await this.spaceDapp.joinSpace(spaceId, this.riverConnection.userId, signer); logger.log('joinSpace transaction', issued); } await this.riverConnection.login({ spaceId }); await this.riverConnection.call(async (client) => { await client.joinStream(spaceId); await client.joinStream(makeDefaultChannelStreamId(spaceId)); }); } /** Creates a channel in the space. * @param channelName - The name of the channel. * @param signer - The signer to use to create the channel. * @param opts - Additional options for the channel creation. * @returns The `channelId` of the created channel. */ async createChannel(channelName, signer, opts) { const spaceId = this.data.id; const channelId = makeUniqueChannelStreamId(spaceId); const roles = await this.spaceDapp.getRoles(spaceId); const tx = await this.spaceDapp.createChannel(spaceId, channelName, '', channelId, roles.filter((role) => role.name !== 'Owner').map((role) => role.roleId), signer); const receipt = await tx.wait(); logger.log('createChannel receipt', receipt); await this.riverConnection.call((client) => client.createChannel(spaceId, channelName, opts?.topic ?? '', channelId)); return channelId; } /** Gets a channel by its id. * @param channelId - The `channelId` of the channel. * @returns The {@link Channel} model. */ getChannel(channelId) { check(isChannelStreamId(channelId), 'channelId is not a channel stream id'); if (!this.channels[channelId]) { this.channels[channelId] = new Channel(channelId, this.data.id, this.riverConnection, this.spaceDapp, this.store); } return this.channels[channelId]; } /** Gets the default channel in the space. * Every space has a default channel. * @returns The {@link Channel} model. */ getDefaultChannel() { return this.channels[makeDefaultChannelStreamId(this.data.id)]; } onStreamInitialized = (streamId) => { if (this.data.id === streamId) { const stream = this.riverConnection.client?.stream(streamId); check(isDefined(stream), 'stream is not defined'); const channelIds = [...stream.view.spaceContent.spaceChannelsMetadata.keys()]; for (const channelId of channelIds) { if (!this.channels[channelId]) { this.channels[channelId] = new Channel(channelId, this.data.id, this.riverConnection, this.spaceDapp, this.store); } } this.setData({ initialized: true, channelIds: channelIds }); } }; onSpaceChannelCreated = (streamId, channelId) => { if (streamId === this.data.id) { if (!this.channels[channelId]) { this.channels[channelId] = new Channel(channelId, this.data.id, this.riverConnection, this.spaceDapp, this.store); } if (!this.data.channelIds.includes(channelId)) { this.setData({ channelIds: [...this.data.channelIds, channelId] }); } } }; onSpaceChannelDeleted = (streamId, channelId) => { if (streamId === this.data.id) { delete this.channels[channelId]; this.setData({ channelIds: this.data.channelIds.filter((id) => id !== channelId) }); } }; onSpaceChannelUpdated = (streamId, _channelId, _updatedAtEventNum) => { if (streamId === this.data.id) { // refetch the channel data from on chain } }; }; Space = __decorate([ persistedObservable({ tableName: 'space' }) ], Space); export { Space }; //# sourceMappingURL=space.js.map