]> git.mxchange.org Git - friendica.git/blob - src/Model/Notification.php
Merge pull request #10621 from tobiasd/2021.09-CHANGELOG
[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\Content\Text\Plaintext;
27 use Friendica\Core\Logger;
28 use Friendica\Database\Database;
29 use Friendica\DI;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Friendica\Protocol\Activity;
32 use Psr\Log\LoggerInterface;
33
34 /**
35  * Model for an entry in the notify table
36  *
37  * @property string  hash
38  * @property integer type
39  * @property string  name   Full name of the contact subject
40  * @property string  url    Profile page URL of the contact subject
41  * @property string  photo  Profile photo URL of the contact subject
42  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
43  * @property string  msg
44  * @property integer uid        Owner User Id
45  * @property string  link   Notification URL
46  * @property integer iid        Item Id
47  * @property integer parent Parent Item Id
48  * @property boolean seen   Whether the notification was read or not.
49  * @property string  verb   Verb URL (@see http://activitystrea.ms)
50  * @property string  otype  Subject type ('item', 'intro' or 'mail')
51  *
52  * @property-read string name_cache Full name of the contact subject
53  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
54  */
55 class Notification extends BaseModel
56 {
57         /** @var \Friendica\Repository\Notification */
58         private $repo;
59
60         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = [])
61         {
62                 parent::__construct($dba, $logger, $data);
63
64                 $this->repo = $repo;
65
66                 $this->setNameCache();
67                 $this->setMsgCache();
68         }
69
70         /**
71          * Sets the pre-formatted name (caching)
72          */
73         private function setNameCache()
74         {
75                 try {
76                         $this->name_cache = strip_tags(BBCode::convert($this->source_name));
77                 } catch (InternalServerErrorException $e) {
78                 }
79         }
80
81         /**
82          * Sets the pre-formatted msg (caching)
83          */
84         private function setMsgCache()
85         {
86                 try {
87                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
88                 } catch (InternalServerErrorException $e) {
89                 }
90         }
91
92         public function __set($name, $value)
93         {
94                 parent::__set($name, $value);
95
96                 if ($name == 'msg') {
97                         $this->setMsgCache();
98                 }
99
100                 if ($name == 'source_name') {
101                         $this->setNameCache();
102                 }
103         }
104
105         /**
106          * Formats a notification message with the notification author
107          *
108          * Replace the name with {0} but ensure to make that only once. The {0} is used
109          * later and prints the name in bold.
110          *
111          * @param string $name
112          * @param string $message
113          *
114          * @return string Formatted message
115          */
116         public static function formatMessage($name, $message)
117         {
118                 if ($name != '') {
119                         $pos = strpos($message, $name);
120                 } else {
121                         $pos = false;
122                 }
123
124                 if ($pos !== false) {
125                         $message = substr_replace($message, '{0}', $pos, strlen($name));
126                 }
127
128                 return $message;
129         }
130
131         /**
132          * Fetch the notification type for the given notification
133          *
134          * @param array $notification
135          * @return string
136          */
137         public static function getType(array $notification): string
138         {
139                 if (($notification['vid'] == Verb::getID(Activity::FOLLOW)) && ($notification['type'] == Post\UserNotification::NOTIF_NONE)) {
140                         $contact = Contact::getById($notification['actor-id'], ['pending']);
141                         $type = $contact['pending'] ? 'follow_request' : 'follow';
142                 } elseif (($notification['vid'] == Verb::getID(Activity::ANNOUNCE)) &&
143                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
144                         $type = 'reblog';
145                 } elseif (in_array($notification['vid'], [Verb::getID(Activity::LIKE), Verb::getID(Activity::DISLIKE)]) &&
146                         in_array($notification['type'], [Post\UserNotification::NOTIF_DIRECT_COMMENT, Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT])) {
147                         $type = 'favourite';
148                 } elseif ($notification['type'] == Post\UserNotification::NOTIF_SHARED) {
149                         $type = 'status';
150                 } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_EXPLICIT_TAGGED,
151                         Post\UserNotification::NOTIF_IMPLICIT_TAGGED, Post\UserNotification::NOTIF_DIRECT_COMMENT,
152                         Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT, Post\UserNotification::NOTIF_THREAD_COMMENT])) {
153                         $type = 'mention';
154                 } else {
155                         return '';
156                 }
157
158                 return $type;
159         }
160
161         /**
162          * Create a notification message for the given notification
163          *
164          * @param array $notification
165          * @return array with the elements "causer", "notification", "plain" and "rich"
166          */
167         public static function getMessage(array $notification)
168         {
169                 $message = [];
170
171                 $user = User::getById($notification['uid']);
172                 if (empty($user)) {
173                         Logger::info('User not found', ['application' => $notification['uid']]);
174                         return $message;
175                 }
176
177                 $l10n = DI::l10n()->withLang($user['language']);
178
179                 $causer = $contact = Contact::getById($notification['actor-id'], ['id', 'name', 'url', 'pending']);
180                 if (empty($contact)) {
181                         Logger::info('Contact not found', ['contact' => $notification['actor-id']]);
182                         return $message;
183                 }
184
185                 if ($notification['type'] == Post\UserNotification::NOTIF_NONE) {
186                         if ($contact['pending']) {
187                                 $msg = $l10n->t('%1$s wants to follow you');
188                         } else {
189                                 $msg = $l10n->t('%1$s had started following you');
190                         }
191                         $title = $contact['name'];
192                         $link = DI::baseUrl() . '/contact/' . $contact['id'];
193                 } else {
194                         if (empty($notification['target-uri-id'])) {
195                                 return $message;
196                         }
197
198                         $like     = Verb::getID(Activity::LIKE);
199                         $dislike  = Verb::getID(Activity::DISLIKE);
200                         $announce = Verb::getID(Activity::ANNOUNCE);
201                         $post     = Verb::getID(Activity::POST);
202
203                         if (in_array($notification['type'], [Post\UserNotification::NOTIF_THREAD_COMMENT, Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION, Post\UserNotification::NOTIF_EXPLICIT_TAGGED])) {
204                                 $item = Post::selectFirst([], ['uri-id' => $notification['parent-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
205                                 if (empty($item)) {
206                                         Logger::info('Parent post not found', ['uri-id' => $notification['parent-uri-id']]);
207                                         return $message;
208                                 }
209                         } else {
210                                 $item = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
211                                 if (empty($item)) {
212                                         Logger::info('Post not found', ['uri-id' => $notification['target-uri-id']]);
213                                         return $message;
214                                 }
215
216                                 if ($notification['vid'] == $post) {
217                                         $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $notification['uid']]], ['order' => ['uid' => true]]);
218                                         if (empty($item)) {
219                                                 Logger::info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]);
220                                                 return $message;
221                                         }
222                                 }
223                         }
224
225                         if ($item['owner-id'] != $item['author-id']) {
226                                 $cid = $item['owner-id'];
227                         }
228                         if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) {
229                                 $cid = $item['causer-id'];
230                         }
231
232                         if (($notification['type'] == Post\UserNotification::NOTIF_SHARED) && !empty($cid)) {
233                                 $causer = Contact::getById($cid, ['id', 'name', 'url']);
234                                 if (empty($contact)) {
235                                         Logger::info('Causer not found', ['causer' => $cid]);
236                                         return $message;
237                                 }
238                         } elseif (in_array($notification['type'], [Post\UserNotification::NOTIF_COMMENT_PARTICIPATION, Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION])) {
239                                 $contact = Contact::getById($item['author-id'], ['id', 'name', 'url']);
240                                 if (empty($contact)) {
241                                         Logger::info('Author not found', ['author' => $item['author-id']]);
242                                         return $message;
243                                 }
244                         }
245
246                         $link = DI::baseUrl() . '/display/' . urlencode($item['guid']);
247
248                         $content = Plaintext::getPost($item, 70);
249                         if (!empty($content['text'])) {
250                                 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
251                         } else {
252                                 $title = '';
253                         }
254
255                         switch ($notification['vid']) {
256                                 case $like:
257                                         switch ($notification['type']) {
258                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
259                                                         $msg = $l10n->t('%1$s liked your comment %2$s');
260                                                         break;
261                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
262                                                         $msg = $l10n->t('%1$s liked your post %2$s');
263                                                         break;
264                                                 }
265                                         break;
266                                 case $dislike:
267                                         switch ($notification['type']) {
268                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
269                                                         $msg = $l10n->t('%1$s disliked your comment %2$s');
270                                                         break;
271                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
272                                                         $msg = $l10n->t('%1$s disliked your post %2$s');
273                                                         break;
274                                         }
275                                         break;
276                                 case $announce:
277                                         switch ($notification['type']) {
278                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
279                                                         $msg = $l10n->t('%1$s shared your comment %2$s');
280                                                         break;
281                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
282                                                         $msg = $l10n->t('%1$s shared your post %2$s');
283                                                         break;
284                                                 }
285                                         break;
286                                 case $post:
287                                         switch ($notification['type']) {
288                                                 case Post\UserNotification::NOTIF_EXPLICIT_TAGGED:
289                                                         $msg = $l10n->t('%1$s tagged you on %2$s');
290                                                         break;
291
292                                                 case Post\UserNotification::NOTIF_IMPLICIT_TAGGED:
293                                                         $msg = $l10n->t('%1$s replied to you on %2$s');
294                                                         break;
295
296                                                 case Post\UserNotification::NOTIF_THREAD_COMMENT:
297                                                         $msg = $l10n->t('%1$s commented in your thread %2$s');
298                                                         break;
299
300                                                 case Post\UserNotification::NOTIF_DIRECT_COMMENT:
301                                                         $msg = $l10n->t('%1$s commented on your comment %2$s');
302                                                         break;
303
304                                                 case Post\UserNotification::NOTIF_COMMENT_PARTICIPATION:
305                                                 case Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION:
306                                                         if (($causer['id'] == $contact['id']) && ($title != '')) {
307                                                                 $msg = $l10n->t('%1$s commented in their thread %2$s');
308                                                         } elseif ($causer['id'] == $contact['id']) {
309                                                                 $msg = $l10n->t('%1$s commented in their thread');
310                                                         } elseif ($title != '') {
311                                                                 $msg = $l10n->t('%1$s commented in the thread %2$s from %3$s');
312                                                         } else {
313                                                                 $msg = $l10n->t('%1$s commented in the thread from %3$s');
314                                                         }
315                                                         break;
316
317                                                 case Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT:
318                                                         $msg = $l10n->t('%1$s commented on your thread %2$s');
319                                                         break;
320
321                                                 case Post\UserNotification::NOTIF_SHARED:
322                                                         if (($causer['id'] != $contact['id']) && ($title != '')) {
323                                                                 $msg = $l10n->t('%1$s shared the post %2$s from %3$s');
324                                                         } elseif ($causer['id'] != $contact['id']) {
325                                                                 $msg = $l10n->t('%1$s shared a post from %3$s');
326                                                         } elseif ($title != '') {
327                                                                 $msg = $l10n->t('%1$s shared the post %2$s');
328                                                         } else {
329                                                                 $msg = $l10n->t('%1$s shared a post');
330                                                         }
331                                                         break;
332                                         }
333                                         break;
334                         }
335                 }
336
337                 if (!empty($msg)) {
338                         // Name of the notification's causer
339                         $message['causer'] = $causer['name'];
340                         // Format for the "ping" mechanism
341                         $message['notification'] = sprintf($msg, '{0}', $title, $contact['name']);
342                         // Plain text for the web push api
343                         $message['plain']        = sprintf($msg, $causer['name'], $title, $contact['name']);
344                         // Rich text for other purposes
345                         $message['rich']         = sprintf($msg,
346                                 '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
347                                 '[url=' . $link . ']' . $title . '[/url]',
348                                 '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
349                 }
350
351                 return $message;
352         }
353 }