@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
60 lines (52 loc) • 1.84 kB
text/typescript
import { getActiveClient } from '~/client/api/activeClient';
import { mergeInCache, pullFromCache } from '~/cache/api';
import { createChannel } from '~/channelRepository/api/createChannel';
import { channelLinkedObject } from '~/utils/linkedObject/channelLinkedObject';
import { getChannel } from '~/channelRepository/internalApi/getChannel';
import { constructChannelObject } from '~/channelRepository/utils/constructChannelObject';
/**
* ```js
* import { getStream } from '@amityco/ts-sdk'
* const stream = await getStream('foobar')
* ```
*
* Fetches a {@link Amity.Channel} object linked with a current stream
*
* @param stream {@link Amity.Stream} that has linked live channel
* @returns the associated {@link Amity.Channel<'live'>} object
*
* @category Stream API
* @async
*/
export const getLiveChat = async (
stream: Amity.InternalStream,
): Promise<Amity.Channel<'live'> | undefined> => {
const client = getActiveClient();
client.log('stream/getLiveChat', stream.streamId);
if (stream.channelId) {
const channel = pullFromCache<Amity.StaticInternalChannel>([
'channel',
'get',
stream.channelId,
])?.data;
if (channel) return channelLinkedObject(constructChannelObject(channel));
const { data } = await getChannel(stream.channelId);
return channelLinkedObject(constructChannelObject(data));
}
// No Channel ID
// streamer: create a new live channel
if (stream.userId === client.userId) {
const { data: channel } = await createChannel({
type: 'live',
postId: stream.postId,
videoStreamId: stream.streamId,
});
// Update channelId to stream object in cache
mergeInCache(['stream', 'get', stream.streamId], {
channelId: channel.channelId,
});
return channel;
}
// watcher: return undefined
return undefined;
};