Add new paradigm classes for notify
- Create BaseDepository class - Create Entity, Collection, Factory and Depository classes - Create FormattedNotification Entity, Collection and Factory to remove business logic from Notify repository - Create new NotificationCreationIntercepted exception to allow addons to cancel notification creation - Remove unused frio notifications/notify.tpl template
This commit is contained in:
parent
810699b454
commit
1b4e3564a5
11 changed files with 983 additions and 3 deletions
141
src/Navigation/Notifications/Depository/Notify.php
Normal file
141
src/Navigation/Notifications/Depository/Notify.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?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);
|
||||
}
|
||||
|
||||
public function selectAllForUser(int $uid, array $params = []): Collection\Notifies
|
||||
{
|
||||
return $this->selectForUser($uid, [], $params);
|
||||
}
|
||||
|
||||
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,
|
||||
'url' => $Notify->url,
|
||||
'photo' => $Notify->photo,
|
||||
'msg' => $Notify->msg,
|
||||
'uid' => $Notify->uid,
|
||||
'link' => $Notify->link,
|
||||
'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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue