2023-06-30 11:05:33 +00:00
|
|
|
package libgm
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
import (
|
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-19 20:33:45 +00:00
|
|
|
func (c *Client) ListConversations(count int, folder gmproto.ListConversationsRequest_Folder) (*gmproto.ListConversationsResponse, error) {
|
2023-07-18 21:59:51 +00:00
|
|
|
msgType := gmproto.MessageType_BUGLE_MESSAGE
|
|
|
|
if !c.conversationsFetchedOnce {
|
|
|
|
msgType = gmproto.MessageType_BUGLE_ANNOTATION
|
|
|
|
c.conversationsFetchedOnce = true
|
|
|
|
}
|
|
|
|
return typedResponse[*gmproto.ListConversationsResponse](c.sessionHandler.sendMessageWithParams(SendMessageParams{
|
|
|
|
Action: gmproto.ActionType_LIST_CONVERSATIONS,
|
2023-07-19 20:33:45 +00:00
|
|
|
Data: &gmproto.ListConversationsRequest{Count: int64(count), Folder: folder},
|
2023-07-18 21:59:51 +00:00
|
|
|
MessageType: msgType,
|
|
|
|
}))
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) ListContacts() (*gmproto.ListContactsResponse, error) {
|
2023-07-17 23:57:20 +00:00
|
|
|
payload := &gmproto.ListContactsRequest{
|
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-17 23:11:43 +00:00
|
|
|
return typedResponse[*gmproto.ListContactsResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) ListTopContacts() (*gmproto.ListTopContactsResponse, error) {
|
2023-07-17 23:57:20 +00:00
|
|
|
payload := &gmproto.ListTopContactsRequest{
|
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-17 23:11:43 +00:00
|
|
|
return typedResponse[*gmproto.ListTopContactsResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 23:57:20 +00:00
|
|
|
func (c *Client) GetOrCreateConversation(req *gmproto.GetOrCreateConversationRequest) (*gmproto.GetOrCreateConversationResponse, error) {
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_GET_OR_CREATE_CONVERSATION
|
2023-07-17 23:11:43 +00:00
|
|
|
return typedResponse[*gmproto.GetOrCreateConversationResponse](c.sessionHandler.sendMessage(actionType, req))
|
2023-07-16 21:51:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) GetConversationType(conversationID string) (*gmproto.GetConversationTypeResponse, error) {
|
2023-08-27 09:35:47 +00:00
|
|
|
payload := &gmproto.GetConversationTypeRequest{ConversationID: conversationID}
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_GET_CONVERSATION_TYPE
|
2023-07-17 23:11:43 +00:00
|
|
|
return typedResponse[*gmproto.GetConversationTypeResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 23:19:25 +00:00
|
|
|
func (c *Client) GetConversation(conversationID string) (*gmproto.Conversation, error) {
|
2023-07-17 23:57:20 +00:00
|
|
|
payload := &gmproto.GetConversationRequest{ConversationID: conversationID}
|
2023-07-17 23:19:25 +00:00
|
|
|
actionType := gmproto.ActionType_GET_CONVERSATION
|
|
|
|
resp, err := typedResponse[*gmproto.GetConversationResponse](c.sessionHandler.sendMessage(actionType, payload))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.GetConversation(), nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:59:51 +00:00
|
|
|
func (c *Client) FetchMessages(conversationID string, count int64, cursor *gmproto.Cursor) (*gmproto.ListMessagesResponse, error) {
|
|
|
|
payload := &gmproto.ListMessagesRequest{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-07-18 21:59:51 +00:00
|
|
|
return typedResponse[*gmproto.ListMessagesResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-07-17 23:57:20 +00:00
|
|
|
func (c *Client) SendMessage(payload *gmproto.SendMessageRequest) (*gmproto.SendMessageResponse, error) {
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_SEND_MESSAGE
|
2023-07-17 23:11:43 +00:00
|
|
|
return typedResponse[*gmproto.SendMessageResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-17 23:57:20 +00:00
|
|
|
func (c *Client) GetParticipantThumbnail(convID string) (*gmproto.GetParticipantThumbnailResponse, error) {
|
|
|
|
payload := &gmproto.GetParticipantThumbnailRequest{ConversationID: convID}
|
2023-07-17 13:51:31 +00:00
|
|
|
actionType := gmproto.ActionType_GET_PARTICIPANTS_THUMBNAIL
|
2023-07-17 23:57:20 +00:00
|
|
|
return typedResponse[*gmproto.GetParticipantThumbnailResponse](c.sessionHandler.sendMessage(actionType, payload))
|
2023-07-09 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 22:17:01 +00:00
|
|
|
func (c *Client) UpdateConversation(payload *gmproto.UpdateConversationRequest) (*gmproto.UpdateConversationResponse, error) {
|
|
|
|
actionType := gmproto.ActionType_UPDATE_CONVERSATION
|
|
|
|
return typedResponse[*gmproto.UpdateConversationResponse](c.sessionHandler.sendMessage(actionType, payload))
|
|
|
|
}
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-18 22:17:01 +00:00
|
|
|
func (c *Client) SendReaction(payload *gmproto.SendReactionRequest) (*gmproto.SendReactionResponse, error) {
|
|
|
|
actionType := gmproto.ActionType_SEND_REACTION
|
|
|
|
return typedResponse[*gmproto.SendReactionResponse](c.sessionHandler.sendMessage(actionType, payload))
|
|
|
|
}
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-18 22:17:01 +00:00
|
|
|
func (c *Client) DeleteMessage(messageID string) (*gmproto.DeleteMessageResponse, error) {
|
|
|
|
payload := &gmproto.DeleteMessageRequest{MessageID: messageID}
|
|
|
|
actionType := gmproto.ActionType_DELETE_MESSAGE
|
2023-07-09 17:35:29 +00:00
|
|
|
|
2023-07-18 22:17:01 +00:00
|
|
|
return typedResponse[*gmproto.DeleteMessageResponse](c.sessionHandler.sendMessage(actionType, payload))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) MarkRead(conversationID, messageID string) error {
|
|
|
|
payload := &gmproto.MessageReadRequest{ConversationID: conversationID, MessageID: messageID}
|
|
|
|
actionType := gmproto.ActionType_MESSAGE_READ
|
|
|
|
|
|
|
|
_, err := c.sessionHandler.sendMessage(actionType, payload)
|
|
|
|
return err
|
2023-07-09 17:35:29 +00:00
|
|
|
}
|
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 23:57:20 +00:00
|
|
|
payload := &gmproto.TypingUpdateRequest{
|
|
|
|
Data: &gmproto.TypingUpdateRequest_Data{ConversationID: convID, Typing: true},
|
|
|
|
}
|
2023-07-17 13:51:31 +00:00
|
|
|
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
|
|
|
}
|
2023-07-18 22:17:01 +00:00
|
|
|
|
|
|
|
func (c *Client) SetActiveSession() error {
|
|
|
|
c.sessionHandler.ResetSessionID()
|
|
|
|
return c.sessionHandler.sendMessageNoResponse(SendMessageParams{
|
|
|
|
Action: gmproto.ActionType_GET_UPDATES,
|
|
|
|
OmitTTL: true,
|
|
|
|
UseSessionID: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) IsBugleDefault() (*gmproto.IsBugleDefaultResponse, error) {
|
|
|
|
actionType := gmproto.ActionType_IS_BUGLE_DEFAULT
|
|
|
|
return typedResponse[*gmproto.IsBugleDefaultResponse](c.sessionHandler.sendMessage(actionType, nil))
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:58:39 +00:00
|
|
|
func (c *Client) NotifyDittoActivity() (<-chan *IncomingRPCMessage, error) {
|
|
|
|
return c.sessionHandler.sendAsyncMessage(SendMessageParams{
|
|
|
|
Action: gmproto.ActionType_NOTIFY_DITTO_ACTIVITY,
|
|
|
|
Data: &gmproto.NotifyDittoActivityRequest{Success: true},
|
|
|
|
})
|
2023-07-18 22:17:01 +00:00
|
|
|
}
|