1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Transformers;
7
8use App\Models\Comment;
9
10class CommentTransformer extends TransformerAbstract
11{
12 protected array $availableIncludes = [
13 'user',
14 ];
15
16 protected array $defaultIncludes = [
17 'deleted_by_id',
18 'message',
19 'message_html',
20 ];
21
22 protected $permissions = [
23 'deleted_by_id' => 'CommentModerate',
24 'message' => 'CommentShow',
25 'message_html' => 'CommentShow',
26 ];
27
28 public function transform(Comment $comment)
29 {
30 return [
31 'id' => $comment->id,
32 'parent_id' => $comment->parent_id,
33 'user_id' => $comment->user_id,
34 'pinned' => $comment->pinned ?? false,
35 'replies_count' => $comment->replies_count_cache ?? 0,
36 'votes_count' => $comment->votes_count_cache ?? 0,
37
38 'commentable_type' => $comment->commentable_type,
39 'commentable_id' => $comment->commentable_id,
40
41 'legacy_name' => $comment->legacyName(),
42
43 'created_at' => $comment->created_at_json,
44 'updated_at' => $comment->updated_at_json,
45
46 'deleted_at' => $comment->deleted_at_json,
47
48 'edited_at' => $comment->edited_at_json,
49 'edited_by_id' => $comment->edited_by_id,
50 ];
51 }
52
53 public function includeDeletedById(Comment $comment)
54 {
55 return $this->primitive($comment->deleted_by_id);
56 }
57
58 public function includeMessage(Comment $comment)
59 {
60 return $this->primitive($comment->message);
61 }
62
63 public function includeMessageHtml(Comment $comment)
64 {
65 return $this->primitive($comment->message_html);
66 }
67
68 public function includeUser(Comment $comment)
69 {
70 return $this->item($comment->user, new UserCompactTransformer());
71 }
72}