gmessages/libgm/conversations.go

185 lines
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"
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
type Conversations struct {
client *Client
synced bool
2023-06-30 09:54:08 +00:00
}
// default is 25 count
2023-06-30 09:54:08 +00:00
func (c *Conversations) List(count int64) (*binary.Conversations, error) {
payload := &binary.ListCoversationsPayload{Count: count, Field4: 1}
var actionType binary.ActionType
2023-06-30 09:54:08 +00:00
if !c.synced {
actionType = binary.ActionType_LIST_CONVERSATIONS_SYNC
c.synced = true
2023-06-30 09:54:08 +00:00
} else {
actionType = binary.ActionType_LIST_CONVERSATIONS
2023-06-30 09:54:08 +00:00
}
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
2023-06-30 09:54:08 +00:00
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
2023-06-30 09:54:08 +00:00
if err != nil {
return nil, err
}
res, ok := response.Data.Decrypted.(*binary.Conversations)
if !ok {
return nil, fmt.Errorf("failed to assert response into Conversations")
2023-06-30 09:54:08 +00:00
}
return res, nil
2023-06-30 09:54:08 +00:00
}
func (c *Conversations) GetType(conversationId string) (*binary.GetConversationTypeResponse, error) {
payload := &binary.ConversationTypePayload{ConversationID: conversationId}
actionType := binary.ActionType_GET_CONVERSATION_TYPE
2023-06-30 09:54:08 +00:00
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
2023-06-30 09:54:08 +00:00
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
res, ok := response.Data.Decrypted.(*binary.GetConversationTypeResponse)
if !ok {
return nil, fmt.Errorf("failed to assert response into GetConversationTypeResponse")
2023-06-30 09:54:08 +00:00
}
return res, nil
2023-06-30 09:54:08 +00:00
}
func (c *Conversations) FetchMessages(conversationId string, count int64, cursor *binary.Cursor) (*binary.FetchMessagesResponse, error) {
payload := &binary.FetchConversationMessagesPayload{ConversationID: conversationId, Count: count}
if cursor != nil {
payload.Cursor = cursor
2023-06-30 09:54:08 +00:00
}
actionType := binary.ActionType_LIST_MESSAGES
2023-06-30 09:54:08 +00:00
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
2023-06-30 09:54:08 +00:00
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
2023-06-30 09:54:08 +00:00
if err != nil {
return nil, err
}
res, ok := response.Data.Decrypted.(*binary.FetchMessagesResponse)
if !ok {
return nil, fmt.Errorf("failed to assert response into FetchMessagesResponse")
}
2023-06-30 09:54:08 +00:00
return res, nil
}
2023-06-30 09:54:08 +00:00
func (c *Conversations) SendMessage(messageBuilder *MessageBuilder) (*binary.SendMessageResponse, error) {
payload, failedToBuild := messageBuilder.Build()
if failedToBuild != nil {
return nil, failedToBuild
2023-06-30 09:54:08 +00:00
}
actionType := binary.ActionType_SEND_MESSAGE
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
2023-06-30 09:54:08 +00:00
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
res, ok := response.Data.Decrypted.(*binary.SendMessageResponse)
if !ok {
return nil, fmt.Errorf("failed to assert response into SendMessageResponse")
2023-06-30 09:54:08 +00:00
}
c.client.Logger.Debug().Any("res", res).Msg("sent message!")
return res, nil
2023-06-30 09:54:08 +00:00
}
func (c *Conversations) 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)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
res, ok := response.Data.Decrypted.(*binary.ParticipantThumbnail)
if !ok {
return nil, fmt.Errorf("failed to assert response into ParticipantThumbnail")
}
return res, nil
}
func (c *Conversations) Update(convBuilder *ConversationBuilder) (*binary.UpdateConversationResponse, error) {
data := &binary.UpdateConversationPayload{}
payload, buildErr := convBuilder.Build(data)
if buildErr != nil {
panic(buildErr)
}
actionType := binary.ActionType_UPDATE_CONVERSATION
sentRequestId, sendErr := c.client.sessionHandler.completeSendMessage(actionType, true, payload)
if sendErr != nil {
return nil, sendErr
}
response, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return nil, err
}
res, ok := response.Data.Decrypted.(*binary.UpdateConversationResponse)
if !ok {
return nil, fmt.Errorf("failed to assert response into UpdateConversationResponse")
}
return res, nil
}
func (c *Conversations) 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)
if sendErr != nil {
return sendErr
}
_, err := c.client.sessionHandler.WaitForResponse(sentRequestId, actionType)
if err != nil {
return err
}
return nil
}