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\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\Util\DateTimeFormat;
37 use Psr\Log\LoggerInterface;
39 class Notification extends BaseRepository
41 /** @var Factory\Notification */
44 protected static $table_name = 'notification';
46 /** @var IManagePersonalConfigValues */
49 public function __construct(IManagePersonalConfigValues $pconfig, Database $database, LoggerInterface $logger, Factory\Notification $factory)
51 parent::__construct($database, $logger, $factory);
53 $this->pconfig = $pconfig;
57 * @param array $condition
58 * @param array $params
59 * @return Entity\Notification
60 * @throws NotFoundException
62 private function selectOne(array $condition, array $params = []): Entity\Notification
64 return parent::_selectOne($condition, $params);
67 private function select(array $condition, array $params = []): Collection\Notifications
69 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
72 public function countForUser($uid, array $condition, array $params = []): int
74 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
76 return $this->count($condition, $params);
79 public function existsForUser($uid, array $condition): bool
81 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
83 return $this->exists($condition);
88 * @return Entity\Notification
89 * @throws NotFoundException
91 public function selectOneById(int $id): Entity\Notification
93 return $this->selectOne(['id' => $id]);
96 public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
98 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
100 return $this->selectOne($condition, $params);
103 public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
105 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
107 return $this->select($condition, $params);
112 * Returns only the most recent notifications for the same conversation or contact
115 * @return Collection\Notifications
118 public function selectDetailedForUser(int $uid): Collection\Notifications
120 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
121 if (!is_null($notify_type)) {
122 $condition = ["`type` & ? != 0", $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW];
127 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
128 $condition = DBA::mergeConditions($condition, ['NOT `vid` IN (?, ?)', Verb::getID(\Friendica\Protocol\Activity::LIKE), Verb::getID(\Friendica\Protocol\Activity::DISLIKE)]);
131 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
132 $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE)]);
135 return $this->selectForUser($uid, $condition, ['limit' => 50, 'order' => ['id' => true]]);
139 * Returns only the most recent notifications for the same conversation or contact
142 * @return Collection\Notifications
145 public function selectDigestForUser(int $uid): Collection\Notifications
149 $type_condition = '';
150 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
151 if (!is_null($notify_type)) {
152 $type_condition = 'AND `type` & ? != 0';
153 $values[] = $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW;
156 $like_condition = '';
157 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
158 $like_condition = 'AND NOT `vid` IN (?, ?)';
159 $values[] = Verb::getID(\Friendica\Protocol\Activity::LIKE);
160 $values[] = Verb::getID(\Friendica\Protocol\Activity::DISLIKE);
163 $announce_condition = '';
164 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
165 $announce_condition = 'AND vid != ?';
166 $values[] = Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE);
169 $rows = $this->db->p("
170 SELECT notification.*
179 GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
181 ORDER BY `seen`, `id` DESC
185 $Entities = new Collection\Notifications();
186 foreach ($rows as $fields) {
187 $Entities[] = $this->factory->createFromTableRow($fields);
193 public function selectAllForUser(int $uid): Collection\Notifications
195 return $this->selectForUser($uid);
199 * @param array $condition
200 * @param array $params
201 * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
202 * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
204 * @return BaseCollection
206 * @see _selectByBoundaries
208 public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
210 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
212 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
215 public function setAllSeenForUser(int $uid, array $condition = []): bool
217 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
219 return $this->db->update(self::$table_name, ['seen' => true], $condition);
222 public function setAllDismissedForUser(int $uid, array $condition = []): bool
224 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
226 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
230 * @param Entity\Notification $Notification
231 * @return Entity\Notification
234 public function save(Entity\Notification $Notification): Entity\Notification
237 'uid' => $Notification->uid,
238 'vid' => Verb::getID($Notification->verb),
239 'type' => $Notification->type,
240 'actor-id' => $Notification->actorId,
241 'target-uri-id' => $Notification->targetUriId,
242 'parent-uri-id' => $Notification->parentUriId,
243 'seen' => $Notification->seen,
244 'dismissed' => $Notification->dismissed,
247 if ($Notification->id) {
248 $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
250 $fields['created'] = DateTimeFormat::utcNow();
251 $this->db->insert(self::$table_name, $fields);
253 $Notification = $this->selectOneById($this->db->lastInsertId());
256 return $Notification;
259 public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
261 $condition['uid'] = $uid;
262 $condition['vid'] = Verb::getID($verb);
264 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
266 return $this->db->delete(self::$table_name, $condition);