2021-08-29 01:55:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Navigation\Notifications\Depository;
|
|
|
|
|
|
|
|
use Friendica\BaseDepository;
|
|
|
|
use Friendica\Core\Hook;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\Navigation\Notifications\Collection;
|
|
|
|
use Friendica\Navigation\Notifications\Entity;
|
|
|
|
use Friendica\Navigation\Notifications\Exception;
|
|
|
|
use Friendica\Navigation\Notifications\Factory;
|
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class Notify extends BaseDepository
|
|
|
|
{
|
|
|
|
/** @var Factory\Notify */
|
|
|
|
protected $factory;
|
|
|
|
|
|
|
|
protected static $table_name = 'notify';
|
|
|
|
|
|
|
|
public function __construct(Database $database, LoggerInterface $logger, Factory\Notify $factory = null)
|
|
|
|
{
|
|
|
|
parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $condition
|
|
|
|
* @param array $params
|
|
|
|
* @return Entity\Notify
|
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
*/
|
|
|
|
private function selectOne(array $condition, array $params = []): Entity\Notify
|
|
|
|
{
|
|
|
|
return parent::_selectOne($condition, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function select(array $condition, array $params = []): Collection\Notifies
|
|
|
|
{
|
|
|
|
return new Collection\Notifies(parent::_select($condition, $params)->getArrayCopy());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function countForUser($uid, array $condition, array $params = []): int
|
|
|
|
{
|
|
|
|
$condition = DBA::mergeConditions($condition, ['uid' => $uid]);
|
|
|
|
|
|
|
|
return $this->count($condition, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function existsForUser($uid, array $condition): bool
|
|
|
|
{
|
|
|
|
$condition = DBA::mergeConditions($condition, ['uid' => $uid]);
|
|
|
|
|
|
|
|
return $this->exists($condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return Entity\Notify
|
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
*/
|
|
|
|
public function selectOneById(int $id): Entity\Notify
|
|
|
|
{
|
|
|
|
return $this->selectOne(['id' => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function selectForUser(int $uid, array $condition, array $params): Collection\Notifies
|
|
|
|
{
|
|
|
|
$condition = DBA::mergeConditions($condition, ['uid' => $uid]);
|
|
|
|
|
|
|
|
return $this->select($condition, $params);
|
|
|
|
}
|
|
|
|
|
2021-09-19 16:57:16 +00:00
|
|
|
/**
|
|
|
|
* Returns notifications for the user, unread first, ordered in descending chronological order.
|
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
* @param int $limit
|
|
|
|
* @return Collection\Notifies
|
|
|
|
*/
|
|
|
|
public function selectAllForUser(int $uid, int $limit): Collection\Notifies
|
2021-08-29 01:55:04 +00:00
|
|
|
{
|
2021-09-19 16:57:16 +00:00
|
|
|
return $this->selectForUser($uid, [], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => $limit]);
|
2021-08-29 01:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setAllSeenForUser(int $uid, array $condition = []): bool
|
|
|
|
{
|
|
|
|
$condition = DBA::mergeConditions($condition, ['uid' => $uid]);
|
|
|
|
|
|
|
|
return $this->db->update(self::$table_name, ['seen' => true], $condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Entity\Notify $Notify
|
|
|
|
* @return Entity\Notify
|
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws Exception\NotificationCreationInterceptedException
|
|
|
|
*/
|
|
|
|
public function save(Entity\Notify $Notify): Entity\Notify
|
|
|
|
{
|
|
|
|
$fields = [
|
|
|
|
'type' => $Notify->type,
|
|
|
|
'name' => $Notify->name,
|
2021-10-03 18:53:06 +00:00
|
|
|
'url' => (string)$Notify->url,
|
|
|
|
'photo' => (string)$Notify->photo,
|
2021-08-29 01:55:04 +00:00
|
|
|
'msg' => $Notify->msg,
|
|
|
|
'uid' => $Notify->uid,
|
2021-10-03 18:53:06 +00:00
|
|
|
'link' => (string)$Notify->link,
|
2021-08-29 01:55:04 +00:00
|
|
|
'iid' => $Notify->itemId,
|
|
|
|
'parent' => $Notify->parent,
|
|
|
|
'seen' => $Notify->seen,
|
|
|
|
'verb' => $Notify->verb,
|
|
|
|
'otype' => $Notify->otype,
|
|
|
|
'name_cache' => $Notify->name_cache,
|
|
|
|
'msg_cache' => $Notify->msg_cache,
|
|
|
|
'uri-id' => $Notify->uriId,
|
|
|
|
'parent-uri-id' => $Notify->parentUriId,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($Notify->id) {
|
|
|
|
$this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
|
|
|
|
} else {
|
|
|
|
$fields['date'] = DateTimeFormat::utcNow();
|
|
|
|
Hook::callAll('enotify_store', $fields);
|
|
|
|
|
|
|
|
$this->db->insert(self::$table_name, $fields);
|
|
|
|
|
|
|
|
$Notify = $this->selectOneById($this->db->lastInsertId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $Notify;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
|
|
|
|
{
|
|
|
|
$condition = [
|
|
|
|
'(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
|
|
|
|
$Notify->link,
|
|
|
|
$Notify->parent,
|
|
|
|
$Notify->otype,
|
|
|
|
$Notify->uid
|
|
|
|
];
|
|
|
|
return $this->db->update(self::$table_name, ['seen' => true], $condition);
|
|
|
|
}
|
|
|
|
}
|