PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.91
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.91
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 1.0.91, at app/Http/Controllers/ActivityController.php

180 lines 6.0 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\Notification;
9 use FluentCommunity\App\Models\NotificationSubscriber;
10 use FluentCommunity\App\Models\BaseSpace;
11 use FluentCommunity\App\Models\SpaceUserPivot;
12 use FluentCommunity\App\Services\ProfileHelper;
13 use FluentCommunity\Framework\Http\Request\Request;
14 use FluentCommunity\Framework\Support\Arr;
15
16 class ActivityController extends Controller
17 {
18 public function getActivities(Request $request)
19 {
20 $context = $request->get('context', []);
21
22 $activities = Activity::where(function ($q) {
23 $userId = get_current_user_id();
24 $q->where('is_public', 1);
25 if (!$userId) {
26 return $q;
27 }
28
29 $q->orWhere(function ($query) use ($userId) {
30 $spaceIds = get_user_meta($userId, '_fcom_space_ids', true);
31 if ($spaceIds) {
32 $query->whereIn('space_id', $spaceIds);
33 return $query;
34 }
35 });
36 })
37 ->whereIn('action_name', ['feed_published', 'comment_added'])
38 ->with(['xprofile' => function ($q) {
39 $q->select(ProfileHelper::getXProfilePublicFields());
40 }, 'feed', 'space'])
41 ->orderBy('id', 'DESC')
42 ->groupBy('feed_id', 'action_name');
43
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 $formattedActivities = [];
58
59 foreach ($activities as $activity) {
60 $message = $activity->getFormattedMessage();
61 if (!$message) {
62 continue;
63 }
64
65 $route = [
66 'name' => 'single_feed',
67 'params' => [
68 'feed_slug' => $activity->feed->slug,
69 ]
70 ];
71
72 if ($activity->space) {
73 $route['params']['space'] = $activity->space->slug;
74 $route['name'] = 'space_feed';
75 }
76
77 if ($activity->action_name == 'comment_added') {
78 $route['query'] = [
79 'comment_id' => $activity->related_id
80 ];
81 }
82
83 $formattedActivities[] = [
84 'id' => $activity->id,
85 'message' => $message,
86 'xprofile' => $activity->xprofile,
87 'updated_at' => $activity->updated_at->format('Y-m-d H:i:s'),
88 'route' => $route
89 ];
90 }
91
92 if($spaceId) {
93 $afterContent = apply_filters('fluent_community/activity/after_contents_space', '', $spaceId, $context);
94 } else if($userId) {
95 $afterContent = apply_filters('fluent_community/activity/after_contents_user', '', $userId, $context);
96 } else {
97 $afterContent = apply_filters('fluent_community/activity/after_contents', '', $context);
98 }
99
100 $returnData = [
101 'activities' => [
102 'data' => $formattedActivities,
103 'total' => $activities->total(),
104 'per_page' => $activities->perPage(),
105 'current_page' => $activities->currentPage(),
106 ],
107 'after_contents' => $afterContent,
108 ];
109
110 if(!$spaceId) {
111
112 if(!$userId) {
113 $returnData['pinned_posts'] = $this->getPinnedPosts(null, true);
114 }
115
116 return $returnData;
117 }
118
119 if ($request->get('with_pins')) {
120 $returnData['pinned_posts'] = $this->getPinnedPosts($spaceId, $request->get('is_trending'));
121 }
122
123 if ($request->get('with_pening_count')) {
124 $pendingCount = 0;
125 $user = $this->getUser();
126
127 $space = BaseSpace::find($spaceId);
128
129 if ($user && $space && $user->can('can_add_member', $space)) {
130 $pendingCount = SpaceUserPivot::bySpace($space->id)
131 ->where('status', 'pending')
132 ->count();
133 }
134
135 $returnData['pending_count'] = $pendingCount;
136 }
137
138 return $returnData;
139 }
140
141 private function getPinnedPosts($spaceId = null, $isTrending = false)
142 {
143 $postsQuery = Feed::when($spaceId, function ($q) use ($spaceId) {
144 $q->where('space_id', $spaceId);
145 })
146 ->byUserAccess(get_current_user_id())
147 ->where('status', 'published')
148 ->with(['xprofile' => function ($q) {
149 $q->select(ProfileHelper::getXProfilePublicFields());
150 }], 'space')
151 ->limit(5);
152
153 if ($isTrending && !$spaceId) {
154 // Find trending posts which are created within last 7 days and order by reactions and comments count
155 // make the comments_count * 2 to give more priority to comments
156 $postsQuery->where('created_at', '>=', gmdate('Y-m-d H:i:s', strtotime('-7 days')))
157 ->orderByRaw('(reactions_count + (comments_count * 2)) DESC');
158 } else {
159 $postsQuery->orderBy('id', 'DESC')
160 ->where('priority', 1);
161 }
162
163 $posts = $postsQuery->get();
164
165 $formattedActivities = [];
166
167 foreach ($posts as $post) {
168 $formattedActivities[] = [
169 'id' => $post->id,
170 'message' => $post->getHumanExcerpt(100),
171 'permalink' => $post->getPermalink(),
172 'xprofile' => $post->xprofile,
173 'created_at' => $post->created_at->format('Y-m-d H:i:s'),
174 ];
175 }
176
177 return $formattedActivities;
178 }
179 }
180