Skip to content

Commit

Permalink
add ban/unban handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maltee1 committed Mar 9, 2024
1 parent 6b1a039 commit d5865f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
6 changes: 6 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ type JoinRuleHandlingPortal interface {
HandleMatrixJoinRule(sender User, evt *event.Event)
}

type BanHandlingPortal interface {
Portal
HandleMatrixBan(sender User, evt *event.Event)
HandleMatrixUnban(sender User, evt *event.Event)
}

type User interface {
GetPermissionLevel() bridgeconfig.PermissionLevel
IsLoggedIn() bool
Expand Down
46 changes: 28 additions & 18 deletions bridge/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,37 @@ func (mx *MatrixHandler) HandleMembership(ctx context.Context, evt *event.Event)
} else if user.GetPermissionLevel() < bridgeconfig.PermissionLevelUser || !user.IsLoggedIn() {
return
}

mhp, ok := portal.(MembershipHandlingPortal)
if !ok {
return
bhp, bhpOk := portal.(BanHandlingPortal)
mhp, mhpOk := portal.(MembershipHandlingPortal)
if !(mhpOk || bhpOk) {
return
}
var prevContent *event.MemberEventContent
if evt.Unsigned.PrevContent != nil {
_ = evt.Unsigned.PrevContent.ParseRaw(evt.Type)
var ok bool
prevContent, ok = evt.Unsigned.PrevContent.Parsed.(*event.MemberEventContent)
if !ok {
prevContent = &event.MemberEventContent{Membership: event.MembershipLeave}
}
}

if content.Membership == event.MembershipLeave {
if evt.Unsigned.PrevContent != nil {
_ = evt.Unsigned.PrevContent.ParseRaw(evt.Type)
prevContent, ok := evt.Unsigned.PrevContent.Parsed.(*event.MemberEventContent)
if ok && prevContent.Membership != "join" {
return
}
if bhpOk {
if content.Membership == event.MembershipBan {
bhp.HandleMatrixBan(user, evt)
} else if content.Membership == event.MembershipLeave && prevContent.Membership == event.MembershipBan {
bhp.HandleMatrixUnban(user, evt)
}
if isSelf {
mhp.HandleMatrixLeave(user, evt)
} else if ghost != nil {
mhp.HandleMatrixKick(user, ghost, evt)
}
if mhpOk {
if content.Membership == event.MembershipLeave && prevContent.Membership == event.MembershipJoin {
if isSelf {
mhp.HandleMatrixLeave(user, evt)
} else if ghost != nil {
mhp.HandleMatrixKick(user, ghost, evt)
}
} else if content.Membership == event.MembershipInvite && !isSelf && ghost != nil {
mhp.HandleMatrixInvite(user, ghost, evt)
}
} else if content.Membership == event.MembershipInvite && !isSelf && ghost != nil {
mhp.HandleMatrixInvite(user, ghost, evt)
}
// TODO kicking/inviting non-ghost users users
}
Expand Down

0 comments on commit d5865f4

Please sign in to comment.