// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import {buildQueryString} from '@utils/helpers'; import {PER_PAGE_DEFAULT} from './constants'; import type ClientBase from './base'; export interface ClientChannelsMix { getAllChannels: (page?: number, perPage?: number, notAssociatedToGroup?: string, excludeDefaultChannels?: boolean, includeTotalCount?: boolean) => Promise; createChannel: (channel: Channel) => Promise; createDirectChannel: (userIds: string[]) => Promise; createGroupChannel: (userIds: string[]) => Promise; deleteChannel: (channelId: string) => Promise; unarchiveChannel: (channelId: string) => Promise; updateChannel: (channel: Channel) => Promise; convertChannelToPrivate: (channelId: string) => Promise; updateChannelPrivacy: (channelId: string, privacy: any) => Promise; patchChannel: (channelId: string, channelPatch: Partial) => Promise; updateChannelNotifyProps: (props: ChannelNotifyProps & {channel_id: string; user_id: string}) => Promise; setChannelAutotranslation: (channelId: string, enabled: boolean) => Promise; setMyChannelAutotranslation: (channelId: string, enabled: boolean) => Promise; getChannel: (channelId: string, groupLabel?: RequestGroupLabel) => Promise; getChannelByName: (teamId: string, channelName: string, includeDeleted?: boolean) => Promise; getChannelByNameAndTeamName: (teamName: string, channelName: string, includeDeleted?: boolean) => Promise; getChannels: (teamId: string, page?: number, perPage?: number) => Promise; getArchivedChannels: (teamId: string, page?: number, perPage?: number) => Promise; getSharedChannels: (teamId: string, page?: number, perPage?: number) => Promise; getMyChannels: (teamId: string, includeDeleted?: boolean, lastDeleteAt?: number, groupLabel?: RequestGroupLabel) => Promise; getMyChannelMember: (channelId: string) => Promise; getMyChannelMembers: (teamId: string, groupLabel?: RequestGroupLabel) => Promise; getChannelMembers: (channelId: string, page?: number, perPage?: number) => Promise; getChannelTimezones: (channelId: string) => Promise; getChannelMember: (channelId: string, userId: string, groupLabel?: RequestGroupLabel) => Promise; getChannelMembersByIds: (channelId: string, userIds: string[]) => Promise; addToChannel: (userId: string, channelId: string, postRootId?: string) => Promise; removeFromChannel: (userId: string, channelId: string) => Promise; getChannelStats: (channelId: string, groupLabel?: RequestGroupLabel) => Promise; getChannelMemberCountsByGroup: (channelId: string, includeTimezones: boolean) => Promise; getChannelAccessControlAttributes: (channelId: string) => Promise; viewMyChannel: (channelId: string, prevChannelId?: string, groupLabel?: RequestGroupLabel) => Promise; autocompleteChannels: (teamId: string, name: string) => Promise; autocompleteChannelsForSearch: (teamId: string, name: string) => Promise; searchChannels: (teamId: string, term: string) => Promise; searchArchivedChannels: (teamId: string, term: string) => Promise; searchAllChannels: (term: string, teamIds: string[], archivedOnly?: boolean) => Promise; updateChannelMemberSchemeRoles: (channelId: string, userId: string, isSchemeUser: boolean, isSchemeAdmin: boolean) => Promise; getMemberInChannel: (channelId: string, userId: string) => Promise; getGroupMessageMembersCommonTeams: (channelId: string) => Promise; convertGroupMessageToPrivateChannel: (channelId: string, teamId: string, displayName: string, name: string) => Promise; getAllChannelsFromAllTeams: (lastDeleteAt: number, includeDeleted: boolean, groupLabel?: RequestGroupLabel) => Promise; getAllMyChannelMembersFromAllTeams: (page: number, perPage: number, groupLabel?: RequestGroupLabel) => Promise; } const ClientChannels = >(superclass: TBase) => class extends superclass { getAllChannels = async (page = 0, perPage = PER_PAGE_DEFAULT, notAssociatedToGroup = '', excludeDefaultChannels = false, includeTotalCount = false) => { const queryData = { page, per_page: perPage, not_associated_to_group: notAssociatedToGroup, exclude_default_channels: excludeDefaultChannels, include_total_count: includeTotalCount, }; return this.doFetch( `${this.getChannelsRoute()}${buildQueryString(queryData)}`, {method: 'get'}, ); }; getAllChannelsFromAllTeams = async (lastDeleteAt: number, includeDeleted: boolean, groupLabel?: RequestGroupLabel) => { const queryData = { last_delete_at: lastDeleteAt, include_deleted: includeDeleted, }; return this.doFetch( `${this.getUserRoute('me')}/channels${buildQueryString(queryData)}`, {method: 'get', groupLabel}, ); }; getAllMyChannelMembersFromAllTeams = async (page: number, perPage: number, groupLabel?: RequestGroupLabel) => { const queryData = { page, per_page: perPage, }; return this.doFetch( `${this.getUserRoute('me')}/channel_members${buildQueryString(queryData)}`, {method: 'get', groupLabel}, ); }; createChannel = async (channel: Channel) => { return this.doFetch( `${this.getChannelsRoute()}`, {method: 'post', body: channel}, ); }; createDirectChannel = async (userIds: string[]) => { return this.doFetch( `${this.getChannelsRoute()}/direct`, {method: 'post', body: userIds}, ); }; createGroupChannel = async (userIds: string[]) => { return this.doFetch( `${this.getChannelsRoute()}/group`, {method: 'post', body: userIds}, ); }; deleteChannel = async (channelId: string) => { return this.doFetch( `${this.getChannelRoute(channelId)}`, {method: 'delete'}, ); }; unarchiveChannel = async (channelId: string) => { return this.doFetch( `${this.getChannelRoute(channelId)}/restore`, {method: 'post'}, ); }; updateChannel = async (channel: Channel) => { return this.doFetch( `${this.getChannelRoute(channel.id)}`, {method: 'put', body: channel}, ); }; convertChannelToPrivate = async (channelId: string) => { this.updateChannelPrivacy(channelId, 'P'); }; updateChannelPrivacy = async (channelId: string, privacy: any) => { return this.doFetch( `${this.getChannelRoute(channelId)}/privacy`, {method: 'put', body: {privacy}}, ); }; patchChannel = async (channelId: string, channelPatch: ChannelPatch) => { return this.doFetch( `${this.getChannelRoute(channelId)}/patch`, {method: 'put', body: channelPatch}, ); }; updateChannelNotifyProps = async (props: ChannelNotifyProps & {channel_id: string; user_id: string}) => { return this.doFetch( `${this.getChannelMemberRoute(props.channel_id, props.user_id)}/notify_props`, {method: 'put', body: props}, ); }; setChannelAutotranslation = async (channelId: string, enabled: boolean) => { return this.patchChannel(channelId, {autotranslation: enabled}); }; setMyChannelAutotranslation = async (channelId: string, enabled: boolean) => { return this.doFetch( `${this.getChannelMemberRoute(channelId, 'me')}/autotranslation`, {method: 'put', body: {autotranslation_disabled: !enabled}}, ); }; getChannel = async (channelId: string, groupLabel?: RequestGroupLabel) => { return this.doFetch( this.getChannelRoute(channelId), {method: 'get', groupLabel}, ); }; getChannelByName = async (teamId: string, channelName: string, includeDeleted = false) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/name/${channelName}${buildQueryString({ include_deleted: includeDeleted, })}`, {method: 'get'}, ); }; getChannelByNameAndTeamName = async (teamName: string, channelName: string, includeDeleted = false) => { return this.doFetch( `${this.getTeamNameRoute(teamName)}/channels/name/${channelName}?include_deleted=${includeDeleted}`, {method: 'get'}, ); }; getChannels = async (teamId: string, page = 0, perPage = PER_PAGE_DEFAULT) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels${buildQueryString({ page, per_page: perPage, })}`, {method: 'get'}, ); }; getArchivedChannels = async (teamId: string, page = 0, perPage = PER_PAGE_DEFAULT) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/deleted${buildQueryString({ page, per_page: perPage, })}`, {method: 'get'}, ); }; getSharedChannels = async (teamId: string, page = 0, perPage = PER_PAGE_DEFAULT) => { return this.doFetch( `${this.getSharedChannelsRoute()}/${teamId}${buildQueryString({page, per_page: perPage})}`, {method: 'get'}, ); }; getMyChannels = async (teamId: string, includeDeleted = false, since = 0, groupLabel?: RequestGroupLabel) => { return this.doFetch( `${this.getUserRoute('me')}/teams/${teamId}/channels${buildQueryString({ include_deleted: includeDeleted, last_delete_at: since, })}`, {method: 'get', groupLabel}, ); }; getMyChannelMember = async (channelId: string) => { return this.doFetch( `${this.getChannelMemberRoute(channelId, 'me')}`, {method: 'get'}, ); }; getMyChannelMembers = async (teamId: string, groupLabel?: RequestGroupLabel) => { return this.doFetch( `${this.getUserRoute('me')}/teams/${teamId}/channels/members`, {method: 'get', groupLabel}, ); }; getChannelMembers = async (channelId: string, page = 0, perPage = PER_PAGE_DEFAULT) => { return this.doFetch( `${this.getChannelMembersRoute(channelId)}${buildQueryString({page, per_page: perPage})}`, {method: 'get'}, ); }; getChannelTimezones = async (channelId: string) => { return this.doFetch( `${this.getChannelRoute(channelId)}/timezones`, {method: 'get'}, ); }; getChannelMember = async (channelId: string, userId: string, groupLabel?: RequestGroupLabel) => { return this.doFetch( `${this.getChannelMemberRoute(channelId, userId)}`, {method: 'get', groupLabel}, ); }; getChannelMembersByIds = async (channelId: string, userIds: string[]) => { return this.doFetch( `${this.getChannelMembersRoute(channelId)}/ids`, {method: 'post', body: userIds}, ); }; addToChannel = async (userId: string, channelId: string, postRootId = '') => { const member = {user_id: userId, channel_id: channelId, post_root_id: postRootId}; return this.doFetch( `${this.getChannelMembersRoute(channelId)}`, {method: 'post', body: member}, ); }; removeFromChannel = async (userId: string, channelId: string) => { return this.doFetch( `${this.getChannelMemberRoute(channelId, userId)}`, {method: 'delete'}, ); }; getChannelStats = async (channelId: string, groupLabel?: RequestGroupLabel) => { return this.doFetch( `${this.getChannelRoute(channelId)}/stats`, {method: 'get', groupLabel}, ); }; getChannelMemberCountsByGroup = async (channelId: string, includeTimezones: boolean) => { return this.doFetch( `${this.getChannelRoute(channelId)}/member_counts_by_group?include_timezones=${includeTimezones}`, {method: 'get'}, ); }; getChannelAccessControlAttributes = async (channelId: string) => { return this.doFetch( `${this.getChannelRoute(channelId)}/access_control/attributes`, {method: 'get'}, ); }; viewMyChannel = async (channelId: string, prevChannelId?: string, groupLabel?: RequestGroupLabel) => { // collapsed_threads_supported is not based on user preferences but to know if "CLIENT" supports CRT const data = {channel_id: channelId, prev_channel_id: prevChannelId, collapsed_threads_supported: true}; return this.doFetch( `${this.getChannelsRoute()}/members/me/view`, {method: 'post', body: data, groupLabel}, ); }; autocompleteChannels = async (teamId: string, name: string) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/autocomplete${buildQueryString({name})}`, {method: 'get'}, ); }; autocompleteChannelsForSearch = async (teamId: string, name: string) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/search_autocomplete${buildQueryString({name})}`, {method: 'get'}, ); }; searchChannels = async (teamId: string, term: string) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/search`, {method: 'post', body: {term}}, ); }; searchArchivedChannels = async (teamId: string, term: string) => { return this.doFetch( `${this.getTeamRoute(teamId)}/channels/search_archived`, {method: 'post', body: {term}}, ); }; searchAllChannels = async (term: string, teamIds: string[], archivedOnly = false) => { const queryParams = {include_deleted: false, system_console: false, exclude_default_channels: false}; const body = { term, team_ids: teamIds, deleted: archivedOnly, exclude_default_channels: true, exclude_group_constrained: true, public: true, private: false, }; return this.doFetch( `${this.getChannelsRoute()}/search${buildQueryString(queryParams)}`, {method: 'post', body}, ); }; // Update a channel member's scheme_admin/scheme_user properties. Typically // this should either be scheme_admin=false, scheme_user=true for ordinary // channel member, or scheme_admin=true, scheme_user=true for a channel // admin. updateChannelMemberSchemeRoles = (channelId: string, userId: string, isSchemeUser: boolean, isSchemeAdmin: boolean) => { const body = {scheme_user: isSchemeUser, scheme_admin: isSchemeAdmin}; return this.doFetch( `${this.getChannelMembersRoute(channelId)}/${userId}/schemeRoles`, {method: 'put', body}, ); }; getMemberInChannel = (channelId: string, userId: string) => { return this.doFetch( `${this.getChannelMembersRoute(channelId)}/${userId}`, {method: 'get'}, ); }; getGroupMessageMembersCommonTeams = (channelId: string) => { return this.doFetch( `${this.getChannelRoute(channelId)}/common_teams`, {method: 'get'}, ); }; convertGroupMessageToPrivateChannel = (channelId: string, teamId: string, displayName: string, name: string) => { const body = { channel_id: channelId, team_id: teamId, display_name: displayName, name, }; return this.doFetch( `${this.getChannelRoute(channelId)}/convert_to_channel?team-id=${teamId}`, {method: 'post', body}, ); }; }; export default ClientChannels;