3 * @copyright Copyright (C) 2010-2023, 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\Post\UserNotification;
31 use Friendica\Model\Verb;
32 use Friendica\Navigation\Notifications\Collection;
33 use Friendica\Navigation\Notifications\Entity;
34 use Friendica\Navigation\Notifications\Factory;
35 use Friendica\Network\HTTPException\NotFoundException;
36 use Friendica\Protocol\Activity;
37 use Friendica\Util\DateTimeFormat;
38 use Psr\Log\LoggerInterface;
40 class Notification extends BaseRepository
42 /** @var Factory\Notification */
45 protected static $table_name = 'notification';
47 /** @var IManagePersonalConfigValues */
50 public function __construct(IManagePersonalConfigValues $pconfig, Database $database, LoggerInterface $logger, Factory\Notification $factory)
52 parent::__construct($database, $logger, $factory);
54 $this->pconfig = $pconfig;
58 * @param array $condition
59 * @param array $params
60 * @return Entity\Notification
61 * @throws NotFoundException
63 private function selectOne(array $condition, array $params = []): Entity\Notification
65 return parent::_selectOne($condition, $params);
68 private function select(array $condition, array $params = []): Collection\Notifications
70 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
73 public function countForUser($uid, array $condition, array $params = []): int
75 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
77 return $this->count($condition, $params);
80 public function existsForUser($uid, array $condition): bool
82 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
84 return $this->exists($condition);
89 * @return Entity\Notification
90 * @throws NotFoundException
92 public function selectOneById(int $id): Entity\Notification
94 return $this->selectOne(['id' => $id]);
97 public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
99 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
101 return $this->selectOne($condition, $params);
104 public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
106 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
108 return $this->select($condition, $params);
113 * Returns only the most recent notifications for the same conversation or contact
117 * @return Collection\Notifications
120 public function selectDetailedForUser(int $uid): Collection\Notifications
122 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
123 if (!is_null($notify_type)) {
124 $condition = ["`type` & ? != 0", $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW];
129 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
130 $condition = DBA::mergeConditions($condition, ['NOT `vid` IN (?, ?)', Verb::getID(\Friendica\Protocol\Activity::LIKE), Verb::getID(\Friendica\Protocol\Activity::DISLIKE)]);
133 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
134 $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE)]);
137 return $this->selectForUser($uid, $condition, ['limit' => 50, 'order' => ['id' => true]]);
141 * Returns only the most recent notifications for the same conversation or contact
145 * @return Collection\Notifications
148 public function selectDigestForUser(int $uid): Collection\Notifications
152 $type_condition = '';
153 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
154 if (!is_null($notify_type)) {
155 $type_condition = 'AND `type` & ? != 0';
156 $values[] = $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW;
159 $like_condition = '';
160 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
161 $like_condition = 'AND NOT `vid` IN (?, ?)';
162 $values[] = Verb::getID(\Friendica\Protocol\Activity::LIKE);
163 $values[] = Verb::getID(\Friendica\Protocol\Activity::DISLIKE);
166 $announce_condition = '';
167 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
168 $announce_condition = 'AND vid != ?';
169 $values[] = Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE);
172 $rows = $this->db->p("
173 SELECT notification.*
182 GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
184 ORDER BY `seen`, `id` DESC
188 $Entities = new Collection\Notifications();
189 foreach ($rows as $fields) {
190 $Entities[] = $this->factory->createFromTableRow($fields);
196 public function selectAllForUser(int $uid): Collection\Notifications
198 return $this->selectForUser($uid);
202 * @param array $condition
203 * @param array $params
204 * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
205 * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
208 * @return BaseCollection
210 * @see _selectByBoundaries
212 public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
214 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
216 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
219 public function setAllSeenForUser(int $uid, array $condition = []): bool
221 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
223 return $this->db->update(self::$table_name, ['seen' => true], $condition);
226 public function setAllDismissedForUser(int $uid, array $condition = []): bool
228 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
230 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
234 * @param Entity\Notification $Notification
235 * @return Entity\Notification
238 public function save(Entity\Notification $Notification): Entity\Notification
241 'uid' => $Notification->uid,
242 'vid' => Verb::getID($Notification->verb),
243 'type' => $Notification->type,
244 'actor-id' => $Notification->actorId,
245 'target-uri-id' => $Notification->targetUriId,
246 'parent-uri-id' => $Notification->parentUriId,
247 'seen' => $Notification->seen,
248 'dismissed' => $Notification->dismissed,
251 if ($Notification->id) {
252 $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
254 $fields['created'] = DateTimeFormat::utcNow();
255 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
257 $Notification = $this->selectOneById($this->db->lastInsertId());
260 return $Notification;
263 public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
265 $condition['uid'] = $uid;
266 $condition['vid'] = Verb::getID($verb);
268 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
270 return $this->db->delete(self::$table_name, $condition);
273 public function deleteForItem(int $itemUriId): bool
276 'vid' => Verb::getID(Activity::POST),
277 'target-uri-id' => $itemUriId,
281 'vid' => Verb::getID(Activity::POST),
282 'parent-uri-id' => $itemUriId,
285 $this->logger->info('deleteForItem', ['conditionTarget' => $conditionTarget, 'conditionParent' => $conditionParent]);
288 $this->db->delete(self::$table_name, $conditionTarget)
289 && $this->db->delete(self::$table_name, $conditionParent);