nolint:gocyclo
(ctx context.Context, req *api.QueryRestrictedJoinAllowedRequest, res *api.QueryRestrictedJoinAllowedResponse)
| 768 | |
| 769 | // nolint:gocyclo |
| 770 | func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.QueryRestrictedJoinAllowedRequest, res *api.QueryRestrictedJoinAllowedResponse) error { |
| 771 | // Look up if we know anything about the room. If it doesn't exist |
| 772 | // or is a stub entry then we can't do anything. |
| 773 | roomInfo, err := r.DB.RoomInfo(ctx, req.RoomID) |
| 774 | if err != nil { |
| 775 | return fmt.Errorf("r.DB.RoomInfo: %w", err) |
| 776 | } |
| 777 | if roomInfo == nil || roomInfo.IsStub() { |
| 778 | return nil // fmt.Errorf("room %q doesn't exist or is stub room", req.RoomID) |
| 779 | } |
| 780 | // If the room version doesn't allow restricted joins then don't |
| 781 | // try to process any further. |
| 782 | allowRestrictedJoins, err := roomInfo.RoomVersion.MayAllowRestrictedJoinsInEventAuth() |
| 783 | if err != nil { |
| 784 | return fmt.Errorf("roomInfo.RoomVersion.AllowRestrictedJoinsInEventAuth: %w", err) |
| 785 | } else if !allowRestrictedJoins { |
| 786 | return nil |
| 787 | } |
| 788 | // Start off by populating the "resident" flag in the response. If we |
| 789 | // come across any rooms in the request that are missing, we will unset |
| 790 | // the flag. |
| 791 | res.Resident = true |
| 792 | // Get the join rules to work out if the join rule is "restricted". |
| 793 | joinRulesEvent, err := r.DB.GetStateEvent(ctx, req.RoomID, gomatrixserverlib.MRoomJoinRules, "") |
| 794 | if err != nil { |
| 795 | return fmt.Errorf("r.DB.GetStateEvent: %w", err) |
| 796 | } |
| 797 | if joinRulesEvent == nil { |
| 798 | return nil |
| 799 | } |
| 800 | var joinRules gomatrixserverlib.JoinRuleContent |
| 801 | if err = json.Unmarshal(joinRulesEvent.Content(), &joinRules); err != nil { |
| 802 | return fmt.Errorf("json.Unmarshal: %w", err) |
| 803 | } |
| 804 | // If the join rule isn't "restricted" then there's nothing more to do. |
| 805 | res.Restricted = joinRules.JoinRule == gomatrixserverlib.Restricted |
| 806 | if !res.Restricted { |
| 807 | return nil |
| 808 | } |
| 809 | // If the user is already invited to the room then the join is allowed |
| 810 | // but we don't specify an authorised via user, since the event auth |
| 811 | // will allow the join anyway. |
| 812 | var pending bool |
| 813 | if pending, _, _, err = helpers.IsInvitePending(ctx, r.DB, req.RoomID, req.UserID); err != nil { |
| 814 | return fmt.Errorf("helpers.IsInvitePending: %w", err) |
| 815 | } else if pending { |
| 816 | res.Allowed = true |
| 817 | return nil |
| 818 | } |
| 819 | // We need to get the power levels content so that we can determine which |
| 820 | // users in the room are entitled to issue invites. We need to use one of |
| 821 | // these users as the authorising user. |
| 822 | powerLevelsEvent, err := r.DB.GetStateEvent(ctx, req.RoomID, gomatrixserverlib.MRoomPowerLevels, "") |
| 823 | if err != nil { |
| 824 | return fmt.Errorf("r.DB.GetStateEvent: %w", err) |
| 825 | } |
| 826 | var powerLevels gomatrixserverlib.PowerLevelContent |
| 827 | if err = json.Unmarshal(powerLevelsEvent.Content(), &powerLevels); err != nil { |
nothing calls this directly
no test coverage detected