feat(client): getSimilarChannels method

This commit is contained in:
alina 🌸 2023-12-01 19:52:48 +03:00
parent 4d27ca56d1
commit 392ff5ad2d
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 50 additions and 0 deletions

View file

@ -59,6 +59,7 @@ import { getChatMembers } from './methods/chats/get-chat-members.js'
import { getChatPreview } from './methods/chats/get-chat-preview.js' import { getChatPreview } from './methods/chats/get-chat-preview.js'
import { getFullChat } from './methods/chats/get-full-chat.js' import { getFullChat } from './methods/chats/get-full-chat.js'
import { getNearbyChats } from './methods/chats/get-nearby-chats.js' import { getNearbyChats } from './methods/chats/get-nearby-chats.js'
import { getSimilarChannels } from './methods/chats/get-similar-channels.js'
import { iterChatEventLog } from './methods/chats/iter-chat-event-log.js' import { iterChatEventLog } from './methods/chats/iter-chat-event-log.js'
import { iterChatMembers } from './methods/chats/iter-chat-members.js' import { iterChatMembers } from './methods/chats/iter-chat-members.js'
import { joinChat } from './methods/chats/join-chat.js' import { joinChat } from './methods/chats/join-chat.js'
@ -1538,6 +1539,19 @@ export interface TelegramClient extends BaseTelegramClient {
* @param longitude Longitude of the location * @param longitude Longitude of the location
*/ */
getNearbyChats(latitude: number, longitude: number): Promise<Chat[]> getNearbyChats(latitude: number, longitude: number): Promise<Chat[]>
/**
* Get channels that are similar to a given channel
*
* > **Note**: This method only returns the channels that the current user
* > is not subscribed to. For non-premium users, this method will only return
* > a few channels (with the total number of similar channels being specified in `.total`)
* >
* > Returns empty array in case there are no similar channels available.
* **Available**: 👤 users only
*
*/
getSimilarChannels(channel: InputPeerLike): Promise<ArrayWithTotal<Chat>>
/** /**
* Iterate over chat event log. * Iterate over chat event log.
* *
@ -5184,6 +5198,7 @@ export class TelegramClient extends BaseTelegramClient {
getChat = getChat.bind(null, this) getChat = getChat.bind(null, this)
getFullChat = getFullChat.bind(null, this) getFullChat = getFullChat.bind(null, this)
getNearbyChats = getNearbyChats.bind(null, this) getNearbyChats = getNearbyChats.bind(null, this)
getSimilarChannels = getSimilarChannels.bind(null, this)
iterChatEventLog = iterChatEventLog.bind(null, this) iterChatEventLog = iterChatEventLog.bind(null, this)
iterChatMembers = iterChatMembers.bind(null, this) iterChatMembers = iterChatMembers.bind(null, this)
joinChat = joinChat.bind(null, this) joinChat = joinChat.bind(null, this)

View file

@ -0,0 +1,35 @@
import { BaseTelegramClient } from '@mtcute/core'
import { ArrayWithTotal, Chat, InputPeerLike } from '../../types/index.js'
import { makeArrayWithTotal } from '../../utils/misc-utils.js'
import { normalizeToInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
// @available=user
/**
* Get channels that are similar to a given channel
*
* > **Note**: This method only returns the channels that the current user
* > is not subscribed to. For non-premium users, this method will only return
* > a few channels (with the total number of similar channels being specified in `.total`)
* >
* > Returns empty array in case there are no similar channels available.
*/
export async function getSimilarChannels(
client: BaseTelegramClient,
channel: InputPeerLike,
): Promise<ArrayWithTotal<Chat>> {
const res = await client.call({
_: 'channels.getChannelRecommendations',
channel: normalizeToInputChannel(await resolvePeer(client, channel), channel),
})
const parsed = res.chats.map((chat) => new Chat(chat))
switch (res._) {
case 'messages.chatsSlice':
return makeArrayWithTotal(parsed, res.count)
case 'messages.chats':
return makeArrayWithTotal(parsed, parsed.length)
}
}