]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Merge branch 'subscription' of github.com:annando/friendica into subscription
[friendica.git] / src / Model / Notification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\BaseModel;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Database\Database;
27 use Friendica\Network\HTTPException\InternalServerErrorException;
28 use Friendica\Protocol\Activity;
29 use Psr\Log\LoggerInterface;
30
31 /**
32  * Model for an entry in the notify table
33  *
34  * @property string  hash
35  * @property integer type
36  * @property string  name   Full name of the contact subject
37  * @property string  url    Profile page URL of the contact subject
38  * @property string  photo  Profile photo URL of the contact subject
39  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
40  * @property string  msg
41  * @property integer uid        Owner User Id
42  * @property string  link   Notification URL
43  * @property integer iid        Item Id
44  * @property integer parent Parent Item Id
45  * @property boolean seen   Whether the notification was read or not.
46  * @property string  verb   Verb URL (@see http://activitystrea.ms)
47  * @property string  otype  Subject type ('item', 'intro' or 'mail')
48  *
49  * @property-read string name_cache Full name of the contact subject
50  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
51  */
52 class Notification extends BaseModel
53 {
54         /** @var \Friendica\Repository\Notification */
55         private $repo;
56
57         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
58         {
59                 parent::__construct($dba, $logger, $data);
60
61                 $this->repo = $repo;
62
63                 $this->setNameCache();
64                 $this->setMsgCache();
65         }
66
67         /**
68          * Sets the pre-formatted name (caching)
69          */
70         private function setNameCache()
71         {
72                 try {
73                         $this->name_cache = strip_tags(BBCode::convert($this->source_name));
74                 } catch (InternalServerErrorException $e) {
75                 }
76         }
77
78         /**
79          * Sets the pre-formatted msg (caching)
80          */
81         private function setMsgCache()
82         {
83                 try {
84                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
85                 } catch (InternalServerErrorException $e) {
86                 }
87         }
88
89         public function __set($name, $value)
90         {
91                 parent::__set($name, $value);
92
93                 if ($name == 'msg') {
94                         $this->setMsgCache();
95                 }
96
97                 if ($name == 'source_name') {
98                         $this->setNameCache();
99                 }
100         }
101
102         /**
103          * Formats a notification message with the notification author
104          *
105          * Replace the name with {0} but ensure to make that only once. The {0} is used
106          * later and prints the name in bold.
107          *
108          * @param string $name
109          * @param string $message
110          *
111          * @return string Formatted message
112          */
113         public static function formatMessage($name, $message)
114         {
115                 if ($name != '') {
116                         $pos = strpos($message, $name);
117                 } else {
118                         $pos = false;
119                 }
120
121                 if ($pos !== false) {
122                         $message = substr_replace($message, '{0}', $pos, strlen($name));
123                 }
124
125                 return $message;
126         }
127
128         /**
129          * Fetch the notification type for the given notification
130          *
131          * @param array $notification 
132          * @return string
133          */
134         public static function getType(array $notification): string
135         {
136                 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
137                         $contact = Contact::getById($notification['actor-id'], ['pending']);
138                         $contact['pending'] ? $type = 'follow_request' : 'follow';
139                 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
140                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
141                         $type = 'reblog';
142                 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
143                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
144                         $type = 'favourite';
145                 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
146                         $type = 'status';
147                 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
148                         Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
149                         Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
150                         $type = 'mention';
151                 } else {
152                         return '';
153                 }
154
155                 return $type;
156         }
157 }