2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/binary"
|
|
|
|
)
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) ListConversations(count int64) (*binary.Conversations, error) {
|
2023-07-09 11:16:52 +00:00
|
|
|
payload := &binary.ListCoversationsPayload{Count: count, Field4: 1}
|
2023-07-15 21:26:22 +00:00
|
|
|
//var actionType binary.ActionType
|
|
|
|
//if !c.synced {
|
|
|
|
// actionType = binary.ActionType_LIST_CONVERSATIONS_SYNC
|
|
|
|
// c.synced = true
|
|
|
|
//} else {
|
|
|
|
actionType := binary.ActionType_LIST_CONVERSATIONS
|
|
|
|
|
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*binary.Conversations)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.Conversations", response.Data.Decrypted)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
return res, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) GetConversationType(conversationID string) (*binary.GetConversationTypeResponse, error) {
|
|
|
|
payload := &binary.ConversationTypePayload{ConversationID: conversationID}
|
2023-07-09 11:16:52 +00:00
|
|
|
actionType := binary.ActionType_GET_CONVERSATION_TYPE
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-07-09 11:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*binary.GetConversationTypeResponse)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.GetConversationTypeResponse", response.Data.Decrypted)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
return res, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) FetchMessages(conversationID string, count int64, cursor *binary.Cursor) (*binary.FetchMessagesResponse, error) {
|
|
|
|
payload := &binary.FetchConversationMessagesPayload{ConversationID: conversationID, Count: count}
|
2023-07-09 11:16:52 +00:00
|
|
|
if cursor != nil {
|
|
|
|
payload.Cursor = cursor
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
actionType := binary.ActionType_LIST_MESSAGES
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*binary.FetchMessagesResponse)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.FetchMessagesResponse", response.Data.Decrypted)
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) SendMessage(payload *binary.SendMessagePayload) (*binary.SendMessageResponse, error) {
|
2023-07-09 11:16:52 +00:00
|
|
|
actionType := binary.ActionType_SEND_MESSAGE
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-07-09 11:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*binary.SendMessageResponse)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.SendMessageResponse", response.Data.Decrypted)
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
return res, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) GetParticipantThumbnail(convID string) (*binary.ParticipantThumbnail, error) {
|
2023-07-09 17:35:29 +00:00
|
|
|
payload := &binary.GetParticipantThumbnailPayload{ConversationID: convID}
|
|
|
|
actionType := binary.ActionType_GET_PARTICIPANTS_THUMBNAIL
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 17:35:29 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-07-09 17:35:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := response.Data.Decrypted.(*binary.ParticipantThumbnail)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.ParticipantThumbnail", response.Data.Decrypted)
|
2023-07-09 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*binary.UpdateConversationResponse, error) {
|
2023-07-09 17:35:29 +00:00
|
|
|
data := &binary.UpdateConversationPayload{}
|
|
|
|
|
|
|
|
payload, buildErr := convBuilder.Build(data)
|
|
|
|
if buildErr != nil {
|
|
|
|
panic(buildErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
actionType := binary.ActionType_UPDATE_CONVERSATION
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-09 17:35:29 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return nil, sendErr
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
response, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-07-09 17:35:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := response.Data.Decrypted.(*binary.UpdateConversationResponse)
|
|
|
|
if !ok {
|
2023-07-15 21:16:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *binary.UpdateConversationResponse", response.Data.Decrypted)
|
2023-07-09 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2023-07-15 16:48:11 +00:00
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
func (c *Client) SetTyping(convID string) error {
|
2023-07-15 16:48:11 +00:00
|
|
|
payload := &binary.TypingUpdatePayload{Data: &binary.SetTypingIn{ConversationID: convID, Typing: true}}
|
|
|
|
actionType := binary.ActionType_TYPING_UPDATES
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
sentRequestId, sendErr := c.sessionHandler.completeSendMessage(actionType, true, payload)
|
2023-07-15 16:48:11 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return sendErr
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:26:22 +00:00
|
|
|
_, err := c.sessionHandler.WaitForResponse(sentRequestId, actionType)
|
2023-07-15 16:48:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|