PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
← All changes | app/Http/Controllers/ActivityController.php +67 -49 1.0.932.10.0 View file →
@@ -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();
@@ -61,20 +76,10 @@
61 76 if (!$message) {
62 77 continue;
63 78 }
64 79
65 - $route = [
66 - 'name' => 'single_feed',
67 - 'params' => [
68 - 'feed_slug' => $activity->feed->slug,
69 - ]
70 - ];
80 + $route = $activity->feed->getJsRoute();
71 81
72 - if ($activity->space) {
73 - $route['params']['space'] = $activity->space->slug;
74 - $route['name'] = 'space_feed';
75 - }
76 -
77 82 if ($activity->action_name == 'comment_added') {
78 83 $route['query'] = [
79 84 'comment_id' => $activity->related_id
80 85 ];
@@ -88,33 +93,43 @@
88 93 'route' => $route
89 94 ];
90 95 }
91 96
92 - if($spaceId) {
97 + if ($spaceId) {
93 98 $afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context);
94 - } else if($userId) {
99 + $beforeContent = apply_filters('fluent_community/activity/before_contents_space', '', $spaceId, $context);
100 + } else if ($userId) {
95 101 $afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context);
102 + $beforeContent = apply_filters('fluent_community/activity/before_contents_user', '', $userId, $context);
96 103 } else {
97 104 $afterContent = apply_filters('fluent_community/activity/after_contents', '', $context);
105 + $beforeContent = apply_filters('fluent_community/activity/before_contents', '', $context);
98 106 }
99 107
108 + $totalCount = count($formattedActivities);
109 +
110 + $hasMore = $totalCount > $perPage;
111 +
112 + if ($hasMore) {
113 + array_pop($formattedActivities);
114 + }
115 +
100 116 $returnData = [
101 - 'activities' => [
117 + 'activities' => [
102 118 'data' => $formattedActivities,
103 - 'total' => $activities->total(),
104 - 'per_page' => $activities->perPage(),
105 - 'current_page' => $activities->currentPage(),
119 + 'has_more' => (boolean) $hasMore,
120 + 'per_page' => $perPage,
121 + 'current_page' => $page,
106 122 ],
107 - 'after_contents' => $afterContent,
123 + 'after_contents' => $afterContent,
124 + 'before_contents' => $beforeContent,
108 125 ];
109 126
110 - if(!$spaceId) {
111 -
112 - if(!$userId) {
127 + if (!$spaceId) {
128 + if (!$userId) {
113 129 $returnData['pinned_posts'] = $this->getPinnedPosts(null, true);
114 130 }
115 -
116 - return $returnData;
131 + return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
117 132 }
118 133
119 134 if ($request->get('with_pins')) {
120 135 $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending'));
@@ -119,9 +134,9 @@
119 134 if ($request->get('with_pins')) {
120 135 $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending'));
121 136 }
122 137
123 - if ($request->get('with_pening_count')) {
138 + if ($request->get('with_pending_count')) {
124 139 $pendingCount = 0;
125 140 $user = $this->getUser();
126 141
127 142 $space = BaseSpace::find($spaceId);
@@ -134,9 +149,9 @@
134 149
135 150 $returnData['pending_count'] = $pendingCount;
136 151 }
137 152
138 - return $returnData;
153 + return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
139 154 }
140 155
141 156 private function getPinnedPosts($spaceId = null, $isTrending = false)
142 157 {
@@ -144,8 +159,11 @@
144 159 $q->where('space_id', $spaceId);
145 160 })
146 161 ->byUserAccess(get_current_user_id())
147 162 ->where('status', 'published')
163 + ->whereHas('xprofile', function ($q) {
164 + $q->where('status', 'active');
165 + })
148 166 ->with(['xprofile' => function ($q) {
149 167 $q->select(ProfileHelper::getXProfilePublicFields());
150 168 }], 'space')
151 169 ->limit(5);
@@ -173,7 +191,7 @@
173 191 'created_at' => $post->created_at->format('Y-m-d H:i:s'),
174 192 ];
175 193 }
176 194
177 - return $formattedActivities;
195 + return apply_filters('fluent_community/pinned_posts_api_response', $formattedActivities, $spaceId, $isTrending);
178 196 }
179 197 }