gmessages/libgm/conversations.go

188 lines
5.5 KiB
Go
Raw Normal View History

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
//if !c.synced {
2023-07-17 13:51:31 +00:00
// actionType = gmproto.ActionType_LIST_CONVERSATIONS_SYNC
// c.synced = true
//} else {
2023-07-17 13:51:31 +00:00
actionType := gmproto.ActionType_LIST_CONVERSATIONS
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)
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
}
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{
I1: 1,
I2: 350,
I3: 50,
}
2023-07-17 13:51:31 +00:00
actionType := gmproto.ActionType_LIST_CONTACTS
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)
if !ok {
2023-07-17 13:51:31 +00:00
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListContactsResponse", response.Data.Decrypted)
}
return res, nil
}
2023-07-17 13:51:31 +00:00
func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
payload := &gmproto.ListTopContactsPayload{
Count: 8,
}
2023-07-17 13:51:31 +00:00
actionType := gmproto.ActionType_LIST_TOP_CONTACTS
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)
if !ok {
2023-07-17 13:51:31 +00:00
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ListTopContactsResponse", response.Data.Decrypted)
}
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 22:52:13 +00:00
response, err := c.sessionHandler.sendMessage(actionType, req)
if err != nil {
return nil, err
}
2023-07-17 13:51:31 +00:00
res, ok := response.Data.Decrypted.(*gmproto.GetOrCreateConversationResponse)
if !ok {
2023-07-17 13:51:31 +00:00
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.GetOrCreateConversationResponse", response.Data.Decrypted)
}
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)
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)
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
}
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}
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)
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-06-30 09:54:08 +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-15 22:45:57 +00:00
response, err := c.sessionHandler.sendMessage(actionType, payload)
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)
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
}
return res, nil
2023-06-30 09:54:08 +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-15 22:45:57 +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.ParticipantThumbnail)
if !ok {
2023-07-17 13:51:31 +00:00
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.ParticipantThumbnail", response.Data.Decrypted)
}
return res, nil
}
2023-07-17 13:51:31 +00:00
func (c *Client) UpdateConversation(convBuilder *ConversationBuilder) (*gmproto.UpdateConversationResponse, error) {
data := &gmproto.UpdateConversationPayload{}
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-15 22:45:57 +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.UpdateConversationResponse)
if !ok {
2023-07-17 13:51:31 +00:00
return nil, fmt.Errorf("unexpected response type %T, expected *gmproto.UpdateConversationResponse", response.Data.Decrypted)
}
return res, nil
}
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 22:45:57 +00:00
_, err := c.sessionHandler.sendMessage(actionType, payload)
return err
}