| @@ -4,15 +4,13 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentCommunity\App\Http\Controllers\Controller; |
| 6 | 6 | use FluentCommunity\App\Models\Activity; |
| 7 | 7 | use FluentCommunity\App\Models\Feed; |
| 8 | -use FluentCommunity\App\Models\Notification; | |
| 9 | -use FluentCommunity\App\Models\NotificationSubscriber; | |
| 10 | 8 | use FluentCommunity\App\Models\BaseSpace; |
| 11 | 9 | use FluentCommunity\App\Models\SpaceUserPivot; |
| 12 | 10 | use FluentCommunity\App\Services\ProfileHelper; |
| 11 | +use FluentCommunity\App\Services\Helper; | |
| 13 | 12 | use FluentCommunity\Framework\Http\Request\Request; |
| 14 | -use FluentCommunity\Framework\Support\Arr; | |
| 15 | 13 | |
| 16 | 14 | class ActivityController extends Controller |
| 17 | 15 | { |
| 18 | 16 | public function getActivities(Request $request) |
| @@ -18,17 +16,28 @@ | ||
| 18 | 16 | public function getActivities(Request $request) |
| 19 | 17 | { |
| 20 | 18 | $context = $request->get('context', []); |
| 21 | 19 | |
| 22 | - $activities = Activity::where(function ($q) { | |
| 23 | - $userId = get_current_user_id(); | |
| 20 | + $spaceId = !empty($context['space_id']) ? (int)$context['space_id'] : null; | |
| 21 | + $userId = !empty($context['user_id']) ? (int)$context['user_id'] : null; | |
| 22 | + | |
| 23 | + $maxPerPage = (int) apply_filters('fluent_community/max_per_page', 100) ?: 100; | |
| 24 | + $perPage = min($maxPerPage, max(1, (int)$request->get('per_page', 5))); | |
| 25 | + $page = max(1, (int)$request->get('page', 1)); | |
| 26 | + | |
| 27 | + $latestActivityIds = Activity::where(function ($q) { | |
| 28 | + if (Helper::isModerator()) { | |
| 29 | + return $q; | |
| 30 | + } | |
| 31 | + | |
| 32 | + $currentUserId = get_current_user_id(); | |
| 24 | 33 | $q->where('is_public', 1); |
| 25 | - if (!$userId) { | |
| 34 | + if (!$currentUserId) { | |
| 26 | 35 | return $q; |
| 27 | 36 | } |
| 28 | 37 | |
| 29 | - $q->orWhere(function ($query) use ($userId) { | |
| 30 | - $spaceIds = get_user_meta($userId, '_fcom_space_ids', true); | |
| 38 | + $q->orWhere(function ($query) use ($currentUserId) { | |
| 39 | + $spaceIds = get_user_meta($currentUserId, '_fcom_space_ids', true); | |
| 31 | 40 | if ($spaceIds) { |
| 32 | 41 | $query->whereIn('space_id', $spaceIds); |
| 33 | 42 | return $query; |
| 34 | 43 | } |
| @@ -34,27 +43,33 @@ | ||
| 34 | 43 | } |
| 35 | 44 | }); |
| 36 | 45 | }) |
| 37 | 46 | ->whereIn('action_name', ['feed_published', 'comment_added']) |
| 47 | + ->when($spaceId, function ($q) use ($spaceId) { | |
| 48 | + $q->where('space_id', $spaceId); | |
| 49 | + }) | |
| 50 | + ->when((!$spaceId && $userId), function ($q) use ($userId) { | |
| 51 | + $q->where('user_id', $userId); | |
| 52 | + }) | |
| 53 | + ->selectRaw('MAX(id) as id') | |
| 54 | + ->groupBy('feed_id', 'action_name') | |
| 55 | + ->pluck('id'); | |
| 56 | + | |
| 57 | + $activities = Activity::whereIn('id', $latestActivityIds) | |
| 38 | 58 | ->with(['xprofile' => function ($q) { |
| 39 | 59 | $q->select(ProfileHelper::getXProfilePublicFields()); |
| 40 | - }, 'feed', 'space']) | |
| 60 | + }, 'feed', 'feed.space', 'space']) | |
| 61 | + ->whereHas('feed', function ($q) { | |
| 62 | + $q->where('status', 'published'); | |
| 63 | + }) | |
| 64 | + ->whereHas('xprofile', function ($q) { | |
| 65 | + $q->where('status', 'active'); | |
| 66 | + }) | |
| 41 | 67 | ->orderBy('id', 'DESC') |
| 42 | - ->groupBy('feed_id', 'action_name'); | |
| 68 | + ->limit($perPage + 1) | |
| 69 | + ->offset(($page - 1) * $perPage) | |
| 70 | + ->get(); | |
| 43 | 71 | |
| 44 | - $spaceId = null; | |
| 45 | - $userId = null; | |
| 46 | - | |
| 47 | - if ($context && !empty($context['space_id'])) { | |
| 48 | - $spaceId = (int) $context['space_id']; | |
| 49 | - $activities->where('space_id', $context['space_id']); | |
| 50 | - } else if($context && !empty($context['user_id'])) { | |
| 51 | - $userId = (int) $context['user_id']; | |
| 52 | - $activities->where('user_ID', $context['user_id']); | |
| 53 | - } | |
| 54 | - | |
| 55 | - $activities = $activities->paginate(); | |
| 56 | - | |
| 57 | 72 | $formattedActivities = []; |
| 58 | 73 | |
| 59 | 74 | foreach ($activities as $activity) { |
| 60 | 75 | $message = $activity->getFormattedMessage(); |
| @@ -78,33 +93,43 @@ | ||
| 78 | 93 | 'route' => $route |
| 79 | 94 | ]; |
| 80 | 95 | } |
| 81 | 96 | |
| 82 | - if($spaceId) { | |
| 97 | + if ($spaceId) { | |
| 83 | 98 | $afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context); |
| 84 | - } else if($userId) { | |
| 99 | + $beforeContent = apply_filters('fluent_community/activity/before_contents_space', '', $spaceId, $context); | |
| 100 | + } else if ($userId) { | |
| 85 | 101 | $afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context); |
| 102 | + $beforeContent = apply_filters('fluent_community/activity/before_contents_user', '', $userId, $context); | |
| 86 | 103 | } else { |
| 87 | 104 | $afterContent = apply_filters('fluent_community/activity/after_contents', '', $context); |
| 105 | + $beforeContent = apply_filters('fluent_community/activity/before_contents', '', $context); | |
| 88 | 106 | } |
| 89 | 107 | |
| 108 | + $totalCount = count($formattedActivities); | |
| 109 | + | |
| 110 | + $hasMore = $totalCount > $perPage; | |
| 111 | + | |
| 112 | + if ($hasMore) { | |
| 113 | + array_pop($formattedActivities); | |
| 114 | + } | |
| 115 | + | |
| 90 | 116 | $returnData = [ |
| 91 | - 'activities' => [ | |
| 117 | + 'activities' => [ | |
| 92 | 118 | 'data' => $formattedActivities, |
| 93 | - 'total' => $activities->total(), | |
| 94 | - 'per_page' => $activities->perPage(), | |
| 95 | - 'current_page' => $activities->currentPage(), | |
| 119 | + 'has_more' => (boolean) $hasMore, | |
| 120 | + 'per_page' => $perPage, | |
| 121 | + 'current_page' => $page, | |
| 96 | 122 | ], |
| 97 | - 'after_contents' => $afterContent, | |
| 123 | + 'after_contents' => $afterContent, | |
| 124 | + 'before_contents' => $beforeContent, | |
| 98 | 125 | ]; |
| 99 | 126 | |
| 100 | - if(!$spaceId) { | |
| 101 | - | |
| 102 | - if(!$userId) { | |
| 127 | + if (!$spaceId) { | |
| 128 | + if (!$userId) { | |
| 103 | 129 | $returnData['pinned_posts'] = $this->getPinnedPosts(null, true); |
| 104 | 130 | } |
| 105 | - | |
| 106 | - return $returnData; | |
| 131 | + return apply_filters('fluent_community/activities_api_response', $returnData, $request->all()); | |
| 107 | 132 | } |
| 108 | 133 | |
| 109 | 134 | if ($request->get('with_pins')) { |
| 110 | 135 | $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending')); |
| @@ -109,9 +134,9 @@ | ||
| 109 | 134 | if ($request->get('with_pins')) { |
| 110 | 135 | $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending')); |
| 111 | 136 | } |
| 112 | 137 | |
| 113 | - if ($request->get('with_pening_count')) { | |
| 138 | + if ($request->get('with_pending_count')) { | |
| 114 | 139 | $pendingCount = 0; |
| 115 | 140 | $user = $this->getUser(); |
| 116 | 141 | |
| 117 | 142 | $space = BaseSpace::find($spaceId); |
| @@ -124,9 +149,9 @@ | ||
| 124 | 149 | |
| 125 | 150 | $returnData['pending_count'] = $pendingCount; |
| 126 | 151 | } |
| 127 | 152 | |
| 128 | - return $returnData; | |
| 153 | + return apply_filters('fluent_community/activities_api_response', $returnData, $request->all()); | |
| 129 | 154 | } |
| 130 | 155 | |
| 131 | 156 | private function getPinnedPosts($spaceId = null, $isTrending = false) |
| 132 | 157 | { |
| @@ -134,8 +159,11 @@ | ||
| 134 | 159 | $q->where('space_id', $spaceId); |
| 135 | 160 | }) |
| 136 | 161 | ->byUserAccess(get_current_user_id()) |
| 137 | 162 | ->where('status', 'published') |
| 163 | + ->whereHas('xprofile', function ($q) { | |
| 164 | + $q->where('status', 'active'); | |
| 165 | + }) | |
| 138 | 166 | ->with(['xprofile' => function ($q) { |
| 139 | 167 | $q->select(ProfileHelper::getXProfilePublicFields()); |
| 140 | 168 | }], 'space') |
| 141 | 169 | ->limit(5); |
| @@ -163,7 +191,7 @@ | ||
| 163 | 191 | 'created_at' => $post->created_at->format('Y-m-d H:i:s'), |
| 164 | 192 | ]; |
| 165 | 193 | } |
| 166 | 194 | |
| 167 | - return $formattedActivities; | |
| 195 | + return apply_filters('fluent_community/pinned_posts_api_response', $formattedActivities, $spaceId, $isTrending); | |
| 168 | 196 | } |
| 169 | 197 | } |