-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathLikeableCommentResponseProvider.class.php
More file actions
122 lines (107 loc) · 3.91 KB
/
LikeableCommentResponseProvider.class.php
File metadata and controls
122 lines (107 loc) · 3.91 KB
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
<?php
namespace wcf\data\comment\response;
use wcf\data\comment\Comment;
use wcf\data\like\ILikeObjectTypeProvider;
use wcf\data\like\object\ILikeObject;
use wcf\data\object\type\AbstractObjectTypeProvider;
use wcf\system\cache\runtime\CommentResponseRuntimeCache;
use wcf\system\comment\CommentHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\like\IViewableLikeProvider;
use wcf\system\WCF;
/**
* Object type provider for likeable comment responses.
*
* @author Matthias Schmidt
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @extends AbstractObjectTypeProvider<LikeableCommentResponse>
* @implements ILikeObjectTypeProvider<LikeableCommentResponse>
*/
class LikeableCommentResponseProvider extends AbstractObjectTypeProvider implements
ILikeObjectTypeProvider,
IViewableLikeProvider
{
/**
* @inheritDoc
*/
public $className = CommentResponse::class;
/**
* @inheritDoc
*/
public $decoratorClassName = LikeableCommentResponse::class;
/**
* @inheritDoc
*/
public $listClassName = CommentResponseList::class;
/**
* @inheritDoc
*/
public function checkPermissions(ILikeObject $object)
{
\assert($object instanceof LikeableCommentResponse);
if (!$object->responseID) {
return false;
}
$comment = new Comment($object->commentID);
if (!$comment->commentID) {
return false;
}
$objectType = CommentHandler::getInstance()->getObjectType($comment->objectTypeID);
return CommentHandler::getInstance()->getCommentManager($objectType->objectType)->isAccessible($comment->objectID);
}
/**
* @inheritDoc
*/
public function prepare(array $likes)
{
$responseIDs = [];
foreach ($likes as $like) {
$responseIDs[] = $like->objectID;
}
// get objects type ids
$conditionBuilder = new PreparedStatementConditionBuilder();
$conditionBuilder->add('comment_response.responseID IN (?)', [$responseIDs]);
$sql = "SELECT comment.objectTypeID, comment_response.responseID
FROM wcf1_comment_response comment_response
LEFT JOIN wcf1_comment comment
ON comment.commentID = comment_response.commentID
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditionBuilder->getParameters());
$responses = $statement->fetchMap('responseID', 'objectTypeID');
// group likes by object type id
$likeData = [];
foreach ($likes as $like) {
if (isset($responses[$like->objectID])) {
if (!isset($likeData[$responses[$like->objectID]])) {
$likeData[$responses[$like->objectID]] = [];
}
$likeData[$responses[$like->objectID]][] = $like;
}
}
foreach ($likeData as $objectTypeID => $likes) {
$objectType = CommentHandler::getInstance()->getObjectType($objectTypeID);
$commentManager = CommentHandler::getInstance()->getCommentManager($objectType->objectType);
if ($commentManager instanceof IViewableLikeProvider) {
$commentManager->prepare($likes);
}
}
}
#[\Override]
public function getObjectByID(int $objectID)
{
return new LikeableCommentResponse(CommentResponseRuntimeCache::getInstance()->getObject($objectID));
}
#[\Override]
public function getObjectsByIDs(array $objectIDs)
{
return \array_map(
static fn(CommentResponse $comment) => new LikeableCommentResponse($comment),
\array_filter(
CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs),
)
);
}
}