Skip to content

Commit

Permalink
render mention
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Dec 22, 2024
1 parent 5616408 commit 84129f4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
8 changes: 7 additions & 1 deletion pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions pkg/connector/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -20,6 +25,7 @@ func (c *GChatClient) gcMembersToMatrix(gcMembers []*proto.UserId) *bridgev2.Cha
}

return &bridgev2.ChatMemberList{
MemberMap: memberMap,
MemberMap: memberMap,
OtherUserID: networkid.UserID(otherUserId),
}
}
33 changes: 33 additions & 0 deletions pkg/msgconv/gchatfmt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -180,6 +181,38 @@ func annotationsToMatrix(
default:
skipEntity = true
}
} else if annotation.GetUrlMetadata() != nil {
fmt.Fprintf(&bodyHtml, "<a href='%s'>%s</a>", 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,
`<a href="%s">%s</a>`,
dmPortals[0].MXID.URI().MatrixToURL(),
dmPortals[0].Name,
)
} else {
userLogin := portal.Bridge.GetCachedUserLoginByID(networkid.UserLoginID(gcid))
if userLogin != nil {
fmt.Fprintf(&bodyHtml,
`<a href="%s">%s</a>`,
userLogin.UserMXID.URI().MatrixToURL(),
entityText,
)

} else {
bodyHtml.WriteString(entityText)
}
}
}
} else {
skipEntity = true
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/msgconv/gchatfmt/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ 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"
)

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,
},
},
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 84129f4

Please sign in to comment.