]> git.mxchange.org Git - friendica.git/blob - src/Repository/Notification.php
Add new paradigm classes for notify
[friendica.git] / src / Repository / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Repository;
23
24 use Exception;
25 use Friendica\BaseRepository;
26 use Friendica\Collection;
27 use Friendica\Core\Hook;
28 use Friendica\Model;
29 use Friendica\Network\HTTPException\InternalServerErrorException;
30 use Friendica\Network\HTTPException\NotFoundException;
31 use Friendica\Util\DateTimeFormat;
32
33 class Notification extends BaseRepository
34 {
35         protected static $table_name = 'notify';
36
37         protected static $model_class = Model\Notification::class;
38
39         protected static $collection_class = Collection\Notifications::class;
40
41         /**
42          * {@inheritDoc}
43          *
44          * @return Model\Notification
45          */
46         protected function create(array $data)
47         {
48                 return new Model\Notification($this->dba, $this->logger, $this, $data);
49         }
50
51         /**
52          * {@inheritDoc}
53          *
54          * @return Collection\Notifications
55          */
56         public function select(array $condition = [], array $params = [])
57         {
58                 $params['order'] = $params['order'] ?? ['date' => 'DESC'];
59
60                 return parent::select($condition, $params);
61         }
62
63         /**
64          * Return one notify instance based on ID / UID
65          *
66          * @param int $id The ID of the notify instance
67          * @param int $uid The user ID, bound to this notify instance (= security check)
68          *
69          * @return Model\Notification
70          * @throws NotFoundException
71          */
72         public function getByID(int $id, int $uid)
73         {
74                 return $this->selectFirst(['id' => $id, 'uid' => $uid]);
75         }
76
77         /**
78          * Set seen state of notifications of the local_user()
79          *
80          * @param bool               $seen   optional true or false. default true
81          * @param Model\Notification $notify optional a notify, which should be set seen (including his parents)
82          *
83          * @return bool true on success, false on error
84          *
85          * @throws Exception
86          */
87         public function setSeen(bool $seen = true, Model\Notification $notify = null)
88         {
89                 if (empty($notify)) {
90                         $this->dba->update('notification', ['seen' => $seen], ['uid' => local_user()]);
91                         $conditions = ['uid' => local_user()];
92                 } else {
93                         if (!empty($notify->{'uri-id'})) {
94                                 $this->dba->update('notification', ['seen' => $seen], ['uid' => local_user(), 'target-uri-id' => $notify->{'uri-id'}]);
95                         }
96
97                         $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
98                                 $notify->link,
99                                 $notify->parent,
100                                 $notify->otype,
101                                 local_user()];
102                 }
103
104                 return $this->dba->update('notify', ['seen' => $seen], $conditions);
105         }
106
107         /**
108          * @param array $fields
109          *
110          * @return Model\Notification|false
111          *
112          * @throws InternalServerErrorException
113          * @throws Exception
114          */
115         public function insert(array $fields)
116         {
117                 $fields['date'] = DateTimeFormat::utcNow();
118
119                 Hook::callAll('enotify_store', $fields);
120
121                 if (empty($fields)) {
122                         $this->logger->debug('Abort adding notification entry');
123                         return false;
124                 }
125
126                 $this->logger->debug('adding notification entry', ['fields' => $fields]);
127
128                 return parent::insert($fields);
129         }
130 }