@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
64 lines (55 loc) • 1.86 kB
text/typescript
import { getActiveClient } from '~/client/api/activeClient';
import { ingestInCache } from '~/cache/api/ingestInCache';
import { prepareChannelPayload } from '../utils';
import { constructChannelObject } from '../utils/constructChannelObject';
/* begin_public_function
id: channel.create
*/
/**
* ```js
* import { createChannel } from '@amityco/ts-sdk-react-native'
* const created = await createChannel({ displayName: 'foobar' })
* ```
*
* Creates an {@link Amity.Channel}
*
* @param bundle The data necessary to create a new {@link Amity.Channel}
* @returns The newly created {@link Amity.Channel}
*
* @category Channel API
* @async
*/
export const createChannel = async <T extends Amity.ChannelType>(
bundle: {
type: T;
userIds?: Amity.InternalUser['userId'][];
// For live channel linked with a livestream post
videoStreamId?: string;
postId?: string;
} & Pick<Amity.Channel<T>, 'displayName' | 'avatarFileId' | 'tags' | 'metadata' | 'isPublic'>,
): Promise<Amity.Cached<Amity.Channel<T>>> => {
const client = getActiveClient();
client.log('user/createChannel', bundle);
let payload;
if (bundle?.type === 'conversation') {
payload = await client.http.post<Amity.ChannelPayload>('/api/v3/channels/conversation', {
...bundle,
isDistinct: true,
});
} else {
const { isPublic, ...restParams } = bundle;
payload = await client.http.post<Amity.ChannelPayload>('/api/v3/channels', {
...restParams,
isPublic: bundle?.type === 'community' ? isPublic : undefined,
});
}
const data = await prepareChannelPayload(payload.data);
const cachedAt = client.cache && Date.now();
if (client.cache) ingestInCache(data, { cachedAt });
const { channels } = data;
return {
data: constructChannelObject(channels[0]),
cachedAt,
};
};
/* end_public_function */