]> git.mxchange.org Git - friendica.git/blob - src/Model/Notify.php
Merge pull request #8199 from nupplaphil/bug/8197_intro_notif_stuck
[friendica.git] / src / Model / Notify.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\Database\Database;
9 use Friendica\Network\HTTPException\InternalServerErrorException;
10 use Psr\Log\LoggerInterface;
11
12 /**
13  * Model for an entry in the notify table
14  *
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`)
29  *
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.
32  */
33 class Notify extends BaseModel
34 {
35         const OTYPE_ITEM   = 'item';
36         const OTYPE_INTRO  = 'intro';
37         const OTYPE_MAIL   = 'mail';
38         const OTYPE_PERSON = 'person';
39
40         /** @var \Friendica\Repository\Notify */
41         private $repo;
42
43         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = [])
44         {
45                 parent::__construct($dba, $logger, $data);
46
47                 $this->repo = $repo;
48
49                 $this->setNameCache();
50                 $this->setMsgCache();
51         }
52
53         /**
54          * Set the notification as seen
55          *
56          * @param bool $seen true, if seen
57          *
58          * @return bool True, if the seen state could be saved
59          */
60         public function setSeen(bool $seen = true)
61         {
62                 $this->seen = $seen;
63                 try {
64                         return $this->repo->update($this);
65                 } catch (Exception $e) {
66                         $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]);
67                         return false;
68                 }
69         }
70
71         /**
72          * Sets the pre-formatted name (caching)
73          */
74         private function setNameCache()
75         {
76                 try {
77                         $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
78                 } catch (InternalServerErrorException $e) {
79                 }
80         }
81
82         /**
83          * Sets the pre-formatted msg (caching)
84          */
85         private function setMsgCache()
86         {
87                 try {
88                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
89                 } catch (InternalServerErrorException $e) {
90                 }
91         }
92
93         public function __set($name, $value)
94         {
95                 parent::__set($name, $value);
96
97                 if ($name == 'msg') {
98                         $this->setMsgCache();
99                 }
100
101                 if ($name == 'source_name') {
102                         $this->setNameCache();
103                 }
104         }
105
106         /**
107          * Formats a notification message with the notification author
108          *
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.
111          *
112          * @param string $name
113          * @param string $message
114          *
115          * @return string Formatted message
116          */
117         public static function formatMessage($name, $message)
118         {
119                 if ($name != '') {
120                         $pos = strpos($message, $name);
121                 } else {
122                         $pos = false;
123                 }
124
125                 if ($pos !== false) {
126                         $message = substr_replace($message, '{0}', $pos, strlen($name));
127                 }
128
129                 return $message;
130         }
131 }