Blame view
app/Queries/Comment/FlagsReportQuery.php
5.17 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
<?php namespace FootyRoom\Queries\Comment; use FootyRoom\Support\Arr; use FootyRoom\Support\AutoMapper; use FootyRoom\Support\MongoClient; use FootyRoom\Queries\Post\OneForumPostQuery; use FootyRoom\Queries\Post\ForumPostQueryHandler; use Illuminate\Support\Str; class FlagsReportQuery { /** * @var \FootyRoom\Support\MongoClient */ protected $mongo; /** * @var \FootyRoom\Queries\Comment\CommentQueryHandler */ protected $commentQh; /** * @var \FootyRoom\Queries\Post\ForumPostQueryHandler */ protected $forumPostQh; /** * Constructor. * * @param \FootyRoom\Support\MongoClient $mongo * @param \FootyRoom\Queries\Comment\CommentQueryHandler $commentQh * @param \FootyRoom\Queries\Post\ForumPostQueryHandler $forumPostQh */ public function __construct(MongoClient $mongo, CommentQueryHandler $commentQh, ForumPostQueryHandler $forumPostQh) { $this->mongo = $mongo->footyroom; $this->commentQh = $commentQh; $this->forumPostQh = $forumPostQh; } /** * Count total number of flag reports. * * @return int */ public function count() { return $this->mongo->selectCollection('comments.flags')->count(); } /** * Find flag reports. * * @param int $userRole * @param int $limit * @param int $offset * * @return \FootyRoom\Queries\Comment\FlagsReport[] */ public function find($userRole, $limit = 10, $offset = 0): array { $cursor = $this->mongo->selectCollection('comments.flags')->find( [], [ 'limit' => $limit, 'skip' => $offset, 'sort' => ['total' => -1], ] ); $flagReports = []; foreach ($cursor as $report) { $flagReports[] = AutoMapper::map($report, FlagsReport::class); } if (!$flagReports) { return []; } $commentIds = array_pluck($flagReports, 'commentId'); $commentQuery = (new CommentQuery()) ->ids($commentIds) ->threaded(false) ->withUserInfo(true) ->viewedBy($userRole); $comments = $this->commentQh->find($commentQuery); $flagReports = Arr::joinObjects($flagReports, $comments, 'commentId', 'comment', 'id'); foreach ($flagReports as $report) { // If comment is actually a forum post then we must provide that // post to front-end so that it can handle post deletion as well. if (Str::startsWith($report->comment->discussionId, 'forumPost')) { $mainCommentQuery = (new CommentQuery($report->comment->discussionId)) ->order('asc') ->limit(1) ->viewedBy($userRole); $mainComment = $this->commentQh->find($mainCommentQuery); if (count($mainComment) > 0 && $mainComment[0]->id == $report->comment->id) { $postId = (int) filter_var($report->comment->discussionId, FILTER_SANITIZE_NUMBER_INT); $postQuery = new OneForumPostQuery($postId); $report->post = $this->forumPostQh->findOne($postQuery); } } } return $flagReports; } /** * Find comment flags report by commentId. * * @param int $commentId * * @return \FootyRoom\Queries\Comment\FlagReport|null */ public function findByCommentId($commentId) { $report = $this->mongo->selectCollection('comments.flags')->findOne([ 'commentId' => $commentId, ]); if (!$report) { return null; } return AutoMapper::map($report, FlagsReport::class); } /** * Record flag to read model. * * @param int $commentId * @param string $flagName * @param string $username * @param $url string */ public function recordFlag($commentId, $flagName, $username, $url) { $upsert = [ '$inc' => [ 'total' => 1, ], '$addToSet' => [ $flagName => $username, ], ]; if ($url) { $upsert['$set'] = [ 'url' => $url, ]; } $this->mongo->selectCollection('comments.flags')->updateOne(['commentId' => $commentId], $upsert, ['upsert' => true]); } /** * Deletes flagged comment specified by commentId. * * @param int $commentId */ public function deleteByCommentId($commentId) { $this->mongo->selectCollection('comments.flags')->deleteOne([ 'commentId' => $commentId, ]); } /** * Removes a flag. * * @param int $commentId * @param string $username * @param string $flagName */ public function removeFlag($commentId, $username, $flagName) { $this->mongo->selectCollection('comments.flags')->updateOne([ 'commentId' => $commentId, ], [ '$inc' => [ 'total' => -1, ], '$pull' => [ $flagName => $username, ], ]); } } |