Skip to content

Commit

Permalink
set room and member avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Jan 1, 2025
1 parent f610532 commit 1557079
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
22 changes: 16 additions & 6 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ func (c *GChatClient) onConnect(ctx context.Context) {
var gcMembers []*proto.UserId
roomType := database.RoomTypeGroupDM
isDm := false
var dmUser *proto.User
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
dmUser = c.users[member.Id]
name = dmUser.Name
break
}
}
Expand All @@ -157,6 +159,18 @@ func (c *GChatClient) onConnect(ctx context.Context) {

}

chatInfo := &bridgev2.ChatInfo{
Name: &name,
Members: c.gcMembersToMatrix(isDm, gcMembers),
Type: &roomType,
}

if dmUser != nil {
chatInfo.Avatar = c.makeAvatar(dmUser.AvatarUrl)
} else if item.AvatarUrl != "" {
chatInfo.Avatar = c.makeAvatar(item.AvatarUrl)
}

c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.ChatResync{
EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventChatResync,
Expand All @@ -166,11 +180,7 @@ func (c *GChatClient) onConnect(ctx context.Context) {
},
CreatePortal: true,
},
ChatInfo: &bridgev2.ChatInfo{
Name: &name,
Members: c.gcMembersToMatrix(isDm, gcMembers),
Type: &roomType,
},
ChatInfo: chatInfo,
})

c.backfillPortal(ctx, item)
Expand Down
30 changes: 29 additions & 1 deletion pkg/connector/mapping.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package connector

import (
"context"
"io"
"net/http"

"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/networkid"

"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
)

func (c *GChatClient) makeAvatar(avatarURL string) *bridgev2.Avatar {
return &bridgev2.Avatar{
ID: networkid.AvatarID(avatarURL),
Get: func(ctx context.Context) ([]byte, error) {
resp, err := http.Get(avatarURL)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
return data, err
},
Remove: avatarURL == "",
}
}

func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *bridgev2.ChatMemberList {
var otherUserId string
memberMap := map[networkid.UserID]bridgev2.ChatMember{}
Expand All @@ -16,12 +36,20 @@ func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *b
otherUserId = gcMember.Id

}
memberMap[userId] = bridgev2.ChatMember{
member := bridgev2.ChatMember{
EventSender: bridgev2.EventSender{
IsFromMe: gcMember.Id == string(c.userLogin.ID),
Sender: userId,
},
}
user := c.users[gcMember.Id]
if user != nil {
member.UserInfo = &bridgev2.UserInfo{
Name: &user.Name,
Avatar: c.makeAvatar(user.AvatarUrl),
}
}
memberMap[userId] = member
}

return &bridgev2.ChatMemberList{
Expand Down

0 comments on commit 1557079

Please sign in to comment.