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 var err error
for _ = range make([]struct{}, 2) { var fromId int64
messages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{ 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, ChatId: chatID,
FromMessageId: fromId,
Limit: limit, Limit: limit,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
// TDlib yields only the latest message on the first request messages = append(messages, newMessages.Messages...)
if !(len(messages.Messages) == 1 && limit > 1) {
if len(newMessages.Messages) == 0 || len(messages) >= int(limit) {
break break
} }
} }
c.sendMessagesReverse(chatID, messages.Messages) c.sendMessagesReverse(chatID, messages)
// chat members // chat members
case "members": case "members":
var query string var query string