]> git.mxchange.org Git - friendica.git/blob - src/Repository/Notify.php
d8887affd50fb07826d0e48c87cf89a6890df566
[friendica.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\Core\Hook;
27 use Friendica\Model;
28 use Friendica\Collection;
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          * {@inheritDoc}
65          *
66          * @return Model\Notify
67          * @throws NotFoundException
68          */
69         public function getByID(int $id)
70         {
71                 return $this->selectFirst(['id' => $id, 'uid' => local_user()]);
72         }
73
74         /**
75          * Set seen state of notifications of the local_user()
76          *
77          * @param bool         $seen   optional true or false. default true
78          * @param Model\Notify $notify optional a notify, which should be set seen (including his parents)
79          *
80          * @return bool true on success, false on error
81          *
82          * @throws Exception
83          */
84         public function setSeen(bool $seen = true, Model\Notify $notify = null)
85         {
86                 if (empty($notify)) {
87                         $conditions = ['uid' => local_user()];
88                 } else {
89                         $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
90                                 $notify->link,
91                                 $notify->parent,
92                                 $notify->otype,
93                                 local_user()];
94                 }
95
96                 return $this->dba->update('notify', ['seen' => $seen], $conditions);
97         }
98
99         /**
100          * @param array $fields
101          *
102          * @return Model\Notify|false
103          *
104          * @throws InternalServerErrorException
105          * @throws Exception
106          */
107         public function insert(array $fields)
108         {
109                 $fields['date'] = DateTimeFormat::utcNow();
110
111                 Hook::callAll('enotify_store', $fields);
112
113                 if (empty($fields)) {
114                         $this->logger->debug('Abort adding notification entry');
115                         return false;
116                 }
117
118                 $this->logger->debug('adding notification entry', ['fields' => $fields]);
119
120                 return parent::insert($fields);
121         }
122 }