3 * @copyright Copyright (C) 2010-2022, the Friendica project
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 namespace Friendica\Navigation\Notifications\Repository;
24 use Friendica\App\BaseURL;
25 use Friendica\BaseRepository;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Text\Plaintext;
28 use Friendica\Core\Config\Capability\IManageConfigValues;
29 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
30 use Friendica\Core\Hook;
31 use Friendica\Core\L10n;
32 use Friendica\Core\System;
33 use Friendica\Database\Database;
34 use Friendica\Database\DBA;
36 use Friendica\Navigation\Notifications\Collection;
37 use Friendica\Navigation\Notifications\Entity;
38 use Friendica\Navigation\Notifications\Exception;
39 use Friendica\Navigation\Notifications\Factory;
40 use Friendica\Network\HTTPException;
41 use Friendica\Object\Api\Mastodon\Notification;
42 use Friendica\Protocol\Activity;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Emailer;
45 use Psr\Log\LoggerInterface;
48 * @deprecated since 2022.05 Use \Friendica\Navigation\Notifications\Repository\Notification instead
50 class Notify extends BaseRepository
52 /** @var Factory\Notify */
61 /** @var IManageConfigValues */
64 /** @var IManagePersonalConfigValues */
70 /** @var Factory\Notification */
71 protected $notification;
73 protected static $table_name = 'notify';
75 public function __construct(Database $database, LoggerInterface $logger, L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, Emailer $emailer, Factory\Notification $notification, Factory\Notify $factory = null)
78 $this->baseUrl = $baseUrl;
79 $this->config = $config;
80 $this->pConfig = $pConfig;
81 $this->emailer = $emailer;
82 $this->notification = $notification;
84 parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger));
88 * @param array $condition
89 * @param array $params
90 * @return Entity\Notify
91 * @throws HTTPException\NotFoundException
93 private function selectOne(array $condition, array $params = []): Entity\Notify
95 return parent::_selectOne($condition, $params);
98 private function select(array $condition, array $params = []): Collection\Notifies
100 return new Collection\Notifies(parent::_select($condition, $params)->getArrayCopy());
103 public function countForUser($uid, array $condition, array $params = []): int
105 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
107 return $this->count($condition, $params);
110 public function existsForUser($uid, array $condition): bool
112 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
114 return $this->exists($condition);
119 * @return Entity\Notify
120 * @throws HTTPException\NotFoundException
122 public function selectOneById(int $id): Entity\Notify
124 return $this->selectOne(['id' => $id]);
127 public function selectForUser(int $uid, array $condition, array $params): Collection\Notifies
129 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
131 return $this->select($condition, $params);
135 * Returns notifications for the user, unread first, ordered in descending chronological order.
139 * @return Collection\Notifies
141 public function selectAllForUser(int $uid, int $limit): Collection\Notifies
143 return $this->selectForUser($uid, [], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => $limit]);
146 public function setAllSeenForUser(int $uid, array $condition = []): bool
148 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
150 return $this->db->update(self::$table_name, ['seen' => true], $condition);
154 * @param Entity\Notify $Notify
155 * @return Entity\Notify
156 * @throws HTTPException\NotFoundException
157 * @throws HTTPException\InternalServerErrorException
158 * @throws Exception\NotificationCreationInterceptedException
160 public function save(Entity\Notify $Notify): Entity\Notify
163 'type' => $Notify->type,
164 'name' => $Notify->name,
165 'url' => $Notify->url,
166 'photo' => $Notify->photo,
167 'msg' => $Notify->msg,
168 'uid' => $Notify->uid,
169 'link' => $Notify->link,
170 'iid' => $Notify->itemId,
171 'parent' => $Notify->parent,
172 'seen' => $Notify->seen,
173 'verb' => $Notify->verb,
174 'otype' => $Notify->otype,
175 'name_cache' => $Notify->name_cache,
176 'msg_cache' => $Notify->msg_cache,
177 'uri-id' => $Notify->uriId,
178 'parent-uri-id' => $Notify->parentUriId,
182 $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
184 $fields['date'] = DateTimeFormat::utcNow();
185 Hook::callAll('enotify_store', $fields);
187 $this->db->insert(self::$table_name, $fields);
189 $Notify = $this->selectOneById($this->db->lastInsertId());
195 public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
198 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
204 return $this->db->update(self::$table_name, ['seen' => true], $condition);
208 * Creates a notification entry and possibly sends a mail
210 * @param array $params Array with the elements:
211 * type, event, otype, activity, verb, uid, cid, item, link,
212 * source_name, source_mail, source_nick, source_link, source_photo,
213 * show_in_notification_page
216 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
218 function createFromArray($params)
220 /** @var string the common prefix of a notification subject */
221 $subjectPrefix = $this->l10n->t('[Friendica:Notify]');
223 // Temporary logging for finding the origin
224 if (!isset($params['uid'])) {
225 $this->logger->notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
228 // Ensure that the important fields are set at any time
229 $fields = ['nickname', 'account-type', 'notify-flags', 'language', 'username', 'email'];
230 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
232 if (!DBA::isResult($user)) {
233 $this->logger->error('Unknown user', ['uid' => $params['uid']]);
237 // There is no need to create notifications for forum accounts
238 if ($user['account-type'] == Model\User::ACCOUNT_TYPE_COMMUNITY) {
242 $params['notify_flags'] = $user['notify-flags'];
243 $params['language'] = $user['language'];
244 $params['to_name'] = $user['username'];
245 $params['to_email'] = $user['email'];
247 // from here on everything is in the recipients language
248 $l10n = $this->l10n->withLang($params['language']);
250 if (!empty($params['cid'])) {
251 $contact = Model\Contact::getById($params['cid'], ['url', 'name', 'photo']);
252 if (DBA::isResult($contact)) {
253 $params['source_link'] = $contact['url'];
254 $params['source_name'] = $contact['name'];
255 $params['source_photo'] = $contact['photo'];
259 $siteurl = $this->baseUrl->get(true);
260 $sitename = $this->config->get('config', 'sitename');
262 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
263 // the database, and an email is sent if applicable.
264 // default, if not specified: true
265 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
267 $title = $params['item']['title'] ?? '';
268 $body = $params['item']['body'] ?? '';
270 $parent_id = $params['item']['parent'] ?? 0;
271 $parent_uri_id = $params['item']['parent-uri-id'] ?? 0;
281 switch ($params['type']) {
282 case Model\Notification\Type::MAIL:
283 $itemlink = $params['link'];
285 $subject = $l10n->t('%s New mail received at %s', $subjectPrefix, $sitename);
287 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
288 $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]');
290 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
291 $tsitelink = sprintf($sitelink, $itemlink);
292 $hsitelink = sprintf($sitelink, '<a href="' . $itemlink . '">' . $sitename . '</a>');
294 // Mail notifications aren't using the "notify" table entry
295 $show_in_notification_page = false;
298 case Model\Notification\Type::COMMENT:
299 if (Model\Post\ThreadUser::getIgnored($parent_uri_id, $params['uid'])) {
300 $this->logger->info('Thread is ignored', ['parent' => $parent_id, 'parent-uri-id' => $parent_uri_id]);
304 $item = Model\Post::selectFirstForUser($params['uid'], Model\Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
309 $item_post_type = Model\Item::postType($item, $l10n);
311 $body = BBCode::toPlaintext($item['body'], false);
312 $title = Plaintext::shorten($body, 70);
313 if (!empty($title)) {
314 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
317 // First go for the general message
319 // "George Bull's post"
320 $message = $l10n->t('%1$s commented on %2$s\'s %3$s %4$s');
321 $dest_str = sprintf($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
325 $message = $l10n->t('%1$s commented on your %2$s %3$s');
326 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
328 } elseif ($item['author-link'] == $params['source_link']) {
329 $message = $l10n->t('%1$s commented on their %2$s %3$s');
330 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
333 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $parent_id, $params['source_name']);
335 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
337 $epreamble = $dest_str;
339 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
340 $tsitelink = sprintf($sitelink, $siteurl);
341 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
342 $itemlink = $params['link'];
345 case Model\Notification\Type::WALL:
346 $subject = $l10n->t('%s %s posted to your profile wall', $subjectPrefix, $params['source_name']);
348 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
349 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
350 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
354 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
355 $tsitelink = sprintf($sitelink, $siteurl);
356 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
357 $itemlink = $params['link'];
360 case Model\Notification\Type::POKE:
361 $subject = $l10n->t('%1$s %2$s poked you', $subjectPrefix, $params['source_name']);
363 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
364 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
365 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
369 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
370 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
371 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
373 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
374 $tsitelink = sprintf($sitelink, $siteurl);
375 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
376 $itemlink = $params['link'];
379 case Model\Notification\Type::INTRO:
380 $itemlink = $params['link'];
381 $subject = $l10n->t('%s Introduction received', $subjectPrefix);
383 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
384 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
386 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
389 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
391 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
392 $tsitelink = sprintf($sitelink, $siteurl);
393 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
395 switch ($params['verb']) {
396 case Activity::FRIEND:
397 // someone started to share with user (mostly OStatus)
398 $subject = $l10n->t('%s A new person is sharing with you', $subjectPrefix);
400 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
401 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
402 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
406 case Activity::FOLLOW:
407 // someone started to follow the user (mostly OStatus)
408 $subject = $l10n->t('%s You have a new follower', $subjectPrefix);
410 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
411 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
412 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
417 // ACTIVITY_REQ_FRIEND is default activity for notifications
422 case Model\Notification\Type::SUGGEST:
423 $itemlink = $params['link'];
424 $subject = $l10n->t('%s Friend suggestion received', $subjectPrefix);
426 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
427 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
429 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
430 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
433 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
434 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
435 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
437 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
438 $tsitelink = sprintf($sitelink, $siteurl);
439 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
442 case Model\Notification\Type::CONFIRM:
443 if ($params['verb'] == Activity::FRIEND) { // mutual connection
444 $itemlink = $params['link'];
445 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
447 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
448 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
450 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
453 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
455 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
456 $tsitelink = sprintf($sitelink, $siteurl);
457 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
458 } else { // ACTIVITY_FOLLOW
459 $itemlink = $params['link'];
460 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
462 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
463 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
465 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
468 $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']);
470 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
472 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
473 $tsitelink = sprintf($sitelink, $siteurl);
474 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
478 case Model\Notification\Type::SYSTEM:
479 switch($params['event']) {
480 case "SYSTEM_REGISTER_REQUEST":
481 $itemlink = $params['link'];
482 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
484 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
485 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
487 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
490 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
491 $params['source_name'],
492 $siteurl, $params['source_mail'],
493 $params['source_nick']
496 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
497 $tsitelink = sprintf($sitelink, $params['link']);
498 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
500 case "SYSTEM_DB_UPDATE_FAIL":
506 $this->logger->notice('Unhandled type', ['type' => $params['type']]);
510 return $this->storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page);
513 private function storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page)
515 $item_id = $params['item']['id'] ?? 0;
516 $uri_id = $params['item']['uri-id'] ?? null;
517 $parent_id = $params['item']['parent'] ?? 0;
518 $parent_uri_id = $params['item']['parent-uri-id'] ?? null;
520 // Ensure that the important fields are set at any time
521 $fields = ['nickname'];
522 $user = Model\User::getById($params['uid'], $fields);
524 $sitename = $this->config->get('config', 'sitename');
526 $nickname = $user['nickname'];
528 $hostname = $this->baseUrl->getHostname();
529 if (strpos($hostname, ':')) {
530 $hostname = substr($hostname, 0, strpos($hostname, ':'));
533 // Creates a new email builder for the notification email
534 $emailBuilder = $this->emailer->newNotifyMail();
536 $emailBuilder->setHeader('X-Friendica-Account', '<' . $nickname . '@' . $hostname . '>');
538 $subject .= " (".$nickname."@".$hostname.")";
542 'subject' => $subject,
543 'preamble' => $preamble,
544 'epreamble' => $epreamble,
546 'sitelink' => $sitelink,
547 'tsitelink' => $tsitelink,
548 'hsitelink' => $hsitelink,
549 'itemlink' => $itemlink
552 Hook::callAll('enotify', $h);
554 $subject = $h['subject'];
556 $preamble = $h['preamble'];
557 $epreamble = $h['epreamble'];
561 $tsitelink = $h['tsitelink'];
562 $hsitelink = $h['hsitelink'];
563 $itemlink = $h['itemlink'];
567 if ($show_in_notification_page) {
568 $Notify = $this->factory->createFromParams($params, $itemlink, $item_id, $uri_id, $parent_id, $parent_uri_id);
570 $Notify = $this->save($Notify);
571 } catch (Exception\NotificationCreationInterceptedException $e) {
572 // Notification insertion can be intercepted by an addon registering the 'enotify_store' hook
576 $Notify->updateMsgFromPreamble($epreamble);
577 $Notify = $this->save($Notify);
579 $itemlink = $this->baseUrl->get() . '/notify/' . $Notify->id;
580 $notify_id = $Notify->id;
583 // send email notification if notification preferences permit
584 if ((intval($params['notify_flags']) & intval($params['type']))
585 || $params['type'] == Model\Notification\Type::SYSTEM) {
587 $this->logger->notice('sending notification email');
589 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
590 $parent = Model\Post::selectFirst(['guid'], ['id' => $params['parent']]);
591 $message_id = "<" . $parent['guid'] . "@" . gethostname() . ">";
593 // Is this the first email notification for this parent item and user?
594 if (!DBA::exists('notify-threads', ['master-parent-uri-id' => $parent_uri_id, 'receiver-uid' => $params['uid']])) {
595 $this->logger->info("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']));
597 $fields = ['notify-id' => $notify_id, 'master-parent-uri-id' => $parent_uri_id,
598 'receiver-uid' => $params['uid'], 'parent-item' => 0];
599 DBA::insert('notify-threads', $fields);
601 $emailBuilder->setHeader('Message-ID', $message_id);
602 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
603 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
604 $this->logger->info($log_msg);
606 // If not, just "follow" the thread.
607 $emailBuilder->setHeader('References', $message_id);
608 $emailBuilder->setHeader('In-Reply-To', $message_id);
609 $this->logger->info("There's already a notification for this parent.");
614 'preamble' => $preamble,
615 'type' => $params['type'],
616 'parent' => $parent_id,
617 'source_name' => $params['source_name'] ?? null,
618 'source_link' => $params['source_link'] ?? null,
619 'source_photo' => $params['source_photo'] ?? null,
620 'uid' => $params['uid'],
621 'hsitelink' => $hsitelink,
622 'tsitelink' => $tsitelink,
623 'itemlink' => $itemlink,
626 'subject' => $subject,
627 'headers' => $emailBuilder->getHeaders(),
630 Hook::callAll('enotify_mail', $datarray);
633 ->withHeaders($datarray['headers'])
634 ->withRecipient($params['to_email'])
636 'uid' => $datarray['uid'],
637 'language' => $params['language'],
639 ->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body'])
640 ->withSiteLink($datarray['tsitelink'], $datarray['hsitelink'])
641 ->withItemLink($datarray['itemlink']);
643 // If a photo is present, add it to the email
644 if (!empty($datarray['source_photo'])) {
645 $emailBuilder->withPhoto(
646 $datarray['source_photo'],
647 $datarray['source_link'] ?? $sitelink,
648 $datarray['source_name'] ?? $sitename);
651 $email = $emailBuilder->build();
653 // use the Emailer class to send the message
654 return $this->emailer->send($email);
660 public function NotifyOnDesktop(Entity\Notification $Notification, string $type = null): bool
662 if (is_null($type)) {
663 $type = \Friendica\Factory\Api\Mastodon\Notification::getType($Notification);
666 if (!in_array($type, [Notification::TYPE_RESHARE, Notification::TYPE_LIKE])) {
670 if ($this->pConfig->get($Notification->uid, 'system', 'notify_like') && ($type == Notification::TYPE_LIKE)) {
674 if ($this->pConfig->get($Notification->uid, 'system', 'notify_announce') && ($type == Notification::TYPE_RESHARE)) {
681 public function createFromNotification(Entity\Notification $Notification)
683 $this->logger->info('Start', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
685 if ($Notification->type === Model\Post\UserNotification::TYPE_NONE) {
686 $this->logger->info('Not an item based notification, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
691 $params['verb'] = $Notification->verb;
692 $params['uid'] = $Notification->uid;
693 $params['otype'] = Model\Notification\ObjectType::ITEM;
695 $user = Model\User::getById($Notification->uid);
697 $params['notify_flags'] = $user['notify-flags'];
698 $params['language'] = $user['language'];
699 $params['to_name'] = $user['username'];
700 $params['to_email'] = $user['email'];
702 // from here on everything is in the recipients language
703 $l10n = $this->l10n->withLang($user['language']);
705 $contact = Model\Contact::getById($Notification->actorId, ['url', 'name', 'photo']);
706 if (DBA::isResult($contact)) {
707 $params['source_link'] = $contact['url'];
708 $params['source_name'] = $contact['name'];
709 $params['source_photo'] = $contact['photo'];
712 $item = Model\Post::selectFirstForUser($Notification->uid, Model\Item::ITEM_FIELDLIST,
713 ['uid' => [0, $Notification->uid], 'uri-id' => $Notification->targetUriId, 'deleted' => false],
714 ['order' => ['uid' => true]]);
716 $this->logger->info('Item not found', ['uri-id' => $Notification->targetUriId, 'type' => $Notification->type]);
720 $params['item'] = $item;
721 $params['parent'] = $item['parent'];
722 $params['link'] = $this->baseUrl->get() . '/display/' . urlencode($item['guid']);
724 $subjectPrefix = $l10n->t('[Friendica:Notify]');
726 if (Model\Post\ThreadUser::getIgnored($Notification->parentUriId, $Notification->uid)) {
727 $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
731 // Check to see if there was already a tag notify or comment notify for this post.
732 // If so don't create a second notification
733 $condition = ['type' => [Model\Notification\Type::TAG_SELF, Model\Notification\Type::COMMENT, Model\Notification\Type::SHARE],
734 'link' => $params['link'], 'verb' => Activity::POST];
735 if ($this->existsForUser($Notification->uid, $condition)) {
736 $this->logger->info('Duplicate found, quitting', $condition + ['uid' => $Notification->uid]);
740 $body = BBCode::toPlaintext($item['body'], false);
741 $title = Plaintext::shorten($body, 70);
742 if (!empty($title)) {
743 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
746 // Some mail software relies on subject field for threading.
747 // So, we cannot have different subjects for notifications of the same thread.
748 // Before this we have the name of the replier on the subject rendering
749 // different subjects for messages on the same thread.
750 if ($Notification->type === Model\Post\UserNotification::TYPE_EXPLICIT_TAGGED) {
751 $params['type'] = Model\Notification\Type::TAG_SELF;
752 $subject = $l10n->t('%s %s tagged you', $subjectPrefix, $contact['name']);
753 } elseif ($Notification->type === Model\Post\UserNotification::TYPE_SHARED) {
754 $params['type'] = Model\Notification\Type::SHARE;
755 $subject = $l10n->t('%s %s shared a new post', $subjectPrefix, $contact['name']);
757 $params['type'] = Model\Notification\Type::COMMENT;
758 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $item['parent'], $contact['name']);
761 $msg = $this->notification->getMessageFromNotification($Notification);
763 $this->logger->info('No notification message, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
767 $preamble = $msg['plain'];
768 $epreamble = $msg['rich'];
770 $sitename = $this->config->get('config', 'sitename');
771 $siteurl = $this->baseUrl->get(true);
773 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
774 $tsitelink = sprintf($sitelink, $siteurl);
775 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
776 $itemlink = $params['link'];
778 $this->logger->info('Perform notification', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
780 return $this->storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $item['body'], $itemlink, true);