add viewerLink during import of Person

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/780/head
Maxence Lange 2019-10-03 10:14:12 -01:00
rodzic c0d71b7e09
commit 55abe1ab21
4 zmienionych plików z 97 dodań i 53 usunięć

Wyświetl plik

@ -169,18 +169,10 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
*/
public function getFromId(string $id): Person {
$qb = $this->getCacheActorsSelectSql();
$this->limitToIdString($qb, $id);
$qb->limitToIdString($id);
$this->leftJoinCacheDocuments($qb, 'icon_id');
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new CacheActorDoesNotExistException();
}
return $this->parseCacheActorsSelectSql($data);
return $this->getCacheActorFromRequest($qb);
}
@ -194,19 +186,11 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
*/
public function getFromAccount(string $account): Person {
$qb = $this->getCacheActorsSelectSql();
$this->limitToAccount($qb, $account);
$qb->limitToAccount($account);
$this->leftJoinCacheDocuments($qb, 'icon_id');
$this->leftJoinDetails($qb);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new CacheActorDoesNotExistException();
}
return $this->parseCacheActorsSelectSql($data);
return $this->getCacheActorFromRequest($qb);
}
@ -225,15 +209,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
$this->leftJoinCacheDocuments($qb, 'icon_id');
$this->leftJoinDetails($qb);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new CacheActorDoesNotExistException('Unknown account \'' . $account . "'");
}
return $this->parseCacheActorsSelectSql($data);
return $this->getCacheActorFromRequest($qb);
}
@ -249,14 +225,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
$this->leftJoinDetails($qb);
$this->limitResults($qb, 25);
$accounts = [];
$cursor = $qb->execute();
while ($data = $cursor->fetch()) {
$accounts[] = $this->parseCacheActorsSelectSql($data);
}
$cursor->closeCursor();
return $accounts;
return $this->getCacheActorsFromRequest($qb);
}
@ -269,14 +238,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
$this->limitToLocal($qb, false);
$this->limitToCreation($qb, self::CACHE_TTL);
$update = [];
$cursor = $qb->execute();
while ($data = $cursor->fetch()) {
$update[] = $this->parseCacheActorsSelectSql($data);
}
$cursor->closeCursor();
return $update;
return $this->getCacheActorsFromRequest($qb);
}

Wyświetl plik

@ -30,7 +30,9 @@ declare(strict_types=1);
namespace OCA\Social\Db;
use daita\MySmallPhpTools\Exceptions\RowNotFoundException;
use daita\MySmallPhpTools\Traits\TArrayTools;
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCP\DB\QueryBuilder\IQueryBuilder;
@ -72,8 +74,8 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
*
* @return IQueryBuilder
*/
protected function getCacheActorsSelectSql(): IQueryBuilder {
$qb = $this->dbConnection->getQueryBuilder();
protected function getCacheActorsSelectSql(): SocialQueryBuilder {
$qb = $this->getQueryBuilder();
/** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->select(
@ -103,15 +105,50 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
}
/**
* @param SocialQueryBuilder $qb
*
* @return Person
* @throws CacheActorDoesNotExistException
*/
protected function getCacheActorFromRequest(SocialQueryBuilder $qb): Person {
/** @var Person $result */
try {
$result = $qb->getRow([$this, 'parseCacheActorsSelectSql']);
} catch (RowNotFoundException $e) {
throw new CacheActorDoesNotExistException($e->getMessage());
}
return $result;
}
/**
* @param SocialQueryBuilder $qb
*
* @return Person[]
*/
public function getCacheActorsFromRequest(SocialQueryBuilder $qb): array {
/** @var Person[] $result */
$result = $qb->getRows([$this, 'parseCacheActorsSelectSql']);
return $result;
}
/**
* @param array $data
*
* @param SocialQueryBuilder $qb
*
* @return Person
*/
protected function parseCacheActorsSelectSql(array $data): Person {
public function parseCacheActorsSelectSql(array $data, SocialQueryBuilder $qb): Person {
$actor = new Person();
$actor->importFromDatabase($data);
$this->assignViewerLink($qb, $actor);
try {
$icon = $this->parseCacheDocumentsLeftJoin($data);
$actor->setIcon($icon);
@ -124,5 +161,24 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
}
/**
* @param SocialQueryBuilder $qb
* @param Person $actor
*/
private function assignViewerLink(SocialQueryBuilder $qb, Person $actor) {
if ($actor->isLocal()) {
$link = Person::LINK_LOCAL;
if ($qb->hasViewer()
&& $qb->getViewer()
->getId() === $actor->getId()) {
$link = Person::LINK_VIEWER;
}
} else {
$link = Person::LINK_REMOTE;
}
$actor->setViewerLink($link);
}
}

Wyświetl plik

@ -407,10 +407,8 @@ class StreamRequestBuilder extends CoreRequestBuilder {
* @throws ItemUnknownException
* @throws SocialAppConfigException
*/
public function parseStreamSelectSql(array $data, string $as = Stream::TYPE): Stream {
if ($as === Stream::TYPE) {
$as = $this->get('type', $data, Stream::TYPE);
}
public function parseStreamSelectSql(array $data): Stream {
$as = $this->get('type', $data, Stream::TYPE);
/** @var Stream $item */
$item = AP::$activityPub->getItemFromType($as);

Wyświetl plik

@ -31,6 +31,7 @@ declare(strict_types=1);
namespace OCA\Social\Model\ActivityPub\Actor;
use daita\MySmallPhpTools\IQueryRow;
use DateTime;
use Exception;
use JsonSerializable;
@ -49,7 +50,7 @@ use OCA\Social\Traits\TDetails;
*
* @package OCA\Social\Model\ActivityPub
*/
class Person extends ACore implements JsonSerializable {
class Person extends ACore implements IQueryRow, JsonSerializable {
use TDetails;
@ -58,6 +59,11 @@ class Person extends ACore implements JsonSerializable {
const TYPE = 'Person';
const LINK_VIEWER = 'viewer';
const LINK_REMOTE = 'remote';
const LINK_LOCAL = 'local';
/** @var string */
private $userId = '';
@ -100,6 +106,9 @@ class Person extends ACore implements JsonSerializable {
/** @var int */
private $avatarVersion = -1;
/** @var string */
private $viewerLink = '';
/**
* Person constructor.
@ -378,6 +387,24 @@ class Person extends ACore implements JsonSerializable {
}
/**
* @return string
*/
public function getViewerLink(): string {
return $this->viewerLink;
}
/**
* @param string $viewerLink
*
* @return Person
*/
public function setViewerLink(string $viewerLink): Person {
$this->viewerLink = $viewerLink;
return $this;
}
/**
* @param array $data
*
@ -467,6 +494,7 @@ class Person extends ACore implements JsonSerializable {
if ($this->isCompleteDetails()) {
$result['details'] = $this->getDetailsAll();
$result['viewerLink'] = $this->getViewerLink();
}
return $result;