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 const OTYPE_ITEM = 'item';
48 const OTYPE_INTRO = 'intro';
49 const OTYPE_MAIL = 'mail';
50 const OTYPE_PERSON = 'person';
52 /** @var \Friendica\Repository\Notification */
55 public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
57 parent::__construct($dba, $logger, $data);
61 $this->setNameCache();
62 $this->setTimestamp();
67 * Set the notification as seen
69 * @param bool $seen true, if seen
71 * @return bool True, if the seen state could be saved
73 public function setSeen(bool $seen = true)
77 return $this->repo->update($this);
78 } catch (Exception $e) {
79 $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
85 * Set some extra properties to the notification from db:
86 * - timestamp as int in default TZ
87 * - date_rel : relative date string
89 private function setTimestamp()
92 $this->timestamp = strtotime(DateTimeFormat::local($this->date));
93 } catch (Exception $e) {
95 $this->dateRel = Temporal::getRelativeDate($this->date);
99 * Sets the pre-formatted name (caching)
101 * @throws InternalServerErrorException
103 private function setNameCache()
105 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
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)
114 private function setMsg()
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) {
124 public function __set($name, $value)
126 parent::__set($name, $value);
128 if ($name == 'date') {
129 $this->setTimestamp();
132 if ($name == 'msg') {
136 if ($name == 'source_name') {
137 $this->setNameCache();
142 * Formats a notification message with the notification author
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.
147 * @param string $name
148 * @param string $message
150 * @return string Formatted message
152 public static function formatMessage($name, $message)
155 $pos = strpos($message, $name);
160 if ($pos !== false) {
161 $message = substr_replace($message, '{0}', $pos, strlen($name));