| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | use FluentCommunity\App\Models\SpaceGroup; |
| 9 | 9 | use FluentCommunity\App\Models\User; |
| 10 | 10 | use FluentCommunity\App\Services\CustomSanitizer; |
| 11 | 11 | use FluentCommunity\App\Services\Helper; |
| 12 | +use FluentCommunity\App\Services\SpaceMenuService; | |
| 12 | 13 | use FluentCommunity\App\Functions\Utility; |
| 13 | 14 | use FluentCommunity\App\Services\ProfileHelper; |
| 14 | 15 | use FluentCommunity\Framework\Http\Request\Request; |
| 15 | 16 | use FluentCommunity\App\Models\Comment; |
| @@ -159,9 +160,10 @@ | ||
| 159 | 160 | $q->where('user_id', get_current_user_id()); |
| 160 | 161 | }]) |
| 161 | 162 | ->where(function ($q) { |
| 162 | 163 | $q->whereHas('space_pivot', function ($q) { |
| 163 | - $q->where('user_id', get_current_user_id()); | |
| 164 | + $q->where('user_id', get_current_user_id()) | |
| 165 | + ->where('status', 'active'); | |
| 164 | 166 | }) |
| 165 | 167 | ->orWhereIn('privacy', ['public', 'private']); |
| 166 | 168 | }) |
| 167 | 169 | ->when($type == 'joined', function ($q) { |
| @@ -208,11 +210,23 @@ | ||
| 208 | 210 | } |
| 209 | 211 | |
| 210 | 212 | public function getAllSpaces(Request $request) |
| 211 | 213 | { |
| 212 | - $spaces = Space::paginate(); | |
| 214 | + $currentUser = $this->getUser(); | |
| 213 | 215 | |
| 214 | - $currentUser = $this->getUser(); | |
| 216 | + $spacesQuery = Space::query(); | |
| 217 | + | |
| 218 | + if (!($currentUser && $currentUser->isCommunityModerator())) { | |
| 219 | + $spacesQuery->where(function ($q) { | |
| 220 | + $q->whereHas('space_pivot', function ($q) { | |
| 221 | + $q->where('user_id', get_current_user_id()) | |
| 222 | + ->where('status', 'active'); | |
| 223 | + }) | |
| 224 | + ->orWhereIn('privacy', ['public', 'private']); | |
| 225 | + }); | |
| 226 | + } | |
| 227 | + $spaces = $spacesQuery->paginate(); | |
| 228 | + | |
| 215 | 229 | $memberCounts = $this->getActiveMemberCounts($spaces->pluck('id')->toArray()); |
| 216 | 230 | |
| 217 | 231 | foreach ($spaces as $space) { |
| 218 | 232 | $shouldHideMembersCount = Arr::get($space->settings, 'hide_members_count') == 'yes'; |
| @@ -374,8 +388,9 @@ | ||
| 374 | 388 | } |
| 375 | 389 | |
| 376 | 390 | public function getMembers(Request $request, $slug) |
| 377 | 391 | { |
| 392 | + /** @var Space $space */ | |
| 378 | 393 | $space = Space::where('slug', $slug) |
| 379 | 394 | ->firstOrFail(); |
| 380 | 395 | |
| 381 | 396 | $user = $this->getUser(); |
| @@ -413,20 +428,37 @@ | ||
| 413 | 428 | ], $pendingRequests, $request->all()); |
| 414 | 429 | } |
| 415 | 430 | } |
| 416 | 431 | |
| 432 | + $defaultDirections = [ | |
| 433 | + 'last_activity' => 'DESC', | |
| 434 | + 'display_name' => 'ASC', | |
| 435 | + 'created_at' => 'DESC', | |
| 436 | + ]; | |
| 437 | + | |
| 438 | + $sortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'created_at'); | |
| 439 | + $sortColumn = in_array($sortBy, array_keys($defaultDirections), true) ? $sortBy : 'created_at'; | |
| 440 | + $sortDir = strtoupper($request->getSafe('sort_dir', 'sanitize_text_field', '')); | |
| 441 | + $sortDirection = in_array($sortDir, ['ASC', 'DESC'], true) ? $sortDir : ($sortColumn === 'created_at' ? 'ASC' : $defaultDirections[$sortColumn]); | |
| 442 | + | |
| 443 | + $profileSort = $sortColumn !== 'created_at'; | |
| 444 | + $orderColumn = $profileSort ? 'fcom_xprofile.' . $sortColumn : 'fcom_space_user.created_at'; | |
| 445 | + | |
| 417 | 446 | $spaceMembers = SpaceUserPivot::bySpace($space->id) |
| 418 | 447 | ->whereHas('xprofile', function ($q) use ($search) { |
| 419 | - return $q->searchBy($search) | |
| 420 | - ->where('status', 'active'); | |
| 448 | + $q->searchBy($search)->where('status', 'active'); | |
| 421 | 449 | }) |
| 450 | + ->where('fcom_space_user.status', 'active') | |
| 422 | 451 | ->with(['xprofile' => function ($q) { |
| 423 | 452 | $q->select(ProfileHelper::getXProfilePublicFields()); |
| 424 | 453 | }]) |
| 425 | - ->where('status', 'active') | |
| 426 | - ->orderBy('created_at', 'ASC') | |
| 454 | + ->when($profileSort, function ($q) { | |
| 455 | + $q->join('fcom_xprofile', 'fcom_xprofile.user_id', '=', 'fcom_space_user.user_id') | |
| 456 | + ->select('fcom_space_user.*'); | |
| 457 | + }) | |
| 458 | + ->orderBy($orderColumn, $sortDirection) | |
| 427 | 459 | ->paginate(); |
| 428 | - | |
| 460 | + | |
| 429 | 461 | return apply_filters('fluent_community/space_members_api_response', [ |
| 430 | 462 | 'members' => $spaceMembers, |
| 431 | 463 | 'pending_count' => $pendingCount |
| 432 | 464 | ], $spaceMembers, $request->all()); |
| @@ -722,8 +754,12 @@ | ||
| 722 | 754 | $userIds = $userQuery->get() |
| 723 | 755 | ->pluck('ID') |
| 724 | 756 | ->toArray(); |
| 725 | 757 | |
| 758 | + if ($userIds) { | |
| 759 | + update_meta_cache('user', $userIds); | |
| 760 | + } | |
| 761 | + | |
| 726 | 762 | $users = User::select($selects) |
| 727 | 763 | ->whereIn('ID', $userIds) |
| 728 | 764 | ->paginate(100); |
| 729 | 765 | |
| @@ -759,8 +795,61 @@ | ||
| 759 | 795 | 'links' => $links |
| 760 | 796 | ]; |
| 761 | 797 | } |
| 762 | 798 | |
| 799 | + public function getPrimaryMenu(Request $request, $slug) | |
| 800 | + { | |
| 801 | + $space = Space::where('slug', $slug)->first(); | |
| 802 | + | |
| 803 | + if (!$space) { | |
| 804 | + return $this->sendError([ | |
| 805 | + 'message' => __('Space not found', 'fluent-community'), | |
| 806 | + ]); | |
| 807 | + } | |
| 808 | + | |
| 809 | + // Lighter than formatSpaceData(), but listeners like fcom-chat need membership too. | |
| 810 | + $user = $this->getUser(); | |
| 811 | + $space->permissions = $space->getUserPermissions($user); | |
| 812 | + $space->membership = $space->getMembership($user ? $user->ID : null); | |
| 813 | + | |
| 814 | + return [ | |
| 815 | + 'menu_items' => SpaceMenuService::getManagerItems($space), | |
| 816 | + ]; | |
| 817 | + } | |
| 818 | + | |
| 819 | + public function updatePrimaryMenu(Request $request, $slug) | |
| 820 | + { | |
| 821 | + $space = Space::where('slug', $slug)->first(); | |
| 822 | + | |
| 823 | + if (!$space) { | |
| 824 | + return $this->sendError([ | |
| 825 | + 'message' => __('Space not found', 'fluent-community'), | |
| 826 | + ]); | |
| 827 | + } | |
| 828 | + | |
| 829 | + $incoming = $request->get('menu_items', []); | |
| 830 | + | |
| 831 | + if (!is_array($incoming)) { | |
| 832 | + return $this->sendError([ | |
| 833 | + 'message' => __('Menu items must be a list.', 'fluent-community'), | |
| 834 | + ], 422); | |
| 835 | + } | |
| 836 | + | |
| 837 | + $menuItems = CustomSanitizer::sanitizeSpaceMenuItems($incoming); | |
| 838 | + | |
| 839 | + SpaceMenuService::storeMenu($space, $menuItems); | |
| 840 | + | |
| 841 | + // No re-fetch needed — unlike formatSpaceData(), this doesn't make $space unsaveable. | |
| 842 | + $user = $this->getUser(); | |
| 843 | + $space->permissions = $space->getUserPermissions($user); | |
| 844 | + $space->membership = $space->getMembership($user ? $user->ID : null); | |
| 845 | + | |
| 846 | + return [ | |
| 847 | + 'message' => __('Menu has been updated for the space', 'fluent-community'), | |
| 848 | + 'menu_items' => SpaceMenuService::getManagerItems($space), | |
| 849 | + ]; | |
| 850 | + } | |
| 851 | + | |
| 763 | 852 | public function getSpaceGroups(Request $request) |
| 764 | 853 | { |
| 765 | 854 | if ($request->get('options_only')) { |
| 766 | 855 | $groups = SpaceGroup::orderBy('serial', 'ASC') |
| @@ -817,13 +906,28 @@ | ||
| 817 | 906 | 'title' => 'required|unique:fcom_spaces,title', |
| 818 | 907 | 'slug' => 'required|unique:fcom_spaces,slug' |
| 819 | 908 | ]); |
| 820 | 909 | |
| 910 | + $title = sanitize_text_field(Arr::get($data, 'title', '')); | |
| 911 | + $slug = sanitize_title(Arr::get($data, 'slug', '')); | |
| 912 | + $desc = sanitize_textarea_field(Arr::get($data, 'description', '')); | |
| 821 | 913 | |
| 914 | + if (!$title) { | |
| 915 | + return $this->sendError([ | |
| 916 | + 'message' => __('Please enter a valid group title.', 'fluent-community') | |
| 917 | + ]); | |
| 918 | + } | |
| 919 | + | |
| 920 | + if (!$slug) { | |
| 921 | + return $this->sendError([ | |
| 922 | + 'message' => __('Please enter a valid group slug.', 'fluent-community') | |
| 923 | + ]); | |
| 924 | + } | |
| 925 | + | |
| 822 | 926 | $formattedData = [ |
| 823 | - 'title' => sanitize_text_field($data['title']), | |
| 824 | - 'slug' => sanitize_title($data['slug']), | |
| 825 | - 'description' => sanitize_textarea_field($data['description']), | |
| 927 | + 'title' => $title, | |
| 928 | + 'slug' => $slug, | |
| 929 | + 'description' => $desc, | |
| 826 | 930 | 'status' => 'active', |
| 827 | 931 | 'type' => 'space_group', |
| 828 | 932 | 'settings' => [ |
| 829 | 933 | 'always_show_spaces' => Arr::get($data, 'settings.always_show_spaces', 'yes'), |
| @@ -944,9 +1048,19 @@ | ||
| 944 | 1048 | } |
| 945 | 1049 | |
| 946 | 1050 | public function getLockScreenSettings(Request $request, $spaceSlug) |
| 947 | 1051 | { |
| 948 | - $space = Space::where('slug', $spaceSlug)->firstOrFail(); | |
| 1052 | + /** @var Space $space */ | |
| 1053 | + $space = Space::where('slug', $spaceSlug)->first(); | |
| 1054 | + | |
| 1055 | + $userId = $this->getUser() ? $this->getUser()->ID : null; | |
| 1056 | + | |
| 1057 | + if (!$space || ($space->privacy == 'secret' && !$space->getMembership($userId) && !$space->isAdmin($userId, true))) { | |
| 1058 | + return $this->sendError([ | |
| 1059 | + 'message' => __('Space not found', 'fluent-community') | |
| 1060 | + ], 404); | |
| 1061 | + } | |
| 1062 | + | |
| 949 | 1063 | $lockscreen = $space->getLockscreen(); |
| 950 | 1064 | |
| 951 | 1065 | $lockscreen = apply_filters('fluent_community/get_lockscreen_settings', $lockscreen, $space); |
| 952 | 1066 | |