add pullers

This commit is contained in:
Aleksandr Zelenin 2018-10-09 06:46:15 +03:00
parent 287f0c6d4e
commit 0dc94fc121
4 changed files with 157 additions and 0 deletions

46
client/puller/chat.go Normal file
View file

@ -0,0 +1,46 @@
package puller
import (
"github.com/zelenin/go-tdlib/client"
)
func ChatHistory(tdlibClient *client.Client, chatId int64) (chan *client.Message, chan error) {
messageChan := make(chan *client.Message, 10)
errChan := make(chan error, 1)
var fromMessageId int64 = 0
var offset int32 = 0
var limit int32 = 100
go chatHistory(tdlibClient, messageChan, errChan, chatId, fromMessageId, offset, limit, false)
return messageChan, errChan
}
func chatHistory(tdlibClient *client.Client, messageChan chan *client.Message, errChan chan error, chatId int64, fromMessageId int64, offset int32, limit int32, onlyLocal bool) {
defer func() {
close(messageChan)
close(errChan)
}()
for {
messages, err := tdlibClient.GetChatHistory(chatId, fromMessageId, offset, limit, onlyLocal)
if err != nil {
errChan <- err
return
}
if len(messages.Messages) == 0 {
errChan <- EOP
break
}
for _, message := range messages.Messages {
fromMessageId = message.Id
messageChan <- message
}
}
}

56
client/puller/chats.go Normal file
View file

@ -0,0 +1,56 @@
package puller
import (
"math"
"github.com/zelenin/go-tdlib/client"
)
func Chats(tdlibClient *client.Client) (chan *client.Chat, chan error) {
chatChan := make(chan *client.Chat, 10)
errChan := make(chan error, 1)
var offsetOrder client.JsonInt64 = math.MaxInt64
var offsetChatId int64 = 0
var limit int32 = 100
go chats(tdlibClient, chatChan, errChan, offsetOrder, offsetChatId, limit)
return chatChan, errChan
}
func chats(tdlibClient *client.Client, chatChan chan *client.Chat, errChan chan error, offsetOrder client.JsonInt64, offsetChatId int64, limit int32) {
defer func() {
close(chatChan)
close(errChan)
}()
for {
chats, err := tdlibClient.GetChats(offsetOrder, offsetChatId, limit)
if err != nil {
errChan <- err
return
}
if len(chats.ChatIds) == 0 {
errChan <- EOP
break
}
for _, chatId := range chats.ChatIds {
chat, err := tdlibClient.GetChat(chatId)
if err != nil {
errChan <- err
return
}
offsetOrder = chat.Order
offsetChatId = chat.Id
chatChan <- chat
}
}
}

7
client/puller/error.go Normal file
View file

@ -0,0 +1,7 @@
package puller
import (
"errors"
)
var EOP = errors.New("end of pull")

View file

@ -0,0 +1,48 @@
package puller
import (
"github.com/zelenin/go-tdlib/client"
)
func SupergroupMembers(tdlibClient *client.Client, supergroupId int32) (chan *client.ChatMember, chan error) {
chatMemberChan := make(chan *client.ChatMember, 10)
errChan := make(chan error, 1)
var filter client.SupergroupMembersFilter = nil
var offset int32 = 0
var limit int32 = 200
go supergroupMembers(tdlibClient, chatMemberChan, errChan, supergroupId, filter, offset, limit)
return chatMemberChan, errChan
}
func supergroupMembers(tdlibClient *client.Client, chatMemberChan chan *client.ChatMember, errChan chan error, supergroupId int32, filter client.SupergroupMembersFilter, offset int32, limit int32) {
defer func() {
close(chatMemberChan)
close(errChan)
}()
var page int32 = 0
for {
chatMembers, err := tdlibClient.GetSupergroupMembers(supergroupId, filter, page*limit+offset, limit)
if err != nil {
errChan <- err
return
}
if len(chatMembers.Members) == 0 {
errChan <- EOP
break
}
for _, member := range chatMembers.Members {
chatMemberChan <- member
}
page++
}
}