Skip to content

Commit

Permalink
handle gchat space name/member changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed Jan 1, 2025
1 parent 1557079 commit e375933
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
66 changes: 58 additions & 8 deletions pkg/connector/handlegchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/bridgev2/simplevent"
"maunium.net/go/mautrix/event"

"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
)
Expand Down Expand Up @@ -37,12 +38,14 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
switch evt.Type {
case proto.Event_MESSAGE_POSTED:
msg := evt.Body.GetMessagePosted().Message
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Message[*proto.Message]{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventMessage, msg.Creator.UserId.Id, msg.CreateTime),
ID: networkid.MessageID(msg.Id.MessageId),
Data: msg,
ConvertMessageFunc: c.msgConv.ToMatrix,
})
if msg.MessageType != proto.Message_SYSTEM_MESSAGE {
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Message[*proto.Message]{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventMessage, msg.Creator.UserId.Id, msg.CreateTime),
ID: networkid.MessageID(msg.Id.MessageId),
Data: msg,
ConvertMessageFunc: c.msgConv.ToMatrix,
})
}
case proto.Event_MESSAGE_UPDATED:
msg := evt.Body.GetMessagePosted().Message
eventMeta := c.makeEventMeta(evt, bridgev2.RemoteEventEdit, msg.Creator.UserId.Id, msg.LastEditTime)
Expand Down Expand Up @@ -71,9 +74,12 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Receipt{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventReadReceipt, receipt.User.UserId.Id, receipt.ReadTimeMicros),
ReadUpTo: time.UnixMicro(receipt.ReadTimeMicros),
},
)
})
}
case proto.Event_GROUP_UPDATED:
c.handleGroupUpdated(ctx, evt)
case proto.Event_MEMBERSHIP_CHANGED:
c.handleMembershipChanged(ctx, evt)
}

c.setPortalRevision(ctx, evt)
Expand Down Expand Up @@ -113,6 +119,50 @@ func (c *GChatClient) handleReaction(ctx context.Context, evt *proto.Event) {
})
}

func (c *GChatClient) handleGroupUpdated(ctx context.Context, evt *proto.Event) {
new := evt.Body.GetGroupUpdated().New
if new == nil || (new.Name == "" && new.AvatarUrl == "") {
return
}
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.ChatInfoChange{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventChatInfoChange, "", evt.GetGroupRevision().Timestamp),
ChatInfoChange: &bridgev2.ChatInfoChange{
ChatInfo: &bridgev2.ChatInfo{
Name: &new.Name,
Avatar: c.makeAvatar(new.AvatarUrl),
},
},
})
}

func (c *GChatClient) handleMembershipChanged(ctx context.Context, evt *proto.Event) {
userId := evt.Body.GetMembershipChanged().NewMembership.Id.MemberId.GetUserId().Id
member := bridgev2.ChatMember{
EventSender: bridgev2.EventSender{
IsFromMe: userId == string(c.userLogin.ID),
Sender: networkid.UserID(userId),
},
}
switch evt.Body.GetMembershipChanged().NewMembership.MembershipState {
case proto.MembershipState_MEMBER_JOINED:
member.Membership = event.MembershipJoin
case proto.MembershipState_MEMBER_NOT_A_MEMBER:
member.Membership = event.MembershipLeave
case proto.MembershipState_MEMBER_INVITED:
member.Membership = event.MembershipInvite
}
memberMap := map[networkid.UserID]bridgev2.ChatMember{}
memberMap[networkid.UserID(userId)] = member
c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.ChatInfoChange{
EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventChatInfoChange, "", evt.GetGroupRevision().Timestamp),
ChatInfoChange: &bridgev2.ChatInfoChange{
MemberChanges: &bridgev2.ChatMemberList{
MemberMap: memberMap,
},
},
})
}

func (c *GChatClient) ConvertEdit(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message, msg *proto.Message) (*bridgev2.ConvertedEdit, error) {
cm, err := c.msgConv.ToMatrix(ctx, portal, intent, msg)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions pkg/connector/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"

"go.mau.fi/util/ptr"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/networkid"

Expand Down Expand Up @@ -34,14 +35,17 @@ func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *b
userId := networkid.UserID(gcMember.Id)
if isDm && gcMember.Id != string(c.userLogin.ID) {
otherUserId = gcMember.Id

}
isMe := gcMember.Id == string(c.userLogin.ID)
member := bridgev2.ChatMember{
EventSender: bridgev2.EventSender{
IsFromMe: gcMember.Id == string(c.userLogin.ID),
IsFromMe: isMe,
Sender: userId,
},
}
if isMe {
member.PowerLevel = ptr.Ptr(50)
}
user := c.users[gcMember.Id]
if user != nil {
member.UserInfo = &bridgev2.UserInfo{
Expand All @@ -53,6 +57,7 @@ func (c *GChatClient) gcMembersToMatrix(isDm bool, gcMembers []*proto.UserId) *b
}

return &bridgev2.ChatMemberList{
IsFull: true,
MemberMap: memberMap,
OtherUserID: networkid.UserID(otherUserId),
}
Expand Down

0 comments on commit e375933

Please sign in to comment.