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