]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Depository/Notify.php
Add new paradigm classes for notification and introduction notifications
[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         public function selectAllForUser(int $uid, array $params = []): Collection\Notifies
77         {
78                 return $this->selectForUser($uid, [], $params);
79         }
80
81         public function setAllSeenForUser(int $uid, array $condition = []): bool
82         {
83                 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
84
85                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
86         }
87
88         /**
89          * @param Entity\Notify $Notify
90          * @return Entity\Notify
91          * @throws HTTPException\NotFoundException
92          * @throws HTTPException\InternalServerErrorException
93          * @throws Exception\NotificationCreationInterceptedException
94          */
95         public function save(Entity\Notify $Notify): Entity\Notify
96         {
97                 $fields = [
98                         'type'          => $Notify->type,
99                         'name'          => $Notify->name,
100                         'url'           => $Notify->url,
101                         'photo'         => $Notify->photo,
102                         'msg'           => $Notify->msg,
103                         'uid'           => $Notify->uid,
104                         'link'          => $Notify->link,
105                         'iid'           => $Notify->itemId,
106                         'parent'        => $Notify->parent,
107                         'seen'          => $Notify->seen,
108                         'verb'          => $Notify->verb,
109                         'otype'         => $Notify->otype,
110                         'name_cache'    => $Notify->name_cache,
111                         'msg_cache'     => $Notify->msg_cache,
112                         'uri-id'        => $Notify->uriId,
113                         'parent-uri-id' => $Notify->parentUriId,
114                 ];
115
116                 if ($Notify->id) {
117                         $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
118                 } else {
119                         $fields['date'] = DateTimeFormat::utcNow();
120                         Hook::callAll('enotify_store', $fields);
121
122                         $this->db->insert(self::$table_name, $fields);
123
124                         $Notify = $this->selectOneById($this->db->lastInsertId());
125                 }
126
127                 return $Notify;
128         }
129
130         public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
131         {
132                 $condition = [
133                         '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
134                         $Notify->link,
135                         $Notify->parent,
136                         $Notify->otype,
137                         $Notify->uid
138                 ];
139                 return $this->db->update(self::$table_name, ['seen' => true], $condition);
140         }
141 }