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 all commits
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
1 change: 1 addition & 0 deletions bridgev2/matrix/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (br *Connector) Init(bridge *bridgev2.Bridge) {
br.EventProcessor.On(event.StateTopic, br.handleRoomEvent)
br.EventProcessor.On(event.EphemeralEventReceipt, br.handleEphemeralEvent)
br.EventProcessor.On(event.EphemeralEventTyping, br.handleEphemeralEvent)
br.EventProcessor.On(event.StateJoinRules, br.handleRoomEvent)
br.Bot = br.AS.BotIntent()
br.Crypto = NewCryptoHelper(br)
br.Bridge.Commands.(*commands.Processor).AddHandlers(
Expand Down
7 changes: 7 additions & 0 deletions bridgev2/networkinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,13 @@ type PowerLevelHandlingNetworkAPI interface {
HandleMatrixPowerLevels(ctx context.Context, msg *MatrixPowerLevelChange) (bool, error)
}

type MatrixJoinRule MatrixRoomMeta[*event.JoinRulesEventContent]

type JoinRulesHandlingNetworkAPI interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type JoinRulesHandlingNetworkAPI interface {
type JoinRuleHandlingNetworkAPI interface {

NetworkAPI
HandleMatrixJoinRules(ctx context.Context, msg *MatrixJoinRule) (bool, error)
}

type PushType int

func (pt PushType) String() string {
Expand Down
42 changes: 42 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,46 @@ func (portal *Portal) handleMatrixPowerLevels(
}
}

func (portal *Portal) handleMatrixJoinRules(
ctx context.Context,
sender *UserLogin,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this whole function should just be replaced with another case in handleMatrixRoomMeta 🤔

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.(JoinRulesHandlingNetworkAPI)
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 := &MatrixJoinRule{
MatrixEventBase: MatrixEventBase[*event.JoinRulesEventContent]{
Event: evt,
Content: content,
Portal: portal,
OrigSender: origSender,
},
PrevContent: prevContent,
}
_, 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