mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-07-10 20:28:50 +00:00
Refactor DisporaContact Repository, fix PHPStan error
This commit is contained in:
parent
16cbaf890a
commit
459ad05137
1 changed files with 40 additions and 47 deletions
|
@ -7,6 +7,9 @@
|
|||
|
||||
namespace Friendica\Protocol\Diaspora\Repository;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use Friendica\BaseRepository;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\Definition\DbaDefinition;
|
||||
|
@ -14,11 +17,12 @@ use Friendica\Model\APContact;
|
|||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Item;
|
||||
use Friendica\Model\ItemURI;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Protocol\Diaspora\Entity;
|
||||
use Friendica\Protocol\Diaspora\Factory;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\Protocol\Diaspora\Entity\DiasporaContact as DiasporaContactEntity;
|
||||
use Friendica\Protocol\Diaspora\Factory\DiasporaContact as DiasporaContactFactory;
|
||||
use Friendica\Protocol\WebFingerUri;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use InvalidArgumentException;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
@ -30,12 +34,12 @@ class DiasporaContact extends BaseRepository
|
|||
|
||||
protected static $table_name = 'diaspora-contact-view';
|
||||
|
||||
/** @var Factory\DiasporaContact */
|
||||
/** @var DiasporaContactFactory */
|
||||
protected $factory;
|
||||
/** @var DbaDefinition */
|
||||
private $definition;
|
||||
|
||||
public function __construct(DbaDefinition $definition, Database $database, LoggerInterface $logger, Factory\DiasporaContact $factory)
|
||||
public function __construct(DbaDefinition $definition, Database $database, LoggerInterface $logger, DiasporaContactFactory $factory)
|
||||
{
|
||||
parent::__construct($database, $logger, $factory);
|
||||
|
||||
|
@ -43,67 +47,58 @@ class DiasporaContact extends BaseRepository
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function selectOne(array $condition, array $params = []): Entity\DiasporaContact
|
||||
public function selectOne(array $condition, array $params = []): DiasporaContactEntity
|
||||
{
|
||||
return parent::_selectOne($condition, $params);
|
||||
$fields = $this->_selectFirstRowAsArray( $condition, $params);
|
||||
|
||||
return $this->factory->createFromTableRow($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $uriId
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function selectOneByUriId(int $uriId): Entity\DiasporaContact
|
||||
public function selectOneByUriId(int $uriId): DiasporaContactEntity
|
||||
{
|
||||
return $this->selectOne(['uri-id' => $uriId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UriInterface $uri
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function selectOneByUri(UriInterface $uri): Entity\DiasporaContact
|
||||
public function selectOneByUri(UriInterface $uri): DiasporaContactEntity
|
||||
{
|
||||
try {
|
||||
return $this->selectOne(['url' => (string) $uri]);
|
||||
} catch (HTTPException\NotFoundException $e) {
|
||||
} catch (NotFoundException $e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->selectOne(['addr' => (string) $uri]);
|
||||
} catch (HTTPException\NotFoundException $e) {
|
||||
} catch (NotFoundException $e) {
|
||||
}
|
||||
|
||||
return $this->selectOne(['alias' => (string) $uri]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WebFingerUri $uri
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function selectOneByAddr(WebFingerUri $uri): Entity\DiasporaContact
|
||||
public function selectOneByAddr(WebFingerUri $uri): DiasporaContactEntity
|
||||
{
|
||||
return $this->selectOne(['addr' => $uri->getAddr()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $uriId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function existsByUriId(int $uriId): bool
|
||||
{
|
||||
return $this->db->exists(self::$table_name, ['uri-id' => $uriId]);
|
||||
}
|
||||
|
||||
public function save(Entity\DiasporaContact $DiasporaContact): Entity\DiasporaContact
|
||||
public function save(DiasporaContactEntity $DiasporaContact): DiasporaContactEntity
|
||||
{
|
||||
$uriId = $DiasporaContact->uriId ?? ItemURI::insert(['uri' => $DiasporaContact->url, 'guid' => $DiasporaContact->guid]);
|
||||
|
||||
|
@ -145,10 +140,9 @@ class DiasporaContact extends BaseRepository
|
|||
*
|
||||
* @param WebFingerUri $uri Profile address
|
||||
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function getByAddr(WebFingerUri $uri, ?bool $update = self::UPDATE_IF_MISSING_OR_OUTDATED): Entity\DiasporaContact
|
||||
public function getByAddr(WebFingerUri $uri, ?bool $update = self::UPDATE_IF_MISSING_OR_OUTDATED): DiasporaContactEntity
|
||||
{
|
||||
if ($update !== self::ALWAYS_UPDATE) {
|
||||
try {
|
||||
|
@ -156,7 +150,7 @@ class DiasporaContact extends BaseRepository
|
|||
if ($update === self::NEVER_UPDATE) {
|
||||
return $dcontact;
|
||||
}
|
||||
} catch (HTTPException\NotFoundException $e) {
|
||||
} catch (NotFoundException $e) {
|
||||
if ($update === self::NEVER_UPDATE) {
|
||||
throw $e;
|
||||
}
|
||||
|
@ -169,7 +163,7 @@ class DiasporaContact extends BaseRepository
|
|||
|
||||
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
||||
if (empty($contact['uri-id'])) {
|
||||
throw new HTTPException\NotFoundException('Diaspora profile with URI ' . $uri . ' not found');
|
||||
throw new NotFoundException('Diaspora profile with URI ' . $uri . ' not found');
|
||||
}
|
||||
|
||||
return self::selectOneByUriId($contact['uri-id']);
|
||||
|
@ -180,10 +174,9 @@ class DiasporaContact extends BaseRepository
|
|||
*
|
||||
* @param UriInterface $uri Profile URL
|
||||
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws HTTPException\NotFoundException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function getByUrl(UriInterface $uri, ?bool $update = self::UPDATE_IF_MISSING_OR_OUTDATED): Entity\DiasporaContact
|
||||
public function getByUrl(UriInterface $uri, ?bool $update = self::UPDATE_IF_MISSING_OR_OUTDATED): DiasporaContactEntity
|
||||
{
|
||||
if ($update !== self::ALWAYS_UPDATE) {
|
||||
try {
|
||||
|
@ -191,7 +184,7 @@ class DiasporaContact extends BaseRepository
|
|||
if ($update === self::NEVER_UPDATE) {
|
||||
return $dcontact;
|
||||
}
|
||||
} catch (HTTPException\NotFoundException $e) {
|
||||
} catch (NotFoundException $e) {
|
||||
if ($update === self::NEVER_UPDATE) {
|
||||
throw $e;
|
||||
}
|
||||
|
@ -204,7 +197,7 @@ class DiasporaContact extends BaseRepository
|
|||
|
||||
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
||||
if (empty($contact['uri-id'])) {
|
||||
throw new HTTPException\NotFoundException('Diaspora profile with URI ' . $uri . ' not found');
|
||||
throw new NotFoundException('Diaspora profile with URI ' . $uri . ' not found');
|
||||
}
|
||||
|
||||
return self::selectOneByUriId($contact['uri-id']);
|
||||
|
@ -214,27 +207,27 @@ class DiasporaContact extends BaseRepository
|
|||
* Update or create a diaspora-contact entry via a probe array
|
||||
*
|
||||
* @param array $data Probe array
|
||||
* @return Entity\DiasporaContact
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateFromProbeArray(array $data): Entity\DiasporaContact
|
||||
public function updateFromProbeArray(array $data): DiasporaContactEntity
|
||||
{
|
||||
if (empty($data['url'])) {
|
||||
throw new \InvalidArgumentException('Missing url key in Diaspora probe data array');
|
||||
throw new InvalidArgumentException('Missing url key in Diaspora probe data array');
|
||||
}
|
||||
|
||||
if (empty($data['guid'])) {
|
||||
throw new \InvalidArgumentException('Missing guid key in Diaspora probe data array');
|
||||
throw new InvalidArgumentException('Missing guid key in Diaspora probe data array');
|
||||
}
|
||||
|
||||
if (empty($data['pubkey'])) {
|
||||
throw new \InvalidArgumentException('Missing pubkey key in Diaspora probe data array');
|
||||
throw new InvalidArgumentException('Missing pubkey key in Diaspora probe data array');
|
||||
}
|
||||
|
||||
$uriId = ItemURI::insert(['uri' => $data['url'], 'guid' => $data['guid']]);
|
||||
|
||||
$contact = Contact::getByUriId($uriId, ['id', 'created']);
|
||||
$apcontact = APContact::getByURL($data['url'], false);
|
||||
|
||||
if (!empty($apcontact)) {
|
||||
$interacting_count = $apcontact['followers_count'];
|
||||
$interacted_count = $apcontact['following_count'];
|
||||
|
@ -250,7 +243,7 @@ class DiasporaContact extends BaseRepository
|
|||
$DiasporaContact = $this->factory->createfromProbeData(
|
||||
$data,
|
||||
$uriId,
|
||||
new \DateTime($contact['created'] ?? 'now', new \DateTimeZone('UTC')),
|
||||
new DateTime($contact['created'] ?? 'now', new DateTimeZone('UTC')),
|
||||
$interacting_count ?? 0,
|
||||
$interacted_count ?? 0,
|
||||
$post_count ?? 0
|
||||
|
@ -269,7 +262,7 @@ class DiasporaContact extends BaseRepository
|
|||
* @param string $guid Hexadecimal string guid
|
||||
*
|
||||
* @return string the contact url or null
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getUrlByGuid(string $guid): ?string
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue