3 namespace Friendica\Model;
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;
16 * Model for an entry in the notify table
17 * - Including additional calculated properties
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`)
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.
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
42 class Notification extends BaseModel
44 /** @var \Friendica\Repository\Notification */
49 public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
51 parent::__construct($dba, $logger, $data);
55 $this->setNameCache();
56 $this->setTimestamp();
61 * Set the notification as seen
63 * @param bool $seen true, if seen
65 * @return bool True, if the seen state could be saved
67 public function setSeen(bool $seen = true)
71 return $this->repo->update($this);
72 } catch (Exception $e) {
73 $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
79 * Set some extra properties to the notification from db:
80 * - timestamp as int in default TZ
81 * - date_rel : relative date string
83 private function setTimestamp()
86 $this->timestamp = strtotime(DateTimeFormat::local($this->date));
87 } catch (Exception $e) {
89 $this->dateRel = Temporal::getRelativeDate($this->date);
93 * Sets the pre-formatted name (caching)
95 * @throws InternalServerErrorException
97 private function setNameCache()
99 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
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)
108 private function setMsg()
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) {
118 public function __get($name)
126 if (!empty($this->parent)) {
127 $this->parentInst = $this->parentInst ?? $this->repo->getByID($this->parent);
129 $return = $this->parentInst;
133 $return = parent::__get($name);
140 public function __set($name, $value)
142 parent::__set($name, $value);
144 if ($name == 'date') {
145 $this->setTimestamp();
148 if ($name == 'msg') {
152 if ($name == 'source_name') {
153 $this->setNameCache();
158 * Formats a notification message with the notification author
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.
163 * @param string $name
164 * @param string $message
166 * @return string Formatted message
168 public static function formatMessage($name, $message)
171 $pos = strpos($message, $name);
176 if ($pos !== false) {
177 $message = substr_replace($message, '{0}', $pos, strlen($name));