2020-01-25 01:01:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Repository;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Friendica\BaseRepository;
|
|
|
|
use Friendica\Core\Hook;
|
|
|
|
use Friendica\Model;
|
|
|
|
use Friendica\Collection;
|
2020-01-31 20:34:12 +00:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2020-01-25 01:01:49 +00:00
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
2020-01-26 19:30:24 +00:00
|
|
|
class Notify extends BaseRepository
|
2020-01-25 01:01:49 +00:00
|
|
|
{
|
|
|
|
protected static $table_name = 'notify';
|
|
|
|
|
2020-01-26 19:30:24 +00:00
|
|
|
protected static $model_class = Model\Notify::class;
|
2020-01-25 01:01:49 +00:00
|
|
|
|
2020-01-26 19:30:24 +00:00
|
|
|
protected static $collection_class = Collection\Notifies::class;
|
2020-01-25 01:01:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2020-01-26 19:30:24 +00:00
|
|
|
* @return Model\Notify
|
2020-01-25 01:01:49 +00:00
|
|
|
*/
|
|
|
|
protected function create(array $data)
|
|
|
|
{
|
2020-01-26 19:30:24 +00:00
|
|
|
return new Model\Notify($this->dba, $this->logger, $this, $data);
|
2020-01-25 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2020-01-26 19:30:24 +00:00
|
|
|
* @return Collection\Notifies
|
2020-01-25 01:01:49 +00:00
|
|
|
*/
|
|
|
|
public function select(array $condition = [], array $params = [])
|
|
|
|
{
|
|
|
|
$params['order'] = $params['order'] ?? ['date' => 'DESC'];
|
|
|
|
|
|
|
|
return parent::select($condition, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2020-01-26 19:30:24 +00:00
|
|
|
* @return Model\Notify
|
2020-01-25 01:01:49 +00:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function getByID(int $id)
|
|
|
|
{
|
|
|
|
return $this->selectFirst(['id' => $id, 'uid' => local_user()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-31 20:34:12 +00:00
|
|
|
* Set seen state of notifications of the local_user()
|
|
|
|
*
|
|
|
|
* @param bool $seen optional true or false. default true
|
|
|
|
* @param Model\Notify $notify optional a notify, which should be set seen (including his parents)
|
2020-01-25 01:01:49 +00:00
|
|
|
*
|
|
|
|
* @return bool true on success, false on error
|
2020-01-31 20:34:12 +00:00
|
|
|
*
|
2020-01-25 01:01:49 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-01-31 20:34:12 +00:00
|
|
|
public function setSeen(bool $seen = true, Model\Notify $notify = null)
|
2020-01-25 01:01:49 +00:00
|
|
|
{
|
2020-01-31 20:34:12 +00:00
|
|
|
if (empty($notify)) {
|
|
|
|
$conditions = ['uid' => local_user()];
|
|
|
|
} else {
|
|
|
|
$conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
|
|
|
|
$notify->link,
|
|
|
|
$notify->parent,
|
|
|
|
$notify->otype,
|
|
|
|
local_user()];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->dba->update('notify', ['seen' => $seen], $conditions);
|
2020-01-25 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $fields
|
|
|
|
*
|
2020-01-28 00:12:41 +00:00
|
|
|
* @return Model\Notify|false
|
2020-01-25 01:01:49 +00:00
|
|
|
*
|
2020-01-31 20:34:12 +00:00
|
|
|
* @throws InternalServerErrorException
|
2020-01-25 01:01:49 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function insert(array $fields)
|
|
|
|
{
|
2020-01-31 20:34:12 +00:00
|
|
|
$fields['date'] = DateTimeFormat::utcNow();
|
2020-01-25 01:01:49 +00:00
|
|
|
|
|
|
|
Hook::callAll('enotify_store', $fields);
|
|
|
|
|
2020-01-28 00:12:41 +00:00
|
|
|
if (empty($fields)) {
|
2020-01-28 00:15:21 +00:00
|
|
|
$this->logger->debug('Abort adding notification entry');
|
2020-01-28 00:12:41 +00:00
|
|
|
return false;
|
2020-01-25 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->debug('adding notification entry', ['fields' => $fields]);
|
|
|
|
|
|
|
|
return parent::insert($fields);
|
|
|
|
}
|
|
|
|
}
|