3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Repository;
25 use Friendica\BaseRepository;
26 use Friendica\Collection;
27 use Friendica\Core\Hook;
29 use Friendica\Network\HTTPException\InternalServerErrorException;
30 use Friendica\Network\HTTPException\NotFoundException;
31 use Friendica\Util\DateTimeFormat;
33 class Notify extends BaseRepository
35 protected static $table_name = 'notify';
37 protected static $model_class = Model\Notify::class;
39 protected static $collection_class = Collection\Notifies::class;
44 * @return Model\Notify
46 protected function create(array $data)
48 return new Model\Notify($this->dba, $this->logger, $this, $data);
54 * @return Collection\Notifies
56 public function select(array $condition = [], array $params = [])
58 $params['order'] = $params['order'] ?? ['date' => 'DESC'];
60 return parent::select($condition, $params);
64 * Return one notify instance based on ID / UID
66 * @param int $id The ID of the notify instance
67 * @param int $uid The user ID, bound to this notify instance (= security check)
69 * @return Model\Notify
70 * @throws NotFoundException
72 public function getByID(int $id, int $uid)
74 return $this->selectFirst(['id' => $id, 'uid' => $uid]);
78 * Set seen state of notifications of the local_user()
80 * @param bool $seen optional true or false. default true
81 * @param Model\Notify $notify optional a notify, which should be set seen (including his parents)
83 * @return bool true on success, false on error
87 public function setSeen(bool $seen = true, Model\Notify $notify = null)
90 $conditions = ['uid' => local_user()];
92 $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
99 return $this->dba->update('notify', ['seen' => $seen], $conditions);
103 * @param array $fields
105 * @return Model\Notify|false
107 * @throws InternalServerErrorException
110 public function insert(array $fields)
112 $fields['date'] = DateTimeFormat::utcNow();
114 Hook::callAll('enotify_store', $fields);
116 if (empty($fields)) {
117 $this->logger->debug('Abort adding notification entry');
121 $this->logger->debug('adding notification entry', ['fields' => $fields]);
123 return parent::insert($fields);