From eef10d9a83c80b6f2467229b7ec52ae6b56d8234 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 8 Apr 2023 11:31:19 -0100 Subject: [PATCH] improve relationships Signed-off-by: Maxence Lange --- lib/Db/FollowsRequest.php | 4 ++-- lib/Service/FollowService.php | 41 ++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php index 920fe44b..1b4ac09d 100644 --- a/lib/Db/FollowsRequest.php +++ b/lib/Db/FollowsRequest.php @@ -136,8 +136,8 @@ class FollowsRequest extends FollowsRequestBuilder { */ public function getByPersons(string $actorId, string $remoteActorId): Follow { $qb = $this->getFollowsSelectSql(); - $this->limitToActorId($qb, $actorId); - $this->limitToObjectId($qb, $remoteActorId); + $qb->limitToActorIdPrim($qb->prim($actorId)); + $qb->limitToObjectIdPrim($qb->prim($remoteActorId)); return $this->getFollowFromRequest($qb); } diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index cab69ab0..27aba151 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -304,42 +304,43 @@ class FollowService { $actorNids[$actor->getNid()] = $actor->getId(); } - $follows = $this->followsRequest->getFollows(array_values($actorNids)); foreach ($actorNids as $actorNid => $actorId) { if ($actorNid === $this->viewer->getNid()) { continue; // ignore current session } - // might be resource heavy, need to be checked/optimized ? - $relationships[] = $this->generateRelationship($actorNid, $actorId, $follows); + $relationships[] = $this->generateRelationship($actorNid, $this->viewer->getId(), $actorId); } return $relationships; } /** - * @param int $objectNid - * @param string $objectId - * @param Follow[] $follows + * @param int $nid + * @param string $viewerId + * @param string $actorId * * @return Relationship */ - private function generateRelationship(int $objectNid, string $objectId, array $follows): Relationship { - $relationship = new Relationship($objectNid); + private function generateRelationship(int $nid, string $viewerId, string $actorId): Relationship { + $relationship = new Relationship($nid); - foreach ($follows as $follow) { - if ($follow->getType() === Follow::TYPE) { - if ($follow->getObjectId() === $objectId) { - if ($follow->isAccepted()) { - $relationship->setFollowing(true); - } else { - $relationship->setRequested(true); - } - } - if ($follow->getActorId() === $objectId && $follow->isAccepted()) { - $relationship->setFollowedBy(true); - } + try { + $follow = $this->followsRequest->getByPersons($viewerId, $actorId); + if ($follow->isAccepted()) { + $relationship->setFollowing(true); + } else { + $relationship->setRequested(true); } + } catch (FollowNotFoundException $e) { + } + + try { + $follow = $this->followsRequest->getByPersons($actorId, $viewerId); + if ($follow->isAccepted()) { + $relationship->setFollowedBy(true); + } + } catch (FollowNotFoundException $e) { } return $relationship;