]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Repository/Notification.php
Legacy "include" fragments have been removed
[friendica.git] / src / Navigation / Notifications / Repository / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Util\DateTimeFormat;
37 use Psr\Log\LoggerInterface;
38
39 class Notification extends BaseRepository
40 {
41         /** @var Factory\Notification  */
42         protected $factory;
43
44         protected static $table_name = 'notification';
45
46         /** @var IManagePersonalConfigValues */
47         private $pconfig;
48
49         public function __construct(IManagePersonalConfigValues $pconfig, Database $database, LoggerInterface $logger, Factory\Notification $factory)
50         {
51                 parent::__construct($database, $logger, $factory);
52
53                 $this->pconfig = $pconfig;
54         }
55
56         /**
57          * @param array $condition
58          * @param array $params
59          * @return Entity\Notification
60          * @throws NotFoundException
61          */
62         private function selectOne(array $condition, array $params = []): Entity\Notification
63         {
64                 return parent::_selectOne($condition, $params);
65         }
66
67         private function select(array $condition, array $params = []): Collection\Notifications
68         {
69                 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
70         }
71
72         public function countForUser($uid, array $condition, array $params = []): int
73         {
74                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
75
76                 return $this->count($condition, $params);
77         }
78
79         public function existsForUser($uid, array $condition): bool
80         {
81                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
82
83                 return $this->exists($condition);
84         }
85
86         /**
87          * @param int $id
88          * @return Entity\Notification
89          * @throws NotFoundException
90          */
91         public function selectOneById(int $id): Entity\Notification
92         {
93                 return $this->selectOne(['id' => $id]);
94         }
95
96         public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
97         {
98                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
99
100                 return $this->selectOne($condition, $params);
101         }
102
103         public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
104         {
105                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
106
107                 return $this->select($condition, $params);
108         }
109
110
111         /**
112          * Returns only the most recent notifications for the same conversation or contact
113          *
114          * @param int $uid
115          *
116          * @return Collection\Notifications
117          * @throws Exception
118          */
119         public function selectDetailedForUser(int $uid): Collection\Notifications
120         {
121                 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
122                 if (!is_null($notify_type)) {
123                         $condition = ["`type` & ? != 0", $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW];
124                 } else {
125                         $condition = [];
126                 }
127
128                 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
129                         $condition = DBA::mergeConditions($condition, ['NOT `vid` IN (?, ?)', Verb::getID(\Friendica\Protocol\Activity::LIKE), Verb::getID(\Friendica\Protocol\Activity::DISLIKE)]);
130                 }
131
132                 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
133                         $condition = DBA::mergeConditions($condition, ['`vid` != ?', Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE)]);
134                 }
135
136                 return $this->selectForUser($uid, $condition, ['limit' => 50, 'order' => ['id' => true]]);
137         }
138
139         /**
140          * Returns only the most recent notifications for the same conversation or contact
141          *
142          * @param int $uid
143          *
144          * @return Collection\Notifications
145          * @throws Exception
146          */
147         public function selectDigestForUser(int $uid): Collection\Notifications
148         {
149                 $values = [$uid];
150
151                 $type_condition = '';
152                 $notify_type = $this->pconfig->get($uid, 'system', 'notify_type');
153                 if (!is_null($notify_type)) {
154                         $type_condition = 'AND `type` & ? != 0';
155                         $values[] = $notify_type | UserNotification::TYPE_SHARED | UserNotification::TYPE_FOLLOW;
156                 }
157
158                 $like_condition = '';
159                 if (!$this->pconfig->get($uid, 'system', 'notify_like')) {
160                         $like_condition = 'AND NOT `vid` IN (?, ?)';
161                         $values[] = Verb::getID(\Friendica\Protocol\Activity::LIKE);
162                         $values[] = Verb::getID(\Friendica\Protocol\Activity::DISLIKE);
163                 }
164
165                 $announce_condition = '';
166                 if (!$this->pconfig->get($uid, 'system', 'notify_announce')) {
167                         $announce_condition = 'AND vid != ?';
168                         $values[] = Verb::getID(\Friendica\Protocol\Activity::ANNOUNCE);
169                 }
170
171                 $rows = $this->db->p("
172                 SELECT notification.*
173                 FROM notification
174                 WHERE `id` IN (
175                     SELECT MAX(`id`)
176                     FROM `notification`
177                     WHERE `uid` = ?
178                         $type_condition
179                     $like_condition
180                     $announce_condition
181                     GROUP BY IFNULL(`parent-uri-id`, `actor-id`)
182                 )
183                 ORDER BY `seen`, `id` DESC
184                 LIMIT 50
185                 ", ...$values);
186
187                 $Entities = new Collection\Notifications();
188                 foreach ($rows as $fields) {
189                         $Entities[] = $this->factory->createFromTableRow($fields);
190                 }
191
192                 return $Entities;
193         }
194
195         public function selectAllForUser(int $uid): Collection\Notifications
196         {
197                 return $this->selectForUser($uid);
198         }
199
200         /**
201          * @param array    $condition
202          * @param array    $params
203          * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
204          * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
205          * @param int      $limit
206          *
207          * @return BaseCollection
208          * @throws Exception
209          * @see _selectByBoundaries
210          */
211         public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
212         {
213                 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
214
215                 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
216         }
217
218         public function setAllSeenForUser(int $uid, array $condition = []): bool
219         {
220                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
221
222                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
223         }
224
225         public function setAllDismissedForUser(int $uid, array $condition = []): bool
226         {
227                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
228
229                 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
230         }
231
232         /**
233          * @param Entity\Notification $Notification
234          * @return Entity\Notification
235          * @throws Exception
236          */
237         public function save(Entity\Notification $Notification): Entity\Notification
238         {
239                 $fields = [
240                         'uid'           => $Notification->uid,
241                         'vid'           => Verb::getID($Notification->verb),
242                         'type'          => $Notification->type,
243                         'actor-id'      => $Notification->actorId,
244                         'target-uri-id' => $Notification->targetUriId,
245                         'parent-uri-id' => $Notification->parentUriId,
246                         'seen'          => $Notification->seen,
247                         'dismissed'     => $Notification->dismissed,
248                 ];
249
250                 if ($Notification->id) {
251                         $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
252                 } else {
253                         $fields['created'] = DateTimeFormat::utcNow();
254                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
255
256                         $Notification = $this->selectOneById($this->db->lastInsertId());
257                 }
258
259                 return $Notification;
260         }
261
262         public function deleteForUserByVerb(int $uid, string $verb, array $condition = []): bool
263         {
264                 $condition['uid'] = $uid;
265                 $condition['vid'] = Verb::getID($verb);
266
267                 $this->logger->notice('deleteForUserByVerb', ['condition' => $condition]);
268
269                 return $this->db->delete(self::$table_name, $condition);
270         }
271 }