PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
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 / ReactionController.php

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

225 lines 7.4 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\Models\Comment;
6 use FluentCommunity\App\Models\Feed;
7 use FluentCommunity\App\Models\Reaction;
8 use FluentCommunity\App\Services\FeedsHelper;
9 use FluentCommunity\App\Services\ProfileHelper;
10 use FluentCommunity\Framework\Http\Request\Request;
11 use FluentCommunity\Framework\Support\Arr;
12
13 class ReactionController extends Controller
14 {
15 public function getByFeedId(Request $request)
16 {
17 $feedId = $request->getSafe('feed_id', 'intval');
18
19 if (!$feedId) {
20 return [
21 'reactions' => []
22 ];
23 }
24
25 $feed = Feed::withoutGlobalScopes()->byUserAccess(get_current_user_id())->findOrFail($feedId);
26
27 $reactions = $feed->reactions()
28 ->whereHas('xprofile')
29 ->with([
30 'xprofile' => function ($q) {
31 $q->select(ProfileHelper::getXProfilePublicFields());
32 }
33 ])
34 ->where('type', 'like')
35 ->distinct('user_id')
36 ->limit(100)
37 ->get(); // Todo: Add lazy loading in the future
38
39 return apply_filters('fluent_community/reactions_api_response', [
40 'reactions' => $reactions
41 ], $reactions, $request->all());
42 }
43
44 public function getByCommentId(Request $request)
45 {
46 $commentId = $request->getSafe('comment_id', 'intval');
47
48 if (!$commentId) {
49 return [
50 'reactions' => []
51 ];
52 }
53
54 $comment = Comment::findOrFail($commentId);
55
56 // Just validate the permission
57 Feed::withoutGlobalScopes()->byUserAccess(get_current_user_id())->findOrFail($comment->post_id);
58
59 $reactions = $comment
60 ->reactions()
61 ->whereHas('xprofile')
62 ->with([
63 'xprofile' => function ($q) {
64 $q->select(ProfileHelper::getXProfilePublicFields());
65 }
66 ])
67 ->where('type', 'like')
68 ->distinct('user_id')
69 ->limit(100)
70 ->get(); // Todo: Add lazy loading in the future
71
72 return apply_filters('fluent_community/reactions_api_response', [
73 'reactions' => $reactions
74 ], $reactions, $request->all());
75 }
76
77 public function addOrRemovePostReact(Request $request, $feed_id)
78 {
79 $currentUser = $this->getUser(true);
80 $feed = Feed::withoutGlobalScopes()->byUserAccess($currentUser->ID)->findOrFail($feed_id);
81 $type = $request->get('react_type', 'like');
82 $willRemove = $request->get('remove');
83
84 if ($feed->status != 'published') {
85 return $this->sendError([
86 'message' => __('This post is not published yet', 'fluent-community')
87 ]);
88 }
89
90 if ($currentUser->ID === $feed->user_id && apply_filters('fluent_community/disable_self_post_react', false, $feed)) {
91 return $this->sendError([
92 'message' => __('You cannot react to your own post', 'fluent-community')
93 ]);
94 }
95
96 $react = Reaction::where('user_id', $currentUser->ID)
97 ->where('object_id', $feed->id)
98 ->where('type', $type)
99 ->objectType('feed')
100 ->first();
101
102 if ($willRemove) {
103 if ($react) {
104 $react->delete();
105 if ($type == 'like') {
106 $feed->reactions_count = $feed->reactions_count - 1;
107 $feed->timestamps = false; // Don't update the updated_at timestamp
108 $feed->save();
109 do_action('fluent_community/feed/react_removed', $feed);
110 }
111 }
112
113 return [
114 'message' => __('Reaction has been removed', 'fluent-community'),
115 'new_count' => $feed->reactions_count
116 ];
117 }
118
119 if ($react) {
120 return [
121 'message' => __('You have already reacted to this post', 'fluent-community'),
122 'new_count' => $feed->reactions_count
123 ];
124 }
125
126 $react = Reaction::create([
127 'user_id' => $currentUser->ID,
128 'object_id' => $feed->id,
129 'type' => $type,
130 'object_type' => 'feed'
131 ]);
132
133 if ($type == 'like') {
134 $feed->reactions_count = $feed->reactions_count + 1;
135 $feed->timestamps = false; // Don't update the updated_at timestamp
136 $feed->save();
137
138 $react->load('xprofile');
139 do_action('fluent_community/feed/react_added', $react, $feed);
140 }
141
142 return [
143 'message' => __('Reaction has been added', 'fluent-community'),
144 'new_count' => $feed->reactions_count
145 ];
146 }
147
148 public function castSurveyVote(Request $request, $feed_id)
149 {
150 $userId = $this->getUserId();
151
152 $feed = Feed::byUserAccess($userId)
153 ->where('id', $feed_id)
154 ->first();
155
156 if (!$feed || $feed->content_type != 'survey' || !$userId) {
157 return $this->sendError([
158 'message' => __('Sorry! you do not have access to this post or invalid request', 'fluent-community')
159 ]);
160 }
161
162 $surveyConfig = Arr::get($feed->meta, 'survey_config', []);
163 if (empty($surveyConfig['options'])) {
164 return $this->sendError([
165 'message' => __('Sorry! This survey configuration is invalid', 'fluent-community')
166 ]);
167 }
168
169 $endDate = Arr::get($surveyConfig, 'end_date');
170 if ($endDate && strtotime($endDate) < current_time('timestamp')) {
171 return $this->sendError([
172 'message' => __('Sorry! This survey has ended', 'fluent-community')
173 ]);
174 }
175
176 $voteIndexes = (array) $request->get('vote_indexes', []);
177
178 $voteIndexes = array_values(array_map('sanitize_text_field', $voteIndexes));
179
180 $feed = FeedsHelper::castSurveyVote($voteIndexes, $feed, $userId);
181 $surveyConfig = Arr::get($feed->meta, 'survey_config', []);
182 $options = Arr::get($surveyConfig, 'options', []);
183 if (empty($options) || !is_array($options)) {
184 $options = [];
185 }
186
187 $votedOptions = $feed->getSurveyCastsByUserId($userId);
188
189 foreach ($options as $index => $option) {
190 if (in_array(Arr::get($option, 'slug'), $votedOptions, true)) {
191 $surveyConfig['options'][$index]['voted'] = true;
192 }
193 }
194
195 $surveyConfig = apply_filters('fluent_community/survey_config_response', $surveyConfig, $feed, $userId);
196
197 return [
198 'survey_config' => $surveyConfig
199 ];
200 }
201
202 public function getSurveyVoters($feedId, $optionSlug)
203 {
204 $currentUserId = get_current_user_id();
205
206 $feed = Feed::withoutGlobalScopes()->byUserAccess($currentUserId)->findOrFail($feedId);
207
208 $voters = $feed->surveyVotes()
209 ->where('object_type', $optionSlug)
210 ->whereHas('xprofile')
211 ->with([
212 'xprofile' => function ($q) {
213 $q->select(ProfileHelper::getXProfilePublicFields());
214 }
215 ])
216 ->limit(100)
217 ->get(); // Todo: Add lazy loading in the future
218
219 $data = [
220 'voters' => $voters
221 ];
222 return apply_filters('fluent_community/survey_voters_api_response', $data, $this->request->all());
223 }
224 }
225