Remove unnecessary wrapping of API methods

This commit is contained in:
Tulir Asokan 2023-07-16 00:26:22 +03:00
parent d04b1bde39
commit 3f912f87cf
8 changed files with 60 additions and 92 deletions

View file

@ -87,7 +87,7 @@ func (portal *Portal) deterministicEventID(messageID string, part int) id.EventI
func (portal *Portal) forwardBackfill(ctx context.Context, user *User, after time.Time, limit int64, markRead bool) {
log := zerolog.Ctx(ctx)
resp, err := user.Client.Conversations.FetchMessages(portal.ID, limit, nil)
resp, err := user.Client.FetchMessages(portal.ID, limit, nil)
if err != nil {
portal.zlog.Error().Err(err).Msg("Failed to fetch messages")
return

View file

@ -34,9 +34,6 @@ type Proxy func(*http.Request) (*url.URL, error)
type EventHandler func(evt interface{})
type Client struct {
Logger zerolog.Logger
Conversations *Conversations
Session *Session
Messages *Messages
rpc *RPC
pairer *Pairer
evHandler EventHandler
@ -70,8 +67,6 @@ func NewClient(authData *AuthData, logger zerolog.Logger) *Client {
sessionHandler.client = cli
rpc := &RPC{client: cli, http: &http.Client{Transport: &http.Transport{Proxy: cli.proxy}}}
cli.rpc = rpc
cli.Logger.Debug().Any("data", cli.authData.Cryptor).Msg("Cryptor")
cli.setApiMethods()
cli.FetchConfigVersion()
return cli
}
@ -122,12 +117,12 @@ func (c *Client) Connect() error {
go c.rpc.ListenReceiveMessages(rpcPayload)
c.sessionHandler.startAckInterval()
bugleRes, bugleErr := c.Session.IsBugleDefault()
bugleRes, bugleErr := c.IsBugleDefault()
if bugleErr != nil {
panic(bugleErr)
}
c.Logger.Info().Any("isBugle", bugleRes.Success).Msg("IsBugleDefault")
sessionErr := c.Session.SetActiveSession()
sessionErr := c.SetActiveSession()
if sessionErr != nil {
panic(sessionErr)
}
@ -201,12 +196,6 @@ func (c *Client) triggerEvent(evt interface{}) {
}
}
func (c *Client) setApiMethods() {
c.Conversations = &Conversations{client: c}
c.Session = &Session{client: c}
c.Messages = &Messages{client: c}
}
func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
reqId := util.RandomUUIDv4()
downloadMetadata := &binary.UploadImagePayload{

View file

@ -6,30 +6,21 @@ import (
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
type Conversations struct {
client *Client
synced bool
}
// default is 25 count
func (c *Conversations) List(count int64) (*binary.Conversations, error) {
func (c *Client) ListConversations(count int64) (*binary.Conversations, error) {
payload := &binary.ListCoversationsPayload{Count: count, Field4: 1}
var actionType binary.ActionType
//var actionType binary.ActionType
//if !c.synced {
// actionType = binary.ActionType_LIST_CONVERSATIONS_SYNC
// c.synced = true
//} else {
actionType := binary.ActionType_LIST_CONVERSATIONS
if !c.synced {
actionType = binary.ActionType_LIST_CONVERSATIONS_SYNC
c.synced = true
} else {
actionType = binary.ActionType_LIST_CONVERSATIONS
}
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -42,16 +33,16 @@ func (c *Conversations) List(count int64) (*binary.Conversations, error) {
return res, nil
}
func (c *Conversations) GetType(conversationId string) (*binary.GetConversationTypeResponse, error) {
payload := &binary.ConversationTypePayload{ConversationID: conversationId}
func (c *Client) GetConversationType(conversationID string) (*binary.GetConversationTypeResponse, error) {
payload := &binary.ConversationTypePayload{ConversationID: conversationID}
actionType := binary.ActionType_GET_CONVERSATION_TYPE
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -64,20 +55,20 @@ func (c *Conversations) GetType(conversationId string) (*binary.GetConversationT
return res, nil
}
func (c *Conversations) FetchMessages(conversationId string, count int64, cursor *binary.Cursor) (*binary.FetchMessagesResponse, error) {
payload := &binary.FetchConversationMessagesPayload{ConversationID: conversationId, Count: count}
func (c *Client) FetchMessages(conversationID string, count int64, cursor *binary.Cursor) (*binary.FetchMessagesResponse, error) {
payload := &binary.FetchConversationMessagesPayload{ConversationID: conversationID, Count: count}
if cursor != nil {
payload.Cursor = cursor
}
actionType := binary.ActionType_LIST_MESSAGES
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -90,15 +81,15 @@ func (c *Conversations) FetchMessages(conversationId string, count int64, cursor
return res, nil
}
func (c *Conversations) SendMessage(payload *binary.SendMessagePayload) (*binary.SendMessageResponse, error) {
func (c *Client) SendMessage(payload *binary.SendMessagePayload) (*binary.SendMessageResponse, error) {
actionType := binary.ActionType_SEND_MESSAGE
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -108,20 +99,19 @@ func (c *Conversations) SendMessage(payload *binary.SendMessagePayload) (*binary
return nil, fmt.Errorf("unexpected response type %T, expected *binary.SendMessageResponse", response.Data.Decrypted)
}
c.client.Logger.Debug().Any("res", res).Msg("sent message!")
return res, nil
}
func (c *Conversations) GetParticipantThumbnail(convID string) (*binary.ParticipantThumbnail, error) {
func (c *Client) GetParticipantThumbnail(convID string) (*binary.ParticipantThumbnail, error) {
payload := &binary.GetParticipantThumbnailPayload{ConversationID: convID}
actionType := binary.ActionType_GET_PARTICIPANTS_THUMBNAIL
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -134,7 +124,7 @@ func (c *Conversations) GetParticipantThumbnail(convID string) (*binary.Particip
return res, nil
}
func (c *Conversations) Update(convBuilder *ConversationBuilder) (*binary.UpdateConversationResponse, error) {
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*binary.UpdateConversationResponse, error) {
data := &binary.UpdateConversationPayload{}
payload, buildErr := convBuilder.Build(data)
@ -144,12 +134,12 @@ func (c *Conversations) Update(convBuilder *ConversationBuilder) (*binary.Update
actionType := binary.ActionType_UPDATE_CONVERSATION
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -162,16 +152,16 @@ func (c *Conversations) Update(convBuilder *ConversationBuilder) (*binary.Update
return res, nil
}
func (c *Conversations) SetTyping(convID string) error {
func (c *Client) SetTyping(convID string) error {
payload := &binary.TypingUpdatePayload{Data: &binary.SetTypingIn{ConversationID: convID, Typing: true}}
actionType := binary.ActionType_TYPING_UPDATES
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return sendErr
}
_, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
_, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return err
}

View file

@ -6,19 +6,15 @@ import (
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
type Messages struct {
client *Client
}
func (m *Messages) React(payload *binary.SendReactionPayload) (*binary.SendReactionResponse, error) {
func (c *Client) SendReaction(payload *binary.SendReactionPayload) (*binary.SendReactionResponse, error) {
actionType := binary.ActionType_SEND_REACTION
sentRequestId, sendErr := m.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := m.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -28,20 +24,19 @@ func (m *Messages) React(payload *binary.SendReactionPayload) (*binary.SendReact
return nil, fmt.Errorf("unexpected response type %T, expected *binary.SendReactionResponse", response.Data.Decrypted)
}
m.client.Logger.Debug().Any("res", res).Msg("sent reaction!")
return res, nil
}
func (m *Messages) Delete(messageId string) (*binary.DeleteMessageResponse, error) {
payload := &binary.DeleteMessagePayload{MessageID: messageId}
func (c *Client) DeleteMessage(messageID string) (*binary.DeleteMessageResponse, error) {
payload := &binary.DeleteMessagePayload{MessageID: messageID}
actionType := binary.ActionType_DELETE_MESSAGE
sentRequestId, sendErr := m.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := m.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -51,20 +46,19 @@ func (m *Messages) Delete(messageId string) (*binary.DeleteMessageResponse, erro
return nil, fmt.Errorf("unexpected response type %T, expected *binary.DeleteMessagesResponse", response.Data.Decrypted)
}
m.client.Logger.Debug().Any("res", res).Msg("deleted message!")
return res, nil
}
func (m *Messages) MarkRead(conversationID, messageID string) error {
func (c *Client) MarkRead(conversationID, messageID string) error {
payload := &binary.MessageReadPayload{ConversationID: conversationID, MessageID: messageID}
actionType := binary.ActionType_MESSAGE_READ
sentRequestId, sendErr := m.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return sendErr
}
_, err := m.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
_, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return err
}

View file

@ -82,7 +82,7 @@ func (r *RPC) ListenReceiveMessages(payload []byte) {
r.conn = resp.Body
if r.client.authData.DevicePair != nil {
go func() {
err := r.client.Session.NotifyDittoActivity()
err := r.client.NotifyDittoActivity()
if err != nil {
r.client.Logger.Err(err).Msg("Error notifying ditto activity")
}

View file

@ -6,32 +6,27 @@ import (
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
type Session struct {
client *Client
}
// start receiving updates from mobile on this session
func (s *Session) SetActiveSession() error {
s.client.sessionHandler.ResetSessionId()
func (c *Client) SetActiveSession() error {
c.sessionHandler.ResetSessionId()
actionType := binary.ActionType_GET_UPDATES
_, sendErr := s.client.sessionHandler.completeSendMessage(actionType, false, nil)
_, sendErr := c.sessionHandler.completeSendMessage(actionType, false, nil)
if sendErr != nil {
return sendErr
}
return nil
}
func (s *Session) IsBugleDefault() (*binary.IsBugleDefaultResponse, error) {
s.client.sessionHandler.ResetSessionId()
func (c *Client) IsBugleDefault() (*binary.IsBugleDefaultResponse, error) {
c.sessionHandler.ResetSessionId()
actionType := binary.ActionType_IS_BUGLE_DEFAULT
sentRequestId, sendErr := s.client.sessionHandler.completeSendMessage(actionType, true, nil)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, nil)
if sendErr != nil {
return nil, sendErr
}
response, err := s.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
@ -44,16 +39,16 @@ func (s *Session) IsBugleDefault() (*binary.IsBugleDefaultResponse, error) {
return res, nil
}
func (s *Session) NotifyDittoActivity() error {
func (c *Client) NotifyDittoActivity() error {
payload := &binary.NotifyDittoActivityPayload{Success: true}
actionType := binary.ActionType_NOTIFY_DITTO_ACTIVITY
sentRequestId, sendErr := s.client.sessionHandler.completeSendMessage(actionType, true, payload)
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return sendErr
}
_, err := s.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
_, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return err
}

View file

@ -9,12 +9,12 @@ import (
func (c *Client) handleClientReady(newSessionId string) {
c.Logger.Info().Any("sessionId", newSessionId).Msg("Client is ready!")
conversations, convErr := c.Conversations.List(25)
conversations, convErr := c.ListConversations(25)
if convErr != nil {
panic(convErr)
}
c.Logger.Debug().Any("conversations", conversations).Msg("got conversations")
notifyErr := c.Session.NotifyDittoActivity()
notifyErr := c.NotifyDittoActivity()
if notifyErr != nil {
panic(notifyErr)
}

View file

@ -1274,7 +1274,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing
if req, err := portal.convertMatrixMessage(ctx, sender, content, txnID); err != nil {
go ms.sendMessageMetrics(evt, err, "Error converting", true)
} else if _, err = sender.Client.Conversations.SendMessage(req); err != nil {
} else if _, err = sender.Client.SendMessage(req); err != nil {
go ms.sendMessageMetrics(evt, err, "Error sending", true)
} else {
go ms.sendMessageMetrics(evt, nil, "", true)
@ -1308,7 +1308,7 @@ func (portal *Portal) HandleMatrixReadReceipt(brUser bridge.User, eventID id.Eve
targetMessage = lastMessage
}
log = log.With().Str("message_id", targetMessage.ID).Logger()
err = user.Client.Messages.MarkRead(portal.ID, targetMessage.ID)
err = user.Client.MarkRead(portal.ID, targetMessage.ID)
if err != nil {
log.Err(err).Msg("Failed to mark message as read")
} else {
@ -1353,7 +1353,7 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event) error
if existingReaction != nil {
action = binary.Reaction_SWITCH
}
resp, err := sender.Client.Messages.React(&binary.SendReactionPayload{
resp, err := sender.Client.SendReaction(&binary.SendReactionPayload{
MessageID: msg.ID,
ReactionData: binary.MakeReactionData(emoji),
Action: action,
@ -1402,7 +1402,7 @@ func (portal *Portal) handleMatrixMessageRedaction(ctx context.Context, sender *
} else if msg == nil {
return errTargetNotFound
}
resp, err := sender.Client.Messages.Delete(msg.ID)
resp, err := sender.Client.DeleteMessage(msg.ID)
if err != nil {
return fmt.Errorf("failed to send message removal: %w", err)
} else if !resp.Success {
@ -1425,7 +1425,7 @@ func (portal *Portal) handleMatrixReactionRedaction(ctx context.Context, sender
return errTargetNotFound
}
resp, err := sender.Client.Messages.React(&binary.SendReactionPayload{
resp, err := sender.Client.SendReaction(&binary.SendReactionPayload{
MessageID: existingReaction.MessageID,
ReactionData: binary.MakeReactionData(existingReaction.Reaction),
Action: binary.Reaction_REMOVE,