0

I'm trying to replicate the global chat search as in telegram.

enter image description here

I'm using getChats method for searching, but the problem is that the method only returns a list of ids.

In addition to the id, I would also like to get the name and avatar of the chat.

enter image description here

Therefore, I have to go through the chatids in the forEach and for each id call the getChat method that returns the data I need. This, in turn, causes severe problems with the query execution time. (14 seconds). In a telegram, the search takes ~2 seconds. I don’t know how they did it, I re-read all the documentation and did not find a method that would allow me to pass the name of the chat and get, in addition to identifiers, also a title and an image. Has anyone already encountered a similar problem?

import BaseAction from "./BaseAction";
import airgram from "../airgram/airgram";
import { ChatsUnion, ChatUnion } from 'airgram';

class SearchChatsAction implements BaseAction
{
    async run(name: string): Promise<any>
    {
        const output = await airgram.api.searchPublicChats({
            query: name
        });

        const promises: Array<any> = [];
        const result: Array<any> = [];
        for (const chatId of (output.response as ChatsUnion).chatIds)
        {
            promises.push(
                airgram.api.getChat({
                    chatId: chatId
                }).then(output => {
                    result.push({
                        id: (output.response as ChatUnion).id,
                        title: (output.response as ChatUnion).title
                    });
                })
            );
        }

        await Promise.all(promises);
        return result;
    }
}

export default SearchChatsAction;
pibocik125
  • 103
  • 2
  • 12

1 Answers1

0

I think the issue you're facing is because of API. You should try using different API. If you check these two documentations:

  1. searchPublicChat
  2. searchPublicChats

The API you're using returns just chatIds but searchPublicChat will contain all the information of searched chat.

vivekpadia70
  • 1,039
  • 3
  • 10
  • 30