mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 23:48:27 +00:00
Improve error handling some more
This commit is contained in:
parent
6123c0edca
commit
9e694f3fe5
1 changed files with 25 additions and 5 deletions
|
@ -347,23 +347,43 @@ func (r *Joiner) checkIfRestrictedJoin(
|
||||||
Msg: fmt.Sprintf("Unable to retrieve the join rules: %s", err),
|
Msg: fmt.Sprintf("Unable to retrieve the join rules: %s", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
joinRuleContent := &gomatrixserverlib.JoinRuleContent{
|
// First unmarshal the join rule itself. It might seem strange that this is
|
||||||
|
// a two-step process, but the Complement tests specifically populate the
|
||||||
|
// 'allow' field with a nonsense value that won't unmarshal and therefore
|
||||||
|
// trying to unmarshal a gomatrixserverlib.JoinRuleContent fails entirely.
|
||||||
|
// We need to get the join rule first to check if the room is restricted
|
||||||
|
// though, regardless of what the 'allow' key contains.
|
||||||
|
joinRule := struct {
|
||||||
|
JoinRule string `json:"join_rule"`
|
||||||
|
}{
|
||||||
JoinRule: gomatrixserverlib.Public,
|
JoinRule: gomatrixserverlib.Public,
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(joinRuleEvent.Content(), &joinRuleContent); err != nil {
|
if err = json.Unmarshal(joinRuleEvent.Content(), &joinRule); err != nil {
|
||||||
return false, nil, &rsAPI.PerformError{
|
return false, nil, &rsAPI.PerformError{
|
||||||
Code: rsAPI.PerformErrorNotAllowed,
|
Code: rsAPI.PerformErrorNotAllowed,
|
||||||
Msg: fmt.Sprintf("The room join rules are invalid: %s", err),
|
Msg: fmt.Sprintf("The room join rules are invalid: %s", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
roomIDs := make([]string, 0, len(joinRuleContent.Allow))
|
if joinRule.JoinRule != gomatrixserverlib.Restricted {
|
||||||
for _, allowed := range joinRuleContent.Allow {
|
return false, nil, nil
|
||||||
|
}
|
||||||
|
// Then try and extract the join rule 'allow' key. It's possible that this
|
||||||
|
// step can fail but we need to be OK with that — if we do, we will just
|
||||||
|
// treat it as if it is an empty list.
|
||||||
|
var joinRuleAllow struct {
|
||||||
|
Allow []gomatrixserverlib.JoinRuleContentAllowRule `json:"allow"`
|
||||||
|
}
|
||||||
|
_ = json.Unmarshal(joinRuleEvent.Content(), &joinRuleAllow)
|
||||||
|
// Now create a list of room IDs that we can check in order to validate
|
||||||
|
// that the restricted join can be completed.
|
||||||
|
roomIDs := make([]string, 0, len(joinRuleAllow.Allow))
|
||||||
|
for _, allowed := range joinRuleAllow.Allow {
|
||||||
if allowed.Type != gomatrixserverlib.MRoomMembership {
|
if allowed.Type != gomatrixserverlib.MRoomMembership {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
roomIDs = append(roomIDs, allowed.RoomID)
|
roomIDs = append(roomIDs, allowed.RoomID)
|
||||||
}
|
}
|
||||||
return joinRuleContent.JoinRule == gomatrixserverlib.Restricted, roomIDs, nil
|
return true, roomIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Joiner) attemptRestrictedJoinUsingRoomID(
|
func (r *Joiner) attemptRestrictedJoinUsingRoomID(
|
||||||
|
|
Loading…
Reference in a new issue