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

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