Skip to content

Commit

Permalink
implement GetChatInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Jan 2, 2025
1 parent 57c48aa commit ccebf1d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
44 changes: 11 additions & 33 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func (c *GChatClient) GetCapabilities(ctx context.Context, portal *bridgev2.Port
}

func (c *GChatClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) {
return nil, nil
groupId := portalToGroupId(portal)
return c.groupToChatInfo(ctx, groupId)
}

func (c *GChatClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) {
Expand Down Expand Up @@ -142,50 +143,27 @@ func (c *GChatClient) onConnect(ctx context.Context) {
}

for _, item := range res.WorldItems {
name := item.RoomName
var gcMembers []*proto.UserId
roomType := database.RoomTypeGroupDM
isDm := false
var dmUser *proto.User
var chatInfo *bridgev2.ChatInfo
if item.DmMembers != nil {
roomType = database.RoomTypeDM
gcMembers = item.DmMembers.Members
isDm = true
var dmUser *proto.User
for _, member := range item.DmMembers.Members {
if member.Id != string(c.userLogin.ID) {
dmUser = c.users[member.Id]
name = dmUser.Name
break
}
}
chatInfo = &bridgev2.ChatInfo{
Name: &dmUser.Name,
Members: c.gcMembersToMatrix(true, item.DmMembers.Members),
Type: ptr.Ptr(database.RoomTypeDM),
Avatar: c.makeAvatar(dmUser.AvatarUrl),
}
} else {
group, err := c.client.GetGroup(ctx, &proto.GetGroupRequest{
GroupId: item.GroupId,
FetchOptions: []proto.GetGroupRequest_FetchOptions{
proto.GetGroupRequest_MEMBERS,
},
})
chatInfo, err = c.groupToChatInfo(ctx, item.GroupId)
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()
}

}

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{
Expand Down
9 changes: 7 additions & 2 deletions pkg/connector/handlegchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (c *GChatClient) makeEventMeta(evt *proto.Event, typ bridgev2.RemoteEventTy
SenderLogin: networkid.UserLoginID(senderId),
Sender: networkid.UserID(senderId),
},
Timestamp: time.UnixMicro(ts),
CreatePortal: typ == bridgev2.RemoteEventMessage,
Timestamp: time.UnixMicro(ts),
}
}

Expand Down Expand Up @@ -153,8 +154,12 @@ func (c *GChatClient) handleMembershipChanged(ctx context.Context, evt *proto.Ev
}
memberMap := map[networkid.UserID]bridgev2.ChatMember{}
memberMap[networkid.UserID(userId)] = member
ts := time.Now().UnixMicro()
if evt.GetGroupRevision() != nil {
ts = evt.GetGroupRevision().Timestamp
}
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.ChatInfoChange{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventChatInfoChange, "", evt.GetGroupRevision().Timestamp),
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventChatInfoChange, "", ts),
ChatInfoChange: &bridgev2.ChatInfoChange{
MemberChanges: &bridgev2.ChatMemberList{
MemberMap: memberMap,
Expand Down
7 changes: 0 additions & 7 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"time"

"google.golang.org/protobuf/encoding/prototext"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
Expand All @@ -20,12 +19,6 @@ var (
_ bridgev2.TypingHandlingNetworkAPI = (*GChatClient)(nil)
)

func portalToGroupId(portal *bridgev2.Portal) *proto.GroupId {
groupId := &proto.GroupId{}
prototext.Unmarshal([]byte(portal.ID), groupId)
return groupId
}

func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (message *bridgev2.MatrixMessageResponse, err error) {
groupId := portalToGroupId(msg.Portal)

Expand Down
35 changes: 35 additions & 0 deletions pkg/connector/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import (
"net/http"

"go.mau.fi/util/ptr"
"google.golang.org/protobuf/encoding/prototext"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"

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

func portalToGroupId(portal *bridgev2.Portal) *proto.GroupId {
groupId := &proto.GroupId{}
prototext.Unmarshal([]byte(portal.ID), groupId)
return groupId
}

func (c *GChatClient) makeAvatar(avatarURL string) *bridgev2.Avatar {
return &bridgev2.Avatar{
ID: networkid.AvatarID(avatarURL),
Expand Down Expand Up @@ -69,3 +77,30 @@ func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *b
OtherUserID: networkid.UserID(otherUserId),
}
}

func (c *GChatClient) groupToChatInfo(ctx context.Context, groupId *proto.GroupId) (*bridgev2.ChatInfo, error) {
group, err := c.client.GetGroup(ctx, &proto.GetGroupRequest{
GroupId: groupId,
FetchOptions: []proto.GetGroupRequest_FetchOptions{
proto.GetGroupRequest_MEMBERS,
},
})
if err != nil {
return &bridgev2.ChatInfo{}, err
}
isDm := group.Group.GroupType == proto.Group_HUMAN_DM || group.Group.GroupType == proto.Group_BOT_DM
roomType := database.RoomTypeGroupDM
if isDm {
roomType = database.RoomTypeDM
}
gcMembers := make([]*proto.UserId, len(group.Memberships))
for i, membership := range group.Memberships {
gcMembers[i] = membership.Id.MemberId.GetUserId()
}
return &bridgev2.ChatInfo{
Name: &group.Group.Name,
Members: c.gcMembersToMatrix(isDm, gcMembers),
Type: &roomType,
Avatar: c.makeAvatar(group.Group.AvatarUrl),
}, nil
}
8 changes: 4 additions & 4 deletions pkg/gchatmeow/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func (c *Channel) Listen(ctx context.Context, maxAge time.Duration) error {
}
c.csessionidParam = csessionidParam

start := time.Now()
// start := time.Now()

for retries <= c.maxRetries {
if time.Since(start) > maxAge {
return ErrChannelLifetime
}
// if time.Since(start) > maxAge {
// return ErrChannelLifetime
// }

if retries > 0 && !skipBackoff {
backoffTime := time.Duration(pow(c.retryBackoffBase, retries)) * time.Second
Expand Down

0 comments on commit ccebf1d

Please sign in to comment.