improve relationships

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/1723/head
Maxence Lange 2023-04-08 11:31:19 -01:00
rodzic 961f8c021e
commit eef10d9a83
2 zmienionych plików z 23 dodań i 22 usunięć

Wyświetl plik

@ -136,8 +136,8 @@ class FollowsRequest extends FollowsRequestBuilder {
*/ */
public function getByPersons(string $actorId, string $remoteActorId): Follow { public function getByPersons(string $actorId, string $remoteActorId): Follow {
$qb = $this->getFollowsSelectSql(); $qb = $this->getFollowsSelectSql();
$this->limitToActorId($qb, $actorId); $qb->limitToActorIdPrim($qb->prim($actorId));
$this->limitToObjectId($qb, $remoteActorId); $qb->limitToObjectIdPrim($qb->prim($remoteActorId));
return $this->getFollowFromRequest($qb); return $this->getFollowFromRequest($qb);
} }

Wyświetl plik

@ -304,42 +304,43 @@ class FollowService {
$actorNids[$actor->getNid()] = $actor->getId(); $actorNids[$actor->getNid()] = $actor->getId();
} }
$follows = $this->followsRequest->getFollows(array_values($actorNids));
foreach ($actorNids as $actorNid => $actorId) { foreach ($actorNids as $actorNid => $actorId) {
if ($actorNid === $this->viewer->getNid()) { if ($actorNid === $this->viewer->getNid()) {
continue; // ignore current session continue; // ignore current session
} }
// might be resource heavy, need to be checked/optimized ? $relationships[] = $this->generateRelationship($actorNid, $this->viewer->getId(), $actorId);
$relationships[] = $this->generateRelationship($actorNid, $actorId, $follows);
} }
return $relationships; return $relationships;
} }
/** /**
* @param int $objectNid * @param int $nid
* @param string $objectId * @param string $viewerId
* @param Follow[] $follows * @param string $actorId
* *
* @return Relationship * @return Relationship
*/ */
private function generateRelationship(int $objectNid, string $objectId, array $follows): Relationship { private function generateRelationship(int $nid, string $viewerId, string $actorId): Relationship {
$relationship = new Relationship($objectNid); $relationship = new Relationship($nid);
foreach ($follows as $follow) { try {
if ($follow->getType() === Follow::TYPE) { $follow = $this->followsRequest->getByPersons($viewerId, $actorId);
if ($follow->getObjectId() === $objectId) { if ($follow->isAccepted()) {
if ($follow->isAccepted()) { $relationship->setFollowing(true);
$relationship->setFollowing(true); } else {
} else { $relationship->setRequested(true);
$relationship->setRequested(true);
}
}
if ($follow->getActorId() === $objectId && $follow->isAccepted()) {
$relationship->setFollowedBy(true);
}
} }
} catch (FollowNotFoundException $e) {
}
try {
$follow = $this->followsRequest->getByPersons($actorId, $viewerId);
if ($follow->isAccepted()) {
$relationship->setFollowedBy(true);
}
} catch (FollowNotFoundException $e) {
} }
return $relationship; return $relationship;