go-tdlib/client/puller/chats.go

63 lines
1.2 KiB
Go
Raw Normal View History

2018-10-09 03:46:15 +00:00
package puller
import (
2018-10-23 12:49:10 +00:00
"math"
2018-10-09 03:46:15 +00:00
2018-10-23 12:49:10 +00:00
"github.com/zelenin/go-tdlib/client"
2018-10-09 03:46:15 +00:00
)
func Chats(tdlibClient *client.Client) (chan *client.Chat, chan error) {
2018-10-23 12:49:10 +00:00
chatChan := make(chan *client.Chat, 10)
errChan := make(chan error, 1)
2018-10-09 03:46:15 +00:00
2018-10-23 12:49:10 +00:00
var offsetOrder client.JsonInt64 = math.MaxInt64
var offsetChatId int64 = 0
var limit int32 = 100
2018-10-09 03:46:15 +00:00
2018-10-23 12:49:10 +00:00
go chats(tdlibClient, chatChan, errChan, offsetOrder, offsetChatId, limit)
2018-10-09 03:46:15 +00:00
2018-10-23 12:49:10 +00:00
return chatChan, errChan
2018-10-09 03:46:15 +00:00
}
func chats(tdlibClient *client.Client, chatChan chan *client.Chat, errChan chan error, offsetOrder client.JsonInt64, offsetChatId int64, limit int32) {
2018-10-23 12:49:10 +00:00
defer func() {
close(chatChan)
close(errChan)
}()
for {
chats, err := tdlibClient.GetChats(&client.GetChatsRequest{
OffsetOrder: offsetOrder,
OffsetChatId: offsetChatId,
Limit: limit,
})
if err != nil {
errChan <- err
return
}
if len(chats.ChatIds) == 0 {
errChan <- EOP
break
}
for _, chatId := range chats.ChatIds {
chat, err := tdlibClient.GetChat(&client.GetChatRequest{
ChatId: chatId,
})
if err != nil {
errChan <- err
return
}
offsetOrder = chat.Order
offsetChatId = chat.Id
chatChan <- chat
}
}
2018-10-09 03:46:15 +00:00
}