import querystring from 'querystring' import * as Sendbird from './sendbird' import { GetChannelInputType, GetChannelsReturnType, GetMessagesForChannelReturnType, GetChannelsForUserIdInputType, Channel, ChannelUpdateRequest, GetChannelByUrlOptionalParameters, ChannelResource, } from './types' export async function updateChannel({ channelUrl, data }: ChannelUpdateRequest): Promise { if (!channelUrl) { throw new Error('Can\'t update channel without channel url') } if (!data) { throw new Error('Can\'t update channel without data') } return await Sendbird.doPut(`/group_channels/${channelUrl}`, data) } export async function deleteChannel({ channelUrl }: { channelUrl: string }): Promise { if (!channelUrl) { throw new Error('Can\'t update channel without channel url') } return await Sendbird.doDelete(`/group_channels/${channelUrl}`) } export async function getChannels({ token, ...rest }: GetChannelInputType): Promise { return await Sendbird.doGet( `/group_channels?${querystring.stringify({ token: token, ...rest, })}` ) } export async function getChannelsForUserId({ userId, ...rest }: GetChannelsForUserIdInputType): Promise { return await Sendbird.doGet( `/users/${userId}/my_group_channels?${querystring.stringify({ ...rest, })}` ) } export async function getChannelByUrl({ channelUrl, ...rest }: GetChannelByUrlOptionalParameters): Promise { return await Sendbird.doGet( `/group_channels/${channelUrl}?${querystring.stringify({ ...rest, })}` ) } export async function getMessagesForChannel({ channelUrl, messageTs, limit, }: { channelUrl: string messageTs: number limit: number }): Promise { return await Sendbird.doGet( `/group_channels/${channelUrl}/messages?${querystring.stringify({ message_ts: messageTs, next_limit: limit || 200, })}` ) } // Operator ar "admin" users that can delete message and more export async function addOperatorsToChannel({ channelUrl, operatorIds, }: { channelUrl: string operatorIds: string[] }): Promise { return await Sendbird.doPost(`/group_channels/${channelUrl}/operators`, { operator_ids: operatorIds, }) } export async function inviteUsersToChannel({ channelUrl, userIds, }: { channelUrl: string userIds: string[] }): Promise { return await Sendbird.doPost(`/group_channels/${channelUrl}/invite`, { user_ids: userIds }) } export async function unhideChannel({ channelUrl, userId, }: { channelUrl: string userId: string }): Promise { return await Sendbird.doDelete(`/group_channels/${channelUrl}/hide`, { user_id: userId }) } export async function hideChannel({ channelUrl, userId, }: { channelUrl: string userId: string }): Promise { return await Sendbird.doPut(`/group_channels/${channelUrl}/hide`, { user_id: userId }) } export async function createChannel({ name, userIds, }: { name: string userIds: string[] }): Promise { return await Sendbird.doPost('/group_channels', { name, user_ids: userIds }) } export async function removeUsersFromChannel({ userIds, channelUrl, }: { userIds: string[] channelUrl: string }): Promise { return await Sendbird.doPut(`/group_channels/${channelUrl}/leave`, { user_ids: userIds }) }