]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Depository/Notify.php
Simplify Depository\Notify->selectAllForUser parameters
[friendica.git] / src / Navigation / Notifications / Depository / Notify.php
1 <?php
2
3 namespace Friendica\Navigation\Notifications\Depository;
4
5 use Friendica\BaseDepository;
6 use Friendica\Core\Hook;
7 use Friendica\Database\Database;
8 use Friendica\Database\DBA;
9 use Friendica\Navigation\Notifications\Collection;
10 use Friendica\Navigation\Notifications\Entity;
11 use Friendica\Navigation\Notifications\Exception;
12 use Friendica\Navigation\Notifications\Factory;
13 use Friendica\Network\HTTPException;
14 use Friendica\Util\DateTimeFormat;
15 use Psr\Log\LoggerInterface;
16
17 class Notify extends BaseDepository
18 {
19         /** @var Factory\Notify  */
20         protected $factory;
21
22         protected static $table_name = 'notify';
23
24         public function __construct(Database $database, LoggerInterface $logger, Factory\Notify $factory = null)
25         {
26                 parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger));
27         }
28
29         /**
30          * @param array $condition
31          * @param array $params
32          * @return Entity\Notify
33          * @throws HTTPException\NotFoundException
34          */
35         private function selectOne(array $condition, array $params = []): Entity\Notify
36         {
37                 return parent::_selectOne($condition, $params);
38         }
39
40         private function select(array $condition, array $params = []): Collection\Notifies
41         {
42                 return new Collection\Notifies(parent::_select($condition, $params)->getArrayCopy());
43         }
44
45         public function countForUser($uid, array $condition, array $params = []): int
46         {
47                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
48
49                 return $this->count($condition, $params);
50         }
51
52         public function existsForUser($uid, array $condition): bool
53         {
54                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
55
56                 return $this->exists($condition);
57         }
58
59         /**
60          * @param int $id
61          * @return Entity\Notify
62          * @throws HTTPException\NotFoundException
63          */
64         public function selectOneById(int $id): Entity\Notify
65         {
66                 return $this->selectOne(['id' => $id]);
67         }
68
69         public function selectForUser(int $uid, array $condition, array $params): Collection\Notifies
70         {
71                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
72
73                 return $this->select($condition, $params);
74         }
75
76         /**
77          * Returns notifications for the user, unread first, ordered in descending chronological order.
78          *
79          * @param int $uid
80          * @param int $limit
81          * @return Collection\Notifies
82          */
83         public function selectAllForUser(int $uid, int $limit): Collection\Notifies
84         {
85                 return $this->selectForUser($uid, [], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => $limit]);
86         }
87
88         public function setAllSeenForUser(int $uid, array $condition = []): bool
89         {
90                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
91
92                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
93         }
94
95         /**
96          * @param Entity\Notify $Notify
97          * @return Entity\Notify
98          * @throws HTTPException\NotFoundException
99          * @throws HTTPException\InternalServerErrorException
100          * @throws Exception\NotificationCreationInterceptedException
101          */
102         public function save(Entity\Notify $Notify): Entity\Notify
103         {
104                 $fields = [
105                         'type'          => $Notify->type,
106                         'name'          => $Notify->name,
107                         'url'           => $Notify->url,
108                         'photo'         => $Notify->photo,
109                         'msg'           => $Notify->msg,
110                         'uid'           => $Notify->uid,
111                         'link'          => $Notify->link,
112                         'iid'           => $Notify->itemId,
113                         'parent'        => $Notify->parent,
114                         'seen'          => $Notify->seen,
115                         'verb'          => $Notify->verb,
116                         'otype'         => $Notify->otype,
117                         'name_cache'    => $Notify->name_cache,
118                         'msg_cache'     => $Notify->msg_cache,
119                         'uri-id'        => $Notify->uriId,
120                         'parent-uri-id' => $Notify->parentUriId,
121                 ];
122
123                 if ($Notify->id) {
124                         $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
125                 } else {
126                         $fields['date'] = DateTimeFormat::utcNow();
127                         Hook::callAll('enotify_store', $fields);
128
129                         $this->db->insert(self::$table_name, $fields);
130
131                         $Notify = $this->selectOneById($this->db->lastInsertId());
132                 }
133
134                 return $Notify;
135         }
136
137         public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
138         {
139                 $condition = [
140                         '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
141                         $Notify->link,
142                         $Notify->parent,
143                         $Notify->otype,
144                         $Notify->uid
145                 ];
146                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
147         }
148 }