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

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

226 lines 7.5 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 $type = in_array($type, ['like', 'bookmark'], true) ? $type : 'like';
83 $willRemove = $request->get('remove');
84
85 if ($feed->status != 'published') {
86 return $this->sendError([
87 'message' => __('This post is not published yet', 'fluent-community')
88 ]);
89 }
90
91 if (!$willRemove && (int) $currentUser->ID === (int) $feed->user_id && apply_filters('fluent_community/disable_self_post_react', false, $feed)) {
92 return $this->sendError([
93 'message' => __('You cannot react to your own post', 'fluent-community')
94 ]);
95 }
96
97 $react = Reaction::where('user_id', $currentUser->ID)
98 ->where('object_id', $feed->id)
99 ->where('type', $type)
100 ->objectType('feed')
101 ->first();
102
103 if ($willRemove) {
104 if ($react) {
105 $react->delete();
106 if ($type == 'like') {
107 $feed->reactions_count = $feed->reactions_count - 1;
108 $feed->timestamps = false; // Don't update the updated_at timestamp
109 $feed->save();
110 do_action('fluent_community/feed/react_removed', $feed);
111 }
112 }
113
114 return [
115 'message' => __('Reaction has been removed', 'fluent-community'),
116 'new_count' => $feed->reactions_count
117 ];
118 }
119
120 if ($react) {
121 return [
122 'message' => __('You have already reacted to this post', 'fluent-community'),
123 'new_count' => $feed->reactions_count
124 ];
125 }
126
127 $react = Reaction::create([
128 'user_id' => $currentUser->ID,
129 'object_id' => $feed->id,
130 'type' => $type,
131 'object_type' => 'feed'
132 ]);
133
134 if ($type == 'like') {
135 $feed->reactions_count = $feed->reactions_count + 1;
136 $feed->timestamps = false; // Don't update the updated_at timestamp
137 $feed->save();
138
139 $react->load('xprofile');
140 do_action('fluent_community/feed/react_added', $react, $feed);
141 }
142
143 return [
144 'message' => __('Reaction has been added', 'fluent-community'),
145 'new_count' => $feed->reactions_count
146 ];
147 }
148
149 public function castSurveyVote(Request $request, $feed_id)
150 {
151 $userId = $this->getUserId();
152
153 $feed = Feed::byUserAccess($userId)
154 ->where('id', $feed_id)
155 ->first();
156
157 if (!$feed || $feed->content_type != 'survey' || !$userId) {
158 return $this->sendError([
159 'message' => __('Sorry! you do not have access to this post or invalid request', 'fluent-community')
160 ]);
161 }
162
163 $surveyConfig = Arr::get($feed->meta, 'survey_config', []);
164 if (empty($surveyConfig['options'])) {
165 return $this->sendError([
166 'message' => __('Sorry! This survey configuration is invalid', 'fluent-community')
167 ]);
168 }
169
170 $endDate = Arr::get($surveyConfig, 'end_date');
171 if ($endDate && strtotime($endDate) < current_time('timestamp')) {
172 return $this->sendError([
173 'message' => __('Sorry! This survey has ended', 'fluent-community')
174 ]);
175 }
176
177 $voteIndexes = (array) $request->get('vote_indexes', []);
178
179 $voteIndexes = array_values(array_map('sanitize_text_field', $voteIndexes));
180
181 $feed = FeedsHelper::castSurveyVote($voteIndexes, $feed, $userId);
182 $surveyConfig = Arr::get($feed->meta, 'survey_config', []);
183 $options = Arr::get($surveyConfig, 'options', []);
184 if (empty($options) || !is_array($options)) {
185 $options = [];
186 }
187
188 $votedOptions = $feed->getSurveyCastsByUserId($userId);
189
190 foreach ($options as $index => $option) {
191 if (in_array(Arr::get($option, 'slug'), $votedOptions, true)) {
192 $surveyConfig['options'][$index]['voted'] = true;
193 }
194 }
195
196 $surveyConfig = apply_filters('fluent_community/survey_config_response', $surveyConfig, $feed, $userId);
197
198 return [
199 'survey_config' => $surveyConfig
200 ];
201 }
202
203 public function getSurveyVoters($feedId, $optionSlug)
204 {
205 $currentUserId = get_current_user_id();
206
207 $feed = Feed::withoutGlobalScopes()->byUserAccess($currentUserId)->findOrFail($feedId);
208
209 $voters = $feed->surveyVotes()
210 ->where('object_type', $optionSlug)
211 ->whereHas('xprofile')
212 ->with([
213 'xprofile' => function ($q) {
214 $q->select(ProfileHelper::getXProfilePublicFields());
215 }
216 ])
217 ->limit(100)
218 ->get(); // Todo: Add lazy loading in the future
219
220 $data = [
221 'voters' => $voters
222 ];
223 return apply_filters('fluent_community/survey_voters_api_response', $data, $this->request->all());
224 }
225 }
226