Skip to content

Commit af40085

Browse files
committed
render/send deleted msg
1 parent 9fac411 commit af40085

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

pkg/connector/handlegchat.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
5353
Data: msg,
5454
ConvertEditFunc: c.ConvertEdit,
5555
})
56+
case proto.Event_MESSAGE_DELETED:
57+
msg := evt.Body.GetMessageDeleted()
58+
eventMeta := c.makeEventMeta(evt, bridgev2.RemoteEventMessageRemove, "", msg.Timestamp)
59+
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Message[*proto.Message]{
60+
EventMeta: eventMeta,
61+
TargetMessage: networkid.MessageID(msg.MessageId.MessageId),
62+
})
5663
}
5764

5865
c.setPortalRevision(ctx, evt)

pkg/connector/handlematrix.go

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
var (
16-
_ bridgev2.EditHandlingNetworkAPI = (*GChatClient)(nil)
17-
_ bridgev2.ReactionHandlingNetworkAPI = (*GChatClient)(nil)
16+
_ bridgev2.EditHandlingNetworkAPI = (*GChatClient)(nil)
17+
_ bridgev2.ReactionHandlingNetworkAPI = (*GChatClient)(nil)
18+
_ bridgev2.RedactionHandlingNetworkAPI = (*GChatClient)(nil)
1819
)
1920

2021
func portalToGroupId(portal *bridgev2.Portal) (*proto.GroupId, error) {
@@ -72,17 +73,7 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
7273
topicId = string(msg.ThreadRoot.ID)
7374
}
7475
messageInfo.ReplyTo = &proto.SendReplyTarget{
75-
Id: &proto.MessageId{
76-
ParentId: &proto.MessageParentId{
77-
Parent: &proto.MessageParentId_TopicId{
78-
TopicId: &proto.TopicId{
79-
GroupId: groupId,
80-
TopicId: topicId,
81-
},
82-
},
83-
},
84-
MessageId: replyToId,
85-
},
76+
Id: c.makeMessageId(msg.Portal, topicId, replyToId),
8677
CreateTime: msg.ReplyTo.Timestamp.UnixMicro(),
8778
}
8879
}
@@ -146,30 +137,15 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
146137
}
147138

