From 9431bbb63031a3036ca3e78a4803ea6c08497f5f Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Thu, 26 Dec 2024 10:21:50 +0900 Subject: [PATCH] render/send typing --- pkg/connector/handlegchat.go | 5 +++++ pkg/connector/handlematrix.go | 35 ++++++++++++++++++++++++----------- pkg/gchatmeow/api.go | 6 ++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/connector/handlegchat.go b/pkg/connector/handlegchat.go index 3541f97..3335148 100644 --- a/pkg/connector/handlegchat.go +++ b/pkg/connector/handlegchat.go @@ -60,6 +60,11 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) { EventMeta: eventMeta, TargetMessage: networkid.MessageID(msg.MessageId.MessageId), }) + case proto.Event_TYPING_STATE_CHANGED: + state := evt.Body.GetTypingStateChanged() + c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Message[*proto.Message]{ + EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventTyping, state.UserId.Id, state.StartTimestampUsec), + }) } c.setPortalRevision(ctx, evt) diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index 96c1df3..bf3e13c 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -16,23 +16,17 @@ var ( _ bridgev2.EditHandlingNetworkAPI = (*GChatClient)(nil) _ bridgev2.ReactionHandlingNetworkAPI = (*GChatClient)(nil) _ bridgev2.RedactionHandlingNetworkAPI = (*GChatClient)(nil) + _ bridgev2.TypingHandlingNetworkAPI = (*GChatClient)(nil) ) -func portalToGroupId(portal *bridgev2.Portal) (*proto.GroupId, error) { +func portalToGroupId(portal *bridgev2.Portal) *proto.GroupId { groupId := &proto.GroupId{} - err := prototext.Unmarshal([]byte(portal.ID), groupId) - if err != nil { - return nil, err - } - - return groupId, nil + prototext.Unmarshal([]byte(portal.ID), groupId) + return groupId } func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (message *bridgev2.MatrixMessageResponse, err error) { - groupId, err := portalToGroupId(msg.Portal) - if err != nil { - return nil, err - } + groupId := portalToGroupId(msg.Portal) var plainGroupId string if groupId.GetDmId() != nil { @@ -218,3 +212,22 @@ func (c *GChatClient) doHandleMatrixReaction(ctx context.Context, portal *bridge }) return err } + +func (c *GChatClient) HandleMatrixTyping(ctx context.Context, msg *bridgev2.MatrixTyping) error { + if msg.Type == bridgev2.TypingTypeText { + state := proto.TypingState_STOPPED + if msg.IsTyping { + state = proto.TypingState_TYPING + } + _, err := c.client.SetTypingState(ctx, &proto.SetTypingStateRequest{ + Context: &proto.TypingContext{ + Context: &proto.TypingContext_GroupId{ + GroupId: portalToGroupId(msg.Portal), + }, + }, + State: state, + }) + return err + } + return nil +} diff --git a/pkg/gchatmeow/api.go b/pkg/gchatmeow/api.go index 1eade8b..8bba57f 100644 --- a/pkg/gchatmeow/api.go +++ b/pkg/gchatmeow/api.go @@ -174,6 +174,12 @@ func (c *Client) UpdateReaction(ctx context.Context, request *proto.UpdateReacti return response, c.gcRequest(ctx, "update_reaction", request, response) } +func (c *Client) SetTypingState(ctx context.Context, request *proto.SetTypingStateRequest) (*proto.SetTypingStateResponse, error) { + request.RequestHeader = c.gcRequestHeader + response := &proto.SetTypingStateResponse{} + return response, c.gcRequest(ctx, "set_typing_state", 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"},