2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-06-30 09:54:08 +00:00
|
|
|
)
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) ListConversations(count int64, folder gmproto.ListConversationsPayload_Folder) (*gmproto.Conversations, error) {
|
|
|
|
payload := &gmproto.ListConversationsPayload{Count: count, Folder: folder}
|
|
|
|
//var actionType gmproto.ActionType
|
2023-07-15 21:26:22 +00:00
|
|
|
//if !c.synced {
|
2023-07-17 13:51:31 +00:00
|
|
|
// actionType = gmproto.ActionType_LIST_CONVERSATIONS_SYNC
|
2023-07-15 21:26:22 +00:00
|
|
|
// c.synced = true
|
|
|
|
//} else {
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_LIST_CONVERSATIONS
|
2023-07-15 21:26:22 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.Conversations)
|
2023-07-09 11:16:52 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.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-17 13:51:31 +00:00
|
|
|
func (c *Client) ListContacts() (*gmproto.ListContactsResponse, error) {
|
|
|
|
payload := &gmproto.ListContactsPayload{
|
2023-07-16 21:51:17 +00:00
|
|
|
I1: 1,
|
|
|
|
I2: 350,
|
|
|
|
I3: 50,
|
|
|
|
}
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_LIST_CONTACTS
|
2023-07-16 21:51:17 +00:00
|
|
|
|
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.ListContactsResponse)
|
2023-07-16 21:51:17 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListContactsResponse", response.Data.Decrypted)
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
|
|
|
|
payload := &gmproto.ListTopContactsPayload{
|
2023-07-16 21:51:17 +00:00
|
|
|
Count: 8,
|
|
|
|
}
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_LIST_TOP_CONTACTS
|
2023-07-16 21:51:17 +00:00
|
|
|
|
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.ListTopContactsResponse)
|
2023-07-16 21:51:17 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListTopContactsResponse", response.Data.Decrypted)
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) GetOrCreateConversation(req *gmproto.GetOrCreateConversationPayload) (*gmproto.GetOrCreateConversationResponse, error) {
|
|
|
|
actionType := gmproto.ActionType_GET_OR_CREATE_CONVERSATION
|
2023-07-16 21:51:17 +00:00
|
|
|
|
2023-07-16 22:52:13 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, req)
|
2023-07-16 21:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.GetOrCreateConversationResponse)
|
2023-07-16 21:51:17 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.GetOrCreateConversationResponse", response.Data.Decrypted)
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) GetConversationType(conversationID string) (*gmproto.GetConversationTypeResponse, error) {
|
|
|
|
payload := &gmproto.ConversationTypePayload{ConversationID: conversationID}
|
|
|
|
actionType := gmproto.ActionType_GET_CONVERSATION_TYPE
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.GetConversationTypeResponse)
|
2023-07-09 11:16:52 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.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-17 13:51:31 +00:00
|
|
|
func (c *Client) FetchMessages(conversationID string, count int64, cursor *gmproto.Cursor) (*gmproto.FetchMessagesResponse, error) {
|
|
|
|
payload := &gmproto.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-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_LIST_MESSAGES
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-06-30 09:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.FetchMessagesResponse)
|
2023-07-09 11:16:52 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.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-17 13:51:31 +00:00
|
|
|
func (c *Client) SendMessage(payload *gmproto.SendMessagePayload) (*gmproto.SendMessageResponse, error) {
|
|
|
|
actionType := gmproto.ActionType_SEND_MESSAGE
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-07-09 11:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.SendMessageResponse)
|
2023-07-09 11:16:52 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.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-17 13:51:31 +00:00
|
|
|
func (c *Client) GetParticipantThumbnail(convID string) (*gmproto.ParticipantThumbnail, error) {
|
|
|
|
payload := &gmproto.GetParticipantThumbnailPayload{ConversationID: convID}
|
|
|
|
actionType := gmproto.ActionType_GET_PARTICIPANTS_THUMBNAIL
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-07-09 17:35:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.ParticipantThumbnail)
|
2023-07-09 17:35:29 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ParticipantThumbnail", response.Data.Decrypted)
|
2023-07-09 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*gmproto.UpdateConversationResponse, error) {
|
|
|
|
data := &gmproto.UpdateConversationPayload{}
|
2023-07-09 17:35:29 +00:00
|
|
|
|
|
|
|
payload, buildErr := convBuilder.Build(data)
|
|
|
|
if buildErr != nil {
|
|
|
|
panic(buildErr)
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_UPDATE_CONVERSATION
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
response, err := c.sessionHandler.sendMessage(actionType, payload)
|
2023-07-09 17:35:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
res, ok := response.Data.Decrypted.(*gmproto.UpdateConversationResponse)
|
2023-07-09 17:35:29 +00:00
|
|
|
if !ok {
|
2023-07-17 13:51:31 +00:00
|
|
|
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.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-17 13:51:31 +00:00
|
|
|
payload := &gmproto.TypingUpdatePayload{Data: &gmproto.SetTypingIn{ConversationID: convID, Typing: true}}
|
|
|
|
actionType := gmproto.ActionType_TYPING_UPDATES
|
2023-07-15 16:48:11 +00:00
|
|
|
|
2023-07-15 22:45:57 +00:00
|
|
|
_, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
|
|
return err
|
2023-07-15 16:48:11 +00:00
|
|
|
}
|