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

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