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 c016f5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 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
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

0 comments on commit c016f5d

Please sign in to comment.