namespace Friendica\Protocol\Diaspora\Repository;
+use DateTime;
+use DateTimeZone;
+use Exception;
use Friendica\BaseRepository;
use Friendica\Database\Database;
use Friendica\Database\Definition\DbaDefinition;
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;
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);
}
/**
- * @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]);
*
* @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 {
if ($update === self::NEVER_UPDATE) {
return $dcontact;
}
- } catch (HTTPException\NotFoundException $e) {
+ } catch (NotFoundException $e) {
if ($update === self::NEVER_UPDATE) {
throw $e;
}
$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']);
*
* @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 {
if ($update === self::NEVER_UPDATE) {
return $dcontact;
}
- } catch (HTTPException\NotFoundException $e) {
+ } catch (NotFoundException $e) {
if ($update === self::NEVER_UPDATE) {
throw $e;
}
$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']);
* 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'];
$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
* @param string $guid Hexadecimal string guid
*
* @return string the contact url or null
- * @throws \Exception
+ * @throws Exception
*/
public function getUrlByGuid(string $guid): ?string
{