3 namespace Friendica\Model;
6 use Friendica\BaseModel;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Database\Database;
9 use Friendica\Network\HTTPException\InternalServerErrorException;
10 use Psr\Log\LoggerInterface;
13 * Model for an entry in the notify table
15 * @property string hash
16 * @property integer type
17 * @property string name Full name of the contact subject
18 * @property string url Profile page URL of the contact subject
19 * @property string photo Profile photo URL of the contact subject
20 * @property string date YYYY-MM-DD hh:mm:ss local server time
21 * @property string msg
22 * @property integer uid Owner User Id
23 * @property string link Notification URL
24 * @property integer iid Item Id
25 * @property integer parent Parent Item Id
26 * @property boolean seen Whether the notification was read or not.
27 * @property string verb Verb URL (@see http://activitystrea.ms)
28 * @property string otype Subject type (`item`, `intro` or `mail`)
30 * @property-read string name_cache Full name of the contact subject
31 * @property-read string msg_cache Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
33 class Notify extends BaseModel
35 const OTYPE_ITEM = 'item';
36 const OTYPE_INTRO = 'intro';
37 const OTYPE_MAIL = 'mail';
38 const OTYPE_PERSON = 'person';
40 /** @var \Friendica\Repository\Notify */
43 public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = [])
45 parent::__construct($dba, $logger, $data);
49 $this->setNameCache();
54 * Set the notification as seen
56 * @param bool $seen true, if seen
58 * @return bool True, if the seen state could be saved
60 public function setSeen(bool $seen = true)
64 return $this->repo->update($this);
65 } catch (Exception $e) {
66 $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
72 * Sets the pre-formatted name (caching)
74 private function setNameCache()
77 $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
78 } catch (InternalServerErrorException $e) {
83 * Sets the pre-formatted msg (caching)
85 private function setMsgCache()
88 $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
89 } catch (InternalServerErrorException $e) {
93 public function __set($name, $value)
95 parent::__set($name, $value);
101 if ($name == 'source_name') {
102 $this->setNameCache();
107 * Formats a notification message with the notification author
109 * Replace the name with {0} but ensure to make that only once. The {0} is used
110 * later and prints the name in bold.
112 * @param string $name
113 * @param string $message
115 * @return string Formatted message
117 public static function formatMessage($name, $message)
120 $pos = strpos($message, $name);
125 if ($pos !== false) {
126 $message = substr_replace($message, '{0}', $pos, strlen($name));