Skip to content

Commit

Permalink
Send correct event to other users when creating group (mautrix#491)
Browse files Browse the repository at this point in the history
Use empty message with GroupV2Context instead of an empty group change
  • Loading branch information
maltee1 authored Apr 2, 2024
1 parent f6e6982 commit efc22ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
13 changes: 5 additions & 8 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ func fnCreate(ce *WrappedCommandEvent) {
var avatarHash string
var avatarURL id.ContentURI
var avatarBytes []byte
avatarSet := false
if ok {
roomAvatarEvent.Content.ParseRaw(event.StateRoomAvatar)
avatarURL = roomAvatarEvent.Content.AsRoomAvatar().URL
Expand All @@ -783,6 +784,7 @@ func fnCreate(ce *WrappedCommandEvent) {
hash := sha256.Sum256(avatarBytes)
avatarHash = hex.EncodeToString(hash[:])
ce.ZLog.Debug().Stringers("%s set the group avatar to %s", []fmt.Stringer{ce.User.MXID, avatarURL})
avatarSet = true
}
}
var encryptionEvent *event.EncryptionEventContent
Expand Down Expand Up @@ -856,7 +858,7 @@ func fnCreate(ce *WrappedCommandEvent) {
Str("room_name", roomName).
Any("participants", participants).
Msg("Creating Signal group for Matrix room")
group, err := ce.User.Client.CreateGroupOnServer(ce.Ctx, &signalmeow.Group{
group, err := ce.User.Client.CreateGroup(ce.Ctx, &signalmeow.Group{
Title: roomName,
Description: roomTopic,
Members: participants,
Expand Down Expand Up @@ -899,16 +901,11 @@ func fnCreate(ce *WrappedCommandEvent) {
}
portal.Encrypted = true
}
revision, err := ce.User.Client.UpdateGroup(ce.Ctx, &signalmeow.GroupChange{}, gid)
if err != nil {
ce.Reply("Failed to update Group")
return
}
portal.Revision = revision
portal.Revision = group.Revision
portal.AvatarHash = avatarHash
portal.AvatarURL = avatarURL
portal.AvatarPath = group.AvatarPath
portal.AvatarSet = true
portal.AvatarSet = avatarSet
err = portal.Update(ce.Ctx)
if err != nil {
ce.ZLog.Err(err).Msg("Failed to save portal after creating group")
Expand Down
21 changes: 19 additions & 2 deletions pkg/signalmeow/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ func (cli *Client) UpdateGroup(ctx context.Context, groupChange *GroupChange, gi
return 0, err
}
groupContext := &signalpb.GroupContextV2{Revision: &groupChange.Revision, GroupChange: groupChangeBytes, MasterKey: masterKeyBytes[:]}
_, err = cli.SendGroupChange(ctx, group, groupContext, groupChange)
_, err = cli.SendGroupUpdate(ctx, group, groupContext, groupChange)
if err != nil {
log.Err(err).Msg("Error sending GroupChange to group members")
}
Expand Down Expand Up @@ -1622,7 +1622,7 @@ func (cli *Client) EncryptGroup(ctx context.Context, decryptedGroup *Group, grou
return encryptedGroup, nil
}

func (cli *Client) CreateGroupOnServer(ctx context.Context, decryptedGroup *Group, avatarBytes []byte) (*Group, error) {
func (cli *Client) createGroupOnServer(ctx context.Context, decryptedGroup *Group, avatarBytes []byte) (*Group, error) {
log := zerolog.Ctx(ctx).With().Str("action", "CreateGroupOnServer").Logger()
masterKeyByteArray := make([]byte, 32)
rand.Read(masterKeyByteArray)
Expand Down Expand Up @@ -1708,3 +1708,20 @@ func GenerateInviteLinkPassword() types.SerializedInviteLinkPassword {
rand.Read(inviteLinkPasswordBytes)
return InviteLinkPasswordFromBytes(inviteLinkPasswordBytes)
}

func (cli *Client) CreateGroup(ctx context.Context, decryptedGroup *Group, avatarBytes []byte) (*Group, error) {
log := zerolog.Ctx(ctx).With().Str("action", "CreateGroup").Logger()
group, err := cli.createGroupOnServer(ctx, decryptedGroup, avatarBytes)
if err != nil {
log.Err(err).Msg("Error creating group on server")
return nil, err
}
masterKeyBytes := masterKeyToBytes(group.groupMasterKey)
groupContext := &signalpb.GroupContextV2{Revision: &group.Revision, MasterKey: masterKeyBytes[:]}
_, err = cli.SendGroupUpdate(ctx, group, groupContext, nil)
if err != nil {
log.Err(err).Msg("Error sending GroupUpdate to group members")
return nil, err
}
return group, nil
}
14 changes: 8 additions & 6 deletions pkg/signalmeow/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func wrapDataMessageInContent(dm *signalpb.DataMessage) *signalpb.Content {
}
}

func (cli *Client) SendGroupChange(ctx context.Context, group *Group, groupContext *signalpb.GroupContextV2, groupChange *GroupChange) (*GroupMessageSendResult, error) {
func (cli *Client) SendGroupUpdate(ctx context.Context, group *Group, groupContext *signalpb.GroupContextV2, groupChange *GroupChange) (*GroupMessageSendResult, error) {
log := zerolog.Ctx(ctx).With().
Str("action", "send group change message").
Stringer("group_id", group.GroupIdentifier).
Expand All @@ -548,11 +548,13 @@ func (cli *Client) SendGroupChange(ctx context.Context, group *Group, groupConte
for _, member := range group.PendingMembers {
recipients = append(recipients, &member.GroupMember)
}
for _, member := range groupChange.AddPendingMembers {
recipients = append(recipients, &member.GroupMember)
}
for _, member := range groupChange.AddMembers {
recipients = append(recipients, &member.GroupMember)
if groupChange != nil {
for _, member := range groupChange.AddPendingMembers {
recipients = append(recipients, &member.GroupMember)
}
for _, member := range groupChange.AddMembers {
recipients = append(recipients, &member.GroupMember)
}
}
return cli.sendToGroup(ctx, recipients, content, timestamp)
}
Expand Down

0 comments on commit efc22ef

Please sign in to comment.