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\Hook;
30 use Friendica\Core\L10n;
31 use Friendica\Core\System;
32 use Friendica\Database\Database;
33 use Friendica\Database\DBA;
35 use Friendica\Navigation\Notifications\Collection;
36 use Friendica\Navigation\Notifications\Entity;
37 use Friendica\Navigation\Notifications\Exception;
38 use Friendica\Navigation\Notifications\Factory;
39 use Friendica\Network\HTTPException;
40 use Friendica\Protocol\Activity;
41 use Friendica\Util\DateTimeFormat;
42 use Friendica\Util\Emailer;
43 use Psr\Log\LoggerInterface;
46 * @deprecated since 2022.05 Use \Friendica\Navigation\Notifications\Repository\Notification instead
48 class Notify extends BaseRepository
50 /** @var Factory\Notify */
59 /** @var IManageConfigValues */
65 /** @var Factory\Notification */
66 protected $notification;
68 protected static $table_name = 'notify';
70 public function __construct(Database $database, LoggerInterface $logger, L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, Emailer $emailer, Factory\Notification $notification, Factory\Notify $factory = null)
73 $this->baseUrl = $baseUrl;
74 $this->config = $config;
75 $this->emailer = $emailer;
76 $this->notification = $notification;
78 parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger));
82 * @param array $condition
83 * @param array $params
84 * @return Entity\Notify
85 * @throws HTTPException\NotFoundException
87 private function selectOne(array $condition, array $params = []): Entity\Notify
89 return parent::_selectOne($condition, $params);
92 private function select(array $condition, array $params = []): Collection\Notifies
94 return new Collection\Notifies(parent::_select($condition, $params)->getArrayCopy());
97 public function countForUser($uid, array $condition, array $params = []): int
99 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
101 return $this->count($condition, $params);
104 public function existsForUser($uid, array $condition): bool
106 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
108 return $this->exists($condition);
113 * @return Entity\Notify
114 * @throws HTTPException\NotFoundException
116 public function selectOneById(int $id): Entity\Notify
118 return $this->selectOne(['id' => $id]);
121 public function selectForUser(int $uid, array $condition, array $params): Collection\Notifies
123 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
125 return $this->select($condition, $params);
129 * Returns notifications for the user, unread first, ordered in descending chronological order.
133 * @return Collection\Notifies
135 public function selectAllForUser(int $uid, int $limit): Collection\Notifies
137 return $this->selectForUser($uid, [], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => $limit]);
140 public function setAllSeenForUser(int $uid, array $condition = []): bool
142 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
144 return $this->db->update(self::$table_name, ['seen' => true], $condition);
148 * @param Entity\Notify $Notify
149 * @return Entity\Notify
150 * @throws HTTPException\NotFoundException
151 * @throws HTTPException\InternalServerErrorException
152 * @throws Exception\NotificationCreationInterceptedException
154 public function save(Entity\Notify $Notify): Entity\Notify
157 'type' => $Notify->type,
158 'name' => $Notify->name,
159 'url' => $Notify->url,
160 'photo' => $Notify->photo,
161 'msg' => $Notify->msg,
162 'uid' => $Notify->uid,
163 'link' => $Notify->link,
164 'iid' => $Notify->itemId,
165 'parent' => $Notify->parent,
166 'seen' => $Notify->seen,
167 'verb' => $Notify->verb,
168 'otype' => $Notify->otype,
169 'name_cache' => $Notify->name_cache,
170 'msg_cache' => $Notify->msg_cache,
171 'uri-id' => $Notify->uriId,
172 'parent-uri-id' => $Notify->parentUriId,
176 $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
178 $fields['date'] = DateTimeFormat::utcNow();
179 Hook::callAll('enotify_store', $fields);
181 $this->db->insert(self::$table_name, $fields);
183 $Notify = $this->selectOneById($this->db->lastInsertId());
189 public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
192 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
198 return $this->db->update(self::$table_name, ['seen' => true], $condition);
202 * Creates a notification entry and possibly sends a mail
204 * @param array $params Array with the elements:
205 * type, event, otype, activity, verb, uid, cid, item, link,
206 * source_name, source_mail, source_nick, source_link, source_photo,
207 * show_in_notification_page
210 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
212 function createFromArray($params)
214 /** @var string the common prefix of a notification subject */
215 $subjectPrefix = $this->l10n->t('[Friendica:Notify]');
217 // Temporary logging for finding the origin
218 if (!isset($params['uid'])) {
219 $this->logger->notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
222 // Ensure that the important fields are set at any time
223 $fields = ['nickname', 'account-type', 'notify-flags', 'language', 'username', 'email'];
224 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
226 if (!DBA::isResult($user)) {
227 $this->logger->error('Unknown user', ['uid' => $params['uid']]);
231 // There is no need to create notifications for forum accounts
232 if ($user['account-type'] == Model\User::ACCOUNT_TYPE_COMMUNITY) {
236 $params['notify_flags'] = $user['notify-flags'];
237 $params['language'] = $user['language'];
238 $params['to_name'] = $user['username'];
239 $params['to_email'] = $user['email'];
241 // from here on everything is in the recipients language
242 $l10n = $this->l10n->withLang($params['language']);
244 if (!empty($params['cid'])) {
245 $contact = Model\Contact::getById($params['cid'], ['url', 'name', 'photo']);
246 if (DBA::isResult($contact)) {
247 $params['source_link'] = $contact['url'];
248 $params['source_name'] = $contact['name'];
249 $params['source_photo'] = $contact['photo'];
253 $siteurl = $this->baseUrl->get(true);
254 $sitename = $this->config->get('config', 'sitename');
256 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
257 // the database, and an email is sent if applicable.
258 // default, if not specified: true
259 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
261 $title = $params['item']['title'] ?? '';
262 $body = $params['item']['body'] ?? '';
264 $parent_id = $params['item']['parent'] ?? 0;
265 $parent_uri_id = $params['item']['parent-uri-id'] ?? 0;
275 switch ($params['type']) {
276 case Model\Notification\Type::MAIL:
277 $itemlink = $params['link'];
279 $subject = $l10n->t('%s New mail received at %s', $subjectPrefix, $sitename);
281 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
282 $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]');
284 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
285 $tsitelink = sprintf($sitelink, $itemlink);
286 $hsitelink = sprintf($sitelink, '<a href="' . $itemlink . '">' . $sitename . '</a>');
288 // Mail notifications aren't using the "notify" table entry
289 $show_in_notification_page = false;
292 case Model\Notification\Type::COMMENT:
293 if (Model\Post\ThreadUser::getIgnored($parent_uri_id, $params['uid'])) {
294 $this->logger->info('Thread is ignored', ['parent' => $parent_id, 'parent-uri-id' => $parent_uri_id]);
298 $item = Model\Post::selectFirstForUser($params['uid'], Model\Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
303 $item_post_type = Model\Item::postType($item, $l10n);
305 $body = BBCode::toPlaintext($item['body'], false);
306 $title = Plaintext::shorten($body, 70);
307 if (!empty($title)) {
308 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
311 // First go for the general message
313 // "George Bull's post"
314 $message = $l10n->t('%1$s commented on %2$s\'s %3$s %4$s');
315 $dest_str = sprintf($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
319 $message = $l10n->t('%1$s commented on your %2$s %3$s');
320 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
322 } elseif ($item['author-link'] == $params['source_link']) {
323 $message = $l10n->t('%1$s commented on their %2$s %3$s');
324 $dest_str = sprintf($message, $params['source_name'], $item_post_type, $title);
327 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $parent_id, $params['source_name']);
329 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
331 $epreamble = $dest_str;
333 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
334 $tsitelink = sprintf($sitelink, $siteurl);
335 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
336 $itemlink = $params['link'];
339 case Model\Notification\Type::WALL:
340 $subject = $l10n->t('%s %s posted to your profile wall', $subjectPrefix, $params['source_name']);
342 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
343 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
344 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
348 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
349 $tsitelink = sprintf($sitelink, $siteurl);
350 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
351 $itemlink = $params['link'];
354 case Model\Notification\Type::POKE:
355 $subject = $l10n->t('%1$s %2$s poked you', $subjectPrefix, $params['source_name']);
357 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
358 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
359 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
363 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
364 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
365 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
367 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
368 $tsitelink = sprintf($sitelink, $siteurl);
369 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
370 $itemlink = $params['link'];
373 case Model\Notification\Type::INTRO:
374 $itemlink = $params['link'];
375 $subject = $l10n->t('%s Introduction received', $subjectPrefix);
377 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
378 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
380 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
383 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
385 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
386 $tsitelink = sprintf($sitelink, $siteurl);
387 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
389 switch ($params['verb']) {
390 case Activity::FRIEND:
391 // someone started to share with user (mostly OStatus)
392 $subject = $l10n->t('%s A new person is sharing with you', $subjectPrefix);
394 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
395 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
396 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
400 case Activity::FOLLOW:
401 // someone started to follow the user (mostly OStatus)
402 $subject = $l10n->t('%s You have a new follower', $subjectPrefix);
404 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
405 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
406 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
411 // ACTIVITY_REQ_FRIEND is default activity for notifications
416 case Model\Notification\Type::SUGGEST:
417 $itemlink = $params['link'];
418 $subject = $l10n->t('%s Friend suggestion received', $subjectPrefix);
420 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
421 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
423 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
424 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
427 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
428 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
429 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
431 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
432 $tsitelink = sprintf($sitelink, $siteurl);
433 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
436 case Model\Notification\Type::CONFIRM:
437 if ($params['verb'] == Activity::FRIEND) { // mutual connection
438 $itemlink = $params['link'];
439 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
441 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
442 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
444 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
447 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
449 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
450 $tsitelink = sprintf($sitelink, $siteurl);
451 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
452 } else { // ACTIVITY_FOLLOW
453 $itemlink = $params['link'];
454 $subject = $l10n->t('%s Connection accepted', $subjectPrefix);
456 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
457 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
459 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
462 $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']);
464 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
466 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
467 $tsitelink = sprintf($sitelink, $siteurl);
468 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
472 case Model\Notification\Type::SYSTEM:
473 switch($params['event']) {
474 case "SYSTEM_REGISTER_REQUEST":
475 $itemlink = $params['link'];
476 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
478 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
479 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
481 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
484 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
485 $params['source_name'],
486 $siteurl, $params['source_mail'],
487 $params['source_nick']
490 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
491 $tsitelink = sprintf($sitelink, $params['link']);
492 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
494 case "SYSTEM_DB_UPDATE_FAIL":
500 $this->logger->notice('Unhandled type', ['type' => $params['type']]);
504 return $this->storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page);
507 private function storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page)
509 $item_id = $params['item']['id'] ?? 0;
510 $uri_id = $params['item']['uri-id'] ?? null;
511 $parent_id = $params['item']['parent'] ?? 0;
512 $parent_uri_id = $params['item']['parent-uri-id'] ?? null;
514 // Ensure that the important fields are set at any time
515 $fields = ['nickname'];
516 $user = Model\User::getById($params['uid'], $fields);
518 $sitename = $this->config->get('config', 'sitename');
520 $nickname = $user['nickname'];
522 $hostname = $this->baseUrl->getHostname();
523 if (strpos($hostname, ':')) {
524 $hostname = substr($hostname, 0, strpos($hostname, ':'));
527 // Creates a new email builder for the notification email
528 $emailBuilder = $this->emailer->newNotifyMail();
530 $emailBuilder->setHeader('X-Friendica-Account', '<' . $nickname . '@' . $hostname . '>');
532 $subject .= " (".$nickname."@".$hostname.")";
536 'subject' => $subject,
537 'preamble' => $preamble,
538 'epreamble' => $epreamble,
540 'sitelink' => $sitelink,
541 'tsitelink' => $tsitelink,
542 'hsitelink' => $hsitelink,
543 'itemlink' => $itemlink
546 Hook::callAll('enotify', $h);
548 $subject = $h['subject'];
550 $preamble = $h['preamble'];
551 $epreamble = $h['epreamble'];
555 $tsitelink = $h['tsitelink'];
556 $hsitelink = $h['hsitelink'];
557 $itemlink = $h['itemlink'];
561 if ($show_in_notification_page) {
562 $Notify = $this->factory->createFromParams($params, $itemlink, $item_id, $uri_id, $parent_id, $parent_uri_id);
564 $Notify = $this->save($Notify);
565 } catch (Exception\NotificationCreationInterceptedException $e) {
566 // Notification insertion can be intercepted by an addon registering the 'enotify_store' hook
570 $Notify->updateMsgFromPreamble($epreamble);
571 $Notify = $this->save($Notify);
573 $itemlink = $this->baseUrl->get() . '/notify/' . $Notify->id;
574 $notify_id = $Notify->id;
577 // send email notification if notification preferences permit
578 if ((intval($params['notify_flags']) & intval($params['type']))
579 || $params['type'] == Model\Notification\Type::SYSTEM) {
581 $this->logger->notice('sending notification email');
583 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
584 $parent = Model\Post::selectFirst(['guid'], ['id' => $params['parent']]);
585 $message_id = "<" . $parent['guid'] . "@" . gethostname() . ">";
587 // Is this the first email notification for this parent item and user?
588 if (!DBA::exists('notify-threads', ['master-parent-uri-id' => $parent_uri_id, 'receiver-uid' => $params['uid']])) {
589 $this->logger->info("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']));
591 $fields = ['notify-id' => $notify_id, 'master-parent-uri-id' => $parent_uri_id,
592 'receiver-uid' => $params['uid'], 'parent-item' => 0];
593 DBA::insert('notify-threads', $fields);
595 $emailBuilder->setHeader('Message-ID', $message_id);
596 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
597 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
598 $this->logger->info($log_msg);
600 // If not, just "follow" the thread.
601 $emailBuilder->setHeader('References', $message_id);
602 $emailBuilder->setHeader('In-Reply-To', $message_id);
603 $this->logger->info("There's already a notification for this parent.");
608 'preamble' => $preamble,
609 'type' => $params['type'],
610 'parent' => $parent_id,
611 'source_name' => $params['source_name'] ?? null,
612 'source_link' => $params['source_link'] ?? null,
613 'source_photo' => $params['source_photo'] ?? null,
614 'uid' => $params['uid'],
615 'hsitelink' => $hsitelink,
616 'tsitelink' => $tsitelink,
617 'itemlink' => $itemlink,
620 'subject' => $subject,
621 'headers' => $emailBuilder->getHeaders(),
624 Hook::callAll('enotify_mail', $datarray);
627 ->withHeaders($datarray['headers'])
628 ->withRecipient($params['to_email'])
630 'uid' => $datarray['uid'],
631 'language' => $params['language'],
633 ->withNotification($datarray['subject'], $datarray['preamble'], $datarray['title'], $datarray['body'])
634 ->withSiteLink($datarray['tsitelink'], $datarray['hsitelink'])
635 ->withItemLink($datarray['itemlink']);
637 // If a photo is present, add it to the email
638 if (!empty($datarray['source_photo'])) {
639 $emailBuilder->withPhoto(
640 $datarray['source_photo'],
641 $datarray['source_link'] ?? $sitelink,
642 $datarray['source_name'] ?? $sitename);
645 $email = $emailBuilder->build();
647 // use the Emailer class to send the message
648 return $this->emailer->send($email);
654 public function createFromNotification(Entity\Notification $Notification)
656 $this->logger->info('Start', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
658 if ($Notification->type === Model\Post\UserNotification::TYPE_NONE) {
659 $this->logger->info('Not an item based notification, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
664 $params['verb'] = $Notification->verb;
665 $params['uid'] = $Notification->uid;
666 $params['otype'] = Model\Notification\ObjectType::ITEM;
668 $user = Model\User::getById($Notification->uid);
670 $params['notify_flags'] = $user['notify-flags'];
671 $params['language'] = $user['language'];
672 $params['to_name'] = $user['username'];
673 $params['to_email'] = $user['email'];
675 // from here on everything is in the recipients language
676 $l10n = $this->l10n->withLang($user['language']);
678 $contact = Model\Contact::getById($Notification->actorId, ['url', 'name', 'photo']);
679 if (DBA::isResult($contact)) {
680 $params['source_link'] = $contact['url'];
681 $params['source_name'] = $contact['name'];
682 $params['source_photo'] = $contact['photo'];
685 $item = Model\Post::selectFirstForUser($Notification->uid, Model\Item::ITEM_FIELDLIST,
686 ['uid' => [0, $Notification->uid], 'uri-id' => $Notification->targetUriId, 'deleted' => false],
687 ['order' => ['uid' => true]]);
689 $this->logger->info('Item not found', ['uri-id' => $Notification->targetUriId, 'type' => $Notification->type]);
693 $params['item'] = $item;
694 $params['parent'] = $item['parent'];
695 $params['link'] = $this->baseUrl->get() . '/display/' . urlencode($item['guid']);
697 $subjectPrefix = $l10n->t('[Friendica:Notify]');
699 if (Model\Post\ThreadUser::getIgnored($Notification->parentUriId, $Notification->uid)) {
700 $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
704 // Check to see if there was already a tag notify or comment notify for this post.
705 // If so don't create a second notification
706 $condition = ['type' => [Model\Notification\Type::TAG_SELF, Model\Notification\Type::COMMENT, Model\Notification\Type::SHARE],
707 'link' => $params['link'], 'verb' => Activity::POST];
708 if ($this->existsForUser($Notification->uid, $condition)) {
709 $this->logger->info('Duplicate found, quitting', $condition + ['uid' => $Notification->uid]);
713 $body = BBCode::toPlaintext($item['body'], false);
714 $title = Plaintext::shorten($body, 70);
715 if (!empty($title)) {
716 $title = '"' . trim(str_replace("\n", " ", $title)) . '"';
719 // Some mail software relies on subject field for threading.
720 // So, we cannot have different subjects for notifications of the same thread.
721 // Before this we have the name of the replier on the subject rendering
722 // different subjects for messages on the same thread.
723 if ($Notification->type === Model\Post\UserNotification::TYPE_EXPLICIT_TAGGED) {
724 $params['type'] = Model\Notification\Type::TAG_SELF;
725 $subject = $l10n->t('%s %s tagged you', $subjectPrefix, $contact['name']);
726 } elseif ($Notification->type === Model\Post\UserNotification::TYPE_SHARED) {
727 $params['type'] = Model\Notification\Type::SHARE;
728 $subject = $l10n->t('%s %s shared a new post', $subjectPrefix, $contact['name']);
730 $params['type'] = Model\Notification\Type::COMMENT;
731 $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $item['parent'], $contact['name']);
734 $msg = $this->notification->getMessageFromNotification($Notification);
736 $this->logger->info('No notification message, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
740 $preamble = $msg['plain'];
741 $epreamble = $msg['rich'];
743 $sitename = $this->config->get('config', 'sitename');
744 $siteurl = $this->baseUrl->get(true);
746 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
747 $tsitelink = sprintf($sitelink, $siteurl);
748 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
749 $itemlink = $params['link'];
751 $this->logger->info('Perform notification', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
753 return $this->storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $item['body'], $itemlink, true);