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