Skip to content

Commit

Permalink
send reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Dec 19, 2024
1 parent 968a6bc commit e664dcd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/connector/handlegchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (c *GChatClient) handleReaction(ctx context.Context, evt *proto.Event) {
Sender: networkid.UserID(sender),
},
},
EmojiID: networkid.EmojiID(reaction.Emoji.GetUnicode()),
Emoji: reaction.Emoji.GetUnicode(),
TargetMessage: networkid.MessageID(messageId),
})
Expand Down
71 changes: 70 additions & 1 deletion pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"context"
"time"

"google.golang.org/protobuf/encoding/prototext"
"maunium.net/go/mautrix/bridgev2"
Expand All @@ -13,6 +14,10 @@ import (
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
)

var (
_ bridgev2.ReactionHandlingNetworkAPI = (*GChatClient)(nil)
)

func portalToGroupId(portal *bridgev2.Portal) (*proto.GroupId, error) {
groupId := &proto.GroupId{}
err := prototext.Unmarshal([]byte(portal.ID), groupId)
Expand Down Expand Up @@ -85,6 +90,7 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
}

var msgID string
var timestamp int64

if msg.ThreadRoot != nil {
threadId := ptr.Ptr(string(msg.ThreadRoot.ID))
Expand Down Expand Up @@ -112,6 +118,7 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
return nil, err
}
msgID = *res.Message.Id.MessageId
timestamp = *res.Message.CreateTime
} else {
req := &proto.CreateTopicRequest{
GroupId: groupId,
Expand All @@ -124,13 +131,75 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
return nil, err
}
msgID = *res.Topic.Id.TopicId
timestamp = *res.Topic.CreateTimeUsec
}

msg.AddPendingToIgnore(networkid.TransactionID(msgID))
return &bridgev2.MatrixMessageResponse{
DB: &database.Message{
ID: networkid.MessageID(msgID),
ID: networkid.MessageID(msgID),
Timestamp: time.UnixMicro(timestamp),
},
RemovePending: networkid.TransactionID(msgID),
}, nil
}

func (c *GChatClient) PreHandleMatrixReaction(_ context.Context, msg *bridgev2.MatrixReaction) (bridgev2.MatrixReactionPreResponse, error) {
emoji := msg.Content.RelatesTo.Key
return bridgev2.MatrixReactionPreResponse{
SenderID: networkid.UserID(c.userLogin.ID),
EmojiID: networkid.EmojiID(emoji),
Emoji: emoji,
}, nil
}

func (c *GChatClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (*database.Reaction, error) {
return nil, c.doHandleMatrixReaction(ctx, msg.Portal,
string(msg.TargetMessage.ThreadRoot),
string(msg.TargetMessage.ID), msg.PreHandleResp.Emoji, proto.UpdateReactionRequest_ADD)
}

func (c *GChatClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error {
dbMsg, err := c.userLogin.Bridge.DB.Message.GetLastPartByID(ctx, c.userLogin.ID, msg.TargetReaction.MessageID)
if err != nil {
return err
}
var topicId string
if dbMsg != nil {
topicId = string(dbMsg.ThreadRoot)
}
return c.doHandleMatrixReaction(ctx, msg.Portal,
topicId,
string(msg.TargetReaction.MessageID), string(msg.TargetReaction.EmojiID), proto.UpdateReactionRequest_REMOVE)
}

func (c *GChatClient) doHandleMatrixReaction(ctx context.Context, portal *bridgev2.Portal, topicId, messageId, emoji string, typ proto.UpdateReactionRequest_ReactionUpdateType) error {
groupId, err := portalToGroupId(portal)
if err != nil {
return err
}

if topicId == "" {
topicId = messageId
}
_, err = c.client.UpdateReaction(ctx, &proto.UpdateReactionRequest{
MessageId: &proto.MessageId{
ParentId: &proto.MessageParentId{
Parent: &proto.MessageParentId_TopicId{
TopicId: &proto.TopicId{
GroupId: groupId,
TopicId: &topicId,
},
},
},
MessageId: &messageId,
},
Emoji: &proto.Emoji{
Content: &proto.Emoji_Unicode{
Unicode: emoji,
},
},
Type: typ.Enum(),
})
return err
}
6 changes: 6 additions & 0 deletions pkg/gchatmeow/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ func (c *Client) CatchUpGroup(ctx context.Context, request *proto.CatchUpGroupRe
return response, c.gcRequest(ctx, "catch_up_group", request, response)
}

func (c *Client) UpdateReaction(ctx context.Context, request *proto.UpdateReactionRequest) (*proto.UpdateReactionResponse, error) {
request.RequestHeader = c.gcRequestHeader
response := &proto.UpdateReactionResponse{}
return response, c.gcRequest(ctx, "update_reaction", request, response)
}

func (c *Client) UploadFile(ctx context.Context, data []byte, groupId string, fileName string, mimeType string) (*proto.UploadMetadata, error) {
headers := http.Header{
"x-goog-upload-protocol": {"resumable"},
Expand Down

0 comments on commit e664dcd

Please sign in to comment.