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\Model\UserItem;
36 use Friendica\Protocol\Activity;
39 * Creates a notification entry and possibly sends a mail
41 * @param array $params Array with the elements:
42 * type, event, otype, activity, verb, uid, cid, origin_cid, item, link,
43 * source_name, source_mail, source_nick, source_link, source_photo,
44 * show_in_notification_page
47 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49 function notification($params)
51 /** @var string the common prefix of a notification subject */
52 $subjectPrefix = DI::l10n()->t('[Friendica:Notify]');
54 // Temporary logging for finding the origin
55 if (!isset($params['uid'])) {
56 Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
59 // Ensure that the important fields are set at any time
60 $fields = ['nickname', 'page-flags', 'notify-flags', 'language', 'username', 'email'];
61 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
63 if (!DBA::isResult($user)) {
64 Logger::error('Unknown user', ['uid' => $params['uid']]);
68 // There is no need to create notifications for forum accounts
69 if (in_array($user['page-flags'], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
73 $nickname = $user['nickname'];
75 $params['notify_flags'] = $user['notify-flags'];
76 $params['language'] = $user['language'];
77 $params['to_name'] = $user['username'];
78 $params['to_email'] = $user['email'];
80 // from here on everything is in the recipients language
81 $l10n = DI::l10n()->withLang($params['language']);
83 if (!empty($params['cid'])) {
84 $contact = Contact::getById($params['cid'], ['url', 'name', 'photo']);
85 if (DBA::isResult($contact)) {
86 $params['source_link'] = $contact['url'];
87 $params['source_name'] = $contact['name'];
88 $params['source_photo'] = $contact['photo'];
92 if (!empty($params['origin_cid'])) {
93 $contact = Contact::getById($params['origin_cid'], ['url', 'name', 'photo']);
94 if (DBA::isResult($contact)) {
95 $params['origin_link'] = $contact['url'];
96 $params['origin_name'] = $contact['name'];
97 $params['origin_photo'] = $contact['photo'];
101 $siteurl = DI::baseUrl()->get(true);
102 $sitename = DI::config()->get('config', 'sitename');
104 $hostname = DI::baseUrl()->getHostname();
105 if (strpos($hostname, ':')) {
106 $hostname = substr($hostname, 0, strpos($hostname, ':'));
109 // Creates a new email builder for the notification email
110 $emailBuilder = DI::emailer()->newNotifyMail();
112 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
113 // the database, and an email is sent if applicable.
114 // default, if not specified: true
115 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
117 $emailBuilder->setHeader('X-Friendica-Account', '<' . $nickname . '@' . $hostname . '>');
119 $title = $params['item']['title'] ?? '';
120 $body = $params['item']['body'] ?? '';
122 $item_id = $params['item']['id'] ?? 0;
123 $uri_id = $params['item']['uri-id'] ?? 0;
124 $parent_id = $params['item']['parent'] ?? 0;
125 $parent_uri_id = $params['item']['parent-uri-id'] ?? 0;
135 if ($params['type'] == Notification\Type::MAIL) {
136 $itemlink = $params['link'];
138 $subject = $l10n->t('%s New mail received at %s', $subjectPrefix, $sitename);
140 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
141 $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]');
143 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
144 $tsitelink = sprintf($sitelink, $itemlink);
145 $hsitelink = sprintf($sitelink, '<a href="' . $itemlink . '">' . $sitename . '</a>');
147 // Mail notifications aren't using the "notify" table entry
148 $show_in_notification_page = false;
151 if ($params['type'] == Notification\Type::COMMENT || $params['type'] == Notification\Type::TAG_SELF) {
152 $thread = Post::selectFirstThreadForUser($params['uid'], ['ignored'], ['iid' => $parent_id, 'deleted' => false]);
153 if (DBA::isResult($thread) && $thread['ignored']) {
154 Logger::log('Thread ' . $parent_id . ' will be ignored', Logger::DEBUG);
158 // Check to see if there was already a tag notify or comment notify for this post.
159 // If so don't create a second notification
160 /// @todo In the future we should store the notification with the highest "value" and replace notifications
161 $condition = ['type' => [Notification\Type::TAG_SELF, Notification\Type::COMMENT, Notification\Type::SHARE],
162 'link' => $params['link'], 'uid' => $params['uid']];
163 if (DBA::exists('notify', $condition)) {
167 // if it's a post figure out who's post it is.
169 if ($params['otype'] === Notification\ObjectType::ITEM && $parent_id) {
170 $item = Post::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
177 $item_post_type = Item::postType($item);
179 $content = Plaintext::getPost($item, 70);
180 if (!empty($content['text'])) {
181 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
186 // First go for the general message
188 // "George Bull's post"
189 if ($params['activity']['origin_comment']) {
190 $message = $l10n->t('%1$s replied to you on %2$s\'s %3$s %4$s');
191 } elseif ($params['activity']['explicit_tagged']) {
192 $message = $l10n->t('%1$s tagged you on %2$s\'s %3$s %4$s');
194 $message = $l10n->t('%1$s commented on %2$s\'s %3$s %4$s');
197 $dest_str = sprintf($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
199 // Then look for the special cases
202 if ($params['activity']['origin_thread']) {
203 if ($params['activity']['origin_comment']) {
204 $message = $l10n->t('%1$s replied to you on your %2$s %3$s');
205 } elseif ($params['activity']['explicit_tagged']) {
206 $message = $l10n->t('%1$s tagged you on your %2$s %3$s');
208 $message = $l10n->t('%1$s commented on your %2$s %3$s');
211 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
213 } elseif ($item['author-link'] == $params['source_link']) {
214 if ($params['activity']['origin_comment']) {
215 $message = $l10n->t('%1$s replied to you on their %2$s %3$s');
216 } elseif ($params['activity']['explicit_tagged']) {
217 $message = $l10n->t('%1$s tagged you on their %2$s %3$s');
219 $message = $l10n->t('%1$s commented on their %2$s %3$s');
222 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
225 // Some mail software relies on subject field for threading.
226 // So, we cannot have different subjects for notifications of the same thread.
227 // Before this we have the name of the replier on the subject rendering
228 // different subjects for messages on the same thread.
229 if ($params['activity']['explicit_tagged']) {
230 $subject = $l10n->t('%s %s tagged you', $subjectPrefix, $params['source_name']);
232 $preamble = $l10n->t('%1$s tagged you at %2$s', $params['source_name'], $sitename);
234 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $parent_id, $params['source_name']);
236 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
239 $epreamble = $dest_str;
241 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
242 $tsitelink = sprintf($sitelink, $siteurl);
243 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
244 $itemlink = $params['link'];
247 if ($params['type'] == Notification\Type::WALL) {
248 $subject = $l10n->t('%s %s posted to your profile wall', $subjectPrefix, $params['source_name']);
250 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
251 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
252 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
256 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
257 $tsitelink = sprintf($sitelink, $siteurl);
258 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
259 $itemlink = $params['link'];
262 if ($params['type'] == Notification\Type::SHARE) {
263 if ($params['origin_link'] == $params['source_link']) {
264 $subject = $l10n->t('%s %s shared a new post', $subjectPrefix, $params['source_name']);
266 $preamble = $l10n->t('%1$s shared a new post at %2$s', $params['source_name'], $sitename);
267 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url].',
268 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
272 $subject = $l10n->t('%s %s shared a post from %s', $subjectPrefix, $params['source_name'], $params['origin_name']);
274 $preamble = $l10n->t('%1$s shared a post from %2$s at %3$s', $params['source_name'], $params['origin_name'], $sitename);
275 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url] from %3$s.',
276 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
277 $params['link'], '[url='.$params['origin_link'].']'.$params['origin_name'].'[/url]'
281 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
282 $tsitelink = sprintf($sitelink, $siteurl);
283 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
284 $itemlink = $params['link'];
287 if ($params['type'] == Notification\Type::POKE) {
288 $subject = $l10n->t('%1$s %2$s poked you', $subjectPrefix, $params['source_name']);
290 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
291 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
292 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
296 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
297 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
298 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
300 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
301 $tsitelink = sprintf($sitelink, $siteurl);
302 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
303 $itemlink = $params['link'];
306 if ($params['type'] == Notification\Type::TAG_SHARE) {
307 $itemlink = $params['link'];
308 $subject = $l10n->t('%s %s tagged your post', $subjectPrefix, $params['source_name']);
310 $preamble = $l10n->t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
311 $epreamble = $l10n->t('%1$s tagged [url=%2$s]your post[/url]',
312 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
316 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
317 $tsitelink = sprintf($sitelink, $siteurl);
318 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
321 if ($params['type'] == Notification\Type::INTRO) {
322 $itemlink = $params['link'];
323 $subject = $l10n->t('%s Introduction received', $subjectPrefix);
325 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
326 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
328 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
331 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
333 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
334 $tsitelink = sprintf($sitelink, $siteurl);
335 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
337 switch ($params['verb']) {
338 case Activity::FRIEND:
339 // someone started to share with user (mostly OStatus)
340 $subject = $l10n->t('%s A new person is sharing with you', $subjectPrefix);
342 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
343 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
344 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
348 case Activity::FOLLOW:
349 // someone started to follow the user (mostly OStatus)
350 $subject = $l10n->t('%s You have a new follower', $subjectPrefix);
352 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
353 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
354 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
359 // ACTIVITY_REQ_FRIEND is default activity for notifications
364 if ($params['type'] == Notification\Type::SUGGEST) {
365 $itemlink = $params['link'];
366 $subject = $l10n->t('%s Friend suggestion received', $subjectPrefix);
368 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
369 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
371 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
372 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
375 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
376 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
377 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
379 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
380 $tsitelink = sprintf($sitelink, $siteurl);
381 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
384 if ($params['type'] == Notification\Type::CONFIRM) {
385 if ($params['verb'] == Activity::FRIEND) { // mutual connection
386 $itemlink = $params['link'];
387 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
389 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
390 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
392 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
395 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
397 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
398 $tsitelink = sprintf($sitelink, $siteurl);
399 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
400 } else { // ACTIVITY_FOLLOW
401 $itemlink = $params['link'];
402 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
404 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
405 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
407 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
410 $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']);
412 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
414 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
415 $tsitelink = sprintf($sitelink, $siteurl);
416 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
420 if ($params['type'] == Notification\Type::SYSTEM) {
421 switch($params['event']) {
422 case "SYSTEM_REGISTER_REQUEST":
423 $itemlink = $params['link'];
424 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
426 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
427 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
429 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
432 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
433 $params['source_name'],
434 $siteurl, $params['source_mail'],
435 $params['source_nick']
438 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
439 $tsitelink = sprintf($sitelink, $params['link']);
440 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
442 case "SYSTEM_DB_UPDATE_FAIL":
447 $subject .= " (".$nickname."@".$hostname.")";
451 'subject' => $subject,
452 'preamble' => $preamble,
453 'epreamble' => $epreamble,
455 'sitelink' => $sitelink,
456 'tsitelink' => $tsitelink,
457 'hsitelink' => $hsitelink,
458 'itemlink' => $itemlink
461 Hook::callAll('enotify', $h);
463 $subject = $h['subject'];
465 $preamble = $h['preamble'];
466 $epreamble = $h['epreamble'];
470 $tsitelink = $h['tsitelink'];
471 $hsitelink = $h['hsitelink'];
472 $itemlink = $h['itemlink'];
476 if ($show_in_notification_page) {
478 'name' => $params['source_name'] ?? '',
479 'name_cache' => substr(strip_tags(BBCode::convert($params['source_name'])), 0, 255),
480 'url' => $params['source_link'] ?? '',
481 'photo' => $params['source_photo'] ?? '',
482 'link' => $itemlink ?? '',
483 'uid' => $params['uid'] ?? 0,
484 'type' => $params['type'] ?? '',
485 'verb' => $params['verb'] ?? '',
486 'otype' => $params['otype'] ?? '',
488 if (!empty($item_id)) {
489 $fields['iid'] = $item_id;
491 if (!empty($uri_id)) {
492 $fields['uri-id'] = $uri_id;
494 if (!empty($parent_id)) {
495 $fields['parent'] = $parent_id;
497 if (!empty($parent_uri_id)) {
498 $fields['parent-uri-id'] = $parent_uri_id;
500 $notification = DI::notify()->insert($fields);
502 // Notification insertion can be intercepted by an addon registering the 'enotify_store' hook
503 if (!$notification) {
507 $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]);
509 DI::notify()->update($notification);
511 $itemlink = DI::baseUrl() . '/notification/' . $notification->id;
512 $notify_id = $notification->id;
515 // send email notification if notification preferences permit
516 if ((intval($params['notify_flags']) & intval($params['type']))
517 || $params['type'] == Notification\Type::SYSTEM) {
519 Logger::log('sending notification email');
521 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
522 $parent = Post::selectFirst(['guid'], ['id' => $params['parent']]);
523 $message_id = "<" . $parent['guid'] . "@" . gethostname() . ">";
525 // Is this the first email notification for this parent item and user?
526 if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
527 Logger::log("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']), Logger::DEBUG);
529 $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'],
530 'master-parent-uri-id' => $parent_uri_id,
531 'receiver-uid' => $params['uid'], 'parent-item' => 0];
532 DBA::insert('notify-threads', $fields);
534 $emailBuilder->setHeader('Message-ID', $message_id);
535 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
536 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
537 Logger::log($log_msg, Logger::DEBUG);
539 // If not, just "follow" the thread.
540 $emailBuilder->setHeader('References', $message_id);
541 $emailBuilder->setHeader('In-Reply-To', $message_id);
542 Logger::log("There's already a notification for this parent.", Logger::DEBUG);
547 'preamble' => $preamble,
548 'type' => $params['type'],
549 'parent' => $parent_id,
550 'source_name' => $params['source_name'] ?? null,
551 'source_link' => $params['source_link'] ?? null,
552 'source_photo' => $params['source_photo'] ?? null,
553 'uid' => $params['uid'],
554 'hsitelink' => $hsitelink,
555 'tsitelink' => $tsitelink,
556 'itemlink' => $itemlink,
559 'subject' => $subject,
560 'headers' => $emailBuilder->getHeaders(),
563 Hook::callAll('enotify_mail', $datarray);
566 ->withHeaders($datarray['headers'])
567 ->withRecipient($params['to_email'])
569 'uid' => $datarray['uid'],
570 'language' => $params['language'],
572 ->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body'])
573 ->withSiteLink($datarray['tsitelink'], $datarray['hsitelink'])
574 ->withItemLink($datarray['itemlink']);
576 // If a photo is present, add it to the email
577 if (!empty($datarray['source_photo'])) {
578 $emailBuilder->withPhoto(
579 $datarray['source_photo'],
580 $datarray['source_link'] ?? $sitelink,
581 $datarray['source_name'] ?? $sitename);
584 $email = $emailBuilder->build();
586 // use the Emailer class to send the message
587 return DI::emailer()->send($email);
594 * Checks for users who should be notified
596 * @param int $itemid ID of the item for which the check should be done
597 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
599 function check_user_notification($itemid) {
600 // fetch all users with notifications
601 $useritems = DBA::select('user-item', ['uid', 'notification-type'], ['iid' => $itemid]);
602 while ($useritem = DBA::fetch($useritems)) {
603 check_item_notification($itemid, $useritem['uid'], $useritem['notification-type']);
605 DBA::close($useritems);
609 * Checks for item related notifications and sends them
611 * @param int $itemid ID of the item for which the check should be done
612 * @param int $uid User ID
613 * @param int $notification_type Notification bits
615 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
617 function check_item_notification($itemid, $uid, $notification_type) {
618 $fields = ['id', 'uri-id', 'mention', 'parent', 'parent-uri-id', 'thr-parent-id',
619 'title', 'body', 'author-link', 'author-name', 'author-avatar', 'author-id',
620 'gravity', 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
621 $condition = ['id' => $itemid, 'deleted' => false];
622 $item = Post::selectFirstForUser($uid, $fields, $condition);
623 if (!DBA::isResult($item)) {
627 if (!DI::pConfig()->get(local_user(), 'system', 'notify_ignored', true) && Contact\User::isIgnored($item['author-id'], $uid)) {
628 Logger::info('Author is ignored, dropping notification', ['cid' => $item['author-id'], 'uid' => $uid]);
632 // Generate the notification array
634 $params['otype'] = Notification\ObjectType::ITEM;
635 $params['uid'] = $uid;
636 $params['origin_cid'] = $params['cid'] = $item['author-id'];
637 $params['item'] = $item;
638 $params['link'] = DI::baseUrl() . '/display/' . urlencode($item['guid']);
640 // Set the activity flags
641 $params['activity']['explicit_tagged'] = ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED);
642 $params['activity']['implicit_tagged'] = ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED);
643 $params['activity']['origin_comment'] = ($notification_type & UserItem::NOTIF_DIRECT_COMMENT);
644 $params['activity']['origin_thread'] = ($notification_type & UserItem::NOTIF_THREAD_COMMENT);
645 $params['activity']['thread_comment'] = ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION);
646 $params['activity']['thread_activity'] = ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION);
648 // Tagging a user in a direct post (first comment level) means a direct comment
649 if ($params['activity']['explicit_tagged'] && ($notification_type & UserItem::NOTIF_DIRECT_THREAD_COMMENT)) {
650 $params['activity']['origin_comment'] = true;
653 if ($notification_type & UserItem::NOTIF_SHARED) {
654 $params['type'] = Notification\Type::SHARE;
655 $params['verb'] = Activity::POST;
657 // Special treatment for posts that had been shared via "announce"
658 if ($item['gravity'] == GRAVITY_ACTIVITY) {
659 $parent_item = Post::selectFirst($fields, ['uri-id' => $item['thr-parent-id'], 'uid' => [$uid, 0]]);
660 if (DBA::isResult($parent_item)) {
661 // Don't notify on own entries
662 if (User::getIdForURL($parent_item['author-link']) == $uid) {
666 $params['origin_cid'] = $parent_item['author-id'];
667 $params['item'] = $parent_item;
670 } elseif ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED) {
671 $params['type'] = Notification\Type::TAG_SELF;
672 $params['verb'] = Activity::TAG;
673 } elseif ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED) {
674 $params['type'] = Notification\Type::COMMENT;
675 $params['verb'] = Activity::POST;
676 } elseif ($notification_type & UserItem::NOTIF_THREAD_COMMENT) {
677 $params['type'] = Notification\Type::COMMENT;
678 $params['verb'] = Activity::POST;
679 } elseif ($notification_type & UserItem::NOTIF_DIRECT_COMMENT) {
680 $params['type'] = Notification\Type::COMMENT;
681 $params['verb'] = Activity::POST;
682 } elseif ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION) {
683 $params['type'] = Notification\Type::COMMENT;
684 $params['verb'] = Activity::POST;
685 } elseif ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION) {
686 $params['type'] = Notification\Type::COMMENT;
687 $params['verb'] = Activity::POST;
692 notification($params);