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

179 lines 5.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\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
12 class ReactionController extends Controller
13 {
14 public function getByFeedId(Request $request)
15 {
16 $feedId = $request->getSafe('feed_id', 'intval');
17
18 if (!$feedId) {
19 return [
20 'reactions' => []
21 ];
22 }
23
24 $feed = Feed::withoutGlobalScopes()->byUserAccess(get_current_user_id())->findOrFail($feedId);
25
26 $reactions = $feed->reactions()
27 ->whereHas('xprofile')
28 ->with([
29 'xprofile' => function ($q) {
30 $q->select(ProfileHelper::getXProfilePublicFields());
31 }
32 ])
33 ->limit(100)
34 ->get(); // Todo: Add lazy loading in the future
35
36 return [
37 'reactions' => $reactions
38 ];
39 }
40
41 public function getByCommentId(Request $request)
42 {
43 $commentId = $request->getSafe('comment_id', 'intval');
44
45 if (!$commentId) {
46 return [
47 'reactions' => []
48 ];
49 }
50
51 $comment = Comment::findOrFail($commentId);
52
53 // Just validate the permission
54 $feed = Feed::withoutGlobalScopes()->byUserAccess(get_current_user_id())->findOrFail($comment->post_id);
55
56 $reactions = $comment
57 ->reactions()
58 ->whereHas('xprofile')
59 ->with([
60 'xprofile' => function ($q) {
61 $q->select(ProfileHelper::getXProfilePublicFields());
62 }
63 ])
64 ->limit(100)
65 ->get(); // Todo: Add lazy loading in the future
66
67 return [
68 'reactions' => $reactions
69 ];
70 }
71
72 public function addOrRemovePostReact(Request $request, $feed_id)
73 {
74 $feed = Feed::withoutGlobalScopes()->findOrFail($feed_id);
75 $type = $request->get('react_type', 'like');
76 $willRemove = $request->get('remove');
77
78 $react = Reaction::where('user_id', get_current_user_id())
79 ->where('object_id', $feed->id)
80 ->where('type', $type)
81 ->objectType('feed')
82 ->first();
83
84 if ($willRemove) {
85 if ($react) {
86 $react->delete();
87
88 if ($type == 'like') {
89 $feed->reactions_count = $feed->reactions_count - 1;
90 $feed->save();
91 }
92 }
93
94 return [
95 'message' => 'Reaction has been removed',
96 'new_count' => $feed->reactions_count
97 ];
98 }
99
100 if ($react) {
101 return [
102 'message' => 'You have already reacted to this post',
103 'new_count' => $feed->reactions_count
104 ];
105 }
106
107 $react = Reaction::create([
108 'user_id' => get_current_user_id(),
109 'object_id' => $feed->id,
110 'type' => $type,
111 'object_type' => 'feed'
112 ]);
113
114 if ($type == 'like') {
115 $feed->reactions_count = $feed->reactions_count + 1;
116 $feed->save();
117
118 $react->load('xprofile');
119 do_action('fluent_community/feed/react_added', $react, $feed);
120 }
121
122 return [
123 'message' => 'Reaction has been added',
124 'new_count' => $feed->reactions_count
125 ];
126 }
127
128 public function castSurveyVote(Request $request, $feed_id)
129 {
130 $feed = Feed::where('id', $feed_id)
131 ->byUserAccess($this->getUserId())
132 ->first();
133
134 if (!$feed || $feed->content_type != 'survey') {
135 return $this->sendError([
136 'message' => __('Sorry! you do not have access to this post or invalid request', 'fluent-community')
137 ]);
138 }
139
140 $voteIndexes = $request->get('vote_indexes', []);
141
142 $feed = FeedsHelper::castSurveyVote($voteIndexes, $feed, $this->getUserId());
143 $surveyConfig = $feed->meta['survey_config'];
144 $votedOptions = $feed->getSurveyCastsByUserId($this->getUserId());
145
146 foreach ($surveyConfig['options'] as $index => $option) {
147 if (in_array($option['slug'], $votedOptions)) {
148 $surveyConfig['options'][$index]['voted'] = true;
149 }
150 }
151
152 return [
153 'survey_config' => $surveyConfig
154 ];
155 }
156
157 public function getSurveyVoters($feedId, $optionSlug)
158 {
159 $currentUserId = get_current_user_id();
160
161 $feed = Feed::withoutGlobalScopes()->byUserAccess($currentUserId)->findOrFail($feedId);
162
163 $voters = $feed->surveyVotes()
164 ->where('object_type', $optionSlug)
165 ->whereHas('xprofile')
166 ->with([
167 'xprofile' => function ($q) {
168 $q->select(ProfileHelper::getXProfilePublicFields());
169 }
170 ])
171 ->limit(100)
172 ->get(); // Todo: Add lazy loading in the future
173
174 return [
175 'voters' => $voters
176 ];
177 }
178 }
179