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