Use generics for casting responses
This commit is contained in:
parent
4599f3f0e5
commit
a6b91da574
4 changed files with 25 additions and 149 deletions
|
@ -1,8 +1,6 @@
|
||||||
package libgm
|
package libgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,17 +13,7 @@ func (c *Client) ListConversations(count int64, folder gmproto.ListConversations
|
||||||
//} else {
|
//} else {
|
||||||
actionType := gmproto.ActionType_LIST_CONVERSATIONS
|
actionType := gmproto.ActionType_LIST_CONVERSATIONS
|
||||||
|
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
return typedResponse[*gmproto.Conversations](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.Conversations)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.Conversations", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ListContacts() (*gmproto.ListContactsResponse, error) {
|
func (c *Client) ListContacts() (*gmproto.ListContactsResponse, error) {
|
||||||
|
@ -35,18 +23,7 @@ func (c *Client) ListContacts() (*gmproto.ListContactsResponse, error) {
|
||||||
I3: 50,
|
I3: 50,
|
||||||
}
|
}
|
||||||
actionType := gmproto.ActionType_LIST_CONTACTS
|
actionType := gmproto.ActionType_LIST_CONTACTS
|
||||||
|
return typedResponse[*gmproto.ListContactsResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.ListContactsResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListContactsResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
|
func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
|
||||||
|
@ -54,51 +31,18 @@ func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
|
||||||
Count: 8,
|
Count: 8,
|
||||||
}
|
}
|
||||||
actionType := gmproto.ActionType_LIST_TOP_CONTACTS
|
actionType := gmproto.ActionType_LIST_TOP_CONTACTS
|
||||||
|
return typedResponse[*gmproto.ListTopContactsResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.ListTopContactsResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListTopContactsResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetOrCreateConversation(req *gmproto.GetOrCreateConversationPayload) (*gmproto.GetOrCreateConversationResponse, error) {
|
func (c *Client) GetOrCreateConversation(req *gmproto.GetOrCreateConversationPayload) (*gmproto.GetOrCreateConversationResponse, error) {
|
||||||
actionType := gmproto.ActionType_GET_OR_CREATE_CONVERSATION
|
actionType := gmproto.ActionType_GET_OR_CREATE_CONVERSATION
|
||||||
|
return typedResponse[*gmproto.GetOrCreateConversationResponse](c.sessionHandler.sendMessage(actionType, req))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.GetOrCreateConversationResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.GetOrCreateConversationResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetConversationType(conversationID string) (*gmproto.GetConversationTypeResponse, error) {
|
func (c *Client) GetConversationType(conversationID string) (*gmproto.GetConversationTypeResponse, error) {
|
||||||
payload := &gmproto.ConversationTypePayload{ConversationID: conversationID}
|
payload := &gmproto.ConversationTypePayload{ConversationID: conversationID}
|
||||||
actionType := gmproto.ActionType_GET_CONVERSATION_TYPE
|
actionType := gmproto.ActionType_GET_CONVERSATION_TYPE
|
||||||
|
return typedResponse[*gmproto.GetConversationTypeResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.GetConversationTypeResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.GetConversationTypeResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) FetchMessages(conversationID string, count int64, cursor *gmproto.Cursor) (*gmproto.FetchMessagesResponse, error) {
|
func (c *Client) FetchMessages(conversationID string, count int64, cursor *gmproto.Cursor) (*gmproto.FetchMessagesResponse, error) {
|
||||||
|
@ -106,53 +50,19 @@ func (c *Client) FetchMessages(conversationID string, count int64, cursor *gmpro
|
||||||
if cursor != nil {
|
if cursor != nil {
|
||||||
payload.Cursor = cursor
|
payload.Cursor = cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
actionType := gmproto.ActionType_LIST_MESSAGES
|
actionType := gmproto.ActionType_LIST_MESSAGES
|
||||||
|
return typedResponse[*gmproto.FetchMessagesResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.FetchMessagesResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.FetchMessagesResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SendMessage(payload *gmproto.SendMessagePayload) (*gmproto.SendMessageResponse, error) {
|
func (c *Client) SendMessage(payload *gmproto.SendMessagePayload) (*gmproto.SendMessageResponse, error) {
|
||||||
actionType := gmproto.ActionType_SEND_MESSAGE
|
actionType := gmproto.ActionType_SEND_MESSAGE
|
||||||
|
return typedResponse[*gmproto.SendMessageResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.SendMessageResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.SendMessageResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetParticipantThumbnail(convID string) (*gmproto.ParticipantThumbnail, error) {
|
func (c *Client) GetParticipantThumbnail(convID string) (*gmproto.ParticipantThumbnail, error) {
|
||||||
payload := &gmproto.GetParticipantThumbnailPayload{ConversationID: convID}
|
payload := &gmproto.GetParticipantThumbnailPayload{ConversationID: convID}
|
||||||
actionType := gmproto.ActionType_GET_PARTICIPANTS_THUMBNAIL
|
actionType := gmproto.ActionType_GET_PARTICIPANTS_THUMBNAIL
|
||||||
|
return typedResponse[*gmproto.ParticipantThumbnail](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.ParticipantThumbnail)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ParticipantThumbnail", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*gmproto.UpdateConversationResponse, error) {
|
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*gmproto.UpdateConversationResponse, error) {
|
||||||
|
@ -165,17 +75,7 @@ func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*gmproto.
|
||||||
|
|
||||||
actionType := gmproto.ActionType_UPDATE_CONVERSATION
|
actionType := gmproto.ActionType_UPDATE_CONVERSATION
|
||||||
|
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
return typedResponse[*gmproto.UpdateConversationResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.UpdateConversationResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.UpdateConversationResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetTyping(convID string) error {
|
func (c *Client) SetTyping(convID string) error {
|
||||||
|
|
|
@ -1,42 +1,19 @@
|
||||||
package libgm
|
package libgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) SendReaction(payload *gmproto.SendReactionPayload) (*gmproto.SendReactionResponse, error) {
|
func (c *Client) SendReaction(payload *gmproto.SendReactionPayload) (*gmproto.SendReactionResponse, error) {
|
||||||
actionType := gmproto.ActionType_SEND_REACTION
|
actionType := gmproto.ActionType_SEND_REACTION
|
||||||
|
return typedResponse[*gmproto.SendReactionResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.SendReactionResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.SendReactionResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteMessage(messageID string) (*gmproto.DeleteMessageResponse, error) {
|
func (c *Client) DeleteMessage(messageID string) (*gmproto.DeleteMessageResponse, error) {
|
||||||
payload := &gmproto.DeleteMessagePayload{MessageID: messageID}
|
payload := &gmproto.DeleteMessagePayload{MessageID: messageID}
|
||||||
actionType := gmproto.ActionType_DELETE_MESSAGE
|
actionType := gmproto.ActionType_DELETE_MESSAGE
|
||||||
|
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
return typedResponse[*gmproto.DeleteMessageResponse](c.sessionHandler.sendMessage(actionType, payload))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.DeleteMessageResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.DeleteMessagesResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) MarkRead(conversationID, messageID string) error {
|
func (c *Client) MarkRead(conversationID, messageID string) error {
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package libgm
|
package libgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,20 +12,8 @@ func (c *Client) SetActiveSession() error {
|
||||||
|
|
||||||
func (c *Client) IsBugleDefault() (*gmproto.IsBugleDefaultResponse, error) {
|
func (c *Client) IsBugleDefault() (*gmproto.IsBugleDefaultResponse, error) {
|
||||||
c.sessionHandler.ResetSessionID()
|
c.sessionHandler.ResetSessionID()
|
||||||
|
|
||||||
actionType := gmproto.ActionType_IS_BUGLE_DEFAULT
|
actionType := gmproto.ActionType_IS_BUGLE_DEFAULT
|
||||||
|
return typedResponse[*gmproto.IsBugleDefaultResponse](c.sessionHandler.sendMessage(actionType, nil))
|
||||||
response, err := c.sessionHandler.sendMessage(actionType, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, ok := response.DecryptedMessage.(*gmproto.IsBugleDefaultResponse)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.IsBugleDefaultResponse", response.DecryptedMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) NotifyDittoActivity() error {
|
func (c *Client) NotifyDittoActivity() error {
|
||||||
|
|
|
@ -61,6 +61,19 @@ func (s *SessionHandler) sendAsyncMessage(actionType gmproto.ActionType, encrypt
|
||||||
return ch, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func typedResponse[T proto.Message](resp *IncomingRPCMessage, err error) (casted T, retErr error) {
|
||||||
|
if err != nil {
|
||||||
|
retErr = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ok bool
|
||||||
|
casted, ok = resp.DecryptedMessage.(T)
|
||||||
|
if !ok {
|
||||||
|
retErr = fmt.Errorf("unexpected response type %T, expected %T", resp.DecryptedMessage, casted)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SessionHandler) sendMessage(actionType gmproto.ActionType, encryptedData proto.Message) (*IncomingRPCMessage, error) {
|
func (s *SessionHandler) sendMessage(actionType gmproto.ActionType, encryptedData proto.Message) (*IncomingRPCMessage, error) {
|
||||||
ch, err := s.sendAsyncMessage(actionType, encryptedData)
|
ch, err := s.sendAsyncMessage(actionType, encryptedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue