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
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';
39 /** @var array Array of URL parameters */
41 self::NETWORK => 'network',
42 self::SYSTEM => 'system',
44 self::PERSONAL => 'personal',
45 self::INTRO => 'intros',
48 /** @var array Array of the allowed notifies and their printable name */
50 self::NETWORK => 'Network',
51 self::SYSTEM => 'System',
53 self::PERSONAL => 'Personal',
54 self::INTRO => 'Introductions',
57 /** @var array The array of access keys for notify pages */
62 self::PERSONAL => 'r',
70 /** @var App\Arguments */
72 /** @var App\BaseURL */
76 /** @var LoggerInterface */
79 public function __construct(Database $dba, L10n $l10n, App\Arguments $args, App\BaseURL $baseUrl,
80 IPConfig $pConfig, LoggerInterface $logger)
85 $this->baseUrl = $baseUrl;
86 $this->pConfig = $pConfig;
87 $this->logger = $logger;
91 * Set some extra properties to note array from db:
92 * - timestamp as int in default TZ
93 * - date_rel : relative date string
94 * - msg_html: message as html string
95 * - msg_plain: message as plain text string
97 * @param array $notes array of note arrays from db
99 * @return array Copy of input array with added properties
103 private function setExtra(array $notes)
106 foreach ($notes as $note) {
107 $local_time = DateTimeFormat::local($note['date']);
108 $note['timestamp'] = strtotime($local_time);
109 $note['date_rel'] = Temporal::getRelativeDate($note['date']);
110 $note['msg_html'] = BBCode::convert($note['msg'], false);
111 $note['msg_plain'] = explode("\n", trim(HTML::toPlaintext($note['msg_html'], 0)))[0];
119 * Get all notifications for local_user()
121 * @param array $filter optional Array "column name"=>value: filter query by columns values
122 * @param array $order optional Array to order by
123 * @param string $limit optional Query limits
125 * @return array|bool of results or false on errors
128 public function getAll(array $filter = [], array $order = ['date' => 'DESC'], string $limit = "")
132 $params['order'] = $order;
134 if (!empty($limit)) {
135 $params['limit'] = $limit;
138 $dbFilter = array_merge($filter, ['uid' => local_user()]);
140 $stmtNotifies = $this->dba->select('notify', [], $dbFilter, $params);
142 if ($this->dba->isResult($stmtNotifies)) {
143 return $this->setExtra($this->dba->toArray($stmtNotifies));
150 * Get one note for local_user() by $id value
152 * @param int $id identity
154 * @return array note values or null if not found
157 public function getByID(int $id)
159 $stmtNotify = $this->dba->selectFirst('notify', [], ['id' => $id, 'uid' => local_user()]);
160 if ($this->dba->isResult($stmtNotify)) {
161 return $this->setExtra([$stmtNotify])[0];
167 * set seen state of $note of local_user()
169 * @param array $note note array
170 * @param bool $seen optional true or false, default true
172 * @return bool true on success, false on errors
175 public function setSeen(array $note, bool $seen = true)
177 return $this->dba->update('notify', ['seen' => $seen], [
178 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
187 * Set seen state of all notifications of local_user()
189 * @param bool $seen optional true or false. default true
191 * @return bool true on success, false on error
194 public function setAllSeen(bool $seen = true)
196 return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]);
200 * List of pages for the Notifications TabBar
202 * @return array with with notifications TabBar data
205 public function getTabs()
207 $selected = $this->args->get(1, '');
211 foreach (self::URL_TYPES as $type => $url) {
213 'label' => $this->l10n->t(self::PRINT_TYPES[$type]),
214 'url' => 'notifications/' . $url,
215 'sel' => (($selected == $url) ? 'active' : ''),
216 'id' => $type . '-tab',
217 'accesskey' => self::ACCESS_KEYS[$type],
225 * Format the notification query in an usable array
227 * @param array $notifies The array from the db query
228 * @param string $ident The notifications identifier (e.g. network)
231 * string 'label' => The type of the notification
232 * string 'link' => URL to the source
233 * string 'image' => The avatar image
234 * string 'url' => The profile url of the contact
235 * string 'text' => The notification text
236 * string 'when' => The date of the notification
237 * string 'ago' => T relative date of the notification
238 * bool 'seen' => Is the notification marked as "seen"
241 private function formatList(array $notifies, string $ident = "")
243 $formattedNotifies = [];
245 foreach ($notifies as $notify) {
246 // Because we use different db tables for the notification query
247 // we have sometimes $notify['unseen'] and sometimes $notify['seen].
248 // So we will have to transform $notify['unseen']
249 if (array_key_exists('unseen', $notify)) {
250 $notify['seen'] = ($notify['unseen'] > 0 ? false : true);
253 // For feed items we use the user's contact, since the avatar is mostly self choosen.
254 if (!empty($notify['network']) && $notify['network'] == Protocol::FEED) {
255 $notify['author-avatar'] = $notify['contact-avatar'];
258 // Depending on the identifier of the notification we need to use different defaults
261 $default_item_label = 'notify';
262 $default_item_link = $this->baseUrl->get(true) . '/notify/view/' . $notify['id'];
263 $default_item_image = ProxyUtils::proxifyUrl($notify['photo'], false, ProxyUtils::SIZE_MICRO);
264 $default_item_url = $notify['url'];
265 $default_item_text = strip_tags(BBCode::convert($notify['msg']));
266 $default_item_when = DateTimeFormat::local($notify['date'], 'r');
267 $default_item_ago = Temporal::getRelativeDate($notify['date']);
271 $default_item_label = 'comment';
272 $default_item_link = $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'];
273 $default_item_image = ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO);
274 $default_item_url = $notify['author-link'];
275 $default_item_text = $this->l10n->t("%s commented on %s's post", $notify['author-name'], $notify['parent-author-name']);
276 $default_item_when = DateTimeFormat::local($notify['created'], 'r');
277 $default_item_ago = Temporal::getRelativeDate($notify['created']);
281 $default_item_label = (($notify['id'] == $notify['parent']) ? 'post' : 'comment');
282 $default_item_link = $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'];
283 $default_item_image = ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO);
284 $default_item_url = $notify['author-link'];
285 $default_item_text = (($notify['id'] == $notify['parent'])
286 ? $this->l10n->t("%s created a new post", $notify['author-name'])
287 : $this->l10n->t("%s commented on %s's post", $notify['author-name'], $notify['parent-author-name']));
288 $default_item_when = DateTimeFormat::local($notify['created'], 'r');
289 $default_item_ago = Temporal::getRelativeDate($notify['created']);
292 // Transform the different types of notification in an usable array
293 switch ($notify['verb']) {
297 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
298 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
299 'url' => $notify['author-link'],
300 'text' => $this->l10n->t("%s liked %s's post", $notify['author-name'], $notify['parent-author-name']),
301 'when' => $default_item_when,
302 'ago' => $default_item_ago,
303 'seen' => $notify['seen']
307 case Activity::DISLIKE:
309 'label' => 'dislike',
310 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
311 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
312 'url' => $notify['author-link'],
313 'text' => $this->l10n->t("%s disliked %s's post", $notify['author-name'], $notify['parent-author-name']),
314 'when' => $default_item_when,
315 'ago' => $default_item_ago,
316 'seen' => $notify['seen']
320 case Activity::ATTEND:
323 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
324 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
325 'url' => $notify['author-link'],
326 'text' => $this->l10n->t("%s is attending %s's event", $notify['author-name'], $notify['parent-author-name']),
327 'when' => $default_item_when,
328 'ago' => $default_item_ago,
329 'seen' => $notify['seen']
333 case Activity::ATTENDNO:
335 'label' => 'attendno',
336 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
337 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
338 'url' => $notify['author-link'],
339 'text' => $this->l10n->t("%s is not attending %s's event", $notify['author-name'], $notify['parent-author-name']),
340 'when' => $default_item_when,
341 'ago' => $default_item_ago,
342 'seen' => $notify['seen']
346 case Activity::ATTENDMAYBE:
348 'label' => 'attendmaybe',
349 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
350 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
351 'url' => $notify['author-link'],
352 'text' => $this->l10n->t("%s may attend %s's event", $notify['author-name'], $notify['parent-author-name']),
353 'when' => $default_item_when,
354 'ago' => $default_item_ago,
355 'seen' => $notify['seen']
359 case Activity::FRIEND:
360 if (!isset($notify['object'])) {
363 'link' => $default_item_link,
364 'image' => $default_item_image,
365 'url' => $default_item_url,
366 'text' => $default_item_text,
367 'when' => $default_item_when,
368 'ago' => $default_item_ago,
369 'seen' => $notify['seen']
373 /// @todo Check if this part here is used at all
374 $this->logger->info('Complete data.', ['notify' => $notify, 'callStack' => System::callstack(20)]);
376 $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
377 $obj = XML::parseString($xmlHead . $notify['object']);
378 $notify['fname'] = $obj->title;
382 'link' => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
383 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
384 'url' => $notify['author-link'],
385 'text' => $this->l10n->t("%s is now friends with %s", $notify['author-name'], $notify['fname']),
386 'when' => $default_item_when,
387 'ago' => $default_item_ago,
388 'seen' => $notify['seen']
394 'label' => $default_item_label,
395 'link' => $default_item_link,
396 'image' => $default_item_image,
397 'url' => $default_item_url,
398 'text' => $default_item_text,
399 'when' => $default_item_when,
400 'ago' => $default_item_ago,
401 'seen' => $notify['seen']
405 $formattedNotifies[] = $formattedNotify;
408 return $formattedNotifies;
412 * Get network notifications
414 * @param bool $seen False => only include notifications into the query
415 * which aren't marked as "seen"
416 * @param int $start Start the query at this point
417 * @param int $limit Maximum number of query results
419 * @return array [string, array]
420 * string 'ident' => Notification identifier
421 * array 'notifications' => Network notifications
425 public function getNetworkList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
427 $ident = self::NETWORK;
430 $condition = ['wall' => false, 'uid' => local_user()];
433 $condition['unseen'] = true;
436 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
437 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
438 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
440 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
442 if ($this->dba->isResult($items)) {
443 $notifies = $this->formatList(Item::inArray($items), $ident);
447 'notifications' => $notifies,
455 * Get system notifications
457 * @param bool $seen False => only include notifications into the query
458 * which aren't marked as "seen"
459 * @param int $start Start the query at this point
460 * @param int $limit Maximum number of query results
462 * @return array [string, array]
463 * string 'ident' => Notification identifier
464 * array 'notifications' => System notifications
468 public function getSystemList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
470 $ident = self::SYSTEM;
473 $filter = ['uid' => local_user()];
475 $filter['seen'] = false;
479 $params['order'] = ['date' => 'DESC'];
480 $params['limit'] = [$start, $limit];
482 $stmtNotifies = $this->dba->select('notify',
483 ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
487 if ($this->dba->isResult($stmtNotifies)) {
488 $notifies = $this->formatList($this->dba->toArray($stmtNotifies), $ident);
492 'notifications' => $notifies,
500 * Get personal notifications
502 * @param bool $seen False => only include notifications into the query
503 * which aren't marked as "seen"
504 * @param int $start Start the query at this point
505 * @param int $limit Maximum number of query results
507 * @return array [string, array]
508 * string 'ident' => Notification identifier
509 * array 'notifications' => Personal notifications
513 public function getPersonalList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
515 $ident = self::PERSONAL;
518 $myurl = str_replace('http://', '', DI::app()->contact['nurl']);
519 $diasp_url = str_replace('/profile/', '/u/', $myurl);
521 $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)",
522 local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]'];
525 $condition[0] .= " AND `unseen`";
528 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
529 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
530 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
532 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
534 if ($this->dba->isResult($items)) {
535 $notifies = $this->formatList(Item::inArray($items), $ident);
539 'notifications' => $notifies,
547 * Get home notifications
549 * @param bool $seen False => only include notifications into the query
550 * which aren't marked as "seen"
551 * @param int $start Start the query at this point
552 * @param int $limit Maximum number of query results
554 * @return array [string, array]
555 * string 'ident' => Notification identifier
556 * array 'notifications' => Home notifications
560 public function getHomeList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
565 $condition = ['wall' => true, 'uid' => local_user()];
568 $condition['unseen'] = true;
571 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
572 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
573 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
575 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
577 if ($this->dba->isResult($items)) {
578 $notifies = $this->formatList(Item::inArray($items), $ident);
582 'notifications' => $notifies,
592 * @param bool $all If false only include introductions into the query
593 * which aren't marked as ignored
594 * @param int $start Start the query at this point
595 * @param int $limit Maximum number of query results
596 * @param int $id When set, only the introduction with this id is displayed
598 * @return array [string, array]
599 * string 'ident' => Notification identifier
600 * array 'notifications' => Introductions
602 * @throws ImagickException
605 public function getIntroList(bool $all = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT, int $id = 0)
607 /// @todo sanitize wording according to SELF::INTRO
608 $ident = 'introductions';
614 $sql_extra = " AND NOT `ignore` ";
617 $sql_extra .= " AND NOT `intro`.`blocked` ";
619 $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
622 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
623 $stmtNotifies = $this->dba->p(
624 "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
625 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
626 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
627 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
628 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
629 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
631 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
632 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
633 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
634 WHERE `intro`.`uid` = ? $sql_extra
640 if ($this->dba->isResult($stmtNotifies)) {
641 $notifies = $this->formatIntroList($this->dba->toArray($stmtNotifies));
646 'notifications' => $notifies,
653 * Format the notification query in an usable array
655 * @param array $intros The array from the db query
657 * @return array with the introductions
658 * @throws HTTPException\InternalServerErrorException
659 * @throws ImagickException
661 private function formatIntroList(array $intros)
665 $formattedIntros = [];
667 foreach ($intros as $intro) {
668 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
669 // We have to distinguish between these two because they use different data.
670 // Contact suggestions
672 $return_addr = bin2hex(DI::app()->user['nickname'] . '@' .
673 $this->baseUrl->getHostName() .
674 (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
677 'label' => 'friend_suggestion',
678 'notify_type' => $this->l10n->t('Friend Suggestion'),
679 'intro_id' => $intro['intro_id'],
680 'madeby' => $intro['name'],
681 'madeby_url' => $intro['url'],
682 'madeby_zrl' => Contact::magicLink($intro['url']),
683 'madeby_addr' => $intro['addr'],
684 'contact_id' => $intro['contact-id'],
685 'photo' => (!empty($intro['fphoto']) ? ProxyUtils::proxifyUrl($intro['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
686 'name' => $intro['fname'],
687 'url' => $intro['furl'],
688 'zrl' => Contact::magicLink($intro['furl']),
689 'hidden' => $intro['hidden'] == 1,
690 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
691 'knowyou' => $knowyou,
692 'note' => $intro['note'],
693 'request' => $intro['frequest'] . '?addr=' . $return_addr,
696 // Normal connection requests
698 $intro = $this->getMissingIntroData($intro);
700 if (empty($intro['url'])) {
704 // Don't show these data until you are connected. Diaspora is doing the same.
705 if ($intro['gnetwork'] === Protocol::DIASPORA) {
706 $intro['glocation'] = "";
707 $intro['gabout'] = "";
708 $intro['ggender'] = "";
711 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
712 'notify_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
713 'dfrn_id' => $intro['issued-id'],
714 'uid' => $_SESSION['uid'],
715 'intro_id' => $intro['intro_id'],
716 'contact_id' => $intro['contact-id'],
717 'photo' => (!empty($intro['photo']) ? ProxyUtils::proxifyUrl($intro['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
718 'name' => $intro['name'],
719 'location' => BBCode::convert($intro['glocation'], false),
720 'about' => BBCode::convert($intro['gabout'], false),
721 'keywords' => $intro['gkeywords'],
722 'gender' => $intro['ggender'],
723 'hidden' => $intro['hidden'] == 1,
724 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
725 'url' => $intro['url'],
726 'zrl' => Contact::magicLink($intro['url']),
727 'addr' => $intro['gaddr'],
728 'network' => $intro['gnetwork'],
729 'knowyou' => $intro['knowyou'],
730 'note' => $intro['note'],
734 $formattedIntros[] = $intro;
737 return $formattedIntros;
741 * Check for missing contact data and try to fetch the data from
744 * @param array $intro The input array with the intro data
746 * @return array The array with the intro data
747 * @throws HTTPException\InternalServerErrorException
749 private function getMissingIntroData(array $intro)
751 // If the network and the addr isn't available from the gcontact
752 // table entry, take the one of the contact table entry
753 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
754 $intro['gnetwork'] = $intro['network'];
756 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
757 $intro['gaddr'] = $intro['addr'];
760 // If the network and addr is still not available
761 // get the missing data data from other sources
762 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
763 $ret = Contact::getDetailsByURL($intro['url']);
765 if (empty($intro['gnetwork']) && !empty($ret['network'])) {
766 $intro['gnetwork'] = $ret['network'];
768 if (empty($intro['gaddr']) && !empty($ret['addr'])) {
769 $intro['gaddr'] = $ret['addr'];