3 * @file include/enotify.php
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
13 use Friendica\Model\Item;
14 use Friendica\Model\ItemContent;
15 use Friendica\Model\Notify;
16 use Friendica\Model\User;
17 use Friendica\Model\UserItem;
18 use Friendica\Object\EMail;
19 use Friendica\Protocol\Activity;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Emailer;
24 * Creates a notification entry and possibly sends a mail
26 * @param array $params Array with the elements:
27 * uid, item, parent, type, otype, verb, event,
28 * link, subject, body, to_name, to_email, source_name,
29 * source_link, activity, preamble, notify_flags,
30 * language, show_in_notification_page
32 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
34 function notification($params)
38 // Temporary logging for finding the origin
39 if (!isset($params['uid'])) {
40 Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
43 // Ensure that the important fields are set at any time
44 $fields = ['notify-flags', 'language', 'username', 'email'];
45 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
47 if (!DBA::isResult($user)) {
48 Logger::error('Unknown user', ['uid' => $params['uid']]);
52 $params['notify_flags'] = ($params['notify_flags'] ?? '') ?: $user['notify-flags'];
53 $params['language'] = ($params['language'] ?? '') ?: $user['language'];
54 $params['to_name'] = ($params['to_name'] ?? '') ?: $user['username'];
55 $params['to_email'] = ($params['to_email'] ?? '') ?: $user['email'];
57 // from here on everything is in the recipients language
58 $l10n = DI::l10n()->withLang($params['language']);
60 $banner = $l10n->t('Friendica Notification');
61 $product = FRIENDICA_PLATFORM;
62 $siteurl = DI::baseUrl()->get(true);
63 $thanks = $l10n->t('Thank You,');
64 $sitename = DI::config()->get('config', 'sitename');
65 if (DI::config()->get('config', 'admin_name')) {
66 $site_admin = $l10n->t('%1$s, %2$s Administrator', DI::config()->get('config', 'admin_name'), $sitename);
68 $site_admin = $l10n->t('%s Administrator', $sitename);
71 $sender_name = $sitename;
72 $hostname = DI::baseUrl()->getHostname();
73 if (strpos($hostname, ':')) {
74 $hostname = substr($hostname, 0, strpos($hostname, ':'));
77 $sender_email = $a->getSenderEmailAddress();
79 if ($params['type'] != SYSTEM_EMAIL) {
80 $user = DBA::selectFirst('user', ['nickname', 'page-flags'],
81 ['uid' => $params['uid']]);
83 // There is no need to create notifications for forum accounts
84 if (!DBA::isResult($user) || in_array($user["page-flags"], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
87 $nickname = $user["nickname"];
92 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
93 // the database, and an email is sent if applicable.
94 // default, if not specified: true
95 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
97 $additional_mail_header = "";
98 $additional_mail_header .= "Precedence: list\n";
99 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
100 $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
101 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
102 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
103 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
104 $additional_mail_header .= "List-Archive: <".DI::baseUrl()."/notifications/system>\n";
106 if (array_key_exists('item', $params)) {
107 $title = $params['item']['title'];
108 $body = $params['item']['body'];
113 if (isset($params['item']['id'])) {
114 $item_id = $params['item']['id'];
119 if (isset($params['parent'])) {
120 $parent_id = $params['parent'];
133 if ($params['type'] == NOTIFY_MAIL) {
134 $itemlink = $siteurl.'/message/'.$params['item']['id'];
135 $params["link"] = $itemlink;
137 $subject = $l10n->t('[Friendica:Notify] New mail received at %s', $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, $siteurl.'/message/'.$params['item']['id']);
144 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'/message/'.$params['item']['id'].'">'.$sitename.'</a>');
147 if ($params['type'] == NOTIFY_COMMENT || $params['type'] == NOTIFY_TAGSELF) {
148 $thread = Item::selectFirstThreadForUser($params['uid'], ['ignored'], ['iid' => $parent_id, 'deleted' => false]);
149 if (DBA::isResult($thread) && $thread['ignored']) {
150 Logger::log('Thread ' . $parent_id . ' will be ignored', Logger::DEBUG);
154 // Check to see if there was already a tag notify or comment notify for this post.
155 // If so don't create a second notification
156 /// @todo In the future we should store the notification with the highest "value" and replace notifications
157 $condition = ['type' => [NOTIFY_TAGSELF, NOTIFY_COMMENT, NOTIFY_SHARE],
158 'link' => $params['link'], 'uid' => $params['uid']];
159 if (DBA::exists('notify', $condition)) {
163 // if it's a post figure out who's post it is.
165 if ($params['otype'] === Notify::OTYPE_ITEM && $parent_id) {
166 $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
173 $item_post_type = Item::postType($item);
175 $content = ItemContent::getPlaintextPost($item, 70);
176 if (!empty($content['text'])) {
177 $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
182 // First go for the general message
184 // "George Bull's post"
185 if ($params['activity']['origin_comment']) {
186 $message = '%1$s replied to you on %2$s\'s %3$s %4$s';
187 } elseif ($params['activity']['explicit_tagged']) {
188 $message = '%1$s tagged you on %2$s\'s %3$s %4$s';
190 $message = '%1$s commented on %2$s\'s %3$s %4$s';
193 $dest_str = $l10n->t($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
195 // Then look for the special cases
198 if ($params['activity']['origin_thread']) {
199 if ($params['activity']['origin_comment']) {
200 $message = '%1$s replied to you on your %2$s %3$s';
201 } elseif ($params['activity']['explicit_tagged']) {
202 $message = '%1$s tagged you on your %2$s %3$s';
204 $message = '%1$s commented on your %2$s %3$s';
207 $dest_str = $l10n->t($message, $params['source_name'], $item_post_type, $title);
209 } elseif ($item['author-link'] == $params['source_link']) {
210 if ($params['activity']['origin_comment']) {
211 $message = '%1$s replied to you on their %2$s %3$s';
212 } elseif ($params['activity']['explicit_tagged']) {
213 $message = '%1$s tagged you on their %2$s %3$s';
215 $message = '%1$s commented on their %2$s %3$s';
218 $dest_str = $l10n->t($message, $params['source_name'], $item_post_type, $title);
221 // Some mail software relies on subject field for threading.
222 // So, we cannot have different subjects for notifications of the same thread.
223 // Before this we have the name of the replier on the subject rendering
224 // different subjects for messages on the same thread.
225 if ($params['activity']['explicit_tagged']) {
226 $subject = $l10n->t('[Friendica:Notify] %s tagged you', $params['source_name']);
228 $preamble = $l10n->t('%1$s tagged you at %2$s', $params['source_name'], $sitename);
230 $subject = $l10n->t('[Friendica:Notify] Comment to conversation #%1$d by %2$s', $parent_id, $params['source_name']);
232 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
235 $epreamble = $dest_str;
237 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
238 $tsitelink = sprintf($sitelink, $siteurl);
239 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
240 $itemlink = $params['link'];
243 if ($params['type'] == NOTIFY_WALL) {
244 $subject = $l10n->t('[Friendica:Notify] %s posted to your profile wall', $params['source_name']);
246 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
247 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
248 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
252 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
253 $tsitelink = sprintf($sitelink, $siteurl);
254 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
255 $itemlink = $params['link'];
258 if ($params['type'] == NOTIFY_SHARE) {
259 $subject = $l10n->t('[Friendica:Notify] %s shared a new post', $params['source_name']);
261 $preamble = $l10n->t('%1$s shared a new post at %2$s', $params['source_name'], $sitename);
262 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url].',
263 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
267 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
268 $tsitelink = sprintf($sitelink, $siteurl);
269 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
270 $itemlink = $params['link'];
273 if ($params['type'] == NOTIFY_POKE) {
274 $subject = $l10n->t('[Friendica:Notify] %1$s poked you', $params['source_name']);
276 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
277 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
278 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
282 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
283 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
284 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
286 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
287 $tsitelink = sprintf($sitelink, $siteurl);
288 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
289 $itemlink = $params['link'];
292 if ($params['type'] == NOTIFY_TAGSHARE) {
293 $itemlink = $params['link'];
294 $subject = $l10n->t('[Friendica:Notify] %s tagged your post', $params['source_name']);
296 $preamble = $l10n->t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
297 $epreamble = $l10n->t('%1$s tagged [url=%2$s]your post[/url]',
298 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
302 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
303 $tsitelink = sprintf($sitelink, $siteurl);
304 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
307 if ($params['type'] == NOTIFY_INTRO) {
308 $itemlink = $params['link'];
309 $subject = $l10n->t('[Friendica:Notify] Introduction received');
311 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
312 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
314 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
317 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
319 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
320 $tsitelink = sprintf($sitelink, $siteurl);
321 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
323 switch ($params['verb']) {
324 case Activity::FRIEND:
325 // someone started to share with user (mostly OStatus)
326 $subject = $l10n->t('[Friendica:Notify] A new person is sharing with you');
328 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
329 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
330 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
334 case Activity::FOLLOW:
335 // someone started to follow the user (mostly OStatus)
336 $subject = $l10n->t('[Friendica:Notify] You have a new follower');
338 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
339 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
340 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
345 // ACTIVITY_REQ_FRIEND is default activity for notifications
350 if ($params['type'] == NOTIFY_SUGGEST) {
351 $itemlink = $params['link'];
352 $subject = $l10n->t('[Friendica:Notify] Friend suggestion received');
354 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
355 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
357 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
358 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
361 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
362 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
363 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
365 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
366 $tsitelink = sprintf($sitelink, $siteurl);
367 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
370 if ($params['type'] == NOTIFY_CONFIRM) {
371 if ($params['verb'] == Activity::FRIEND) { // mutual connection
372 $itemlink = $params['link'];
373 $subject = $l10n->t('[Friendica:Notify] Connection accepted');
375 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
376 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
378 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
381 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
383 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
384 $tsitelink = sprintf($sitelink, $siteurl);
385 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
386 } else { // ACTIVITY_FOLLOW
387 $itemlink = $params['link'];
388 $subject = $l10n->t('[Friendica:Notify] Connection accepted');
390 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
391 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
393 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
396 $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']);
398 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
400 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
401 $tsitelink = sprintf($sitelink, $siteurl);
402 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
406 if ($params['type'] == NOTIFY_SYSTEM) {
407 switch($params['event']) {
408 case "SYSTEM_REGISTER_REQUEST":
409 $itemlink = $params['link'];
410 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
412 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
413 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
415 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
418 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
419 $params['source_name'],
420 $siteurl, $params['source_mail'],
421 $params['source_nick']
424 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
425 $tsitelink = sprintf($sitelink, $params['link']);
426 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
428 case "SYSTEM_DB_UPDATE_FAIL":
433 if ($params['type'] == SYSTEM_EMAIL) {
434 // not part of the notifications.
435 // it just send a mail to the user.
436 // It will be used by the system to send emails to users (like
437 // password reset, invitations and so) using one look (but without
438 // add a notification to the user, with could be inexistent)
439 if (!isset($params['subject'])) {
440 Logger::warning('subject isn\'t set.', ['type' => $params['type']]);
442 $subject = $params['subject'] ?? '';
444 if (!isset($params['preamble'])) {
445 Logger::warning('preamble isn\'t set.', ['type' => $params['type'], 'subject' => $subject]);
447 $preamble = $params['preamble'] ?? '';
449 if (!isset($params['body'])) {
450 Logger::warning('body isn\'t set.', ['type' => $params['type'], 'subject' => $subject, 'preamble' => $preamble]);
452 $body = $params['body'] ?? '';
454 $show_in_notification_page = false;
457 $subject .= " (".$nickname."@".$hostname.")";
461 'subject' => $subject,
462 'preamble' => $preamble,
463 'epreamble' => $epreamble,
465 'sitelink' => $sitelink,
466 'tsitelink' => $tsitelink,
467 'hsitelink' => $hsitelink,
468 'itemlink' => $itemlink
471 Hook::callAll('enotify', $h);
473 $subject = $h['subject'];
475 $preamble = $h['preamble'];
476 $epreamble = $h['epreamble'];
480 $tsitelink = $h['tsitelink'];
481 $hsitelink = $h['hsitelink'];
482 $itemlink = $h['itemlink'];
486 if ($show_in_notification_page) {
487 $notification = DI::notify()->insert([
488 'name' => $params['source_name'],
489 'url' => $params['source_link'],
490 'photo' => $params['source_photo'],
491 'uid' => $params['uid'],
493 'parent' => $parent_id,
494 'type' => $params['type'],
495 'verb' => $params['verb'],
496 'otype' => $params['otype'],
499 $notification->link = DI::baseUrl() . '/notification/view/' . $notification->id;
500 $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]);
502 DI::notify()->update($notification);
504 $itemlink = $notification->link;
505 $notify_id = $notification->id;
508 // send email notification if notification preferences permit
509 if ((intval($params['notify_flags']) & intval($params['type']))
510 || $params['type'] == NOTIFY_SYSTEM
511 || $params['type'] == SYSTEM_EMAIL) {
513 Logger::log('sending notification email');
515 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
516 $id_for_parent = $params['parent'] . "@" . $hostname;
518 // Is this the first email notification for this parent item and user?
519 if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
520 Logger::log("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']), Logger::DEBUG);
522 $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'],
523 'receiver-uid' => $params['uid'], 'parent-item' => 0];
524 DBA::insert('notify-threads', $fields);
526 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
527 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
528 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
529 Logger::log($log_msg, Logger::DEBUG);
531 // If not, just "follow" the thread.
532 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
533 Logger::log("There's already a notification for this parent.", Logger::DEBUG);
537 $textversion = BBCode::toPlaintext($body);
538 $htmlversion = BBCode::convert($body);
541 $datarray['banner'] = $banner;
542 $datarray['product'] = $product;
543 $datarray['preamble'] = $preamble;
544 $datarray['sitename'] = $sitename;
545 $datarray['siteurl'] = $siteurl;
546 $datarray['type'] = $params['type'];
547 $datarray['parent'] = $parent_id;
548 $datarray['source_name'] = $params['source_name'] ?? '';
549 $datarray['source_link'] = $params['source_link'] ?? '';
550 $datarray['source_photo'] = $params['source_photo'] ?? '';
551 $datarray['uid'] = $params['uid'];
552 $datarray['username'] = $params['to_name'] ?? '';
553 $datarray['hsitelink'] = $hsitelink;
554 $datarray['tsitelink'] = $tsitelink;
555 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
556 $datarray['titemlink'] = $itemlink;
557 $datarray['thanks'] = $thanks;
558 $datarray['site_admin'] = $site_admin;
559 $datarray['title'] = stripslashes($title);
560 $datarray['htmlversion'] = $htmlversion;
561 $datarray['textversion'] = $textversion;
562 $datarray['subject'] = $subject;
563 $datarray['headers'] = $additional_mail_header;
565 Hook::callAll('enotify_mail', $datarray);
567 // check whether sending post content in email notifications is allowed
568 // always true for SYSTEM_EMAIL
569 $content_allowed = ((!DI::config()->get('system', 'enotify_no_content')) || ($params['type'] == SYSTEM_EMAIL));
571 // load the template for private message notifications
572 $tpl = Renderer::getMarkupTemplate('email_notify_html.tpl');
573 $email_html_body = Renderer::replaceMacros($tpl, [
574 '$banner' => $datarray['banner'],
575 '$product' => $datarray['product'],
576 '$preamble' => str_replace("\n", "<br>\n", $datarray['preamble']),
577 '$sitename' => $datarray['sitename'],
578 '$siteurl' => $datarray['siteurl'],
579 '$source_name' => $datarray['source_name'],
580 '$source_link' => $datarray['source_link'],
581 '$source_photo' => $datarray['source_photo'],
582 '$username' => $datarray['username'],
583 '$hsitelink' => $datarray['hsitelink'],
584 '$hitemlink' => $datarray['hitemlink'],
585 '$thanks' => $datarray['thanks'],
586 '$site_admin' => $datarray['site_admin'],
587 '$title' => $datarray['title'],
588 '$htmlversion' => $datarray['htmlversion'],
589 '$content_allowed' => $content_allowed,
592 // load the template for private message notifications
593 $tpl = Renderer::getMarkupTemplate('email_notify_text.tpl');
594 $email_text_body = Renderer::replaceMacros($tpl, [
595 '$banner' => $datarray['banner'],
596 '$product' => $datarray['product'],
597 '$preamble' => $datarray['preamble'],
598 '$sitename' => $datarray['sitename'],
599 '$siteurl' => $datarray['siteurl'],
600 '$source_name' => $datarray['source_name'],
601 '$source_link' => $datarray['source_link'],
602 '$source_photo' => $datarray['source_photo'],
603 '$username' => $datarray['username'],
604 '$tsitelink' => $datarray['tsitelink'],
605 '$titemlink' => $datarray['titemlink'],
606 '$thanks' => $datarray['thanks'],
607 '$site_admin' => $datarray['site_admin'],
608 '$title' => $datarray['title'],
609 '$textversion' => $datarray['textversion'],
610 '$content_allowed' => $content_allowed,
613 $email = new EMail($sender_name, $sender_email, $sender_email, $params['to_email'],
614 $datarray['subject'], $email_html_body, $email_text_body,
615 $datarray['headers'], $params['uid']);
617 // use the Emailer class to send the message
618 return DI::emailer()->send($email);
625 * Checks for users who should be notified
627 * @param int $itemid ID of the item for which the check should be done
628 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
630 function check_user_notification($itemid) {
631 // fetch all users with notifications
632 $useritems = DBA::select('user-item', ['uid', 'notification-type'], ['iid' => $itemid]);
633 while ($useritem = DBA::fetch($useritems)) {
634 check_item_notification($itemid, $useritem['uid'], $useritem['notification-type']);
636 DBA::close($useritems);
640 * Checks for item related notifications and sends them
642 * @param int $itemid ID of the item for which the check should be done
643 * @param int $uid User ID
644 * @param int $notification_type Notification bits
646 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
648 function check_item_notification($itemid, $uid, $notification_type) {
649 $fields = ['id', 'mention', 'tag', 'parent', 'title', 'body',
650 'author-link', 'author-name', 'author-avatar', 'author-id',
651 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
652 $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'deleted' => false];
653 $item = Item::selectFirstForUser($uid, $fields, $condition);
654 if (!DBA::isResult($item)) {
658 // Generate the notification array
660 $params['uid'] = $uid;
661 $params['item'] = $item;
662 $params['parent'] = $item['parent'];
663 $params['link'] = DI::baseUrl() . '/display/' . urlencode($item['guid']);
664 $params['otype'] = 'item';
665 $params['source_name'] = $item['author-name'];
666 $params['source_link'] = $item['author-link'];
667 $params['source_photo'] = $item['author-avatar'];
669 // Set the activity flags
670 $params['activity']['explicit_tagged'] = ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED);
671 $params['activity']['implicit_tagged'] = ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED);
672 $params['activity']['origin_comment'] = ($notification_type & UserItem::NOTIF_DIRECT_COMMENT);
673 $params['activity']['origin_thread'] = ($notification_type & UserItem::NOTIF_THREAD_COMMENT);
674 $params['activity']['thread_comment'] = ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION);
675 $params['activity']['thread_activity'] = ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION);
677 // Tagging a user in a direct post (first comment level) means a direct comment
678 if ($params['activity']['explicit_tagged'] && ($notification_type & UserItem::NOTIF_DIRECT_THREAD_COMMENT)) {
679 $params['activity']['origin_comment'] = true;
682 if ($notification_type & UserItem::NOTIF_SHARED) {
683 $params['type'] = NOTIFY_SHARE;
684 $params['verb'] = Activity::POST;
685 } elseif ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED) {
686 $params['type'] = NOTIFY_TAGSELF;
687 $params['verb'] = Activity::TAG;
688 } elseif ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED) {
689 $params['type'] = NOTIFY_COMMENT;
690 $params['verb'] = Activity::POST;
691 } elseif ($notification_type & UserItem::NOTIF_THREAD_COMMENT) {
692 $params['type'] = NOTIFY_COMMENT;
693 $params['verb'] = Activity::POST;
694 } elseif ($notification_type & UserItem::NOTIF_DIRECT_COMMENT) {
695 $params['type'] = NOTIFY_COMMENT;
696 $params['verb'] = Activity::POST;
697 } elseif ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION) {
698 $params['type'] = NOTIFY_COMMENT;
699 $params['verb'] = Activity::POST;
700 } elseif ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION) {
701 $params['type'] = NOTIFY_COMMENT;
702 $params['verb'] = Activity::POST;
707 notification($params);