]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
11105d798392f65723c81bf65ee1b6c0a6a0ccea
[friendica.git] / src / Model / Notification.php
1 <?php
2
3 namespace Friendica\Model;
4
5 use Exception;
6 use Friendica\BaseModel;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Content\Text\HTML;
9 use Friendica\Database\Database;
10 use Friendica\Network\HTTPException\InternalServerErrorException;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Temporal;
13 use Psr\Log\LoggerInterface;
14
15 /**
16  * Model for an entry in the notify table
17  * - Including additional, calculated properties
18  *
19  * Is used either for frontend interactions or for API-based interaction
20  * @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification
21  *
22  * @property string  hash
23  * @property integer type
24  * @property string  name   Full name of the contact subject
25  * @property string  url    Profile page URL of the contact subject
26  * @property string  photo  Profile photo URL of the contact subject
27  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
28  * @property string  msg
29  * @property integer uid        Owner User Id
30  * @property string  link   Notification URL
31  * @property integer iid        Item Id
32  * @property integer parent Parent Item Id
33  * @property boolean seen   Whether the notification was read or not.
34  * @property string  verb   Verb URL (@see http://activitystrea.ms)
35  * @property string  otype  Subject type (`item`, `intro` or `mail`)
36  *
37  * @property-read string name_cache Full name of the contact subject
38  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
39  *
40  * @property-read integer timestamp  Unix timestamp
41  * @property-read string  dateRel        Time since the note was posted, eg "1 hour ago"
42  * @property-read string  $msg_html
43  * @property-read string  $msg_plain
44  */
45 class Notification extends BaseModel
46 {
47         /** @var \Friendica\Repository\Notification */
48         private $repo;
49         /** @var $this */
50         private $parentInst;
51
52         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
53         {
54                 parent::__construct($dba, $logger, $data);
55
56                 $this->repo = $repo;
57
58                 $this->setNameCache();
59                 $this->setTimestamp();
60                 $this->setMsg();
61         }
62
63         /**
64          * Set the notification as seen
65          *
66          * @param bool $seen true, if seen
67          *
68          * @return bool True, if the seen state could be saved
69          */
70         public function setSeen(bool $seen = true)
71         {
72                 $this->seen = $seen;
73                 try {
74                         return $this->repo->update($this);
75                 } catch (Exception $e) {
76                         $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
77                         return false;
78                 }
79         }
80
81         /**
82          * Set some extra properties to the notification from db:
83          *  - timestamp as int in default TZ
84          *  - date_rel : relative date string
85          */
86         private function setTimestamp()
87         {
88                 try {
89                         $this->timestamp = strtotime(DateTimeFormat::local($this->date));
90                 } catch (Exception $e) {
91                 }
92                 $this->dateRel = Temporal::getRelativeDate($this->date);
93         }
94
95         /**
96          * Sets the pre-formatted name (caching)
97          *
98          * @throws InternalServerErrorException
99          */
100         private function setNameCache()
101         {
102                 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
103         }
104
105         /**
106          * Set some extra properties to the notification from db:
107          *  - msg_html: message as html string
108          *  - msg_plain: message as plain text string
109          *  - msg_cache: The pre-formatted message (caching)
110          */
111         private function setMsg()
112         {
113                 try {
114                         $this->msg_html  = BBCode::convert($this->msg, false);
115                         $this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0];
116                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
117                 } catch (InternalServerErrorException $e) {
118                 }
119         }
120
121         public function __get($name)
122         {
123                 $this->checkValid();
124
125                 $return = null;
126
127                 switch ($name) {
128                         case 'parent':
129                                 if (!empty($this->parent)) {
130                                         $this->parentInst = $this->parentInst ?? $this->repo->getByID($this->parent);
131
132                                         $return = $this->parentInst;
133                                 }
134                                 break;
135                         default:
136                                 $return = parent::__get($name);
137                                 break;
138                 }
139
140                 return $return;
141         }
142
143         public function __set($name, $value)
144         {
145                 parent::__set($name, $value);
146
147                 if ($name == 'date') {
148                         $this->setTimestamp();
149                 }
150
151                 if ($name == 'msg') {
152                         $this->setMsg();
153                 }
154
155                 if ($name == 'source_name') {
156                         $this->setNameCache();
157                 }
158         }
159
160         /**
161          * Formats a notification message with the notification author
162          *
163          * Replace the name with {0} but ensure to make that only once. The {0} is used
164          * later and prints the name in bold.
165          *
166          * @param string $name
167          * @param string $message
168          *
169          * @return string Formatted message
170          */
171         public static function formatMessage($name, $message)
172         {
173                 if ($name != '') {
174                         $pos = strpos($message, $name);
175                 } else {
176                         $pos = false;
177                 }
178
179                 if ($pos !== false) {
180                         $message = substr_replace($message, '{0}', $pos, strlen($name));
181                 }
182
183                 return $message;
184         }
185 }