]> git.mxchange.org Git - friendica.git/blob - src/Model/Notify.php
35d72384e89526cb94e362dbd1bbab159db67478
[friendica.git] / src / Model / Notify.php
1 <?php
2
3 namespace Friendica\Model;
4
5 use Friendica\BaseModel;
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException\InternalServerErrorException;
9 use Psr\Log\LoggerInterface;
10
11 /**
12  * Model for an entry in the notify table
13  *
14  * @property string  hash
15  * @property integer type
16  * @property string  name   Full name of the contact subject
17  * @property string  url    Profile page URL of the contact subject
18  * @property string  photo  Profile photo URL of the contact subject
19  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
20  * @property string  msg
21  * @property integer uid        Owner User Id
22  * @property string  link   Notification URL
23  * @property integer iid        Item Id
24  * @property integer parent Parent Item Id
25  * @property boolean seen   Whether the notification was read or not.
26  * @property string  verb   Verb URL (@see http://activitystrea.ms)
27  * @property string  otype  Subject type (`item`, `intro` or `mail`)
28  *
29  * @property-read string name_cache Full name of the contact subject
30  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
31  */
32 class Notify extends BaseModel
33 {
34         const OTYPE_ITEM   = 'item';
35         const OTYPE_INTRO  = 'intro';
36         const OTYPE_MAIL   = 'mail';
37         const OTYPE_PERSON = 'person';
38
39         /** @var \Friendica\Repository\Notify */
40         private $repo;
41
42         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = [])
43         {
44                 parent::__construct($dba, $logger, $data);
45
46                 $this->repo = $repo;
47
48                 $this->setNameCache();
49                 $this->setMsgCache();
50         }
51
52         /**
53          * Sets the pre-formatted name (caching)
54          */
55         private function setNameCache()
56         {
57                 try {
58                         $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
59                 } catch (InternalServerErrorException $e) {
60                 }
61         }
62
63         /**
64          * Sets the pre-formatted msg (caching)
65          */
66         private function setMsgCache()
67         {
68                 try {
69                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
70                 } catch (InternalServerErrorException $e) {
71                 }
72         }
73
74         public function __set($name, $value)
75         {
76                 parent::__set($name, $value);
77
78                 if ($name == 'msg') {
79                         $this->setMsgCache();
80                 }
81
82                 if ($name == 'source_name') {
83                         $this->setNameCache();
84                 }
85         }
86
87         /**
88          * Formats a notification message with the notification author
89          *
90          * Replace the name with {0} but ensure to make that only once. The {0} is used
91          * later and prints the name in bold.
92          *
93          * @param string $name
94          * @param string $message
95          *
96          * @return string Formatted message
97          */
98         public static function formatMessage($name, $message)
99         {
100                 if ($name != '') {
101                         $pos = strpos($message, $name);
102                 } else {
103                         $pos = false;
104                 }
105
106                 if ($pos !== false) {
107                         $message = substr_replace($message, '{0}', $pos, strlen($name));
108                 }
109
110                 return $message;
111         }
112 }