Skip to content

Commit

Permalink
get space members
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Dec 9, 2024
1 parent 7725ad9 commit 2e4da77
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
36 changes: 22 additions & 14 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewClient(userLogin *bridgev2.UserLogin, client *gchatmeow.Client) *GChatCl
return &GChatClient{
userLogin: userLogin,
client: client,
users: make(map[string]*proto.User),
users: map[string]*proto.User{},
}
}

Expand Down Expand Up @@ -104,24 +104,34 @@ func (c *GChatClient) onConnect(ctx context.Context) {
}

for _, item := range res.WorldItems {
// TODO room name for DM, and full members list
name := item.RoomName
if name == nil && item.DmMembers != nil {
gcMembers := make([]*proto.UserId, 0)

Check failure on line 108 in pkg/connector/client.go

View workflow job for this annotation

GitHub Actions / Lint (latest)

this value of gcMembers is never used (SA4006)
if item.DmMembers != nil {
gcMembers = item.DmMembers.Members
for _, member := range item.DmMembers.Members {
if *member.Id != string(c.userLogin.ID) {
name = c.users[*member.Id].Name
break
}
}
} else {
group, err := c.client.GetGroup(ctx, &proto.GetGroupRequest{
GroupId: item.GroupId,
FetchOptions: []proto.GetGroupRequest_FetchOptions{
proto.GetGroupRequest_MEMBERS,
},
})
if err != nil {
fmt.Println(err)
continue
}
gcMembers = make([]*proto.UserId, len(group.Memberships))
for i, membership := range group.Memberships {
gcMembers[i] = membership.Id.MemberId.GetUserId()
}

}
memberMap := map[networkid.UserID]bridgev2.ChatMember{}
memberMap[networkid.UserID(c.userLogin.ID)] = bridgev2.ChatMember{
EventSender: bridgev2.EventSender{
IsFromMe: true,
Sender: networkid.UserID(c.userLogin.ID),
},
}

c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.ChatResync{
EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventChatResync,
Expand All @@ -132,10 +142,8 @@ func (c *GChatClient) onConnect(ctx context.Context) {
CreatePortal: true,
},
ChatInfo: &bridgev2.ChatInfo{
Name: name,
Members: &bridgev2.ChatMemberList{
MemberMap: memberMap,
},
Name: name,
Members: c.gcMembersToMxMembers(gcMembers),
},
})

Expand Down
25 changes: 25 additions & 0 deletions pkg/connector/mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package connector

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

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

func (c *GChatClient) gcMembersToMxMembers(gcMembers []*proto.UserId) *bridgev2.ChatMemberList {
memberMap := map[networkid.UserID]bridgev2.ChatMember{}
for _, gcMember := range gcMembers {
userId := networkid.UserID(*gcMember.Id)
memberMap[userId] = bridgev2.ChatMember{
EventSender: bridgev2.EventSender{
IsFromMe: *gcMember.Id == string(c.userLogin.ID),
Sender: userId,
},
}
}

return &bridgev2.ChatMemberList{
MemberMap: memberMap,
}
}
7 changes: 7 additions & 0 deletions pkg/gchatmeow/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,10 @@ func (c *Client) CreateTopic(ctx context.Context, request *proto.CreateTopicRequ
err := c.gcRequest(ctx, "create_topic", request, response)
return response, err
}

func (c *Client) GetGroup(ctx context.Context, request *proto.GetGroupRequest) (*proto.GetGroupResponse, error) {
request.RequestHeader = c.gcRequestHeader
response := &proto.GetGroupResponse{}
err := c.gcRequest(ctx, "get_group", request, response)
return response, err
}

0 comments on commit 2e4da77

Please sign in to comment.