3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\Plaintext;
24 use Friendica\Core\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Item;
32 use Friendica\Model\Notification;
33 use Friendica\Model\Post;
34 use Friendica\Model\User;
35 use Friendica\Protocol\Activity;
38 * Creates a notification entry and possibly sends a mail
40 * @param array $params Array with the elements:
41 * type, event, otype, activity, verb, uid, cid, origin_cid, item, link,
42 * source_name, source_mail, source_nick, source_link, source_photo,
43 * show_in_notification_page
46 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
48 function notification($params)
50 /** @var string the common prefix of a notification subject */
51 $subjectPrefix = DI::l10n()->t('[Friendica:Notify]');
53 // Temporary logging for finding the origin
54 if (!isset($params['uid'])) {
55 Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
58 // Ensure that the important fields are set at any time
59 $fields = ['nickname', 'page-flags', 'notify-flags', 'language', 'username', 'email'];
60 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
62 if (!DBA::isResult($user)) {
63 Logger::error('Unknown user', ['uid' => $params['uid']]);
67 // There is no need to create notifications for forum accounts
68 if (in_array($user['page-flags'], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
72 $nickname = $user['nickname'];
74 $params['notify_flags'] = $user['notify-flags'];
75 $params['language'] = $user['language'];
76 $params['to_name'] = $user['username'];
77 $params['to_email'] = $user['email'];
79 // from here on everything is in the recipients language
80 $l10n = DI::l10n()->withLang($params['language']);
82 if (!empty($params['cid'])) {
83 $contact = Contact::getById($params['cid'], ['url', 'name', 'photo']);
84 if (DBA::isResult($contact)) {
85 $params['source_link'] = $contact['url'];
86 $params['source_name'] = $contact['name'];
87 $params['source_photo'] = $contact['photo'];
91 if (!empty($params['origin_cid'])) {
92 $contact = Contact::getById($params['origin_cid'], ['url', 'name', 'photo']);
93 if (DBA::isResult($contact)) {
94 $params['origin_link'] = $contact['url'];
95 $params['origin_name'] = $contact['name'];
96 $params['origin_photo'] = $contact['photo'];
100 $siteurl = DI::baseUrl()->get(true);
101 $sitename = DI::config()->get('config', 'sitename');
103 $hostname = DI::baseUrl()->getHostname();
104 if (strpos($hostname, ':')) {
105 $hostname = substr($hostname, 0, strpos($hostname, ':'));
108 // Creates a new email builder for the notification email
109 $emailBuilder = DI::emailer()->newNotifyMail();
111 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
112 // the database, and an email is sent if applicable.
113 // default, if not specified: true
114 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
116 $emailBuilder->setHeader('X-Friendica-Account', '<' . $nickname . '@' . $hostname . '>');
118 $title = $params['item']['title'] ?? '';
119 $body = $params['item']['body'] ?? '';
121 $item_id = $params['item']['id'] ?? 0;
122 $uri_id = $params['item']['uri-id'] ?? 0;
123 $parent_id = $params['item']['parent'] ?? 0;
124 $parent_uri_id = $params['item']['parent-uri-id'] ?? 0;
134 if ($params['type'] == Notification\Type::MAIL) {
135 $itemlink = $params['link'];
137 $subject = $l10n->t('%s New mail received at %s', $subjectPrefix, $sitename);
139 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
140 $epreamble = $l10n->t('%1$s sent you %2$s.', '[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url=' . $itemlink . ']' . $l10n->t('a private message').'[/url]');
142 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
143 $tsitelink = sprintf($sitelink, $itemlink);
144 $hsitelink = sprintf($sitelink, '<a href="' . $itemlink . '">' . $sitename . '</a>');
146 // Mail notifications aren't using the "notify" table entry
147 $show_in_notification_page = false;
150 if ($params['type'] == Notification\Type::COMMENT || $params['type'] == Notification\Type::TAG_SELF) {
151 if (Post\ThreadUser::getIgnored($parent_uri_id, $params['uid'])) {
152 Logger::info('Thread is ignored', ['parent' => $parent_id, 'parent-uri-id' => $parent_uri_id]);
156 // Check to see if there was already a tag notify or comment notify for this post.
157 // If so don't create a second notification
158 /// @todo In the future we should store the notification with the highest "value" and replace notifications
159 $condition = ['type' => [Notification\Type::TAG_SELF, Notification\Type::COMMENT, Notification\Type::SHARE],
160 'link' => $params['link'], 'uid' => $params['uid']];
161 if (DBA::exists('notify', $condition)) {
165 // if it's a post figure out who's post it is.
167 if ($params['otype'] === Notification\ObjectType::ITEM && $parent_id) {
168 $item = Post::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
175 $item_post_type = Item::postType($item);
177 $content = Plaintext::getPost($item, 70);
178 if (!empty($content['text'])) {
179 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
184 // First go for the general message
186 // "George Bull's post"
187 if ($params['activity']['origin_comment']) {
188 $message = $l10n->t('%1$s replied to you on %2$s\'s %3$s %4$s');
189 } elseif ($params['activity']['explicit_tagged']) {
190 $message = $l10n->t('%1$s tagged you on %2$s\'s %3$s %4$s');
192 $message = $l10n->t('%1$s commented on %2$s\'s %3$s %4$s');
195 $dest_str = sprintf($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
197 // Then look for the special cases
200 if ($params['activity']['origin_thread']) {
201 if ($params['activity']['origin_comment']) {
202 $message = $l10n->t('%1$s replied to you on your %2$s %3$s');
203 } elseif ($params['activity']['explicit_tagged']) {
204 $message = $l10n->t('%1$s tagged you on your %2$s %3$s');
206 $message = $l10n->t('%1$s commented on your %2$s %3$s');
209 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
211 } elseif ($item['author-link'] == $params['source_link']) {
212 if ($params['activity']['origin_comment']) {
213 $message = $l10n->t('%1$s replied to you on their %2$s %3$s');
214 } elseif ($params['activity']['explicit_tagged']) {
215 $message = $l10n->t('%1$s tagged you on their %2$s %3$s');
217 $message = $l10n->t('%1$s commented on their %2$s %3$s');
220 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
223 // Some mail software relies on subject field for threading.
224 // So, we cannot have different subjects for notifications of the same thread.
225 // Before this we have the name of the replier on the subject rendering
226 // different subjects for messages on the same thread.
227 if ($params['activity']['explicit_tagged']) {
228 $subject = $l10n->t('%s %s tagged you', $subjectPrefix, $params['source_name']);
230 $preamble = $l10n->t('%1$s tagged you at %2$s', $params['source_name'], $sitename);
232 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $parent_id, $params['source_name']);
234 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
237 $epreamble = $dest_str;
239 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
240 $tsitelink = sprintf($sitelink, $siteurl);
241 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
242 $itemlink = $params['link'];
245 if ($params['type'] == Notification\Type::WALL) {
246 $subject = $l10n->t('%s %s posted to your profile wall', $subjectPrefix, $params['source_name']);
248 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
249 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
250 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
254 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
255 $tsitelink = sprintf($sitelink, $siteurl);
256 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
257 $itemlink = $params['link'];
260 if ($params['type'] == Notification\Type::SHARE) {
261 if ($params['origin_link'] == $params['source_link']) {
262 $subject = $l10n->t('%s %s shared a new post', $subjectPrefix, $params['source_name']);
264 $preamble = $l10n->t('%1$s shared a new post at %2$s', $params['source_name'], $sitename);
265 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url].',
266 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
270 $subject = $l10n->t('%s %s shared a post from %s', $subjectPrefix, $params['source_name'], $params['origin_name']);
272 $preamble = $l10n->t('%1$s shared a post from %2$s at %3$s', $params['source_name'], $params['origin_name'], $sitename);
273 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url] from %3$s.',
274 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
275 $params['link'], '[url='.$params['origin_link'].']'.$params['origin_name'].'[/url]'
279 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
280 $tsitelink = sprintf($sitelink, $siteurl);
281 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
282 $itemlink = $params['link'];
285 if ($params['type'] == Notification\Type::POKE) {
286 $subject = $l10n->t('%1$s %2$s poked you', $subjectPrefix, $params['source_name']);
288 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
289 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
290 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
294 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
295 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
296 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
298 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
299 $tsitelink = sprintf($sitelink, $siteurl);
300 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
301 $itemlink = $params['link'];
304 if ($params['type'] == Notification\Type::TAG_SHARE) {
305 $itemlink = $params['link'];
306 $subject = $l10n->t('%s %s tagged your post', $subjectPrefix, $params['source_name']);
308 $preamble = $l10n->t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
309 $epreamble = $l10n->t('%1$s tagged [url=%2$s]your post[/url]',
310 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
314 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
315 $tsitelink = sprintf($sitelink, $siteurl);
316 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
319 if ($params['type'] == Notification\Type::INTRO) {
320 $itemlink = $params['link'];
321 $subject = $l10n->t('%s Introduction received', $subjectPrefix);
323 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
324 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
326 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
329 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
331 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
332 $tsitelink = sprintf($sitelink, $siteurl);
333 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
335 switch ($params['verb']) {
336 case Activity::FRIEND:
337 // someone started to share with user (mostly OStatus)
338 $subject = $l10n->t('%s A new person is sharing with you', $subjectPrefix);
340 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
341 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
342 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
346 case Activity::FOLLOW:
347 // someone started to follow the user (mostly OStatus)
348 $subject = $l10n->t('%s You have a new follower', $subjectPrefix);
350 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
351 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
352 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
357 // ACTIVITY_REQ_FRIEND is default activity for notifications
362 if ($params['type'] == Notification\Type::SUGGEST) {
363 $itemlink = $params['link'];
364 $subject = $l10n->t('%s Friend suggestion received', $subjectPrefix);
366 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
367 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
369 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
370 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
373 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
374 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
375 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
377 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
378 $tsitelink = sprintf($sitelink, $siteurl);
379 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
382 if ($params['type'] == Notification\Type::CONFIRM) {
383 if ($params['verb'] == Activity::FRIEND) { // mutual connection
384 $itemlink = $params['link'];
385 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
387 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
388 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
390 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
393 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
395 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
396 $tsitelink = sprintf($sitelink, $siteurl);
397 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
398 } else { // ACTIVITY_FOLLOW
399 $itemlink = $params['link'];
400 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
402 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
403 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
405 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
408 $body = $l10n->t('\'%1$s\' has chosen to accept you a fan, which restricts some forms of communication - such as private messaging and some profile interactions. If this is a celebrity or community page, these settings were applied automatically.', $params['source_name']);
410 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
412 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
413 $tsitelink = sprintf($sitelink, $siteurl);
414 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
418 if ($params['type'] == Notification\Type::SYSTEM) {
419 switch($params['event']) {
420 case "SYSTEM_REGISTER_REQUEST":
421 $itemlink = $params['link'];
422 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
424 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
425 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
427 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
430 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
431 $params['source_name'],
432 $siteurl, $params['source_mail'],
433 $params['source_nick']
436 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
437 $tsitelink = sprintf($sitelink, $params['link']);
438 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
440 case "SYSTEM_DB_UPDATE_FAIL":
445 $subject .= " (".$nickname."@".$hostname.")";
449 'subject' => $subject,
450 'preamble' => $preamble,
451 'epreamble' => $epreamble,
453 'sitelink' => $sitelink,
454 'tsitelink' => $tsitelink,
455 'hsitelink' => $hsitelink,
456 'itemlink' => $itemlink
459 Hook::callAll('enotify', $h);
461 $subject = $h['subject'];
463 $preamble = $h['preamble'];
464 $epreamble = $h['epreamble'];
468 $tsitelink = $h['tsitelink'];
469 $hsitelink = $h['hsitelink'];
470 $itemlink = $h['itemlink'];
474 if ($show_in_notification_page) {
476 'name' => $params['source_name'] ?? '',
477 'name_cache' => substr(strip_tags(BBCode::convert($params['source_name'])), 0, 255),
478 'url' => $params['source_link'] ?? '',
479 'photo' => $params['source_photo'] ?? '',
480 'link' => $itemlink ?? '',
481 'uid' => $params['uid'] ?? 0,
482 'type' => $params['type'] ?? '',
483 'verb' => $params['verb'] ?? '',
484 'otype' => $params['otype'] ?? '',
486 if (!empty($item_id)) {
487 $fields['iid'] = $item_id;
489 if (!empty($uri_id)) {
490 $fields['uri-id'] = $uri_id;
492 if (!empty($parent_id)) {
493 $fields['parent'] = $parent_id;
495 if (!empty($parent_uri_id)) {
496 $fields['parent-uri-id'] = $parent_uri_id;
498 $notification = DI::notify()->insert($fields);
500 // Notification insertion can be intercepted by an addon registering the 'enotify_store' hook
501 if (!$notification) {
505 $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]);
507 DI::notify()->update($notification);
509 $itemlink = DI::baseUrl() . '/notification/' . $notification->id;
510 $notify_id = $notification->id;
513 // send email notification if notification preferences permit
514 if ((intval($params['notify_flags']) & intval($params['type']))
515 || $params['type'] == Notification\Type::SYSTEM) {
517 Logger::log('sending notification email');
519 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
520 $parent = Post::selectFirst(['guid'], ['id' => $params['parent']]);
521 $message_id = "<" . $parent['guid'] . "@" . gethostname() . ">";
523 // Is this the first email notification for this parent item and user?
524 if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
525 Logger::log("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']), Logger::DEBUG);
527 $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'],
528 'master-parent-uri-id' => $parent_uri_id,
529 'receiver-uid' => $params['uid'], 'parent-item' => 0];
530 DBA::insert('notify-threads', $fields);
532 $emailBuilder->setHeader('Message-ID', $message_id);
533 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
534 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
535 Logger::log($log_msg, Logger::DEBUG);
537 // If not, just "follow" the thread.
538 $emailBuilder->setHeader('References', $message_id);
539 $emailBuilder->setHeader('In-Reply-To', $message_id);
540 Logger::log("There's already a notification for this parent.", Logger::DEBUG);
545 'preamble' => $preamble,
546 'type' => $params['type'],
547 'parent' => $parent_id,
548 'source_name' => $params['source_name'] ?? null,
549 'source_link' => $params['source_link'] ?? null,
550 'source_photo' => $params['source_photo'] ?? null,
551 'uid' => $params['uid'],
552 'hsitelink' => $hsitelink,
553 'tsitelink' => $tsitelink,
554 'itemlink' => $itemlink,
557 'subject' => $subject,
558 'headers' => $emailBuilder->getHeaders(),
561 Hook::callAll('enotify_mail', $datarray);
564 ->withHeaders($datarray['headers'])
565 ->withRecipient($params['to_email'])
567 'uid' => $datarray['uid'],
568 'language' => $params['language'],
570 ->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body'])
571 ->withSiteLink($datarray['tsitelink'], $datarray['hsitelink'])
572 ->withItemLink($datarray['itemlink']);
574 // If a photo is present, add it to the email
575 if (!empty($datarray['source_photo'])) {
576 $emailBuilder->withPhoto(
577 $datarray['source_photo'],
578 $datarray['source_link'] ?? $sitelink,
579 $datarray['source_name'] ?? $sitename);
582 $email = $emailBuilder->build();
584 // use the Emailer class to send the message
585 return DI::emailer()->send($email);
592 * Checks for users who should be notified
594 * @param int $uri_id URI ID of the item for which the check should be done
595 * @param int $uid User ID of the item
596 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
598 function check_user_notification(int $uri_id, int $uid) {
599 $condition = ['uri-id' => $uri_id];
601 // fetch all users with notifications on public posts
603 $condition['uid'] = $uid;
606 $usernotifications = DBA::select('post-user-notification', ['uri-id', 'uid', 'notification-type'], $condition);
607 while ($usernotification = DBA::fetch($usernotifications)) {
608 check_item_notification($usernotification['uri-id'], $usernotification['uid'], $usernotification['notification-type']);
610 DBA::close($usernotifications);
614 * Checks for item related notifications and sends them
616 * @param int $uri_id URI ID of the item for which the check should be done
617 * @param int $uid User ID
618 * @param int $notification_type Notification bits
620 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
622 function check_item_notification(int $uri_id, int $uid, int $notification_type) {
623 $fields = ['id', 'uri-id', 'mention', 'parent', 'parent-uri-id', 'thr-parent-id',
624 'title', 'body', 'author-link', 'author-name', 'author-avatar', 'author-id',
625 'gravity', 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
626 $condition = ['uri-id' => $uri_id, 'uid' => $uid, 'deleted' => false];
627 $item = Post::selectFirstForUser($uid, $fields, $condition);
628 if (!DBA::isResult($item)) {
632 if (!DI::pConfig()->get(local_user(), 'system', 'notify_ignored', true) && Contact\User::isIgnored($item['author-id'], $uid)) {
633 Logger::info('Author is ignored, dropping notification', ['cid' => $item['author-id'], 'uid' => $uid]);
637 // Generate the notification array
639 $params['otype'] = Notification\ObjectType::ITEM;
640 $params['uid'] = $uid;
641 $params['origin_cid'] = $params['cid'] = $item['author-id'];
642 $params['item'] = $item;
643 $params['link'] = DI::baseUrl() . '/display/' . urlencode($item['guid']);
645 // Set the activity flags
646 $params['activity']['explicit_tagged'] = ($notification_type & Post\UserNotification::NOTIF_EXPLICIT_TAGGED);
647 $params['activity']['implicit_tagged'] = ($notification_type & Post\UserNotification::NOTIF_IMPLICIT_TAGGED);
648 $params['activity']['origin_comment'] = ($notification_type & Post\UserNotification::NOTIF_DIRECT_COMMENT);
649 $params['activity']['origin_thread'] = ($notification_type & Post\UserNotification::NOTIF_THREAD_COMMENT);
650 $params['activity']['thread_comment'] = ($notification_type & Post\UserNotification::NOTIF_COMMENT_PARTICIPATION);
651 $params['activity']['thread_activity'] = ($notification_type & Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION);
653 // Tagging a user in a direct post (first comment level) means a direct comment
654 if ($params['activity']['explicit_tagged'] && ($notification_type & Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT)) {
655 $params['activity']['origin_comment'] = true;
658 if ($notification_type & Post\UserNotification::NOTIF_SHARED) {
659 $params['type'] = Notification\Type::SHARE;
660 $params['verb'] = Activity::POST;
662 // Special treatment for posts that had been shared via "announce"
663 if ($item['gravity'] == GRAVITY_ACTIVITY) {
664 $parent_item = Post::selectFirst($fields, ['uri-id' => $item['thr-parent-id'], 'uid' => [$uid, 0]]);
665 if (DBA::isResult($parent_item)) {
666 // Don't notify on own entries
667 if (User::getIdForURL($parent_item['author-link']) == $uid) {
671 $params['origin_cid'] = $parent_item['author-id'];
672 $params['item'] = $parent_item;
675 } elseif ($notification_type & Post\UserNotification::NOTIF_EXPLICIT_TAGGED) {
676 $params['type'] = Notification\Type::TAG_SELF;
677 $params['verb'] = Activity::TAG;
678 } elseif ($notification_type & Post\UserNotification::NOTIF_IMPLICIT_TAGGED) {
679 $params['type'] = Notification\Type::COMMENT;
680 $params['verb'] = Activity::POST;
681 } elseif ($notification_type & Post\UserNotification::NOTIF_THREAD_COMMENT) {
682 $params['type'] = Notification\Type::COMMENT;
683 $params['verb'] = Activity::POST;
684 } elseif ($notification_type & Post\UserNotification::NOTIF_DIRECT_COMMENT) {
685 $params['type'] = Notification\Type::COMMENT;
686 $params['verb'] = Activity::POST;
687 } elseif ($notification_type & Post\UserNotification::NOTIF_COMMENT_PARTICIPATION) {
688 $params['type'] = Notification\Type::COMMENT;
689 $params['verb'] = Activity::POST;
690 } elseif ($notification_type & Post\UserNotification::NOTIF_ACTIVITY_PARTICIPATION) {
691 $params['type'] = Notification\Type::COMMENT;
692 $params['verb'] = Activity::POST;
697 notification($params);