Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function loadReactionData(): void
$this->reactionData = [];

$objectType = ReactionHandler::getInstance()->getObjectType($this->getReactionObjectType());
ReactionHandler::getInstance()->loadLikeObjects($objectType, $this->getObjectIDs());
ReactionHandler::getInstance()->loadLikeObjects($objectType, $this->getObjectIDs(), false);

foreach ($this->getObjectIDs() as $objectID) {
$likeObject = ReactionHandler::getInstance()->getLikeObject($objectType, $objectID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use wcf\data\like\ILikeObjectTypeProvider;
use wcf\data\like\IRestrictedLikeObjectTypeProvider;
use wcf\data\like\Like;
use wcf\data\like\object\ILikeObject;
Expand Down Expand Up @@ -49,6 +50,8 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
}

$objectTypeProvider = $objectType->getProcessor();
\assert($objectTypeProvider instanceof ILikeObjectTypeProvider);

$likeable = $objectTypeProvider->getObjectByID($parameters->objectID);
\assert($likeable instanceof ILikeObject);
$likeable->setObjectType($objectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
final class ReactionHandler extends SingletonFactory
{
/**
* @var LikeObject[][]
* @var array<int, array<int, ?LikeObject>>
*/
private array $likeObjectCache = [];

Expand All @@ -43,6 +43,7 @@ final class ReactionHandler extends SingletonFactory

/**
* @var ILikeObject[][]
* @deprecated 6.3
*/
private array $likeableObjectsCache = [];

Expand Down Expand Up @@ -90,6 +91,8 @@ public function getReactionTypeByID(int $reactionID): ?ReactionType

/**
* Builds the data attributes for the object container.
*
* @deprecated 6.3
*/
public function getDataAttributes(string $objectTypeName, int $objectID): string
{
Expand Down Expand Up @@ -122,6 +125,8 @@ public function getDataAttributes(string $objectTypeName, int $objectID): string

/**
* @param list<int> $objectIDs
*
* @deprecated 6.3
*/
public function cacheLikeableObjects(string $objectTypeName, array $objectIDs): void
{
Expand All @@ -148,6 +153,8 @@ public function cacheLikeableObjects(string $objectTypeName, array $objectIDs):

/**
* Get an likeable object from the internal cache.
*
* @deprecated 6.3
*/
public function getLikeableObject(string $objectTypeName, int $objectID): ILikeObject
{
Expand Down Expand Up @@ -181,7 +188,10 @@ public function getObjectType(string $objectName): ?ObjectType

public function getLikeObject(ObjectType $objectType, int $objectID): ?LikeObject
{
if (!isset($this->likeObjectCache[$objectType->objectTypeID][$objectID])) {
if (
!isset($this->likeObjectCache[$objectType->objectTypeID])
|| !\array_key_exists($objectID, $this->likeObjectCache[$objectType->objectTypeID])
) {
$this->loadLikeObjects($objectType, [$objectID]);
}

Expand All @@ -196,7 +206,8 @@ public function getLikeObject(ObjectType $objectType, int $objectID): ?LikeObjec
public function getLikeObjects(ObjectType $objectType): array
{
if (isset($this->likeObjectCache[$objectType->objectTypeID])) {
return $this->likeObjectCache[$objectType->objectTypeID];
// Use `array_filter` to filter `null` values.
return \array_filter($this->likeObjectCache[$objectType->objectTypeID]);
}

return [];
Expand All @@ -208,13 +219,15 @@ public function getLikeObjects(ObjectType $objectType): array
*
* @param list<int> $objectIDs
*/
public function loadLikeObjects(ObjectType $objectType, array $objectIDs): int
public function loadLikeObjects(ObjectType $objectType, array $objectIDs, bool $loadLikeableObjects = true): int
{
if (empty($objectIDs)) {
return 0;
}

$this->cacheLikeableObjects($objectType->objectType, $objectIDs);
if ($loadLikeableObjects) {
$this->cacheLikeableObjects($objectType->objectType, $objectIDs);
}

$i = 0;

Expand Down Expand Up @@ -248,6 +261,10 @@ public function loadLikeObjects(ObjectType $objectType, array $objectIDs): int
$i++;
}

foreach ($objectIDs as $objectID) {
$this->likeObjectCache[$objectType->objectTypeID][$objectID] ??= null;
}

return $i;
}

Expand Down
Loading