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\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Verb;
30 use Friendica\Navigation\Notifications\Collection;
31 use Friendica\Navigation\Notifications\Entity;
32 use Friendica\Navigation\Notifications\Factory;
33 use Friendica\Network\HTTPException\NotFoundException;
34 use Friendica\Util\DateTimeFormat;
35 use Psr\Log\LoggerInterface;
37 class Notification extends BaseRepository
39 /** @var Factory\Notification */
42 protected static $table_name = 'notification';
44 public function __construct(Database $database, LoggerInterface $logger, Factory\Notification $factory)
46 parent::__construct($database, $logger, $factory);
50 * @param array $condition
51 * @param array $params
52 * @return Entity\Notification
53 * @throws NotFoundException
55 private function selectOne(array $condition, array $params = []): Entity\Notification
57 return parent::_selectOne($condition, $params);
60 private function select(array $condition, array $params = []): Collection\Notifications
62 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
65 public function countForUser($uid, array $condition, array $params = []): int
67 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
69 return $this->count($condition, $params);
72 public function existsForUser($uid, array $condition): bool
74 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
76 return $this->exists($condition);
81 * @return Entity\Notification
82 * @throws NotFoundException
84 public function selectOneById(int $id): Entity\Notification
86 return $this->selectOne(['id' => $id]);
89 public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
91 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
93 return $this->selectOne($condition, $params);
96 public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
98 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
100 return $this->select($condition, $params);
104 * Returns only the most recent notifications for the same conversation or contact
107 * @return Collection\Notifications
110 public function selectDigestForUser(int $uid): Collection\Notifications
112 $rows = $this->db->p("
113 SELECT notification.*
119 GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
121 ORDER BY `seen`, `id` DESC
125 $Entities = new Collection\Notifications();
126 foreach ($rows as $fields) {
127 $Entities[] = $this->factory->createFromTableRow($fields);
133 public function selectAllForUser(int $uid): Collection\Notifications
135 return $this->selectForUser($uid);
139 * @param array $condition
140 * @param array $params
141 * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
142 * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
144 * @return BaseCollection
146 * @see _selectByBoundaries
148 public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
150 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
152 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
155 public function setAllSeenForUser(int $uid, array $condition = []): bool
157 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
159 return $this->db->update(self::$table_name, ['seen' => true], $condition);
162 public function setAllDismissedForUser(int $uid, array $condition = []): bool
164 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
166 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
170 * @param Entity\Notification $Notification
171 * @return Entity\Notification
174 public function save(Entity\Notification $Notification): Entity\Notification
177 'uid' => $Notification->uid,
178 'vid' => Verb::getID($Notification->verb),
179 'type' => $Notification->type,
180 'actor-id' => $Notification->actorId,
181 'target-uri-id' => $Notification->targetUriId,
182 'parent-uri-id' => $Notification->parentUriId,
183 'seen' => $Notification->seen,
184 'dismissed' => $Notification->dismissed,
187 if ($Notification->id) {
188 $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
190 $fields['created'] = DateTimeFormat::utcNow();
191 $this->db->insert(self::$table_name, $fields);
193 $Notification = $this->selectOneById($this->db->lastInsertId());
196 return $Notification;
199 public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
201 $condition['uid'] = $uid;
202 $condition['vid'] = Verb::getID($verb);
204 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
206 return $this->db->delete(self::$table_name, $condition);