]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Improvements:
[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         const OTYPE_ITEM   = 'item';
48         const OTYPE_INTRO  = 'intro';
49         const OTYPE_MAIL   = 'mail';
50         const OTYPE_PERSON = 'person';
51
52         /** @var \Friendica\Repository\Notification */
53         private $repo;
54
55         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
56         {
57                 parent::__construct($dba, $logger, $data);
58
59                 $this->repo = $repo;
60
61                 $this->setNameCache();
62                 $this->setTimestamp();
63                 $this->setMsg();
64         }
65
66         /**
67          * Set the notification as seen
68          *
69          * @param bool $seen true, if seen
70          *
71          * @return bool True, if the seen state could be saved
72          */
73         public function setSeen(bool $seen = true)
74         {
75                 $this->seen = $seen;
76                 try {
77                         return $this->repo->update($this);
78                 } catch (Exception $e) {
79                         $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
80                         return false;
81                 }
82         }
83
84         /**
85          * Set some extra properties to the notification from db:
86          *  - timestamp as int in default TZ
87          *  - date_rel : relative date string
88          */
89         private function setTimestamp()
90         {
91                 try {
92                         $this->timestamp = strtotime(DateTimeFormat::local($this->date));
93                 } catch (Exception $e) {
94                 }
95                 $this->dateRel = Temporal::getRelativeDate($this->date);
96         }
97
98         /**
99          * Sets the pre-formatted name (caching)
100          *
101          * @throws InternalServerErrorException
102          */
103         private function setNameCache()
104         {
105                 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
106         }
107
108         /**
109          * Set some extra properties to the notification from db:
110          *  - msg_html: message as html string
111          *  - msg_plain: message as plain text string
112          *  - msg_cache: The pre-formatted message (caching)
113          */
114         private function setMsg()
115         {
116                 try {
117                         $this->msg_html  = BBCode::convert($this->msg, false);
118                         $this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0];
119                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
120                 } catch (InternalServerErrorException $e) {
121                 }
122         }
123
124         public function __set($name, $value)
125         {
126                 parent::__set($name, $value);
127
128                 if ($name == 'date') {
129                         $this->setTimestamp();
130                 }
131
132                 if ($name == 'msg') {
133                         $this->setMsg();
134                 }
135
136                 if ($name == 'source_name') {
137                         $this->setNameCache();
138                 }
139         }
140
141         /**
142          * Formats a notification message with the notification author
143          *
144          * Replace the name with {0} but ensure to make that only once. The {0} is used
145          * later and prints the name in bold.
146          *
147          * @param string $name
148          * @param string $message
149          *
150          * @return string Formatted message
151          */
152         public static function formatMessage($name, $message)
153         {
154                 if ($name != '') {
155                         $pos = strpos($message, $name);
156                 } else {
157                         $pos = false;
158                 }
159
160                 if ($pos !== false) {
161                         $message = substr_replace($message, '{0}', $pos, strlen($name));
162                 }
163
164                 return $message;
165         }
166 }