3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Navigation\Notifications\Repository;
25 use Friendica\BaseCollection;
26 use Friendica\BaseRepository;
27 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
30 use Friendica\Model\Verb;
31 use Friendica\Navigation\Notifications\Collection;
32 use Friendica\Navigation\Notifications\Entity;
33 use Friendica\Navigation\Notifications\Factory;
34 use Friendica\Network\HTTPException\NotFoundException;
35 use Friendica\Util\DateTimeFormat;
36 use Psr\Log\LoggerInterface;
38 class Notification extends BaseRepository
40 /** @var Factory\Notification */
43 protected static $table_name = 'notification';
45 /** @var IManagePersonalConfigValues */
48 public function __construct(IManagePersonalConfigValues $pconfig, Database $database, LoggerInterface $logger, Factory\Notification $factory)
50 parent::__construct($database, $logger, $factory);
52 $this->pconfig = $pconfig;
56 * @param array $condition
57 * @param array $params
58 * @return Entity\Notification
59 * @throws NotFoundException
61 private function selectOne(array $condition, array $params = []): Entity\Notification
63 return parent::_selectOne($condition, $params);
66 private function select(array $condition, array $params = []): Collection\Notifications
68 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
71 public function countForUser($uid, array $condition, array $params = []): int
73 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
75 return $this->count($condition, $params);
78 public function existsForUser($uid, array $condition): bool
80 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
82 return $this->exists($condition);
87 * @return Entity\Notification
88 * @throws NotFoundException
90 public function selectOneById(int $id): Entity\Notification
92 return $this->selectOne(['id' => $id]);
95 public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
97 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
99 return $this->selectOne($condition, $params);
102 public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
104 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
106 return $this->select($condition, $params);
111 * Returns only the most recent notifications for the same conversation or contact
114 * @return Collection\Notifications
117 public function selectDetailedForUser(int $uid): Collection\Notifications
120 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
121 $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::LIKE)]);
124 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
125 $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE)]);
128 return $this->selectForUser($uid, $condition, ['limit' => 50, 'order' => ['id' => true]]);
132 * Returns only the most recent notifications for the same conversation or contact
135 * @return Collection\Notifications
138 public function selectDigestForUser(int $uid): Collection\Notifications
142 $like_condition = '';
143 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
144 $like_condition = 'AND vid != ?';
145 $values[] = Verb::getID(\Friendica\Protocol\Activity::LIKE);
148 $announce_condition = '';
149 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
150 $announce_condition = 'AND vid != ?';
151 $values[] = Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE);
154 $rows = $this->db->p("
155 SELECT notification.*
163 GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
165 ORDER BY `seen`, `id` DESC
169 $Entities = new Collection\Notifications();
170 foreach ($rows as $fields) {
171 $Entities[] = $this->factory->createFromTableRow($fields);
177 public function selectAllForUser(int $uid): Collection\Notifications
179 return $this->selectForUser($uid);
183 * @param array $condition
184 * @param array $params
185 * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
186 * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
188 * @return BaseCollection
190 * @see _selectByBoundaries
192 public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
194 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
196 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
199 public function setAllSeenForUser(int $uid, array $condition = []): bool
201 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
203 return $this->db->update(self::$table_name, ['seen' => true], $condition);
206 public function setAllDismissedForUser(int $uid, array $condition = []): bool
208 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
210 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
214 * @param Entity\Notification $Notification
215 * @return Entity\Notification
218 public function save(Entity\Notification $Notification): Entity\Notification
221 'uid' => $Notification->uid,
222 'vid' => Verb::getID($Notification->verb),
223 'type' => $Notification->type,
224 'actor-id' => $Notification->actorId,
225 'target-uri-id' => $Notification->targetUriId,
226 'parent-uri-id' => $Notification->parentUriId,
227 'seen' => $Notification->seen,
228 'dismissed' => $Notification->dismissed,
231 if ($Notification->id) {
232 $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
234 $fields['created'] = DateTimeFormat::utcNow();
235 $this->db->insert(self::$table_name, $fields);
237 $Notification = $this->selectOneById($this->db->lastInsertId());
240 return $Notification;
243 public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
245 $condition['uid'] = $uid;
246 $condition['vid'] = Verb::getID($verb);
248 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
250 return $this->db->delete(self::$table_name, $condition);