2018-08-30 14:55:42 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2022-01-29 17:04:23 +00:00
|
|
|
"bytes"
|
2021-09-05 01:36:38 +00:00
|
|
|
"context"
|
2018-10-23 12:38:10 +00:00
|
|
|
"errors"
|
2022-01-29 17:04:23 +00:00
|
|
|
"strconv"
|
2018-10-23 12:38:10 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-08-30 14:55:42 +00:00
|
|
|
)
|
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
var pendingUpdateType []Type
|
|
|
|
|
2018-08-30 14:55:42 +00:00
|
|
|
type Client struct {
|
2022-01-29 17:04:23 +00:00
|
|
|
jsonClient *JsonClient
|
|
|
|
extraGenerator ExtraGenerator
|
|
|
|
responses chan *Response
|
2022-02-06 03:22:20 +00:00
|
|
|
pendingResp chan *Response
|
2022-01-29 17:04:23 +00:00
|
|
|
listenerStore *listenerStore
|
|
|
|
catchersStore *sync.Map
|
|
|
|
successMsgStore *sync.Map
|
2022-08-03 01:52:06 +00:00
|
|
|
forwardMsgStore *sync.Map
|
2022-01-29 17:04:23 +00:00
|
|
|
updatesTimeout time.Duration
|
|
|
|
catchTimeout time.Duration
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
func WithExtraGenerator(extraGenerator ExtraGenerator) Option {
|
2018-10-23 12:38:10 +00:00
|
|
|
return func(client *Client) {
|
|
|
|
client.extraGenerator = extraGenerator
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:08:28 +00:00
|
|
|
func WithCatchTimeout(timeout time.Duration) Option {
|
2018-10-23 12:38:10 +00:00
|
|
|
return func(client *Client) {
|
|
|
|
client.catchTimeout = timeout
|
|
|
|
}
|
2018-10-09 03:08:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 21:45:16 +00:00
|
|
|
func WithProxy(req *AddProxyRequest) Option {
|
|
|
|
return func(client *Client) {
|
|
|
|
client.AddProxy(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 23:45:45 +00:00
|
|
|
func SetLogLevel(level int32) {
|
|
|
|
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
|
|
|
NewVerbosityLevel: level,
|
|
|
|
})
|
2022-01-27 05:41:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:45:45 +00:00
|
|
|
func SetFilePath(path string) {
|
|
|
|
_, _ = SetLogStream(&SetLogStreamRequest{
|
|
|
|
LogStream: &LogStreamFile{
|
|
|
|
Path: path,
|
|
|
|
MaxFileSize: 10485760,
|
|
|
|
RedirectStderr: true,
|
|
|
|
},
|
|
|
|
})
|
2019-09-10 12:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
// Keep specific update type in memory when listener is not ready.
|
|
|
|
func SetPendingUpdateType(update ...Type) {
|
|
|
|
for _, v := range update {
|
|
|
|
pendingUpdateType = append(pendingUpdateType, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 14:55:42 +00:00
|
|
|
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
client := &Client{
|
2022-01-29 17:04:23 +00:00
|
|
|
jsonClient: NewJsonClient(),
|
|
|
|
responses: make(chan *Response, 1000),
|
2022-02-06 03:22:20 +00:00
|
|
|
pendingResp: make(chan *Response, 1000),
|
2022-01-29 17:04:23 +00:00
|
|
|
listenerStore: newListenerStore(),
|
|
|
|
catchersStore: &sync.Map{},
|
|
|
|
successMsgStore: &sync.Map{},
|
2022-08-03 01:52:06 +00:00
|
|
|
forwardMsgStore: &sync.Map{},
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-12-23 21:45:16 +00:00
|
|
|
client.extraGenerator = UuidV4Generator()
|
|
|
|
client.catchTimeout = 60 * time.Second
|
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
for _, option := range options {
|
|
|
|
option(client)
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2022-01-04 12:22:29 +00:00
|
|
|
tdlibInstance.addClient(client)
|
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
go client.processPendingResponse()
|
2022-01-04 12:22:29 +00:00
|
|
|
go client.receiver()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
err := Authorize(client, authorizationStateHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return client, nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
func (client *Client) processResponse(response *Response) {
|
|
|
|
if response.Extra != "" {
|
|
|
|
value, ok := client.catchersStore.Load(response.Extra)
|
|
|
|
if ok {
|
|
|
|
value.(chan *Response) <- response
|
2021-12-31 19:03:50 +00:00
|
|
|
}
|
2022-02-06 03:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typ, err := UnmarshalType(response.Data)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-31 19:03:50 +00:00
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
|
2022-08-03 01:52:06 +00:00
|
|
|
sendVal, sOk := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
|
|
|
|
if sOk {
|
|
|
|
sendVal.(chan *Response) <- response
|
|
|
|
}
|
|
|
|
forwardVal, fOk := client.forwardMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
|
|
|
|
if fOk {
|
|
|
|
forwardVal.(chan *Response) <- response
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
2022-02-06 03:22:20 +00:00
|
|
|
}
|
2018-10-23 12:38:10 +00:00
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
if len(client.listenerStore.Listeners()) == 0 {
|
|
|
|
for _, p := range pendingUpdateType {
|
|
|
|
if typ.GetType() == p.GetType() {
|
|
|
|
client.pendingResp <- response
|
2022-01-29 17:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-06 03:22:20 +00:00
|
|
|
}
|
2022-01-29 17:04:23 +00:00
|
|
|
|
2022-02-06 03:22:20 +00:00
|
|
|
needGc := false
|
|
|
|
for _, listener := range client.listenerStore.Listeners() {
|
|
|
|
if listener.IsActive() && listener.Updates != nil && typ.GetType() == listener.Filter.GetType() { // All updates go to Updates channel if type == filter
|
|
|
|
listener.Updates <- typ
|
|
|
|
} else if listener.IsActive() && listener.RawUpdates != nil { // All updates go to RawUpdates channel if filter is empty
|
|
|
|
listener.RawUpdates <- typ
|
|
|
|
} else if !listener.IsActive() { // GC inactive listener
|
|
|
|
needGc = true
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
2022-02-06 03:22:20 +00:00
|
|
|
}
|
|
|
|
if needGc {
|
|
|
|
client.listenerStore.gc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) receiver() {
|
|
|
|
for response := range client.responses {
|
|
|
|
client.processResponse(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) processPendingResponse() {
|
|
|
|
// No need to process pending response if no pending list.
|
|
|
|
if len(pendingUpdateType) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for listener to be ready.
|
|
|
|
for {
|
|
|
|
if len(client.listenerStore.Listeners()) > 0 {
|
|
|
|
break
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
2022-02-06 03:22:20 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start processing pending response
|
|
|
|
for response := range client.pendingResp {
|
|
|
|
client.processResponse(response)
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Send(req Request) (*Response, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
req.Extra = client.extraGenerator()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
catcher := make(chan *Response, 1)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
client.catchersStore.Store(req.Extra, catcher)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
defer func() {
|
|
|
|
client.catchersStore.Delete(req.Extra)
|
2021-09-05 01:36:38 +00:00
|
|
|
close(catcher)
|
2018-10-23 12:38:10 +00:00
|
|
|
}()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
client.jsonClient.Send(req)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2021-09-05 01:36:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), client.catchTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
select {
|
|
|
|
case response := <-catcher:
|
2022-01-29 17:04:23 +00:00
|
|
|
if response.Type != "error" && req.Type == "sendMessage" {
|
|
|
|
m, err := UnmarshalMessage(response.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2022-01-29 17:04:23 +00:00
|
|
|
if m.Content.MessageContentType() == "messageText" || m.Content.MessageContentType() == "messageDice" {
|
|
|
|
successCatcher := make(chan *Response, 1)
|
|
|
|
client.successMsgStore.Store(m.Id, successCatcher)
|
|
|
|
|
|
|
|
defer (func() {
|
|
|
|
client.successMsgStore.Delete(m.Id)
|
|
|
|
close(successCatcher)
|
|
|
|
})()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case modResponse := <-successCatcher:
|
|
|
|
m2, err2 := UnmarshalUpdateMessageSendSucceeded(modResponse.Data)
|
|
|
|
if err2 != nil {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
response.Data = bytes.Replace(response.Data, []byte("{\"@type\":\"messageSendingStatePending\"}"), []byte("{\"@type\":\"updateMessageSendSucceeded\"}"), 1)
|
2022-08-01 19:53:04 +00:00
|
|
|
response.Data = bytes.Replace(response.Data, []byte("\"id\":"+strconv.FormatInt(m.Id, 10)), []byte("\"id\":"+strconv.FormatInt(m2.Message.Id, 10)), 1)
|
2022-01-29 17:04:23 +00:00
|
|
|
return response, nil
|
|
|
|
case <-time.After(1 * time.Second):
|
2022-01-31 19:17:59 +00:00
|
|
|
return response, nil
|
2022-01-29 17:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response, nil
|
2021-09-05 01:36:38 +00:00
|
|
|
case <-ctx.Done():
|
2018-10-23 12:38:10 +00:00
|
|
|
return nil, errors.New("response catching timeout")
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
2018-09-10 22:30:14 +00:00
|
|
|
|
|
|
|
func (client *Client) GetListener() *Listener {
|
2018-10-23 12:38:10 +00:00
|
|
|
listener := &Listener{
|
2022-01-29 01:20:54 +00:00
|
|
|
isActive: true,
|
|
|
|
RawUpdates: make(chan Type, 1000),
|
|
|
|
}
|
|
|
|
client.listenerStore.Add(listener)
|
|
|
|
|
|
|
|
return listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) AddEventReceiver(msgType Type, channelCapacity int) *Listener {
|
|
|
|
listener := &Listener{
|
2022-01-29 17:04:23 +00:00
|
|
|
isActive: true,
|
|
|
|
Updates: make(chan Type, channelCapacity),
|
|
|
|
Filter: msgType,
|
2018-10-23 12:38:10 +00:00
|
|
|
}
|
|
|
|
client.listenerStore.Add(listener)
|
2018-09-10 22:30:14 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return listener
|
2018-09-10 22:30:14 +00:00
|
|
|
}
|
2018-10-24 14:58:48 +00:00
|
|
|
|
|
|
|
func (client *Client) Stop() {
|
|
|
|
client.Destroy()
|
|
|
|
}
|