From 0dc94fc121352a8f5ea14e1b37d0db4bccc2c234 Mon Sep 17 00:00:00 2001 From: Aleksandr Zelenin Date: Tue, 9 Oct 2018 06:46:15 +0300 Subject: [PATCH] add pullers --- client/puller/chat.go | 46 ++++++++++++++++++++++++++++++ client/puller/chats.go | 56 +++++++++++++++++++++++++++++++++++++ client/puller/error.go | 7 +++++ client/puller/supergroup.go | 48 +++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 client/puller/chat.go create mode 100644 client/puller/chats.go create mode 100644 client/puller/error.go create mode 100644 client/puller/supergroup.go diff --git a/client/puller/chat.go b/client/puller/chat.go new file mode 100644 index 0000000..2cac330 --- /dev/null +++ b/client/puller/chat.go @@ -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 + } + } +} diff --git a/client/puller/chats.go b/client/puller/chats.go new file mode 100644 index 0000000..9011181 --- /dev/null +++ b/client/puller/chats.go @@ -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 + } + } +} diff --git a/client/puller/error.go b/client/puller/error.go new file mode 100644 index 0000000..d40b6e4 --- /dev/null +++ b/client/puller/error.go @@ -0,0 +1,7 @@ +package puller + +import ( + "errors" +) + +var EOP = errors.New("end of pull") diff --git a/client/puller/supergroup.go b/client/puller/supergroup.go new file mode 100644 index 0000000..2ad9692 --- /dev/null +++ b/client/puller/supergroup.go @@ -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++ + } +}