| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\App\Http\Controllers; |
| 4 | 4 | |
| 5 | +use FluentCommunity\App\Functions\Utility; | |
| 5 | 6 | use FluentCommunity\App\Models\BaseSpace; |
| 6 | 7 | use FluentCommunity\App\Models\User; |
| 7 | 8 | use FluentCommunity\App\Models\XProfile; |
| 8 | 9 | use FluentCommunity\App\Services\Helper; |
| @@ -12,71 +13,113 @@ | ||
| 12 | 13 | class MembersController extends Controller |
| 13 | 14 | { |
| 14 | 15 | public function getMembers(Request $request) |
| 15 | 16 | { |
| 16 | - $spaceSlug = $request->getSafe('space', 'sanitize_text_field'); | |
| 17 | + $start = microtime(true); | |
| 18 | + $mention = $request->getSafe('mention', 'sanitize_text_field'); | |
| 19 | + if($mention && !get_current_user_id()) { | |
| 20 | + return $this->sendError([ | |
| 21 | + 'message' => __('You must be logged in to mention someone', 'fluent-community'), | |
| 22 | + ]); | |
| 23 | + } | |
| 17 | 24 | |
| 18 | - $space = null; | |
| 19 | - if ($spaceSlug) { | |
| 20 | - $space = BaseSpace::where('slug', $spaceSlug)->first(); | |
| 21 | - if (!$space || !Helper::isUserInSpace(get_current_user_id(), $space->id)) { | |
| 22 | - return $this->sendError([ | |
| 23 | - 'message' => __('Please join the space first to view the members', 'fluent-community'), | |
| 24 | - ]); | |
| 25 | - } | |
| 26 | - } | |
| 25 | + $canAccess = Utility::canViewMembersPage(); | |
| 26 | + $isMod = Helper::isModerator(); | |
| 27 | 27 | |
| 28 | - if (!$space) { | |
| 29 | - $spaceId = $request->getSafe('space_id', 'intval'); | |
| 30 | - if ($spaceId) { | |
| 31 | - $space = BaseSpace::find($spaceId); | |
| 28 | + $members = XProfile::select(ProfileHelper::getXProfilePublicFields()) | |
| 29 | + ->whereHas('user'); | |
| 30 | + | |
| 31 | + if($mention) { | |
| 32 | + $spaceSlug = $request->getSafe('space', 'sanitize_text_field'); | |
| 33 | + $space = null; | |
| 34 | + if ($spaceSlug) { | |
| 35 | + $space = BaseSpace::where('slug', $spaceSlug)->first(); | |
| 32 | 36 | if (!$space || !Helper::isUserInSpace(get_current_user_id(), $space->id)) { |
| 33 | 37 | return $this->sendError([ |
| 34 | - 'message' => 'Space not found', | |
| 38 | + 'message' => __('Please join the space first to view the members', 'fluent-community'), | |
| 39 | + 'permission_failed' => true | |
| 35 | 40 | ]); |
| 36 | 41 | } |
| 37 | 42 | } |
| 43 | + | |
| 44 | + if (!$space) { | |
| 45 | + $spaceId = $request->getSafe('space_id', 'intval'); | |
| 46 | + if ($spaceId) { | |
| 47 | + $space = BaseSpace::withoutGlobalScopes()->find($spaceId); | |
| 48 | + if (!$space || !Helper::isUserInSpace(get_current_user_id(), $space->id)) { | |
| 49 | + return $this->sendError([ | |
| 50 | + 'message' => __('Space not found', 'fluent-community'), | |
| 51 | + 'permission_failed' => true | |
| 52 | + ]); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + if ($space) { | |
| 58 | + $members = $members->whereHas('spaces', function ($query) use ($space) { | |
| 59 | + $query->withoutGlobalScopes()->where('space_id', $space->id); | |
| 60 | + }); | |
| 61 | + } | |
| 62 | + | |
| 63 | + $members = $members->where('status', 'active') | |
| 64 | + ->where('user_id', '!=', get_current_user_id()) | |
| 65 | + ->mentionBy($mention) | |
| 66 | + ->limit(10) | |
| 67 | + ->get(); | |
| 68 | + | |
| 69 | + $data = [ | |
| 70 | + 'members' => [ | |
| 71 | + 'data' => $members | |
| 72 | + ], | |
| 73 | + 'execution_time' => microtime(true) - $start | |
| 74 | + ]; | |
| 75 | + return apply_filters('fluent_community/mention_members_api_response', $data, $request->all()); | |
| 38 | 76 | } |
| 39 | 77 | |
| 40 | - $mention = $request->getSafe('mention', 'sanitize_text_field'); | |
| 41 | - $members = XProfile::select(ProfileHelper::getXProfilePublicFields()) | |
| 42 | - ->whereHas('user') | |
| 43 | - ->searchBy($request->getSafe('search', 'sanitize_text_field')); | |
| 78 | + if(!$canAccess) { | |
| 79 | + return $this->sendError([ | |
| 80 | + 'message' => __('You do not have permission to view members', 'fluent-community'), | |
| 81 | + 'permission_failed' => true | |
| 82 | + ]); | |
| 83 | + } | |
| 44 | 84 | |
| 45 | - if ($mention) { | |
| 46 | - $members = $members->where('user_id', '!=', get_current_user_id()) | |
| 47 | - ->mentionBy($mention); | |
| 48 | - } | |
| 85 | + $sortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'last_activity'); | |
| 49 | 86 | |
| 50 | - $shortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'last_activity'); | |
| 87 | + $validSortFields = ['last_activity', 'display_name', 'created_at']; | |
| 51 | 88 | |
| 52 | - if ($shortBy == 'last_activity') { | |
| 53 | - $members = $members->orderBy('last_activity', 'DESC'); | |
| 54 | - } else { | |
| 55 | - $members = $members->orderBy($shortBy, 'ASC'); | |
| 56 | - } | |
| 89 | + $sortColumn = in_array($sortBy, $validSortFields, true) ? $sortBy : 'last_activity'; | |
| 57 | 90 | |
| 58 | - $members = $members->orderBy('last_activity', 'DESC'); | |
| 91 | + $defaultDirections = [ | |
| 92 | + 'last_activity' => 'DESC', | |
| 93 | + 'display_name' => 'ASC', | |
| 94 | + 'created_at' => 'DESC', | |
| 95 | + ]; | |
| 59 | 96 | |
| 60 | - if ($space) { | |
| 61 | - $members = $members->whereHas('spaces', function ($query) use ($space) { | |
| 62 | - $query->where('space_id', $space->id); | |
| 63 | - }); | |
| 64 | - } | |
| 97 | + $sortDir = strtoupper($request->getSafe('sort_dir', 'sanitize_text_field', '')); | |
| 65 | 98 | |
| 66 | - $currentUser = $this->getUser(); | |
| 99 | + $sortDirection = in_array($sortDir, ['ASC', 'DESC'], true) | |
| 100 | + ? $sortDir | |
| 101 | + : $defaultDirections[$sortColumn]; | |
| 67 | 102 | |
| 68 | - if ($mention || !$currentUser || !$currentUser->isCommunityAdmin()) { | |
| 69 | - $members = $members->where('status', 'active'); | |
| 70 | - } else { | |
| 103 | + $members = $members->orderBy($sortColumn, $sortDirection); | |
| 104 | + | |
| 105 | + $members = $members | |
| 106 | + ->searchBy($request->getSafe('search', 'sanitize_text_field')); | |
| 107 | + | |
| 108 | + if($isMod) { | |
| 71 | 109 | $statuses = $request->getSafe('status', 'sanitize_text_field', 'active'); |
| 72 | - if ($statuses == 'in_active') { | |
| 73 | - $statuses = ['pending', 'blocked']; | |
| 110 | + if ($statuses == 'pending') { | |
| 111 | + $statuses = ['pending']; | |
| 112 | + } else if ($statuses == 'blocked') { | |
| 113 | + $statuses = ['blocked']; | |
| 114 | + } else if($statuses == 'deactivated') { | |
| 115 | + $statuses = ['']; | |
| 74 | 116 | } else { |
| 75 | 117 | $statuses = ['active']; |
| 76 | 118 | } |
| 77 | - | |
| 78 | 119 | $members = $members->whereIn('status', $statuses); |
| 120 | + } else { | |
| 121 | + $members = $members->where('status', 'active'); | |
| 79 | 122 | } |
| 80 | 123 | |
| 81 | 124 | do_action_ref_array('fluent_community/members_query_ref', [&$members, $request->all()]); |
| 82 | 125 | |
| @@ -81,19 +124,12 @@ | ||
| 81 | 124 | do_action_ref_array('fluent_community/members_query_ref', [&$members, $request->all()]); |
| 82 | 125 | |
| 83 | 126 | $members = $members->paginate(); |
| 84 | 127 | |
| 85 | - if ($mention) { | |
| 86 | - return [ | |
| 87 | - 'members' => [ | |
| 88 | - 'data' => $members->items() | |
| 89 | - ] | |
| 90 | - ]; | |
| 91 | - } | |
| 92 | - | |
| 93 | - return [ | |
| 94 | - 'members' => $members | |
| 95 | - ]; | |
| 128 | + return apply_filters('fluent_community/members_api_response', [ | |
| 129 | + 'members' => $members, | |
| 130 | + 'execution_time' => microtime(true) - $start | |
| 131 | + ], $members, $request->all()); | |
| 96 | 132 | } |
| 97 | 133 | |
| 98 | 134 | public function patchMember(Request $request, $userId) |
| 99 | 135 | { |
| @@ -105,9 +141,9 @@ | ||
| 105 | 141 | $wpUser = User::findOrFail($userId); |
| 106 | 142 | |
| 107 | 143 | if ($wpUser->isCommunityAdmin() && $newStatus != 'active') { |
| 108 | 144 | return $this->sendError([ |
| 109 | - 'message' => 'Sorry, you can not change status of another admin. Remove the user from moderators' | |
| 145 | + 'message' => __('Sorry, you cannot change the status of another admin. Remove the user from moderators', 'fluent-community') | |
| 110 | 146 | ]); |
| 111 | 147 | } |
| 112 | 148 | |
| 113 | 149 | $xProfile = XProfile::findOrFail($userId); |