]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Repository/Notification.php
Merge pull request #11129 from urbalazs/copyright-2022
[friendica.git] / src / Navigation / Notifications / Repository / Notification.php
1 <?php
2
3 namespace Friendica\Navigation\Notifications\Repository;
4
5 use Exception;
6 use Friendica\BaseCollection;
7 use Friendica\BaseRepository;
8 use Friendica\Database\Database;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Verb;
11 use Friendica\Navigation\Notifications\Collection;
12 use Friendica\Navigation\Notifications\Entity;
13 use Friendica\Navigation\Notifications\Factory;
14 use Friendica\Network\HTTPException\NotFoundException;
15 use Friendica\Util\DateTimeFormat;
16 use Psr\Log\LoggerInterface;
17
18 class Notification extends BaseRepository
19 {
20         /** @var Factory\Notification  */
21         protected $factory;
22
23         protected static $table_name = 'notification';
24
25         public function __construct(Database $database, LoggerInterface $logger, Factory\Notification $factory = null)
26         {
27                 parent::__construct($database, $logger, $factory ?? new Factory\Notification($logger));
28         }
29
30         /**
31          * @param array $condition
32          * @param array $params
33          * @return Entity\Notification
34          * @throws NotFoundException
35          */
36         private function selectOne(array $condition, array $params = []): Entity\Notification
37         {
38                 return parent::_selectOne($condition, $params);
39         }
40
41         private function select(array $condition, array $params = []): Collection\Notifications
42         {
43                 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
44         }
45
46         public function countForUser($uid, array $condition, array $params = []): int
47         {
48                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
49
50                 return $this->count($condition, $params);
51         }
52
53         public function existsForUser($uid, array $condition): bool
54         {
55                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
56
57                 return $this->exists($condition);
58         }
59
60         /**
61          * @param int $id
62          * @return Entity\Notification
63          * @throws NotFoundException
64          */
65         public function selectOneById(int $id): Entity\Notification
66         {
67                 return $this->selectOne(['id' => $id]);
68         }
69
70         public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
71         {
72                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
73
74                 return $this->selectOne($condition, $params);
75         }
76
77         public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
78         {
79                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
80
81                 return $this->select($condition, $params);
82         }
83
84         public function selectAllForUser(int $uid): Collection\Notifications
85         {
86                 return $this->selectForUser($uid);
87         }
88
89         /**
90          * @param array    $condition
91          * @param array    $params
92          * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
93          * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
94          * @param int      $limit
95          * @return BaseCollection
96          * @throws Exception
97          * @see _selectByBoundaries
98          */
99         public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
100         {
101                 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
102
103                 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
104         }
105
106         public function setAllSeenForUser(int $uid, array $condition = []): bool
107         {
108                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
109
110                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
111         }
112
113         public function setAllDismissedForUser(int $uid, array $condition = []): bool
114         {
115                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
116
117                 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
118         }
119
120         /**
121          * @param Entity\Notification $Notification
122          * @return Entity\Notification
123          * @throws Exception
124          */
125         public function save(Entity\Notification $Notification): Entity\Notification
126         {
127                 $fields = [
128                         'uid'           => $Notification->uid,
129                         'vid'           => Verb::getID($Notification->verb),
130                         'type'          => $Notification->type,
131                         'actor-id'      => $Notification->actorId,
132                         'target-uri-id' => $Notification->targetUriId,
133                         'parent-uri-id' => $Notification->parentUriId,
134                         'seen'          => $Notification->seen,
135                         'dismissed'     => $Notification->dismissed,
136                 ];
137
138                 if ($Notification->id) {
139                         $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
140                 } else {
141                         $fields['created'] = DateTimeFormat::utcNow();
142                         $this->db->insert(self::$table_name, $fields);
143
144                         $Notification = $this->selectOneById($this->db->lastInsertId());
145                 }
146
147                 return $Notification;
148         }
149 }