3 namespace Friendica\Model;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Content\Text\HTML;
9 use Friendica\Core\PConfig\IPConfig;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\System;
13 use Friendica\Database\Database;
15 use Friendica\Protocol\Activity;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Proxy as ProxyUtils;
18 use Friendica\Util\Temporal;
19 use Friendica\Util\XML;
21 use Psr\Log\LoggerInterface;
22 use Friendica\Network\HTTPException;
25 * Methods for read and write notifications from/to database
26 * or for formatting notifications
28 final class Notification
30 /** @var int The default limit of notifies per page */
31 const DEFAULT_PAGE_LIMIT = 80;
33 const NETWORK = 'network';
34 const SYSTEM = 'system';
35 const PERSONAL = 'personal';
37 const INTRO = 'intro';
43 /** @var App\Arguments */
45 /** @var App\BaseURL */
49 /** @var LoggerInterface */
52 public function __construct(Database $dba, L10n $l10n, App\Arguments $args, App\BaseURL $baseUrl,
53 IPConfig $pConfig, LoggerInterface $logger)
58 $this->baseUrl = $baseUrl;
59 $this->pConfig = $pConfig;
60 $this->logger = $logger;
64 * Set some extra properties to note array from db:
65 * - timestamp as int in default TZ
66 * - date_rel : relative date string
67 * - msg_html: message as html string
68 * - msg_plain: message as plain text string
70 * @param array $notes array of note arrays from db
72 * @return array Copy of input array with added properties
76 private function setExtra(array $notes)
79 foreach ($notes as $note) {
80 $local_time = DateTimeFormat::local($note['date']);
81 $note['timestamp'] = strtotime($local_time);
82 $note['date_rel'] = Temporal::getRelativeDate($note['date']);
83 $note['msg_html'] = BBCode::convert($note['msg'], false);
84 $note['msg_plain'] = explode("\n", trim(HTML::toPlaintext($note['msg_html'], 0)))[0];
92 * Get all notifications for local_user()
94 * @param array $filter optional Array "column name"=>value: filter query by columns values
95 * @param array $order optional Array to order by
96 * @param string $limit optional Query limits
98 * @return array|bool of results or false on errors
101 public function getAll(array $filter = [], array $order = ['date' => 'DESC'], string $limit = "")
105 $params['order'] = $order;
107 if (!empty($limit)) {
108 $params['limit'] = $limit;
111 $dbFilter = array_merge($filter, ['uid' => local_user()]);
113 $stmtNotifies = $this->dba->select('notify', [], $dbFilter, $params);
115 if ($this->dba->isResult($stmtNotifies)) {
116 return $this->setExtra($this->dba->toArray($stmtNotifies));
123 * Get one note for local_user() by $id value
125 * @param int $id identity
127 * @return array note values or null if not found
130 public function getByID(int $id)
132 $stmtNotify = $this->dba->selectFirst('notify', [], ['id' => $id, 'uid' => local_user()]);
133 if ($this->dba->isResult($stmtNotify)) {
134 return $this->setExtra([$stmtNotify])[0];
140 * set seen state of $note of local_user()
142 * @param array $note note array
143 * @param bool $seen optional true or false, default true
145 * @return bool true on success, false on errors
148 public function setSeen(array $note, bool $seen = true)
150 return $this->dba->update('notify', ['seen' => $seen], [
151 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
160 * Set seen state of all notifications of local_user()
162 * @param bool $seen optional true or false. default true
164 * @return bool true on success, false on error
167 public function setAllSeen(bool $seen = true)
169 return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]);
173 * Format the notification query in an usable array
175 * @param array $notifications The array from the db query
176 * @param string $ident The notifications identifier (e.g. network)
179 * string 'label' => The type of the notification
180 * string 'link' => URL to the source
181 * string 'image' => The avatar image
182 * string 'url' => The profile url of the contact
183 * string 'text' => The notification text
184 * string 'when' => The date of the notification
185 * string 'ago' => T relative date of the notification
186 * bool 'seen' => Is the notification marked as "seen"
189 private function formatList(array $notifications, string $ident = "")
191 $formattedNotifies = [];
193 foreach ($notifications as $notification) {
194 // Because we use different db tables for the notification query
195 // we have sometimes $notification['unseen'] and sometimes $notification['seen].
196 // So we will have to transform $notification['unseen']
197 if (array_key_exists('unseen', $notification)) {
198 $notification['seen'] = ($notification['unseen'] > 0 ? false : true);
201 // For feed items we use the user's contact, since the avatar is mostly self choosen.
202 if (!empty($notification['network']) && $notification['network'] == Protocol::FEED) {
203 $notification['author-avatar'] = $notification['contact-avatar'];
206 // Depending on the identifier of the notification we need to use different defaults
209 $default_item_label = 'notification';
210 $default_item_link = $this->baseUrl->get(true) . '/notification/view/' . $notification['id'];
211 $default_item_image = ProxyUtils::proxifyUrl($notification['photo'], false, ProxyUtils::SIZE_MICRO);
212 $default_item_url = $notification['url'];
213 $default_item_text = strip_tags(BBCode::convert($notification['msg']));
214 $default_item_when = DateTimeFormat::local($notification['date'], 'r');
215 $default_item_ago = Temporal::getRelativeDate($notification['date']);
219 $default_item_label = 'comment';
220 $default_item_link = $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'];
221 $default_item_image = ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO);
222 $default_item_url = $notification['author-link'];
223 $default_item_text = $this->l10n->t("%s commented on %s's post", $notification['author-name'], $notification['parent-author-name']);
224 $default_item_when = DateTimeFormat::local($notification['created'], 'r');
225 $default_item_ago = Temporal::getRelativeDate($notification['created']);
229 $default_item_label = (($notification['id'] == $notification['parent']) ? 'post' : 'comment');
230 $default_item_link = $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'];
231 $default_item_image = ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO);
232 $default_item_url = $notification['author-link'];
233 $default_item_text = (($notification['id'] == $notification['parent'])
234 ? $this->l10n->t("%s created a new post", $notification['author-name'])
235 : $this->l10n->t("%s commented on %s's post", $notification['author-name'], $notification['parent-author-name']));
236 $default_item_when = DateTimeFormat::local($notification['created'], 'r');
237 $default_item_ago = Temporal::getRelativeDate($notification['created']);
240 // Transform the different types of notification in an usable array
241 switch ($notification['verb']) {
245 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
246 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
247 'url' => $notification['author-link'],
248 'text' => $this->l10n->t("%s liked %s's post", $notification['author-name'], $notification['parent-author-name']),
249 'when' => $default_item_when,
250 'ago' => $default_item_ago,
251 'seen' => $notification['seen']
255 case Activity::DISLIKE:
257 'label' => 'dislike',
258 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
259 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
260 'url' => $notification['author-link'],
261 'text' => $this->l10n->t("%s disliked %s's post", $notification['author-name'], $notification['parent-author-name']),
262 'when' => $default_item_when,
263 'ago' => $default_item_ago,
264 'seen' => $notification['seen']
268 case Activity::ATTEND:
271 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
272 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
273 'url' => $notification['author-link'],
274 'text' => $this->l10n->t("%s is attending %s's event", $notification['author-name'], $notification['parent-author-name']),
275 'when' => $default_item_when,
276 'ago' => $default_item_ago,
277 'seen' => $notification['seen']
281 case Activity::ATTENDNO:
283 'label' => 'attendno',
284 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
285 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
286 'url' => $notification['author-link'],
287 'text' => $this->l10n->t("%s is not attending %s's event", $notification['author-name'], $notification['parent-author-name']),
288 'when' => $default_item_when,
289 'ago' => $default_item_ago,
290 'seen' => $notification['seen']
294 case Activity::ATTENDMAYBE:
296 'label' => 'attendmaybe',
297 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
298 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
299 'url' => $notification['author-link'],
300 'text' => $this->l10n->t("%s may attend %s's event", $notification['author-name'], $notification['parent-author-name']),
301 'when' => $default_item_when,
302 'ago' => $default_item_ago,
303 'seen' => $notification['seen']
307 case Activity::FRIEND:
308 if (!isset($notification['object'])) {
311 'link' => $default_item_link,
312 'image' => $default_item_image,
313 'url' => $default_item_url,
314 'text' => $default_item_text,
315 'when' => $default_item_when,
316 'ago' => $default_item_ago,
317 'seen' => $notification['seen']
321 /// @todo Check if this part here is used at all
322 $this->logger->info('Complete data.', ['notification' => $notification, 'callStack' => System::callstack(20)]);
324 $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
325 $obj = XML::parseString($xmlHead . $notification['object']);
326 $notification['fname'] = $obj->title;
330 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'],
331 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO),
332 'url' => $notification['author-link'],
333 'text' => $this->l10n->t("%s is now friends with %s", $notification['author-name'], $notification['fname']),
334 'when' => $default_item_when,
335 'ago' => $default_item_ago,
336 'seen' => $notification['seen']
342 'label' => $default_item_label,
343 'link' => $default_item_link,
344 'image' => $default_item_image,
345 'url' => $default_item_url,
346 'text' => $default_item_text,
347 'when' => $default_item_when,
348 'ago' => $default_item_ago,
349 'seen' => $notification['seen']
353 $formattedNotifies[] = $formattedNotify;
356 return $formattedNotifies;
360 * Get network notifications
362 * @param bool $seen False => only include notifications into the query
363 * which aren't marked as "seen"
364 * @param int $start Start the query at this point
365 * @param int $limit Maximum number of query results
367 * @return array [string, array]
368 * string 'ident' => Notification identifier
369 * array 'notifications' => Network notifications
373 public function getNetworkList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
375 $ident = self::NETWORK;
378 $condition = ['wall' => false, 'uid' => local_user()];
381 $condition['unseen'] = true;
384 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
385 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
386 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
388 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
390 if ($this->dba->isResult($items)) {
391 $notifies = $this->formatList(Item::inArray($items), $ident);
395 'notifications' => $notifies,
403 * Get system notifications
405 * @param bool $seen False => only include notifications into the query
406 * which aren't marked as "seen"
407 * @param int $start Start the query at this point
408 * @param int $limit Maximum number of query results
410 * @return array [string, array]
411 * string 'ident' => Notification identifier
412 * array 'notifications' => System notifications
416 public function getSystemList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
418 $ident = self::SYSTEM;
421 $filter = ['uid' => local_user()];
423 $filter['seen'] = false;
427 $params['order'] = ['date' => 'DESC'];
428 $params['limit'] = [$start, $limit];
430 $stmtNotifies = $this->dba->select('notify',
431 ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
435 if ($this->dba->isResult($stmtNotifies)) {
436 $notifies = $this->formatList($this->dba->toArray($stmtNotifies), $ident);
440 'notifications' => $notifies,
448 * Get personal notifications
450 * @param bool $seen False => only include notifications into the query
451 * which aren't marked as "seen"
452 * @param int $start Start the query at this point
453 * @param int $limit Maximum number of query results
455 * @return array [string, array]
456 * string 'ident' => Notification identifier
457 * array 'notifications' => Personal notifications
461 public function getPersonalList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
463 $ident = self::PERSONAL;
466 $myurl = str_replace('http://', '', DI::app()->contact['nurl']);
467 $diasp_url = str_replace('/profile/', '/u/', $myurl);
469 $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)",
470 local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]'];
473 $condition[0] .= " AND `unseen`";
476 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
477 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
478 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
480 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
482 if ($this->dba->isResult($items)) {
483 $notifies = $this->formatList(Item::inArray($items), $ident);
487 'notifications' => $notifies,
495 * Get home notifications
497 * @param bool $seen False => only include notifications into the query
498 * which aren't marked as "seen"
499 * @param int $start Start the query at this point
500 * @param int $limit Maximum number of query results
502 * @return array [string, array]
503 * string 'ident' => Notification identifier
504 * array 'notifications' => Home notifications
508 public function getHomeList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
513 $condition = ['wall' => true, 'uid' => local_user()];
516 $condition['unseen'] = true;
519 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
520 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
521 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
523 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
525 if ($this->dba->isResult($items)) {
526 $notifies = $this->formatList(Item::inArray($items), $ident);
530 'notifications' => $notifies,
540 * @param bool $all If false only include introductions into the query
541 * which aren't marked as ignored
542 * @param int $start Start the query at this point
543 * @param int $limit Maximum number of query results
544 * @param int $id When set, only the introduction with this id is displayed
546 * @return array [string, array]
547 * string 'ident' => Notification identifier
548 * array 'notifications' => Introductions
550 * @throws ImagickException
553 public function getIntroList(bool $all = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT, int $id = 0)
555 /// @todo sanitize wording according to SELF::INTRO
556 $ident = 'introductions';
562 $sql_extra = " AND NOT `ignore` ";
565 $sql_extra .= " AND NOT `intro`.`blocked` ";
567 $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
570 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
571 $stmtNotifies = $this->dba->p(
572 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
573 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
574 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
575 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
576 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
577 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
579 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
580 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
581 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
582 WHERE `intro`.`uid` = ? $sql_extra
588 if ($this->dba->isResult($stmtNotifies)) {
589 $notifies = $this->formatIntroList($this->dba->toArray($stmtNotifies));
594 'notifications' => $notifies,
601 * Format the notification query in an usable array
603 * @param array $intros The array from the db query
605 * @return array with the introductions
606 * @throws HTTPException\InternalServerErrorException
607 * @throws ImagickException
609 private function formatIntroList(array $intros)
613 $formattedIntros = [];
615 foreach ($intros as $intro) {
616 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
617 // We have to distinguish between these two because they use different data.
618 // Contact suggestions
620 $return_addr = bin2hex(DI::app()->user['nickname'] . '@' .
621 $this->baseUrl->getHostName() .
622 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
625 'label' => 'friend_suggestion',
626 'notify_type' => $this->l10n->t('Friend Suggestion'),
627 'intro_id' => $intro['intro_id'],
628 'madeby' => $intro['name'],
629 'madeby_url' => $intro['url'],
630 'madeby_zrl' => Contact::magicLink($intro['url']),
631 'madeby_addr' => $intro['addr'],
632 'contact_id' => $intro['contact-id'],
633 'photo' => (!empty($intro['fphoto']) ? ProxyUtils::proxifyUrl($intro['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
634 'name' => $intro['fname'],
635 'url' => $intro['furl'],
636 'zrl' => Contact::magicLink($intro['furl']),
637 'hidden' => $intro['hidden'] == 1,
638 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
639 'knowyou' => $knowyou,
640 'note' => $intro['note'],
641 'request' => $intro['frequest'] . '?addr=' . $return_addr,
644 // Normal connection requests
646 $intro = $this->getMissingIntroData($intro);
648 if (empty($intro['url'])) {
652 // Don't show these data until you are connected. Diaspora is doing the same.
653 if ($intro['gnetwork'] === Protocol::DIASPORA) {
654 $intro['glocation'] = "";
655 $intro['gabout'] = "";
656 $intro['ggender'] = "";
659 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
660 'notify_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
661 'dfrn_id' => $intro['issued-id'],
662 'uid' => $_SESSION['uid'],
663 'intro_id' => $intro['intro_id'],
664 'contact_id' => $intro['contact-id'],
665 'photo' => (!empty($intro['photo']) ? ProxyUtils::proxifyUrl($intro['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
666 'name' => $intro['name'],
667 'location' => BBCode::convert($intro['glocation'], false),
668 'about' => BBCode::convert($intro['gabout'], false),
669 'keywords' => $intro['gkeywords'],
670 'gender' => $intro['ggender'],
671 'hidden' => $intro['hidden'] == 1,
672 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
673 'url' => $intro['url'],
674 'zrl' => Contact::magicLink($intro['url']),
675 'addr' => $intro['gaddr'],
676 'network' => $intro['gnetwork'],
677 'knowyou' => $intro['knowyou'],
678 'note' => $intro['note'],
682 $formattedIntros[] = $intro;
685 return $formattedIntros;
689 * Check for missing contact data and try to fetch the data from
692 * @param array $intro The input array with the intro data
694 * @return array The array with the intro data
695 * @throws HTTPException\InternalServerErrorException
697 private function getMissingIntroData(array $intro)
699 // If the network and the addr isn't available from the gcontact
700 // table entry, take the one of the contact table entry
701 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
702 $intro['gnetwork'] = $intro['network'];
704 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
705 $intro['gaddr'] = $intro['addr'];
708 // If the network and addr is still not available
709 // get the missing data data from other sources
710 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
711 $ret = Contact::getDetailsByURL($intro['url']);
713 if (empty($intro['gnetwork']) && !empty($ret['network'])) {
714 $intro['gnetwork'] = $ret['network'];
716 if (empty($intro['gaddr']) && !empty($ret['addr'])) {
717 $intro['gaddr'] = $ret['addr'];