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 * 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
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`)
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.
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
45 class Notification extends BaseModel
47 /** @var \Friendica\Repository\Notification */
50 public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
52 parent::__construct($dba, $logger, $data);
56 $this->setNameCache();
57 $this->setTimestamp();
62 * Set the notification as seen
64 * @param bool $seen true, if seen
66 * @return bool True, if the seen state could be saved
68 public function setSeen(bool $seen = true)
72 return $this->repo->update($this);
73 } catch (Exception $e) {
74 $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
80 * Set some extra properties to the notification from db:
81 * - timestamp as int in default TZ
82 * - date_rel : relative date string
84 private function setTimestamp()
87 $this->timestamp = strtotime(DateTimeFormat::local($this->date));
88 } catch (Exception $e) {
90 $this->dateRel = Temporal::getRelativeDate($this->date);
94 * Sets the pre-formatted name (caching)
96 * @throws InternalServerErrorException
98 private function setNameCache()
100 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
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)
109 private function setMsg()
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) {
119 public function __set($name, $value)
121 parent::__set($name, $value);
123 if ($name == 'date') {
124 $this->setTimestamp();
127 if ($name == 'msg') {
131 if ($name == 'source_name') {
132 $this->setNameCache();
137 * Formats a notification message with the notification author
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.
142 * @param string $name
143 * @param string $message
145 * @return string Formatted message
147 public static function formatMessage($name, $message)
150 $pos = strpos($message, $name);
155 if ($pos !== false) {
156 $message = substr_replace($message, '{0}', $pos, strlen($name));