feat(client): getNearbyChats method

This commit is contained in:
teidesu 2021-05-10 22:51:35 +03:00
parent f0eb95e4ba
commit 0525800133
2 changed files with 64 additions and 0 deletions

View file

@ -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<Chat>
/**
* Get nearby chats
*
* @param latitude Latitude of the location
* @param longitude Longitude of the location
*/
getNearbyChats(latitude: number, longitude: number): Promise<Chat[]>
/**
* 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

View file

@ -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<Chat[]> {
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<number, Chat> = {}
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<Chat>).distance = peer.distance
}
})
return chats
}