UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

56 lines (47 loc) 1.66 kB
import { getActiveClient } from '~/client/api'; import { createEventSubscriber } from '~/core/events'; import { mergeInCache, pullFromCache, queryCache } from '~/cache/api'; /** * ```js * import { onStreamViewerBanned } from '@amityco/ts-sdk' * const dispose = onStreamViewerBanned(stream => { * // ... * }) * ``` * * Fired when a user in channel linked to stream has been banned * * @param callback The function to call when the event was fired * @returns an {@link Amity.Unsubscriber} function to stop listening * * @category Stream Events */ export const onStreamViewerBanned = ( callback: Amity.Listener<Amity.InternalStream>, ): Amity.Unsubscriber => { const client = getActiveClient(); const filter = (payloads: Amity.StreamViewerBanPayload) => { const { list } = payloads; list.forEach(payload => { mergeInCache(['stream', 'get', payload.streamId], { watcherUrl: null }); // Update isDeleted = true in banned user's messages const messageCache = queryCache<Amity.InternalMessage>(['message', 'get'])?.filter( // Check if creator id and user id are internal or external id ({ data }) => data.creatorId === payload.userId, ); // Update isDeleted for each relavant messages messageCache?.forEach(message => { mergeInCache(message.key, { isDeleted: true }); }); }); const stream = pullFromCache<Amity.InternalStream>(['stream', 'get', list[0].streamId])?.data; if (!stream) return; callback(stream); }; return createEventSubscriber( client, 'stream/onStreamViewerDidBan', 'video-streaming.viewerDidBan', filter, ); };