PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / trunk
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses vtrunk
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
fluent-community / app / Http / Controllers / ActivityController.php

ActivityController.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses trunk, at app/Http/Controllers/ActivityController.php

198 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Http\Controllers;
4
5 use FluentCommunity\App\Http\Controllers\Controller;
6 use FluentCommunity\App\Models\Activity;
7 use FluentCommunity\App\Models\Feed;
8 use FluentCommunity\App\Models\BaseSpace;
9 use FluentCommunity\App\Models\SpaceUserPivot;
10 use FluentCommunity\App\Services\ProfileHelper;
11 use FluentCommunity\App\Services\Helper;
12 use FluentCommunity\Framework\Http\Request\Request;
13
14 class ActivityController extends Controller
15 {
16 public function getActivities(Request $request)
17 {
18 $context = $request->get('context', []);
19
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();
33 $q->where('is_public', 1);
34 if (!$currentUserId) {
35 return $q;
36 }
37
38 $q->orWhere(function ($query) use ($currentUserId) {
39 $spaceIds = get_user_meta($currentUserId, '_fcom_space_ids', true);
40 if ($spaceIds) {
41 $query->whereIn('space_id', $spaceIds);
42 return $query;
43 }
44 });
45 })
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)
58 ->with(['xprofile' => function ($q) {
59 $q->select(ProfileHelper::getXProfilePublicFields());
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 })
67 ->orderBy('id', 'DESC')
68 ->limit($perPage + 1)
69 ->offset(($page - 1) * $perPage)
70 ->get();
71
72 $formattedActivities = [];
73
74 foreach ($activities as $activity) {
75 $message = $activity->getFormattedMessage();
76 if (!$message) {
77 continue;
78 }
79
80 $route = $activity->feed->getJsRoute();
81
82 if ($activity->action_name == 'comment_added') {
83 $route['query'] = [
84 'comment_id' => $activity->related_id
85 ];
86 }
87
88 $formattedActivities[] = [
89 'id' => $activity->id,
90 'message' => $message,
91 'xprofile' => $activity->xprofile,
92 'updated_at' => $activity->updated_at->format('Y-m-d H:i:s'),
93 'route' => $route
94 ];
95 }
96
97 if ($spaceId) {
98 $afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context);
99 $beforeContent = apply_filters('fluent_community/activity/before_contents_space', '', $spaceId, $context);
100 } else if ($userId) {
101 $afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context);
102 $beforeContent = apply_filters('fluent_community/activity/before_contents_user', '', $userId, $context);
103 } else {
104 $afterContent = apply_filters('fluent_community/activity/after_contents', '', $context);
105 $beforeContent = apply_filters('fluent_community/activity/before_contents', '', $context);
106 }
107
108 $totalCount = count($formattedActivities);
109
110 $hasMore = $totalCount > $perPage;
111
112 if ($hasMore) {
113 array_pop($formattedActivities);
114 }
115
116 $returnData = [
117 'activities' => [
118 'data' => $formattedActivities,
119 'has_more' => (boolean) $hasMore,
120 'per_page' => $perPage,
121 'current_page' => $page,
122 ],
123 'after_contents' => $afterContent,
124 'before_contents' => $beforeContent,
125 ];
126
127 if (!$spaceId) {
128 if (!$userId) {
129 $returnData['pinned_posts'] = $this->getPinnedPosts(null, true);
130 }
131 return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
132 }
133
134 if ($request->get('with_pins')) {
135 $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending'));
136 }
137
138 if ($request->get('with_pending_count')) {
139 $pendingCount = 0;
140 $user = $this->getUser();
141
142 $space = BaseSpace::find($spaceId);
143
144 if ($user && $space && $user->can('can_add_member', $space)) {
145 $pendingCount = SpaceUserPivot::bySpace($space->id)
146 ->where('status', 'pending')
147 ->count();
148 }
149
150 $returnData['pending_count'] = $pendingCount;
151 }
152
153 return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
154 }
155
156 private function getPinnedPosts($spaceId = null, $isTrending = false)
157 {
158 $postsQuery = Feed::when($spaceId, function ($q) use ($spaceId) {
159 $q->where('space_id', $spaceId);
160 })
161 ->byUserAccess(get_current_user_id())
162 ->where('status', 'published')
163 ->whereHas('xprofile', function ($q) {
164 $q->where('status', 'active');
165 })
166 ->with(['xprofile' => function ($q) {
167 $q->select(ProfileHelper::getXProfilePublicFields());
168 }], 'space')
169 ->limit(5);
170
171 if ($isTrending && !$spaceId) {
172 // Find trending posts which are created within last 7 days and order by reactions and comments count
173 // make the comments_count * 2 to give more priority to comments
174 $postsQuery->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-7 days')))
175 ->orderByRaw('(reactions_count + (comments_count * 2)) DESC');
176 } else {
177 $postsQuery->orderBy('id', 'DESC')
178 ->where('priority', 1);
179 }
180
181 $posts = $postsQuery->get();
182
183 $formattedActivities = [];
184
185 foreach ($posts as $post) {
186 $formattedActivities[] = [
187 'id' => $post->id,
188 'message' => $post->getHumanExcerpt(100),
189 'permalink' => $post->getPermalink(),
190 'xprofile' => $post->xprofile,
191 'created_at' => $post->created_at->format('Y-m-d H:i:s'),
192 ];
193 }
194
195 return apply_filters('fluent_community/pinned_posts_api_response', $formattedActivities, $spaceId, $isTrending);
196 }
197 }
198