]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Standards
[friendica.git] / src / Model / 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\Model;
23
24 use Friendica\BaseModel;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Database\Database;
27 use Friendica\Network\HTTPException\InternalServerErrorException;
28 use Psr\Log\LoggerInterface;
29
30 /**
31  * Model for an entry in the notify table
32  *
33  * @property string  hash
34  * @property integer type
35  * @property string  name   Full name of the contact subject
36  * @property string  url    Profile page URL of the contact subject
37  * @property string  photo  Profile photo URL of the contact subject
38  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
39  * @property string  msg
40  * @property integer uid        Owner User Id
41  * @property string  link   Notification URL
42  * @property integer iid        Item Id
43  * @property integer parent Parent Item Id
44  * @property boolean seen   Whether the notification was read or not.
45  * @property string  verb   Verb URL (@see http://activitystrea.ms)
46  * @property string  otype  Subject type ('item', 'intro' or 'mail')
47  *
48  * @property-read string name_cache Full name of the contact subject
49  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
50  */
51 class Notification extends BaseModel
52 {
53         /** @var \Friendica\Repository\Notification */
54         private $repo;
55
56         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
57         {
58                 parent::__construct($dba, $logger, $data);
59
60                 $this->repo = $repo;
61
62                 $this->setNameCache();
63                 $this->setMsgCache();
64         }
65
66         /**
67          * Sets the pre-formatted name (caching)
68          */
69         private function setNameCache()
70         {
71                 try {
72                         $this->name_cache = strip_tags(BBCode::convert($this->source_name));
73                 } catch (InternalServerErrorException $e) {
74                 }
75         }
76
77         /**
78          * Sets the pre-formatted msg (caching)
79          */
80         private function setMsgCache()
81         {
82                 try {
83                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
84                 } catch (InternalServerErrorException $e) {
85                 }
86         }
87
88         public function __set($name, $value)
89         {
90                 parent::__set($name, $value);
91
92                 if ($name == 'msg') {
93                         $this->setMsgCache();
94                 }
95
96                 if ($name == 'source_name') {
97                         $this->setNameCache();
98                 }
99         }
100
101         /**
102          * Formats a notification message with the notification author
103          *
104          * Replace the name with {0} but ensure to make that only once. The {0} is used
105          * later and prints the name in bold.
106          *
107          * @param string $name
108          * @param string $message
109          *
110          * @return string Formatted message
111          */
112         public static function formatMessage($name, $message)
113         {
114                 if ($name != '') {
115                         $pos = strpos($message, $name);
116                 } else {
117                         $pos = false;
118                 }
119
120                 if ($pos !== false) {
121                         $message = substr_replace($message, '{0}', $pos, strlen($name));
122                 }
123
124                 return $message;
125         }
126 }