From 2e4da77329f8e08323668559c3b9f03e713662cd Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Mon, 9 Dec 2024 22:45:42 +0900 Subject: [PATCH] get space members --- pkg/connector/client.go | 36 ++++++++++++++++++++++-------------- pkg/connector/mapping.go | 25 +++++++++++++++++++++++++ pkg/gchatmeow/api.go | 7 +++++++ 3 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 pkg/connector/mapping.go diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 0d3914d..eeab16e 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -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{}, } } @@ -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) + 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, @@ -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), }, }) diff --git a/pkg/connector/mapping.go b/pkg/connector/mapping.go new file mode 100644 index 0000000..4c57f8e --- /dev/null +++ b/pkg/connector/mapping.go @@ -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, + } +} diff --git a/pkg/gchatmeow/api.go b/pkg/gchatmeow/api.go index 2bb188f..96980ad 100644 --- a/pkg/gchatmeow/api.go +++ b/pkg/gchatmeow/api.go @@ -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 +}