]> git.mxchange.org Git - friendica.git/blob - src/Repository/Notification.php
ReWork Notification Model/Module/Object/Repository/Factory
[friendica.git] / src / Repository / Notification.php
1 <?php
2
3 namespace Friendica\Repository;
4
5 use Exception;
6 use Friendica\BaseRepository;
7 use Friendica\Core\Hook;
8 use Friendica\Model;
9 use Friendica\Collection;
10 use Friendica\Network\HTTPException\NotFoundException;
11 use Friendica\Util\DateTimeFormat;
12
13 class Notification extends BaseRepository
14 {
15         protected static $table_name = 'notify';
16
17         protected static $model_class = Model\Notification::class;
18
19         protected static $collection_class = Collection\Notifications::class;
20
21         /**
22          * {@inheritDoc}
23          *
24          * @return Model\Notification
25          */
26         protected function create(array $data)
27         {
28                 return new Model\Notification($this->dba, $this->logger, $this, $data);
29         }
30
31         /**
32          * {@inheritDoc}
33          *
34          * @return Collection\Notifications
35          */
36         public function select(array $condition = [], array $params = [])
37         {
38                 $params['order'] = $params['order'] ?? ['date' => 'DESC'];
39
40                 $condition = array_merge($condition, ['uid' => local_user()]);
41
42                 return parent::select($condition, $params);
43         }
44
45         /**
46          * {@inheritDoc}
47          *
48          * @return Model\Notification
49          * @throws NotFoundException
50          */
51         public function getByID(int $id)
52         {
53                 return $this->selectFirst(['id' => $id, 'uid' => local_user()]);
54         }
55
56         /**
57          * {@inheritDoc}
58          *
59          * @return bool true on success, false on error
60          * @throws Exception
61          */
62         public function setAllSeen(bool $seen = true)
63         {
64                 return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]);
65         }
66
67         /**
68          * @param array $fields
69          *
70          * @return Model\Notification
71          *
72          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
73          * @throws Exception
74          */
75         public function insert(array $fields)
76         {
77                 $fields['date']  = DateTimeFormat::utcNow();
78                 $fields['abort'] = false;
79
80                 Hook::callAll('enotify_store', $fields);
81
82                 if ($fields['abort']) {
83                         $this->logger->debug('Abort adding notification entry', ['fields' => $fields]);
84                         return null;
85                 }
86
87                 $this->logger->debug('adding notification entry', ['fields' => $fields]);
88
89                 return parent::insert($fields);
90         }
91 }