Implement robust history fetching

This commit is contained in:
Bohdan Horbeshko 2022-05-18 10:18:47 -04:00
parent b78779dad0
commit ae16fd2a0b

View file

@ -988,24 +988,32 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
}
var messages *client.Messages
var newMessages *client.Messages
var messages []*client.Message
var err error
for _ = range make([]struct{}, 2) {
messages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
var fromId int64
for _ = range make([]struct{}, limit) { // safety limit
if len(messages) > 0 {
fromId = messages[len(messages)-1].Id
}
newMessages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
ChatId: chatID,
FromMessageId: fromId,
Limit: limit,
})
if err != nil {
return err.Error(), true
}
// TDlib yields only the latest message on the first request
if !(len(messages.Messages) == 1 && limit > 1) {
messages = append(messages, newMessages.Messages...)
if len(newMessages.Messages) == 0 || len(messages) >= int(limit) {
break
}
}
c.sendMessagesReverse(chatID, messages.Messages)
c.sendMessagesReverse(chatID, messages)
// chat members
case "members":
var query string