Handle updateMessageSendSucceeded if client send a text/dice message

This commit is contained in:
c0re100 2022-01-30 01:04:23 +08:00
parent b535766aa0
commit e15362a612
No known key found for this signature in database
GPG key ID: 7C3B3004FE745AAF

View file

@ -1,8 +1,10 @@
package client package client
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"strconv"
"sync" "sync"
"time" "time"
) )
@ -13,6 +15,7 @@ type Client struct {
responses chan *Response responses chan *Response
listenerStore *listenerStore listenerStore *listenerStore
catchersStore *sync.Map catchersStore *sync.Map
successMsgStore *sync.Map
updatesTimeout time.Duration updatesTimeout time.Duration
catchTimeout time.Duration catchTimeout time.Duration
} }
@ -59,6 +62,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
responses: make(chan *Response, 1000), responses: make(chan *Response, 1000),
listenerStore: newListenerStore(), listenerStore: newListenerStore(),
catchersStore: &sync.Map{}, catchersStore: &sync.Map{},
successMsgStore: &sync.Map{},
} }
client.extraGenerator = UuidV4Generator() client.extraGenerator = UuidV4Generator()
@ -94,6 +98,13 @@ func (client *Client) receiver() {
continue continue
} }
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
value, ok := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
if ok {
value.(chan *Response) <- response
}
}
needGc := false needGc := false
for _, listener := range client.listenerStore.Listeners() { 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 if listener.IsActive() && listener.Updates != nil && typ.GetType() == listener.Filter.GetType() { // All updates go to Updates channel if type == filter
@ -129,8 +140,37 @@ func (client *Client) Send(req Request) (*Response, error) {
select { select {
case response := <-catcher: case response := <-catcher:
return response, nil if response.Type != "error" && req.Type == "sendMessage" {
m, err := UnmarshalMessage(response.Data)
if err != nil {
return nil, err
}
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)
response.Data = bytes.Replace(response.Data, []byte(strconv.FormatInt(m.Id, 10)), []byte(strconv.FormatInt(m2.Message.Id, 10)), 1)
return response, nil
case <-time.After(1 * time.Second):
client.successMsgStore.Delete(m.Id)
close(successCatcher)
}
}
}
return response, nil
case <-ctx.Done(): case <-ctx.Done():
return nil, errors.New("response catching timeout") return nil, errors.New("response catching timeout")
} }