Implement robust history fetching
This commit is contained in:
parent
b78779dad0
commit
ae16fd2a0b
|
@ -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
|
||||||
ChatId: chatID,
|
if len(messages) > 0 {
|
||||||
Limit: limit,
|
fromId = messages[len(messages)-1].Id
|
||||||
|
}
|
||||||
|
|
||||||
|
newMessages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
|
||||||
|
ChatId: chatID,
|
||||||
|
FromMessageId: fromId,
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in a new issue