148139
func (c *GChatClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.MatrixEdit) error {
149-
groupId, err := portalToGroupId(msg.Portal)
150-
if err != nil {
151-
return err
152-
}
153-
154140
text, entities := c.msgConv.ToGChat(ctx, msg.Content)
155141
msgId := string(msg.EditTarget.ID)
156142
threadId := string(msg.EditTarget.ThreadRoot)
157143
topicId := msgId
158144
if threadId != "" {
159145
topicId = threadId
160146
}
161-
res, err := c.client.EditMessage(ctx, &proto.EditMessageRequest{
162-
MessageId: &proto.MessageId{
163-
ParentId: &proto.MessageParentId{
164-
Parent: &proto.MessageParentId_TopicId{
165-
TopicId: &proto.TopicId{
166-
GroupId: groupId,
167-
TopicId: topicId,
168-
},
169-
},
170-
},
171-
MessageId: msgId,
172-
},
147+
_, err := c.client.EditMessage(ctx, &proto.EditMessageRequest{
148+
MessageId: c.makeMessageId(msg.Portal, topicId, msgId),
173149
TextBody: text,
174150
Annotations: entities,
175151
MessageInfo: &proto.MessageInfo{
@@ -179,7 +155,22 @@ func (c *GChatClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.Matrix
179155
if err != nil {
180156
return err
181157
}
182-
_ = res
158+
return nil
159+
}
160+
161+
func (c *GChatClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) error {
162+
msgId := string(msg.TargetMessage.ID)
163+
threadId := string(msg.TargetMessage.ThreadRoot)
164+
topicId := msgId
165+
if threadId != "" {
166+
topicId = threadId
167+
}
168+
_, err := c.client.DeleteMessage(ctx, &proto.DeleteMessageRequest{
169+
MessageId: c.makeMessageId(msg.Portal, topicId, msgId),
170+
})
171+
if err != nil {
172+
return err
173+
}
183174
return nil
184175
}
185176

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

215206
func (c *GChatClient) doHandleMatrixReaction(ctx context.Context, portal *bridgev2.Portal, topicId, messageId, emoji string, typ proto.UpdateReactionRequest_ReactionUpdateType) error {
216-
groupId, err := portalToGroupId(portal)
217-
if err != nil {
218-
return err
219-
}
220-
221207
if topicId == "" {
222208
topicId = messageId
223209
}
224-
_, err = c.client.UpdateReaction(ctx, &proto.UpdateReactionRequest{
225-
MessageId: &proto.MessageId{
226-
ParentId: &proto.MessageParentId{
227-
Parent: &proto.MessageParentId_TopicId{
228-
TopicId: &proto.TopicId{
229-
GroupId: groupId,
230-
TopicId: topicId,
231-
},
232-
},
233-
},
234-
MessageId: messageId,
235-
},
210+
_, err := c.client.UpdateReaction(ctx, &proto.UpdateReactionRequest{
211+
MessageId: c.makeMessageId(portal, topicId, messageId),
236212
Emoji: &proto.Emoji{
237213
Content: &proto.Emoji_Unicode{
238214
Unicode: emoji,

pkg/connector/ids.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package connector
22

33
import (
4+
"google.golang.org/protobuf/encoding/prototext"
5+
"maunium.net/go/mautrix/bridgev2"
46
"maunium.net/go/mautrix/bridgev2/networkid"
57

68
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
@@ -12,3 +14,19 @@ func (c *GChatClient) makePortalKey(evt *proto.Event) networkid.PortalKey {
1214
Receiver: c.userLogin.ID,
1315
}
1416
}
17+
18+
func (c *GChatClient) makeMessageId(portal *bridgev2.Portal, topicId, msgId string) *proto.MessageId {
19+
groupId := &proto.GroupId{}
20+
prototext.Unmarshal([]byte(portal.ID), groupId)
21+
return &proto.MessageId{
22+
ParentId: &proto.MessageParentId{
23+
Parent: &proto.MessageParentId_TopicId{
24+
TopicId: &proto.TopicId{
25+
GroupId: groupId,
26+
TopicId: topicId,
27+
},
28+
},
29+
},
30+
MessageId: msgId,
31+
}
32+
}

pkg/gchatmeow/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ func (c *Client) EditMessage(ctx context.Context, request *proto.EditMessageRequ
150150
return response, c.gcRequest(ctx, "edit_message", request, response)
151151
}
152152

153+
func (c *Client) DeleteMessage(ctx context.Context, request *proto.DeleteMessageRequest) (*proto.DeleteMessageResponse, error) {
154+
request.RequestHeader = c.gcRequestHeader
155+
response := &proto.DeleteMessageResponse{}
156+
return response, c.gcRequest(ctx, "delete_message", request, response)
157+
}
158+
153159
func (c *Client) GetGroup(ctx context.Context, request *proto.GetGroupRequest) (*proto.GetGroupResponse, error) {
154160
request.RequestHeader = c.gcRequestHeader
155161
response := &proto.GetGroupResponse{}

pkg/gchatmeow/session.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
)
1414

1515
const (
16-
connectTimeout = 30 * time.Second
17-
requestTimeout = 30 * time.Second
16+
timeout = 90 * time.Second
1817
maxRetries = 3
1918
originURL = "https://chat.google.com"
2019
latestChromeVer = "114"
@@ -94,7 +93,7 @@ func NewSession(cookies *Cookies, proxyURL string, userAgent string) (*Session,
9493
TLSClientConfig: nil, // equivalent to ssl=False in Python
9594
DisableCompression: true,
9695
},
97-
Timeout: connectTimeout,
96+
Timeout: timeout,
9897
}
9998

10099
return &Session{

0 commit comments

Comments
 (0)