From 84129f415655a0addf9af722650a94984264bdbd 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/login.go | 2 +- pkg/connector/mapping.go | 10 +++++++-- pkg/msgconv/gchatfmt/convert.go | 33 ++++++++++++++++++++++++++++ pkg/msgconv/gchatfmt/convert_test.go | 13 +++++------ 5 files changed, 55 insertions(+), 11 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/login.go b/pkg/connector/login.go index ef981d0..c12f304 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -105,7 +105,7 @@ func (gl *GChatCookieLogin) SubmitCookies(ctx context.Context, strCookies map[st return &bridgev2.LoginStep{ Type: bridgev2.LoginStepTypeComplete, StepID: LoginStepIDComplete, - Instructions: fmt.Sprintf("Logged in as %s (%d)", user.GetName(), userId), + Instructions: fmt.Sprintf("Logged in as %s (%s)", user.GetName(), userId), CompleteParams: &bridgev2.LoginCompleteParams{ UserLoginID: ul.ID, UserLogin: ul, 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 } diff --git a/pkg/msgconv/gchatfmt/convert_test.go b/pkg/msgconv/gchatfmt/convert_test.go index 1e0cc74..a9956fa 100644 --- a/pkg/msgconv/gchatfmt/convert_test.go +++ b/pkg/msgconv/gchatfmt/convert_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "go.mau.fi/util/ptr" "go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto" "go.mau.fi/mautrix-googlechat/pkg/msgconv/gchatfmt" @@ -13,13 +12,13 @@ import ( func makeAnnotation(start, length int32, format proto.FormatMetadata_FormatType) *proto.Annotation { return &proto.Annotation{ - Type: proto.AnnotationType_FORMAT_DATA.Enum(), - StartIndex: ptr.Ptr(start), - Length: ptr.Ptr(length), - ChipRenderType: ptr.Ptr(proto.Annotation_DO_NOT_RENDER), + Type: proto.AnnotationType_FORMAT_DATA, + StartIndex: start, + Length: length, + ChipRenderType: proto.Annotation_DO_NOT_RENDER, Metadata: &proto.Annotation_FormatMetadata{ FormatMetadata: &proto.FormatMetadata{ - FormatType: format.Enum(), + FormatType: format, }, }, } @@ -66,7 +65,7 @@ func TestParse(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { msg := &proto.Message{ - TextBody: ptr.Ptr(test.ins), + TextBody: test.ins, Annotations: test.ine, } parsed := gchatfmt.Parse(context.TODO(), nil, msg)