Skip to content

Commit

Permalink
render/send deleted msg
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Dec 25, 2024
1 parent 9fac411 commit af40085
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
7 changes: 7 additions & 0 deletions pkg/connector/handlegchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
Data: msg,
ConvertEditFunc: c.ConvertEdit,
})
case proto.Event_MESSAGE_DELETED:
msg := evt.Body.GetMessageDeleted()
eventMeta := c.makeEventMeta(evt, bridgev2.RemoteEventMessageRemove, "", msg.Timestamp)
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Message[*proto.Message]{
EventMeta: eventMeta,
TargetMessage: networkid.MessageID(msg.MessageId.MessageId),
})
}

c.setPortalRevision(ctx, evt)
Expand Down
72 changes: 24 additions & 48 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

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

func portalToGroupId(portal *bridgev2.Portal) (*proto.GroupId, error) {
Expand Down Expand Up @@ -72,17 +73,7 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
topicId = string(msg.ThreadRoot.ID)
}
messageInfo.ReplyTo = &proto.SendReplyTarget{
Id: &proto.MessageId{
ParentId: &proto.MessageParentId{
Parent: &proto.MessageParentId_TopicId{
TopicId: &proto.TopicId{
GroupId: groupId,
TopicId: topicId,
},
},
},
MessageId: replyToId,
},
Id: c.makeMessageId(msg.Portal, topicId, replyToId),
CreateTime: msg.ReplyTo.Timestamp.UnixMicro(),
}
}
Expand Down Expand Up @@ -146,30 +137,15 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
}

func (c *GChatClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.MatrixEdit) error {
groupId, err := portalToGroupId(msg.Portal)
if err != nil {
return err
}

text, entities := c.msgConv.ToGChat(ctx, msg.Content)
msgId := string(msg.EditTarget.ID)
threadId := string(msg.EditTarget.ThreadRoot)
topicId := msgId
if threadId != "" {
topicId = threadId
}
res, err := c.client.EditMessage(ctx, &proto.EditMessageRequest{
MessageId: &proto.MessageId{
ParentId: &proto.MessageParentId{
Parent: &proto.MessageParentId_TopicId{
TopicId: &proto.TopicId{
GroupId: groupId,
TopicId: topicId,
},
},
},
MessageId: msgId,
},
_, err := c.client.EditMessage(ctx, &proto.EditMessageRequest{
MessageId: c.makeMessageId(msg.Portal, topicId, msgId),
TextBody: text,
Annotations: entities,
MessageInfo: &proto.MessageInfo{
Expand All @@ -179,7 +155,22 @@ func (c *GChatClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.Matrix
if err != nil {
return err
}
_ = res
return nil
}

func (c *GChatClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) error {
msgId := string(msg.TargetMessage.ID)
threadId := string(msg.TargetMessage.ThreadRoot)
topicId := msgId
if threadId != "" {
topicId = threadId
}
_, err := c.client.DeleteMessage(ctx, &proto.DeleteMessageRequest{
MessageId: c.makeMessageId(msg.Portal, topicId, msgId),
})
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -213,26 +204,11 @@ func (c *GChatClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridg
}

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,
},
_, err := c.client.UpdateReaction(ctx, &proto.UpdateReactionRequest{
MessageId: c.makeMessageId(portal, topicId, messageId),
Emoji: &proto.Emoji{
Content: &proto.Emoji_Unicode{
Unicode: emoji,
Expand Down
18 changes: 18 additions & 0 deletions pkg/connector/ids.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connector

import (
"google.golang.org/protobuf/encoding/prototext"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/networkid"

"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
Expand All @@ -12,3 +14,19 @@ func (c *GChatClient) makePortalKey(evt *proto.Event) networkid.PortalKey {
Receiver: c.userLogin.ID,
}
}

func (c *GChatClient) makeMessageId(portal *bridgev2.Portal, topicId, msgId string) *proto.MessageId {
groupId := &proto.GroupId{}
prototext.Unmarshal([]byte(portal.ID), groupId)
return &proto.MessageId{
ParentId: &proto.MessageParentId{
Parent: &proto.MessageParentId_TopicId{
TopicId: &proto.TopicId{
GroupId: groupId,
TopicId: topicId,
},
},
},
MessageId: msgId,
}
}
6 changes: 6 additions & 0 deletions pkg/gchatmeow/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (c *Client) EditMessage(ctx context.Context, request *proto.EditMessageRequ
return response, c.gcRequest(ctx, "edit_message", request, response)
}

func (c *Client) DeleteMessage(ctx context.Context, request *proto.DeleteMessageRequest) (*proto.DeleteMessageResponse, error) {
request.RequestHeader = c.gcRequestHeader
response := &proto.DeleteMessageResponse{}
return response, c.gcRequest(ctx, "delete_message", request, response)
}

func (c *Client) GetGroup(ctx context.Context, request *proto.GetGroupRequest) (*proto.GetGroupResponse, error) {
request.RequestHeader = c.gcRequestHeader
response := &proto.GetGroupResponse{}
Expand Down
5 changes: 2 additions & 3 deletions pkg/gchatmeow/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

const (
connectTimeout = 30 * time.Second
requestTimeout = 30 * time.Second
timeout = 90 * time.Second
maxRetries = 3
originURL = "https://chat.google.com"
latestChromeVer = "114"
Expand Down Expand Up @@ -94,7 +93,7 @@ func NewSession(cookies *Cookies, proxyURL string, userAgent string) (*Session,
TLSClientConfig: nil, // equivalent to ssl=False in Python
DisableCompression: true,
},
Timeout: connectTimeout,
Timeout: timeout,
}

return &Session{
Expand Down

0 comments on commit af40085

Please sign in to comment.