diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 18db07c2..834d50e2 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -33,6 +33,7 @@ import { getChatMembers } from './methods/chats/get-chat-members' import { getChatPreview } from './methods/chats/get-chat-preview' import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' +import { getNearbyChats } from './methods/chats/get-nearby-chats' import { iterChatMembers } from './methods/chats/iter-chat-members' import { joinChat } from './methods/chats/join-chat' import { leaveChat } from './methods/chats/leave-chat' @@ -837,6 +838,13 @@ export interface TelegramClient extends BaseTelegramClient { * Use {@link getChatPreview} instead. */ getFullChat(chatId: InputPeerLike): Promise + /** + * Get nearby chats + * + * @param latitude Latitude of the location + * @param longitude Longitude of the location + */ + getNearbyChats(latitude: number, longitude: number): Promise /** * Iterate through chat members * @@ -2759,6 +2767,7 @@ export class TelegramClient extends BaseTelegramClient { getChatPreview = getChatPreview getChat = getChat getFullChat = getFullChat + getNearbyChats = getNearbyChats iterChatMembers = iterChatMembers joinChat = joinChat leaveChat = leaveChat diff --git a/packages/client/src/methods/chats/get-nearby-chats.ts b/packages/client/src/methods/chats/get-nearby-chats.ts new file mode 100644 index 00000000..2586ad1e --- /dev/null +++ b/packages/client/src/methods/chats/get-nearby-chats.ts @@ -0,0 +1,55 @@ +import { TelegramClient } from '../../client' +import { Chat, MtCuteTypeAssertionError } from '../../types' +import { assertTypeIs } from '../../utils/type-assertion' +import { getMarkedPeerId } from '@mtcute/core' +import { tl } from 'packages/tl' + +/** + * Get nearby chats + * + * @param latitude Latitude of the location + * @param longitude Longitude of the location + * @internal + */ +export async function getNearbyChats( + this: TelegramClient, + latitude: number, + longitude: number +): Promise { + const res = await this.call({ + _: 'contacts.getLocated', + geoPoint: { + _: 'inputGeoPoint', + lat: latitude, + long: longitude, + }, + }) + + if (!(res._ === 'updates' || res._ === 'updatesCombined')) { + throw new MtCuteTypeAssertionError( + 'contacts.getLocated', + 'updates | updatesCombined', + res._ + ) + } + + if (!res.updates.length) return [] + + assertTypeIs('contacts.getLocated (@ .updates[0])', res.updates[0], 'updatePeerLocated') + + const chats = res.chats.map((it) => new Chat(this, it)) + + const index: Record = {} + chats.forEach((c) => index[c.id] = c) + + res.updates[0].peers.forEach((peer) => { + if (peer._ === 'peerSelfLocated') return + + const id = getMarkedPeerId(peer.peer) + if (index[id]) { + (index[id] as tl.Mutable).distance = peer.distance + } + }) + + return chats +}