Skip to content

Commit

Permalink
feat(client): getSimilarChannels method
Browse files Browse the repository at this point in the history
  • Loading branch information
teidesu committed Dec 1, 2023
1 parent 4d27ca5 commit 392ff5a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { getChatMembers } from './methods/chats/get-chat-members.js'
import { getChatPreview } from './methods/chats/get-chat-preview.js'
import { getFullChat } from './methods/chats/get-full-chat.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 { iterChatMembers } from './methods/chats/iter-chat-members.js'
import { joinChat } from './methods/chats/join-chat.js'
Expand Down Expand Up @@ -1538,6 +1539,19 @@ export interface TelegramClient extends BaseTelegramClient {
* @param longitude Longitude of the location
*/
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.
*
Expand Down Expand Up @@ -5184,6 +5198,7 @@ export class TelegramClient extends BaseTelegramClient {
getChat = getChat.bind(null, this)
getFullChat = getFullChat.bind(null, this)
getNearbyChats = getNearbyChats.bind(null, this)
getSimilarChannels = getSimilarChannels.bind(null, this)
iterChatEventLog = iterChatEventLog.bind(null, this)
iterChatMembers = iterChatMembers.bind(null, this)
joinChat = joinChat.bind(null, this)
Expand Down
35 changes: 35 additions & 0 deletions packages/client/src/methods/chats/get-similar-channels.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 392ff5a

Please sign in to comment.