From 39816629d693ed69bb611be349b3978767e7694a Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Sat, 7 Dec 2024 22:57:51 +0900 Subject: [PATCH] support sending text msg --- pkg/connector/client.go | 4 ++-- pkg/connector/handlematrix.go | 35 ++++++++++++++++++++++++++++++++++- pkg/gchatmeow/api.go | 7 +++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 272ffff..8278f91 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -34,7 +34,7 @@ func (c *GChatClient) Disconnect() { } func (c *GChatClient) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *bridgev2.NetworkRoomCapabilities { - return nil + return &bridgev2.NetworkRoomCapabilities{} } func (c *GChatClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) { @@ -46,7 +46,7 @@ func (c *GChatClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (* } func (c *GChatClient) IsLoggedIn() bool { - return false + return true } func (c *GChatClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool { diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index f345327..fab0680 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -3,9 +3,42 @@ package connector import ( "context" + "go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" + "google.golang.org/protobuf/encoding/prototext" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" ) +func portalToGroupId(portal *bridgev2.Portal) (*proto.GroupId, error) { + groupId := &proto.GroupId{} + err := prototext.Unmarshal([]byte(portal.ID), groupId) + if err != nil { + return nil, err + } + + return groupId, nil +} + func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (message *bridgev2.MatrixMessageResponse, err error) { - return nil, nil + groupId, err := portalToGroupId(msg.Portal) + if err != nil { + return nil, err + } + + res, err := c.Client.CreateTopic(ctx, &proto.CreateTopicRequest{ + GroupId: groupId, + TextBody: &msg.Content.Body, + }) + if err != nil { + return nil, err + } + msgID := *res.Topic.Id.TopicId + msg.AddPendingToIgnore(networkid.TransactionID(msgID)) + return &bridgev2.MatrixMessageResponse{ + DB: &database.Message{ + ID: networkid.MessageID(msgID), + }, + RemovePending: networkid.TransactionID(msgID), + }, nil } diff --git a/pkg/gchatmeow/api.go b/pkg/gchatmeow/api.go index eca93b8..5928b12 100644 --- a/pkg/gchatmeow/api.go +++ b/pkg/gchatmeow/api.go @@ -126,3 +126,10 @@ func (c *Client) paginatedWorld(ctx context.Context) (*proto.PaginatedWorldRespo err := c.gcRequest(ctx, "paginated_world", request, response) return response, err } + +func (c *Client) CreateTopic(ctx context.Context, request *proto.CreateTopicRequest) (*proto.CreateTopicResponse, error) { + request.RequestHeader = c.gcRequestHeader + response := &proto.CreateTopicResponse{} + err := c.gcRequest(ctx, "create_topic", request, response) + return response, err +}