Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for join rule handling #270

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bridgev2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
ErrMediaConvertFailed error = WrapErrorInStatus(errors.New("failed to convert media")).WithMessage("failed to convert media").WithIsCertain(true).WithSendNotice(true)
ErrMembershipNotSupported error = WrapErrorInStatus(errors.New("this bridge does not support changing group membership")).WithIsCertain(true).WithErrorAsMessage().WithSendNotice(false)
ErrPowerLevelsNotSupported error = WrapErrorInStatus(errors.New("this bridge does not support changing group power levels")).WithIsCertain(true).WithErrorAsMessage().WithSendNotice(false)
ErrJoinRuleNotSupported error = WrapErrorInStatus(errors.New("this bridge does not support changing group join rule")).WithIsCertain(true).WithErrorAsMessage().WithSendNotice(false)
)

// RespError is a class of error that certain network interface methods can return to ensure that the error
Expand Down
11 changes: 11 additions & 0 deletions bridgev2/networkinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,17 @@ type PowerLevelHandlingNetworkAPI interface {
HandleMatrixPowerLevels(ctx context.Context, msg *MatrixPowerLevelChange) (bool, error)
}

type MatrixJoinRulesChange struct {
MatrixRoomMeta[*event.JoinRulesEventContent]
OrigJoinRule event.JoinRule
NewJoinRule event.JoinRule
}
maltee1 marked this conversation as resolved.
Show resolved Hide resolved

type JoinRuleHandlingNetworkAPI interface {
NetworkAPI
HandleMatrixJoinRules(ctx context.Context, msg *MatrixJoinRulesChange) (bool, error)
}

type PushType int

func (pt PushType) String() string {
Expand Down
41 changes: 41 additions & 0 deletions bridgev2/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ func (portal *Portal) handleMatrixEvent(sender *User, evt *event.Event) {
portal.handleMatrixMembership(ctx, login, origSender, evt)
case event.StatePowerLevels:
portal.handleMatrixPowerLevels(ctx, login, origSender, evt)
case event.StateJoinRules:
portal.HandleMatrixJoinRules(ctx, login, origSender, evt)
}
}

Expand Down Expand Up @@ -1333,6 +1335,45 @@ func (portal *Portal) handleMatrixPowerLevels(
}
}

func (portal *Portal) HandleMatrixJoinRules(ctx context.Context, sender *UserLogin, origSender *OrigSender, evt *event.Event) {
log := zerolog.Ctx(ctx)
content, ok := evt.Content.Parsed.(*event.JoinRulesEventContent)
if !ok {
log.Error().Type("content_type", evt.Content.Parsed).Msg("Unexpected parsed content type")
portal.sendErrorStatus(ctx, evt, fmt.Errorf("%w: %T", ErrUnexpectedParsedContentType, evt.Content.Parsed))
return
}
api, ok := sender.Client.(JoinRuleHandlingNetworkAPI)
if !ok {
portal.sendErrorStatus(ctx, evt, ErrJoinRuleNotSupported)
return
}
prevContent := &event.JoinRulesEventContent{}
if evt.Unsigned.PrevContent != nil {
_ = evt.Unsigned.PrevContent.ParseRaw(evt.Type)
prevContent, _ = evt.Unsigned.PrevContent.Parsed.(*event.JoinRulesEventContent)
}
joinRuleChange := &MatrixJoinRulesChange{
MatrixRoomMeta: MatrixRoomMeta[*event.JoinRulesEventContent]{
MatrixEventBase: MatrixEventBase[*event.JoinRulesEventContent]{
Event: evt,
Content: content,
Portal: portal,
OrigSender: origSender,
},
PrevContent: prevContent,
},
OrigJoinRule: content.JoinRule,
NewJoinRule: prevContent.JoinRule,
}
_, err := api.HandleMatrixJoinRules(ctx, joinRuleChange)
if err != nil {
log.Err(err).Msg("Failed to handle Matrix join rule change")
portal.sendErrorStatus(ctx, evt, err)
return
}
}

func (portal *Portal) handleMatrixRedaction(ctx context.Context, sender *UserLogin, origSender *OrigSender, evt *event.Event) {
log := zerolog.Ctx(ctx)
content, ok := evt.Content.Parsed.(*event.RedactionEventContent)
Expand Down
Loading