]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Repository/Notification.php
Merge pull request #13193 from annando/fix-notification-link
[friendica.git] / src / Navigation / Notifications / Repository / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Navigation\Notifications\Repository;
23
24 use Exception;
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;
39
40 class Notification extends BaseRepository
41 {
42         /** @var Factory\Notification  */
43         protected $factory;
44
45         protected static $table_name = 'notification';
46
47         /** @var IManagePersonalConfigValues */
48         private $pconfig;
49
50         public function __construct(IManagePersonalConfigValues $pconfig, Database $database, LoggerInterface $logger, Factory\Notification $factory)
51         {
52                 parent::__construct($database, $logger, $factory);
53
54                 $this->pconfig = $pconfig;
55         }
56
57         /**
58          * @param array $condition
59          * @param array $params
60          * @return Entity\Notification
61          * @throws NotFoundException
62          */
63         private function selectOne(array $condition, array $params = []): Entity\Notification
64         {
65                 return parent::_selectOne($condition, $params);
66         }
67
68         private function select(array $condition, array $params = []): Collection\Notifications
69         {
70                 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
71         }
72
73         public function countForUser($uid, array $condition, array $params = []): int
74         {
75                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
76
77                 return $this->count($condition, $params);
78         }
79
80         public function existsForUser($uid, array $condition): bool
81         {
82                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
83
84                 return $this->exists($condition);
85         }
86
87         /**
88          * @param int $id
89          * @return Entity\Notification
90          * @throws NotFoundException
91          */
92         public function selectOneById(int $id): Entity\Notification
93         {
94                 return $this->selectOne(['id' => $id]);
95         }
96
97         public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
98         {
99                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
100
101                 return $this->selectOne($condition, $params);
102         }
103
104         public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
105         {
106                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
107
108                 return $this->select($condition, $params);
109         }
110
111
112         /**
113          * Returns only the most recent notifications for the same conversation or contact
114          *
115          * @param int $uid
116          *
117          * @return Collection\Notifications
118          * @throws Exception
119          */
120         public function selectDetailedForUser(int $uid): Collection\Notifications
121         {
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];
125                 } else {
126                         $condition = [];
127                 }
128
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)]);
131                 }
132
133                 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
134                         $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE)]);
135                 }
136
137                 return $this->selectForUser($uid, $condition, ['limit' => 50, 'order' => ['id' => true]]);
138         }
139
140         /**
141          * Returns only the most recent notifications for the same conversation or contact
142          *
143          * @param int $uid
144          *
145          * @return Collection\Notifications
146          * @throws Exception
147          */
148         public function selectDigestForUser(int $uid): Collection\Notifications
149         {
150                 $values = [$uid];
151
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;
157                 }
158
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);
164                 }
165
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);
170                 }
171
172                 $rows = $this->db->p("
173                 SELECT notification.*
174                 FROM notification
175                 WHERE `id` IN (
176                     SELECT MAX(`id`)
177                     FROM `notification`
178                     WHERE `uid` = ?
179                         $type_condition
180                     $like_condition
181                     $announce_condition
182                     GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
183                 )
184                 ORDER BY `seen`, `id` DESC
185                 LIMIT 50
186                 ", ...$values);
187
188                 $Entities = new Collection\Notifications();
189                 foreach ($rows as $fields) {
190                         $Entities[] = $this->factory->createFromTableRow($fields);
191                 }
192
193                 return $Entities;
194         }
195
196         public function selectAllForUser(int $uid): Collection\Notifications
197         {
198                 return $this->selectForUser($uid);
199         }
200
201         /**
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
206          * @param int      $limit
207          *
208          * @return BaseCollection
209          * @throws Exception
210          * @see _selectByBoundaries
211          */
212         public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
213         {
214                 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
215
216                 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
217         }
218
219         public function setAllSeenForUser(int $uid, array $condition = []): bool
220         {
221                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
222
223                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
224         }
225
226         public function setAllDismissedForUser(int $uid, array $condition = []): bool
227         {
228                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
229
230                 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
231         }
232
233         /**
234          * @param Entity\Notification $Notification
235          * @return Entity\Notification
236          * @throws Exception
237          */
238         public function save(Entity\Notification $Notification): Entity\Notification
239         {
240                 $fields = [
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,
249                 ];
250
251                 if ($Notification->id) {
252                         $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
253                 } else {
254                         $fields['created'] = DateTimeFormat::utcNow();
255                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
256
257                         $Notification = $this->selectOneById($this->db->lastInsertId());
258                 }
259
260                 return $Notification;
261         }
262
263         public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
264         {
265                 $condition['uid'] = $uid;
266                 $condition['vid'] = Verb::getID($verb);
267
268                 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
269
270                 return $this->db->delete(self::$table_name, $condition);
271         }
272
273         public function deleteForItem(int $itemUriId): bool
274         {
275                 $conditionTarget = [
276                         'vid' => Verb::getID(Activity::POST),
277                         'target-uri-id' => $itemUriId,
278                 ];
279
280                 $conditionParent = [
281                         'vid' => Verb::getID(Activity::POST),
282                         'parent-uri-id' => $itemUriId,
283                 ];
284
285                 $this->logger->info('deleteForItem', ['conditionTarget' => $conditionTarget, 'conditionParent' => $conditionParent]);
286
287                 return
288                         $this->db->delete(self::$table_name, $conditionTarget)
289                         && $this->db->delete(self::$table_name, $conditionParent);
290         }
291 }