From c016f5dff3a6b9bb221eb9f35dac0375794135fa Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Sun, 22 Dec 2024 11:06:46 +0900 Subject: [PATCH] render mention --- pkg/connector/client.go | 8 +++++++- pkg/connector/mapping.go | 10 ++++++++-- pkg/msgconv/gchatfmt/convert.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index c5042dd..2e80b72 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -7,6 +7,7 @@ import ( "time" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/bridgev2/simplevent" @@ -125,8 +126,12 @@ func (c *GChatClient) onConnect(ctx context.Context) { for _, item := range res.WorldItems { name := item.RoomName var gcMembers []*proto.UserId + roomType := database.RoomTypeGroupDM + isDm := false if item.DmMembers != nil { + roomType = database.RoomTypeDM gcMembers = item.DmMembers.Members + isDm = true for _, member := range item.DmMembers.Members { if member.Id != string(c.userLogin.ID) { name = c.users[member.Id].Name @@ -162,7 +167,8 @@ func (c *GChatClient) onConnect(ctx context.Context) { }, ChatInfo: &bridgev2.ChatInfo{ Name: &name, - Members: c.gcMembersToMatrix(gcMembers), + Members: c.gcMembersToMatrix(isDm, gcMembers), + Type: &roomType, }, }) diff --git a/pkg/connector/mapping.go b/pkg/connector/mapping.go index 9133bc6..c7815d6 100644 --- a/pkg/connector/mapping.go +++ b/pkg/connector/mapping.go @@ -7,10 +7,15 @@ import ( "go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" ) -func (c *GChatClient) gcMembersToMatrix(gcMembers []*proto.UserId) *bridgev2.ChatMemberList { +func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *bridgev2.ChatMemberList { + var otherUserId string memberMap := map[networkid.UserID]bridgev2.ChatMember{} for _, gcMember := range gcMembers { userId := networkid.UserID(gcMember.Id) + if isDm && gcMember.Id != string(c.userLogin.ID) { + otherUserId = gcMember.Id + + } memberMap[userId] = bridgev2.ChatMember{ EventSender: bridgev2.EventSender{ IsFromMe: gcMember.Id == string(c.userLogin.ID), @@ -20,6 +25,7 @@ func (c *GChatClient) gcMembersToMatrix(gcMembers []*proto.UserId) *bridgev2.Cha } return &bridgev2.ChatMemberList{ - MemberMap: memberMap, + MemberMap: memberMap, + OtherUserID: networkid.UserID(otherUserId), } } diff --git a/pkg/msgconv/gchatfmt/convert.go b/pkg/msgconv/gchatfmt/convert.go index e8a9e73..d35458c 100644 --- a/pkg/msgconv/gchatfmt/convert.go +++ b/pkg/msgconv/gchatfmt/convert.go @@ -9,6 +9,7 @@ import ( pb "google.golang.org/protobuf/proto" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/event" "go.mau.fi/mautrix-googlechat/pkg/gchatmeow" @@ -180,6 +181,38 @@ func annotationsToMatrix( default: skipEntity = true } + } else if annotation.GetUrlMetadata() != nil { + fmt.Fprintf(&bodyHtml, "%s", annotation.GetUrlMetadata().Url.Url, entityText) + } else if annotation.GetUserMentionMetadata() != nil { + if annotation.GetUserMentionMetadata().Type == proto.UserMentionMetadata_MENTION_ALL { + bodyHtml.WriteString("@room") + } else { + gcid := annotation.GetUserMentionMetadata().Id.Id + dmPortals, err := portal.Bridge.GetDMPortalsWith(ctx, networkid.UserID(gcid)) + if err != nil { + return "", err + } + + if len(dmPortals) != 0 { + fmt.Fprintf(&bodyHtml, + `%s`, + dmPortals[0].MXID.URI().MatrixToURL(), + dmPortals[0].Name, + ) + } else { + userLogin := portal.Bridge.GetCachedUserLoginByID(networkid.UserLoginID(gcid)) + if userLogin != nil { + fmt.Fprintf(&bodyHtml, + `%s`, + userLogin.UserMXID.URI().MatrixToURL(), + entityText, + ) + + } else { + bodyHtml.WriteString(entityText) + } + } + } } else { skipEntity = true }