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;
|
namespace Friendica\Protocol\Diaspora\Repository;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use DateTimeZone;
|
||||||
|
use Exception;
|
||||||
use Friendica\BaseRepository;
|
use Friendica\BaseRepository;
|
||||||
use Friendica\Database\Database;
|
use Friendica\Database\Database;
|
||||||
use Friendica\Database\Definition\DbaDefinition;
|
use Friendica\Database\Definition\DbaDefinition;
|
||||||
|
@ -14,11 +17,12 @@ use Friendica\Model\APContact;
|
||||||
use Friendica\Model\Contact;
|
use Friendica\Model\Contact;
|
||||||
use Friendica\Model\Item;
|
use Friendica\Model\Item;
|
||||||
use Friendica\Model\ItemURI;
|
use Friendica\Model\ItemURI;
|
||||||
use Friendica\Network\HTTPException;
|
use Friendica\Network\HTTPException\NotFoundException;
|
||||||
use Friendica\Protocol\Diaspora\Entity;
|
use Friendica\Protocol\Diaspora\Entity\DiasporaContact as DiasporaContactEntity;
|
||||||
use Friendica\Protocol\Diaspora\Factory;
|
use Friendica\Protocol\Diaspora\Factory\DiasporaContact as DiasporaContactFactory;
|
||||||
use Friendica\Protocol\WebFingerUri;
|
use Friendica\Protocol\WebFingerUri;
|
||||||
use Friendica\Util\DateTimeFormat;
|
use Friendica\Util\DateTimeFormat;
|
||||||
|
use InvalidArgumentException;
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
@ -30,12 +34,12 @@ class DiasporaContact extends BaseRepository
|
||||||
|
|
||||||
protected static $table_name = 'diaspora-contact-view';
|
protected static $table_name = 'diaspora-contact-view';
|
||||||
|
|
||||||
/** @var Factory\DiasporaContact */
|
/** @var DiasporaContactFactory */
|
||||||
protected $factory;
|
protected $factory;
|
||||||
/** @var DbaDefinition */
|
/** @var DbaDefinition */
|
||||||
private $definition;
|
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);
|
parent::__construct($database, $logger, $factory);
|
||||||
|
|
||||||
|
@ -43,67 +47,58 @@ class DiasporaContact extends BaseRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $condition
|
* @throws NotFoundException
|
||||||
* @param array $params
|
|
||||||
* @return Entity\DiasporaContact
|
|
||||||
* @throws HTTPException\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
|
* @throws NotFoundException
|
||||||
* @return Entity\DiasporaContact
|
|
||||||
* @throws HTTPException\NotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function selectOneByUriId(int $uriId): Entity\DiasporaContact
|
public function selectOneByUriId(int $uriId): DiasporaContactEntity
|
||||||
{
|
{
|
||||||
return $this->selectOne(['uri-id' => $uriId]);
|
return $this->selectOne(['uri-id' => $uriId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param UriInterface $uri
|
* @throws NotFoundException
|
||||||
* @return Entity\DiasporaContact
|
|
||||||
* @throws HTTPException\NotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function selectOneByUri(UriInterface $uri): Entity\DiasporaContact
|
public function selectOneByUri(UriInterface $uri): DiasporaContactEntity
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->selectOne(['url' => (string) $uri]);
|
return $this->selectOne(['url' => (string) $uri]);
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $this->selectOne(['addr' => (string) $uri]);
|
return $this->selectOne(['addr' => (string) $uri]);
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->selectOne(['alias' => (string) $uri]);
|
return $this->selectOne(['alias' => (string) $uri]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WebFingerUri $uri
|
* @throws NotFoundException
|
||||||
* @return Entity\DiasporaContact
|
|
||||||
* @throws HTTPException\NotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function selectOneByAddr(WebFingerUri $uri): Entity\DiasporaContact
|
public function selectOneByAddr(WebFingerUri $uri): DiasporaContactEntity
|
||||||
{
|
{
|
||||||
return $this->selectOne(['addr' => $uri->getAddr()]);
|
return $this->selectOne(['addr' => $uri->getAddr()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $uriId
|
* @throws Exception
|
||||||
* @return bool
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function existsByUriId(int $uriId): bool
|
public function existsByUriId(int $uriId): bool
|
||||||
{
|
{
|
||||||
return $this->db->exists(self::$table_name, ['uri-id' => $uriId]);
|
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]);
|
$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 WebFingerUri $uri Profile address
|
||||||
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
||||||
* @return Entity\DiasporaContact
|
* @throws NotFoundException
|
||||||
* @throws HTTPException\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) {
|
if ($update !== self::ALWAYS_UPDATE) {
|
||||||
try {
|
try {
|
||||||
|
@ -156,7 +150,7 @@ class DiasporaContact extends BaseRepository
|
||||||
if ($update === self::NEVER_UPDATE) {
|
if ($update === self::NEVER_UPDATE) {
|
||||||
return $dcontact;
|
return $dcontact;
|
||||||
}
|
}
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
if ($update === self::NEVER_UPDATE) {
|
if ($update === self::NEVER_UPDATE) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +163,7 @@ class DiasporaContact extends BaseRepository
|
||||||
|
|
||||||
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
||||||
if (empty($contact['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']);
|
return self::selectOneByUriId($contact['uri-id']);
|
||||||
|
@ -180,10 +174,9 @@ class DiasporaContact extends BaseRepository
|
||||||
*
|
*
|
||||||
* @param UriInterface $uri Profile URL
|
* @param UriInterface $uri Profile URL
|
||||||
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
* @param boolean $update true = always update, false = never update, null = update when not found or outdated
|
||||||
* @return Entity\DiasporaContact
|
* @throws NotFoundException
|
||||||
* @throws HTTPException\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) {
|
if ($update !== self::ALWAYS_UPDATE) {
|
||||||
try {
|
try {
|
||||||
|
@ -191,7 +184,7 @@ class DiasporaContact extends BaseRepository
|
||||||
if ($update === self::NEVER_UPDATE) {
|
if ($update === self::NEVER_UPDATE) {
|
||||||
return $dcontact;
|
return $dcontact;
|
||||||
}
|
}
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
if ($update === self::NEVER_UPDATE) {
|
if ($update === self::NEVER_UPDATE) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
@ -204,7 +197,7 @@ class DiasporaContact extends BaseRepository
|
||||||
|
|
||||||
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
$contact = Contact::getByURL($uri, $update, ['uri-id']);
|
||||||
if (empty($contact['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']);
|
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
|
* Update or create a diaspora-contact entry via a probe array
|
||||||
*
|
*
|
||||||
* @param array $data 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'])) {
|
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'])) {
|
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'])) {
|
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']]);
|
$uriId = ItemURI::insert(['uri' => $data['url'], 'guid' => $data['guid']]);
|
||||||
|
|
||||||
$contact = Contact::getByUriId($uriId, ['id', 'created']);
|
$contact = Contact::getByUriId($uriId, ['id', 'created']);
|
||||||
$apcontact = APContact::getByURL($data['url'], false);
|
$apcontact = APContact::getByURL($data['url'], false);
|
||||||
|
|
||||||
if (!empty($apcontact)) {
|
if (!empty($apcontact)) {
|
||||||
$interacting_count = $apcontact['followers_count'];
|
$interacting_count = $apcontact['followers_count'];
|
||||||
$interacted_count = $apcontact['following_count'];
|
$interacted_count = $apcontact['following_count'];
|
||||||
|
@ -250,7 +243,7 @@ class DiasporaContact extends BaseRepository
|
||||||
$DiasporaContact = $this->factory->createfromProbeData(
|
$DiasporaContact = $this->factory->createfromProbeData(
|
||||||
$data,
|
$data,
|
||||||
$uriId,
|
$uriId,
|
||||||
new \DateTime($contact['created'] ?? 'now', new \DateTimeZone('UTC')),
|
new DateTime($contact['created'] ?? 'now', new DateTimeZone('UTC')),
|
||||||
$interacting_count ?? 0,
|
$interacting_count ?? 0,
|
||||||
$interacted_count ?? 0,
|
$interacted_count ?? 0,
|
||||||
$post_count ?? 0
|
$post_count ?? 0
|
||||||
|
@ -269,7 +262,7 @@ class DiasporaContact extends BaseRepository
|
||||||
* @param string $guid Hexadecimal string guid
|
* @param string $guid Hexadecimal string guid
|
||||||
*
|
*
|
||||||
* @return string the contact url or null
|
* @return string the contact url or null
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getUrlByGuid(string $guid): ?string
|
public function getUrlByGuid(string $guid): ?string
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue