PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.01
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.6.01, at app/Http/Controllers/ReactionController.php

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