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

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

194 lines 7.2 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 $latestActivityIds = Activity::where(function ($q) {
24 if (Helper::isModerator()) {
25 return $q;
26 }
27
28 $currentUserId = get_current_user_id();
29 $q->where('is_public', 1);
30 if (!$currentUserId) {
31 return $q;
32 }
33
34 $q->orWhere(function ($query) use ($currentUserId) {
35 $spaceIds = get_user_meta($currentUserId, '_fcom_space_ids', true);
36 if ($spaceIds) {
37 $query->whereIn('space_id', $spaceIds);
38 return $query;
39 }
40 });
41 })
42 ->whereIn('action_name', ['feed_published', 'comment_added'])
43 ->when($spaceId, function ($q) use ($spaceId) {
44 $q->where('space_id', $spaceId);
45 })
46 ->when((!$spaceId && $userId), function ($q) use ($userId) {
47 $q->where('user_id', $userId);
48 })
49 ->selectRaw('MAX(id) as id')
50 ->groupBy('feed_id', 'action_name')
51 ->pluck('id');
52
53 $activities = Activity::whereIn('id', $latestActivityIds)
54 ->with(['xprofile' => function ($q) {
55 $q->select(ProfileHelper::getXProfilePublicFields());
56 }, 'feed', 'space'])
57 ->whereHas('feed', function ($q) {
58 $q->where('status', 'published');
59 })
60 ->whereHas('xprofile', function ($q) {
61 $q->where('status', 'active');
62 })
63 ->orderBy('id', 'DESC')
64 ->limit($request->get('per_page', 5) + 1)
65 ->offset(($request->get('page', 1) - 1) * $request->get('per_page', 5))
66 ->get();
67
68 $formattedActivities = [];
69
70 foreach ($activities as $activity) {
71 $message = $activity->getFormattedMessage();
72 if (!$message) {
73 continue;
74 }
75
76 $route = $activity->feed->getJsRoute();
77
78 if ($activity->action_name == 'comment_added') {
79 $route['query'] = [
80 'comment_id' => $activity->related_id
81 ];
82 }
83
84 $formattedActivities[] = [
85 'id' => $activity->id,
86 'message' => $message,
87 'xprofile' => $activity->xprofile,
88 'updated_at' => $activity->updated_at->format('Y-m-d H:i:s'),
89 'route' => $route
90 ];
91 }
92
93 if ($spaceId) {
94 $afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context);
95 $beforeContent = apply_filters('fluent_community/activity/before_contents_space', '', $spaceId, $context);
96 } else if ($userId) {
97 $afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context);
98 $beforeContent = apply_filters('fluent_community/activity/before_contents_user', '', $userId, $context);
99 } else {
100 $afterContent = apply_filters('fluent_community/activity/after_contents', '', $context);
101 $beforeContent = apply_filters('fluent_community/activity/before_contents', '', $context);
102 }
103
104 $totalCount = count($formattedActivities);
105
106 $hasMore = $totalCount > $request->get('per_page', 5);
107
108 if ($hasMore) {
109 array_pop($formattedActivities);
110 }
111
112 $returnData = [
113 'activities' => [
114 'data' => $formattedActivities,
115 'has_more' => (boolean) $hasMore,
116 'per_page' => (int) $request->get('per_page', 5),
117 'current_page' => (int) $request->get('page', 1),
118 ],
119 'after_contents' => $afterContent,
120 'before_contents' => $beforeContent,
121 ];
122
123 if (!$spaceId) {
124 if (!$userId) {
125 $returnData['pinned_posts'] = $this->getPinnedPosts(null, true);
126 }
127 return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
128 }
129
130 if ($request->get('with_pins')) {
131 $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending'));
132 }
133
134 if ($request->get('with_pending_count')) {
135 $pendingCount = 0;
136 $user = $this->getUser();
137
138 $space = BaseSpace::find($spaceId);
139
140 if ($user && $space && $user->can('can_add_member', $space)) {
141 $pendingCount = SpaceUserPivot::bySpace($space->id)
142 ->where('status', 'pending')
143 ->count();
144 }
145
146 $returnData['pending_count'] = $pendingCount;
147 }
148
149 return apply_filters('fluent_community/activities_api_response', $returnData, $request->all());
150 }
151
152 private function getPinnedPosts($spaceId = null, $isTrending = false)
153 {
154 $postsQuery = Feed::when($spaceId, function ($q) use ($spaceId) {
155 $q->where('space_id', $spaceId);
156 })
157 ->byUserAccess(get_current_user_id())
158 ->where('status', 'published')
159 ->whereHas('xprofile', function ($q) {
160 $q->where('status', 'active');
161 })
162 ->with(['xprofile' => function ($q) {
163 $q->select(ProfileHelper::getXProfilePublicFields());
164 }], 'space')
165 ->limit(5);
166
167 if ($isTrending && !$spaceId) {
168 // Find trending posts which are created within last 7 days and order by reactions and comments count
169 // make the comments_count * 2 to give more priority to comments
170 $postsQuery->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-7 days')))
171 ->orderByRaw('(reactions_count + (comments_count * 2)) DESC');
172 } else {
173 $postsQuery->orderBy('id', 'DESC')
174 ->where('priority', 1);
175 }
176
177 $posts = $postsQuery->get();
178
179 $formattedActivities = [];
180
181 foreach ($posts as $post) {
182 $formattedActivities[] = [
183 'id' => $post->id,
184 'message' => $post->getHumanExcerpt(100),
185 'permalink' => $post->getPermalink(),
186 'xprofile' => $post->xprofile,
187 'created_at' => $post->created_at->format('Y-m-d H:i:s'),
188 ];
189 }
190
191 return apply_filters('fluent_community/pinned_posts_api_response', $formattedActivities, $spaceId, $isTrending);
192 }
193 }
194