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