b72ccecf0a3f973605d5ce196d45187c65bdff35
[friendica.git/.git] / src / Repository / Notify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 Notify extends BaseRepository
34 {
35         protected static $table_name = 'notify';
36
37         protected static $model_class = Model\Notify::class;
38
39         protected static $collection_class = Collection\Notifies::class;
40
41         /**
42          * {@inheritDoc}
43          *
44          * @return Model\Notify
45          */
46         protected function create(array $data)
47         {
48                 return new Model\Notify($this->dba, $this->logger, $this, $data);
49         }
50
51         /**
52          * {@inheritDoc}
53          *
54          * @return Collection\Notifies
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\Notify
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\Notify $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\Notify $notify = null)
88         {
89                 if (empty($notify)) {
90                         $conditions = ['uid' => local_user()];
91                 } else {
92                         $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
93                                 $notify->link,
94                                 $notify->parent,
95                                 $notify->otype,
96                                 local_user()];
97                 }
98
99                 return $this->dba->update('notify', ['seen' => $seen], $conditions);
100         }
101
102         /**
103          * @param array $fields
104          *
105          * @return Model\Notify|false
106          *
107          * @throws InternalServerErrorException
108          * @throws Exception
109          */
110         public function insert(array $fields)
111         {
112                 $fields['date'] = DateTimeFormat::utcNow();
113
114                 Hook::callAll('enotify_store', $fields);
115
116                 if (empty($fields)) {
117                         $this->logger->debug('Abort adding notification entry');
118                         return false;
119                 }
120
121                 $this->logger->debug('adding notification entry', ['fields' => $fields]);
122
123                 return parent::insert($fields);
124         }
125 }