From 0850fb88dd82d53bd12b8d2850107a5ef07f9473 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 25 Jan 2020 02:01:49 +0100 Subject: [PATCH 1/5] ReWork Notification Model/Module/Object/Repository/Factory - Introduce Repository for interaction with "notify" table - Introduce Factory for read-only notification objects (they're just loosely based on notification the table!) - Introduce Objects for type-safe usage at the presentation layer - Reworked Model, which is now fully based on the notify table, including generated fields (cache, ..) --- include/api.php | 49 +- include/enotify.php | 78 +- mod/ping.php | 2 +- src/BaseModel.php | 5 + src/BaseRepository.php | 7 +- src/Collection/Notifications.php | 17 + src/DI.php | 20 +- .../Notification/IntroductionFactory.php | 205 +++++ .../Notification/NotificationFactory.php | 355 ++++++++ src/Model/Notification.php | 823 +++--------------- src/Module/BaseNotifications.php | 16 +- src/Module/Notifications/Introductions.php | 97 ++- src/Module/Notifications/Notification.php | 22 +- src/Module/Notifications/Notifications.php | 52 +- src/Object/Notification/Introduction.php | 302 +++++++ src/Object/Notification/Notification.php | 123 +++ src/Repository/Notification.php | 91 ++ 17 files changed, 1413 insertions(+), 851 deletions(-) create mode 100644 src/Collection/Notifications.php create mode 100644 src/Factory/Notification/IntroductionFactory.php create mode 100644 src/Factory/Notification/NotificationFactory.php create mode 100644 src/Object/Notification/Introduction.php create mode 100644 src/Object/Notification/Notification.php create mode 100644 src/Repository/Notification.php diff --git a/include/api.php b/include/api.php index 3836caa19..cb0599d9a 100644 --- a/include/api.php +++ b/include/api.php @@ -5905,19 +5905,20 @@ function api_friendica_notification($type) if ($a->argc!==3) { throw new BadRequestException("Invalid argument count"); } - $notes = DI::notification()->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50); + + $notifications = DI::notification()->select([], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); if ($type == "xml") { $xmlnotes = []; - if (!empty($notes)) { - foreach ($notes as $note) { - $xmlnotes[] = ["@attributes" => $note]; + if (!empty($notifications)) { + foreach ($notifications as $notification) { + $xmlnotes[] = ["@attributes" => $notification->toArray()]; } } - $notes = $xmlnotes; + $notifications = $xmlnotes; } - return api_format_data("notes", $type, ['note' => $notes]); + return api_format_data("notes", $type, ['note' => $notifications->getArrayCopy()]); } /** @@ -5935,37 +5936,37 @@ function api_friendica_notification($type) */ function api_friendica_notification_seen($type) { - $a = DI::app(); + $a = DI::app(); $user_info = api_get_user($a); if (api_user() === false || $user_info === false) { throw new ForbiddenException(); } - if ($a->argc!==4) { + if ($a->argc !== 4) { throw new BadRequestException("Invalid argument count"); } $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0); - $nm = DI::notification(); - $note = $nm->getByID($id); - if (is_null($note)) { - throw new BadRequestException("Invalid argument"); - } + try { + $notification = DI::notification()->getByID($id); + $notification->setSeen(); - $nm->setSeen($note); - if ($note['otype']=='item') { - // would be really better with an ItemsManager and $im->getByID() :-P - $item = Item::selectFirstForUser(api_user(), [], ['id' => $note['iid'], 'uid' => api_user()]); - if (DBA::isResult($item)) { - // we found the item, return it to the user - $ret = api_format_items([$item], $user_info, false, $type); - $data = ['status' => $ret]; - return api_format_data("status", $type, $data); + if ($notification->otype == 'item') { + // would be really better with an ItemsManager and $im->getByID() :-P + $item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->iid, 'uid' => api_user()]); + if (DBA::isResult($item)) { + // we found the item, return it to the user + $ret = api_format_items([$item], $user_info, false, $type); + $data = ['status' => $ret]; + return api_format_data("status", $type, $data); + } + // the item can't be found, but we set the notification as seen, so we count this as a success } - // the item can't be found, but we set the note as seen, so we count this as a success + return api_format_data('result', $type, ['result' => "success"]); + } catch (NotFoundException $e) { + throw new BadRequestException('Invalid argument'); } - return api_format_data('result', $type, ['result' => "success"]); } /// @TODO move to top of file or somewhere better diff --git a/include/enotify.php b/include/enotify.php index 403688af6..32f32afec 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -482,47 +482,25 @@ function notification($params) $notify_id = 0; if ($show_in_notification_page) { - Logger::log("adding notification entry", Logger::DEBUG); + $notification = DI::notification()->insert([ + 'name' => $params['source_name'], + 'url' => $params['source_link'], + 'photo' => $params['source_photo'], + 'uid' => $params['uid'], + 'iid' => $item_id, + 'parent' => $parent_id, + 'type' => $params['type'], + 'verb' => $params['verb'], + 'otype' => $params['otype'], + ]); - /// @TODO One statement is enough - $datarray = []; - $datarray['name'] = $params['source_name']; - $datarray['name_cache'] = strip_tags(BBCode::convert($params['source_name'])); - $datarray['url'] = $params['source_link']; - $datarray['photo'] = $params['source_photo']; - $datarray['date'] = DateTimeFormat::utcNow(); - $datarray['uid'] = $params['uid']; - $datarray['link'] = $itemlink; - $datarray['iid'] = $item_id; - $datarray['parent'] = $parent_id; - $datarray['type'] = $params['type']; - $datarray['verb'] = $params['verb']; - $datarray['otype'] = $params['otype']; - $datarray['abort'] = false; + $notification->link = DI::baseUrl() . '/notification/view/' . $notification->id; + $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]); - Hook::callAll('enotify_store', $datarray); + DI::notification()->update($notification); - if ($datarray['abort']) { - return false; - } - - // create notification entry in DB - $fields = ['name' => $datarray['name'], 'url' => $datarray['url'], - 'photo' => $datarray['photo'], 'date' => $datarray['date'], 'uid' => $datarray['uid'], - 'link' => $datarray['link'], 'iid' => $datarray['iid'], 'parent' => $datarray['parent'], - 'type' => $datarray['type'], 'verb' => $datarray['verb'], 'otype' => $datarray['otype'], - 'name_cache' => $datarray["name_cache"]]; - DBA::insert('notify', $fields); - - $notify_id = DBA::lastInsertId(); - - $itemlink = DI::baseUrl().'/notification/view/'.$notify_id; - $msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $itemlink]); - $msg_cache = format_notification_message($datarray['name_cache'], strip_tags(BBCode::convert($msg))); - - $fields = ['msg' => $msg, 'msg_cache' => $msg_cache]; - $condition = ['id' => $notify_id, 'uid' => $params['uid']]; - DBA::update('notify', $fields, $condition); + $itemlink = $notification->link; + $notify_id = $notification->id; } // send email notification if notification preferences permit @@ -732,27 +710,3 @@ function check_item_notification($itemid, $uid, $notification_type) { notification($params); } - -/** - * Formats a notification message with the notification author - * - * Replace the name with {0} but ensure to make that only once. The {0} is used - * later and prints the name in bold. - * - * @param string $name - * @param string $message - * @return string Formatted message - */ -function format_notification_message($name, $message) { - if ($name != '') { - $pos = strpos($message, $name); - } else { - $pos = false; - } - - if ($pos !== false) { - $message = substr_replace($message, '{0}', $pos, strlen($name)); - } - - return $message; -} diff --git a/mod/ping.php b/mod/ping.php index 0c758bd4f..a786d379f 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -433,7 +433,7 @@ function ping_get_notifications($uid) $notification["message"] = $notification["msg_cache"]; } else { $notification["name"] = strip_tags(BBCode::convert($notification["name"])); - $notification["message"] = format_notification_message($notification["name"], strip_tags(BBCode::convert($notification["msg"]))); + $notification["message"] = Friendica\Model\Notification::formatMessage($notification["name"], strip_tags(BBCode::convert($notification["msg"]))); q( "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d", diff --git a/src/BaseModel.php b/src/BaseModel.php index b2dc7eeda..4e4259170 100644 --- a/src/BaseModel.php +++ b/src/BaseModel.php @@ -53,6 +53,11 @@ abstract class BaseModel return $this->originalData; } + public function resetOriginalData() + { + $this->originalData = $this->data; + } + /** * Performance-improved model creation in a loop * diff --git a/src/BaseRepository.php b/src/BaseRepository.php index c0bcab18f..cce1c50c1 100644 --- a/src/BaseRepository.php +++ b/src/BaseRepository.php @@ -122,7 +122,12 @@ abstract class BaseRepository extends BaseFactory */ public function update(BaseModel $model) { - return $this->dba->update(static::$table_name, $model->toArray(), ['id' => $model->id], $model->getOriginalData()); + if ($this->dba->update(static::$table_name, $model->toArray(), ['id' => $model->id], $model->getOriginalData())) { + $model->resetOriginalData(); + return true; + } + + return false; } /** diff --git a/src/Collection/Notifications.php b/src/Collection/Notifications.php new file mode 100644 index 000000000..1c04bdf4d --- /dev/null +++ b/src/Collection/Notifications.php @@ -0,0 +1,17 @@ +create(Factory\Mastodon\Relationship::class); } + /** + * @return \Friendica\Factory\Notification\NotificationFactory + */ + public static function factNotification() + { + return self::$dice->create(Factory\Notification\NotificationFactory::class); + } + + /** + * @return \Friendica\Factory\Notification\IntroductionFactory + */ + public static function factNotIntro() + { + return self::$dice->create(Factory\Notification\IntroductionFactory::class); + } + // // "Model" namespace instances // @@ -257,11 +273,11 @@ abstract class DI } /** - * @return Model\Notification + * @return Repository\Notification */ public static function notification() { - return self::$dice->create(Model\Notification::class); + return self::$dice->create(Repository\Notification::class); } /** diff --git a/src/Factory/Notification/IntroductionFactory.php b/src/Factory/Notification/IntroductionFactory.php new file mode 100644 index 000000000..73f288326 --- /dev/null +++ b/src/Factory/Notification/IntroductionFactory.php @@ -0,0 +1,205 @@ +dba = $dba; + $this->baseUrl = $baseUrl; + $this->l10n = $l10n; + $this->pConfig = $pConfig; + $this->session = $session; + $this->nick = $app->user['nickname'] ?? ''; + } + + /** + * Get introductions + * + * @param bool $all If false only include introductions into the query + * which aren't marked as ignored + * @param int $start Start the query at this point + * @param int $limit Maximum number of query results + * @param int $id When set, only the introduction with this id is displayed + * + * @return Introduction[] + */ + public function getIntroList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0) + { + $sql_extra = ""; + + if (empty($id)) { + if (!$all) { + $sql_extra = " AND NOT `ignore` "; + } + + $sql_extra .= " AND NOT `intro`.`blocked` "; + } else { + $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id)); + } + + $formattedNotifications = []; + + try { + /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact + $stmtNotifications = $this->dba->p( + "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, + `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`, + `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`, + `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`, + `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`, + `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr` + FROM `intro` + LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` + LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl` + LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` + WHERE `intro`.`uid` = ? $sql_extra + LIMIT ?, ?", + $_SESSION['uid'], + $start, + $limit + ); + + while ($notification = $this->dba->fetch($stmtNotifications)) { + // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. + // We have to distinguish between these two because they use different data. + // Contact suggestions + if ($notification['fid'] ?? '') { + $return_addr = bin2hex($this->nick . '@' . + $this->baseUrl->getHostName() . + (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : '')); + + $formattedNotifications[] = new Introduction([ + 'label' => 'friend_suggestion', + 'str_type' => $this->l10n->t('Friend Suggestion'), + 'intro_id' => $notification['intro_id'], + 'madeby' => $notification['name'], + 'madeby_url' => $notification['url'], + 'madeby_zrl' => Contact::magicLink($notification['url']), + 'madeby_addr' => $notification['addr'], + 'contact_id' => $notification['contact-id'], + 'photo' => (!empty($notification['fphoto']) ? Proxy::proxifyUrl($notification['fphoto'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"), + 'name' => $notification['fname'], + 'url' => $notification['furl'], + 'zrl' => Contact::magicLink($notification['furl']), + 'hidden' => $notification['hidden'] == 1, + 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0), + 'note' => $notification['note'], + 'request' => $notification['frequest'] . '?addr=' . $return_addr]); + + // Normal connection requests + } else { + $notification = $this->getMissingIntroData($notification); + + if (empty($notification['url'])) { + continue; + } + + // Don't show these data until you are connected. Diaspora is doing the same. + if ($notification['gnetwork'] === Protocol::DIASPORA) { + $notification['glocation'] = ""; + $notification['gabout'] = ""; + $notification['ggender'] = ""; + } + + $formattedNotifications[] = new Introduction([ + 'label' => (($notification['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'), + 'str_type' => (($notification['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')), + 'dfrn_id' => $notification['issued-id'], + 'uid' => $this->session->get('uid'), + 'intro_id' => $notification['intro_id'], + 'contact_id' => $notification['contact-id'], + 'photo' => (!empty($notification['photo']) ? Proxy::proxifyUrl($notification['photo'], false, Proxy::SIZE_SMALL) : "images/person-300.jpg"), + 'name' => $notification['name'], + 'location' => BBCode::convert($notification['glocation'], false), + 'about' => BBCode::convert($notification['gabout'], false), + 'keywords' => $notification['gkeywords'], + 'gender' => $notification['ggender'], + 'hidden' => $notification['hidden'] == 1, + 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0), + 'url' => $notification['url'], + 'zrl' => Contact::magicLink($notification['url']), + 'addr' => $notification['gaddr'], + 'network' => $notification['gnetwork'], + 'knowyou' => $notification['knowyou'], + 'note' => $notification['note'], + ]); + } + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]); + } + + return $formattedNotifications; + } + + /** + * Check for missing contact data and try to fetch the data from + * from other sources + * + * @param array $intro The input array with the intro data + * + * @return array The array with the intro data + * + * @throws InternalServerErrorException + */ + private function getMissingIntroData(array $intro) + { + // If the network and the addr isn't available from the gcontact + // table entry, take the one of the contact table entry + if (empty($intro['gnetwork']) && !empty($intro['network'])) { + $intro['gnetwork'] = $intro['network']; + } + if (empty($intro['gaddr']) && !empty($intro['addr'])) { + $intro['gaddr'] = $intro['addr']; + } + + // If the network and addr is still not available + // get the missing data data from other sources + if (empty($intro['gnetwork']) || empty($intro['gaddr'])) { + $ret = Contact::getDetailsByURL($intro['url']); + + if (empty($intro['gnetwork']) && !empty($ret['network'])) { + $intro['gnetwork'] = $ret['network']; + } + if (empty($intro['gaddr']) && !empty($ret['addr'])) { + $intro['gaddr'] = $ret['addr']; + } + } + + return $intro; + } +} diff --git a/src/Factory/Notification/NotificationFactory.php b/src/Factory/Notification/NotificationFactory.php new file mode 100644 index 000000000..2516916d1 --- /dev/null +++ b/src/Factory/Notification/NotificationFactory.php @@ -0,0 +1,355 @@ +dba = $dba; + $this->notification = $notification; + $this->baseUrl = $baseUrl; + $this->l10n = $l10n; + $this->nurl = $app->contact['nurl'] ?? ''; + } + + /** + * Format the item query in an usable array + * + * @param array $item The item from the db query + * + * @return array The item, extended with the notification-specific information + * + * @throws InternalServerErrorException + * @throws Exception + */ + private function formatItem(array $item) + { + $item['seen'] = ($item['unseen'] > 0 ? false : true); + + // For feed items we use the user's contact, since the avatar is mostly self choosen. + if (!empty($item['network']) && $item['network'] == Protocol::FEED) { + $item['author-avatar'] = $item['contact-avatar']; + } + + $item['label'] = (($item['id'] == $item['parent']) ? 'post' : 'comment'); + $item['link'] = $this->baseUrl->get(true) . '/display/' . $item['parent-guid']; + $item['image'] = Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO); + $item['url'] = $item['author-link']; + $item['text'] = (($item['id'] == $item['parent']) + ? $this->l10n->t("%s created a new post", $item['author-name']) + : $this->l10n->t("%s commented on %s's post", $item['author-name'], $item['parent-author-name'])); + $item['when'] = DateTimeFormat::local($item['created'], 'r'); + $item['ago'] = Temporal::getRelativeDate($item['created']); + + return $item; + } + + /** + * @param array $item + * + * @return \Friendica\Object\Notification\Notification + * + * @throws InternalServerErrorException + */ + private function createFromItem(array $item) + { + $item = $this->formatItem($item); + + // Transform the different types of notification in an usable array + switch ($item['verb'] ?? '') { + case Activity::LIKE: + return new \Friendica\Object\Notification\Notification( + 'like', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s liked %s's post", $item['author-name'], $item['parent-author-name']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + case Activity::DISLIKE: + return new \Friendica\Object\Notification\Notification( + 'dislike', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s disliked %s's post", $item['author-name'], $item['parent-author-name']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + case Activity::ATTEND: + return new \Friendica\Object\Notification\Notification( + 'attend', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s is attending %s's event", $item['author-name'], $item['parent-author-name']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + case Activity::ATTENDNO: + return new \Friendica\Object\Notification\Notification( + 'attendno', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s is not attending %s's event", $item['author-name'], $item['parent-author-name']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + case Activity::ATTENDMAYBE: + return new \Friendica\Object\Notification\Notification( + 'attendmaybe', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s may attending %s's event", $item['author-name'], $item['parent-author-name']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + case Activity::FRIEND: + if (!isset($item['object'])) { + return new \Friendica\Object\Notification\Notification( + 'friend', + $item['link'], + $item['image'], + $item['url'], + $item['text'], + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + } + + $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; + $obj = XML::parseString($xmlHead . $item['object']); + $item['fname'] = $obj->title; + + return new \Friendica\Object\Notification\Notification( + 'friend', + $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + $item['author-link'], + $this->l10n->t("%s is now friends with %s", $item['author-name'], $item['fname']), + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + + default: + return new \Friendica\Object\Notification\Notification( + $item['label'], + $item['link'], + $item['image'], + $item['url'], + $item['text'], + $item['when'] ?? '', + $item['ago'] ?? '', + $item['seen'] ?? false); + break; + } + } + + /** + * Get system notifications + * + * @param bool $seen False => only include notifications into the query + * which aren't marked as "seen" + * @param int $start Start the query at this point + * @param int $limit Maximum number of query results + * + * @return \Friendica\Module\Notifications\Notification[] + */ + public function getSystemList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT) + { + $conditions = ['uid' => local_user()]; + + if (!$seen) { + $conditions['seen'] = false; + } + + $params = []; + $params['order'] = ['date' => 'DESC']; + $params['limit'] = [$start, $limit]; + + $formattedNotifications = []; + try { + $notifications = $this->notification->select($conditions, $params); + + foreach ($notifications as $notification) { + $formattedNotifications[] = new \Friendica\Object\Notification\Notification( + 'notification', + $this->baseUrl->get(true) . '/notification/view/' . $notification->id, + Proxy::proxifyUrl($notification->photo, false, Proxy::SIZE_MICRO), + $notification->url, + strip_tags(BBCode::convert($notification->msg)), + DateTimeFormat::local($notification->date, 'r'), + Temporal::getRelativeDate($notification->date), + $notification->seen); + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]); + } + + return $formattedNotifications; + } + + /** + * Get network notifications + * + * @param bool $seen False => only include notifications into the query + * which aren't marked as "seen" + * @param int $start Start the query at this point + * @param int $limit Maximum number of query results + * + * @return \Friendica\Object\Notification\Notification[] + */ + public function getNetworkList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT) + { + $conditions = ['wall' => false, 'uid' => local_user()]; + + if (!$seen) { + $conditions['unseen'] = true; + } + + $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', + 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; + + $formattedNotifications = []; + + try { + $items = Item::selectForUser(local_user(), $fields, $conditions, $params); + + while ($item = $this->dba->fetch($items)) { + $formattedNotifications[] = $this->createFromItem($item); + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]); + } + + return $formattedNotifications; + } + + /** + * Get personal notifications + * + * @param bool $seen False => only include notifications into the query + * which aren't marked as "seen" + * @param int $start Start the query at this point + * @param int $limit Maximum number of query results + * + * @return \Friendica\Object\Notification\Notification[] + */ + public function getPersonalList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT) + { + $myUrl = str_replace('http://', '', $this->nurl); + $diaspUrl = str_replace('/profile/', '/u/', $myUrl); + + $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)", + local_user(), public_contact(), $myUrl . '\\]', $diaspUrl . '\\]']; + + if (!$seen) { + $condition[0] .= " AND `unseen`"; + } + + $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', + 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; + + $formattedNotifications = []; + + try { + $items = Item::selectForUser(local_user(), $fields, $condition, $params); + + while ($item = $this->dba->fetch($items)) { + $formattedNotifications[] = $this->createFromItem($item); + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]); + } + + return $formattedNotifications; + } + + /** + * Get home notifications + * + * @param bool $seen False => only include notifications into the query + * which aren't marked as "seen" + * @param int $start Start the query at this point + * @param int $limit Maximum number of query results + * + * @return \Friendica\Object\Notification\Notification[] + */ + public function getHomeList(bool $seen = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT) + { + $condition = ['wall' => true, 'uid' => local_user()]; + + if (!$seen) { + $condition['unseen'] = true; + } + + $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', + 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; + $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; + + $formattedNotifications = []; + + try { + $items = Item::selectForUser(local_user(), $fields, $condition, $params); + + while ($item = $this->dba->fetch($items)) { + $item = $this->formatItem($item); + + // Overwrite specific fields, not default item format + $item['label'] = 'comment'; + $item['text'] = $this->l10n->t("%s commented on %s's post", $item['author-name'], $item['parent-author-name']); + + $formattedNotifications[] = $this->createFromItem($item); + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['conditions' => $condition, 'exception' => $e]); + } + + return $formattedNotifications; + } +} diff --git a/src/Model/Notification.php b/src/Model/Notification.php index e18486670..ab029f73b 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -3,721 +3,180 @@ namespace Friendica\Model; use Exception; -use Friendica\App; +use Friendica\BaseModel; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; -use Friendica\Core\PConfig\IPConfig; -use Friendica\Core\L10n; -use Friendica\Core\Protocol; -use Friendica\Core\System; use Friendica\Database\Database; -use Friendica\DI; -use Friendica\Protocol\Activity; +use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Util\DateTimeFormat; -use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Temporal; -use Friendica\Util\XML; -use ImagickException; use Psr\Log\LoggerInterface; -use Friendica\Network\HTTPException; /** - * Methods for read and write notifications from/to database - * or for formatting notifications + * Model for an entry in the notify table + * - Including additional calculated properties + * + * @property string hash + * @property integer type + * @property string name Full name of the contact subject + * @property string url Profile page URL of the contact subject + * @property string photo Profile photo URL of the contact subject + * @property string date YYYY-MM-DD hh:mm:ss local server time + * @property string msg + * @property integer uid Owner User Id + * @property string link Notification URL + * @property integer iid Item Id + * @property integer parent Parent Item Id + * @property boolean seen Whether the notification was read or not. + * @property string verb Verb URL (@see http://activitystrea.ms) + * @property string otype Subject type (`item`, `intro` or `mail`) + * + * @property-read string name_cache Full name of the contact subject + * @property-read string msg_cache Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name. + * + * @property-read integer timestamp Unix timestamp + * @property-read string dateRel Time since the note was posted, eg "1 hour ago" + * @property-read string $msg_html + * @property-read string $msg_plain */ -final class Notification +class Notification extends BaseModel { - /** @var int The default limit of notifications per page */ - const DEFAULT_PAGE_LIMIT = 80; + /** @var \Friendica\Repository\Notification */ + private $repo; + /** @var $this */ + private $parentInst; - const NETWORK = 'network'; - const SYSTEM = 'system'; - const PERSONAL = 'personal'; - const HOME = 'home'; - const INTRO = 'intro'; - - /** @var Database */ - private $dba; - /** @var L10n */ - private $l10n; - /** @var App\Arguments */ - private $args; - /** @var App\BaseURL */ - private $baseUrl; - /** @var IPConfig */ - private $pConfig; - /** @var LoggerInterface */ - private $logger; - - public function __construct(Database $dba, L10n $l10n, App\Arguments $args, App\BaseURL $baseUrl, - IPConfig $pConfig, LoggerInterface $logger) + public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = []) { - $this->dba = $dba; - $this->l10n = $l10n; - $this->args = $args; - $this->baseUrl = $baseUrl; - $this->pConfig = $pConfig; - $this->logger = $logger; + parent::__construct($dba, $logger, $data); + + $this->repo = $repo; + + $this->setNameCache(); + $this->setTimestamp(); + $this->setMsg(); } /** - * Set some extra properties to note array from db: + * Set the notification as seen + * + * @param bool $seen true, if seen + * + * @return bool True, if the seen state could be saved + */ + public function setSeen(bool $seen = true) + { + $this->seen = $seen; + try { + return $this->repo->update($this); + } catch (Exception $e) { + $this->logger->warning('Update failed.', ['$this' => $this, 'exception' => $e]); + return false; + } + } + + /** + * Set some extra properties to the notification from db: * - timestamp as int in default TZ * - date_rel : relative date string + */ + private function setTimestamp() + { + try { + $this->timestamp = strtotime(DateTimeFormat::local($this->date)); + } catch (Exception $e) { + } + $this->dateRel = Temporal::getRelativeDate($this->date); + } + + /** + * Sets the pre-formatted name (caching) + * + * @throws InternalServerErrorException + */ + private function setNameCache() + { + $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? '')); + } + + /** + * Set some extra properties to the notification from db: * - msg_html: message as html string * - msg_plain: message as plain text string - * - * @param array $notes array of note arrays from db - * - * @return array Copy of input array with added properties - * - * @throws Exception + * - msg_cache: The pre-formatted message (caching) */ - private function setExtra(array $notes) + private function setMsg() { - $retNotes = []; - foreach ($notes as $note) { - $local_time = DateTimeFormat::local($note['date']); - $note['timestamp'] = strtotime($local_time); - $note['date_rel'] = Temporal::getRelativeDate($note['date']); - $note['msg_html'] = BBCode::convert($note['msg'], false); - $note['msg_plain'] = explode("\n", trim(HTML::toPlaintext($note['msg_html'], 0)))[0]; - - $retNotes[] = $note; + try { + $this->msg_html = BBCode::convert($this->msg, false); + $this->msg_plain = explode("\n", trim(HTML::toPlaintext($this->msg_html, 0)))[0]; + $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg))); + } catch (InternalServerErrorException $e) { + } + } + + public function __get($name) + { + $this->checkValid(); + + $return = null; + + switch ($name) { + case 'parent': + if (!empty($this->parent)) { + $this->parentInst = $this->parentInst ?? $this->repo->getByID($this->parent); + + $return = $this->parentInst; + } + break; + default: + $return = parent::__get($name); + break; + } + + return $return; + } + + public function __set($name, $value) + { + parent::__set($name, $value); + + if ($name == 'date') { + $this->setTimestamp(); + } + + if ($name == 'msg') { + $this->setMsg(); + } + + if ($name == 'source_name') { + $this->setNameCache(); } - return $retNotes; } /** - * Get all notifications for local_user() + * Formats a notification message with the notification author * - * @param array $filter optional Array "column name"=>value: filter query by columns values - * @param array $order optional Array to order by - * @param string $limit optional Query limits + * Replace the name with {0} but ensure to make that only once. The {0} is used + * later and prints the name in bold. * - * @return array|bool of results or false on errors - * @throws Exception + * @param string $name + * @param string $message + * + * @return string Formatted message */ - public function getAll(array $filter = [], array $order = ['date' => 'DESC'], string $limit = "") + public static function formatMessage($name, $message) { - $params = []; - - $params['order'] = $order; - - if (!empty($limit)) { - $params['limit'] = $limit; - } - - $dbFilter = array_merge($filter, ['uid' => local_user()]); - - $stmtNotifications = $this->dba->select('notify', [], $dbFilter, $params); - - if ($this->dba->isResult($stmtNotifications)) { - return $this->setExtra($this->dba->toArray($stmtNotifications)); - } - - return false; - } - - /** - * Get one note for local_user() by $id value - * - * @param int $id identity - * - * @return array note values or null if not found - * @throws Exception - */ - public function getByID(int $id) - { - $stmtNotify = $this->dba->selectFirst('notify', [], ['id' => $id, 'uid' => local_user()]); - if ($this->dba->isResult($stmtNotify)) { - return $this->setExtra([$stmtNotify])[0]; - } - return null; - } - - /** - * set seen state of $note of local_user() - * - * @param array $note note array - * @param bool $seen optional true or false, default true - * - * @return bool true on success, false on errors - * @throws Exception - */ - public function setSeen(array $note, bool $seen = true) - { - return $this->dba->update('notify', ['seen' => $seen], [ - '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?', - $note['link'], - $note['parent'], - $note['otype'], - local_user() - ]); - } - - /** - * Set seen state of all notifications of local_user() - * - * @param bool $seen optional true or false. default true - * - * @return bool true on success, false on error - * @throws Exception - */ - public function setAllSeen(bool $seen = true) - { - return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]); - } - - /** - * Format the notification query in an usable array - * - * @param array $notifications The array from the db query - * @param string $ident The notifications identifier (e.g. network) - * - * @return array - * string 'label' => The type of the notification - * string 'link' => URL to the source - * string 'image' => The avatar image - * string 'url' => The profile url of the contact - * string 'text' => The notification text - * string 'when' => The date of the notification - * string 'ago' => T relative date of the notification - * bool 'seen' => Is the notification marked as "seen" - * @throws Exception - */ - private function formatList(array $notifications, string $ident = "") - { - $formattedNotifications = []; - - foreach ($notifications as $notification) { - // Because we use different db tables for the notification query - // we have sometimes $notification['unseen'] and sometimes $notification['seen]. - // So we will have to transform $notification['unseen'] - if (array_key_exists('unseen', $notification)) { - $notification['seen'] = ($notification['unseen'] > 0 ? false : true); - } - - // For feed items we use the user's contact, since the avatar is mostly self choosen. - if (!empty($notification['network']) && $notification['network'] == Protocol::FEED) { - $notification['author-avatar'] = $notification['contact-avatar']; - } - - // Depending on the identifier of the notification we need to use different defaults - switch ($ident) { - case self::SYSTEM: - $default_item_label = 'notification'; - $default_item_link = $this->baseUrl->get(true) . '/notification/view/' . $notification['id']; - $default_item_image = ProxyUtils::proxifyUrl($notification['photo'], false, ProxyUtils::SIZE_MICRO); - $default_item_url = $notification['url']; - $default_item_text = strip_tags(BBCode::convert($notification['msg'])); - $default_item_when = DateTimeFormat::local($notification['date'], 'r'); - $default_item_ago = Temporal::getRelativeDate($notification['date']); - break; - - case self::HOME: - $default_item_label = 'comment'; - $default_item_link = $this->baseUrl->get(true) . '/display/' . $notification['parent-guid']; - $default_item_image = ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO); - $default_item_url = $notification['author-link']; - $default_item_text = $this->l10n->t("%s commented on %s's post", $notification['author-name'], $notification['parent-author-name']); - $default_item_when = DateTimeFormat::local($notification['created'], 'r'); - $default_item_ago = Temporal::getRelativeDate($notification['created']); - break; - - default: - $default_item_label = (($notification['id'] == $notification['parent']) ? 'post' : 'comment'); - $default_item_link = $this->baseUrl->get(true) . '/display/' . $notification['parent-guid']; - $default_item_image = ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO); - $default_item_url = $notification['author-link']; - $default_item_text = (($notification['id'] == $notification['parent']) - ? $this->l10n->t("%s created a new post", $notification['author-name']) - : $this->l10n->t("%s commented on %s's post", $notification['author-name'], $notification['parent-author-name'])); - $default_item_when = DateTimeFormat::local($notification['created'], 'r'); - $default_item_ago = Temporal::getRelativeDate($notification['created']); - } - - // Transform the different types of notification in an usable array - switch ($notification['verb']) { - case Activity::LIKE: - $formattedNotification = [ - 'label' => 'like', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s liked %s's post", $notification['author-name'], $notification['parent-author-name']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - case Activity::DISLIKE: - $formattedNotification = [ - 'label' => 'dislike', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s disliked %s's post", $notification['author-name'], $notification['parent-author-name']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - case Activity::ATTEND: - $formattedNotification = [ - 'label' => 'attend', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s is attending %s's event", $notification['author-name'], $notification['parent-author-name']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - case Activity::ATTENDNO: - $formattedNotification = [ - 'label' => 'attendno', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s is not attending %s's event", $notification['author-name'], $notification['parent-author-name']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - case Activity::ATTENDMAYBE: - $formattedNotification = [ - 'label' => 'attendmaybe', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s may attend %s's event", $notification['author-name'], $notification['parent-author-name']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - case Activity::FRIEND: - if (!isset($notification['object'])) { - $formattedNotification = [ - 'label' => 'friend', - 'link' => $default_item_link, - 'image' => $default_item_image, - 'url' => $default_item_url, - 'text' => $default_item_text, - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - } - /// @todo Check if this part here is used at all - $this->logger->info('Complete data.', ['notification' => $notification, 'callStack' => System::callstack(20)]); - - $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; - $obj = XML::parseString($xmlHead . $notification['object']); - $notification['fname'] = $obj->title; - - $formattedNotification = [ - 'label' => 'friend', - 'link' => $this->baseUrl->get(true) . '/display/' . $notification['parent-guid'], - 'image' => ProxyUtils::proxifyUrl($notification['author-avatar'], false, ProxyUtils::SIZE_MICRO), - 'url' => $notification['author-link'], - 'text' => $this->l10n->t("%s is now friends with %s", $notification['author-name'], $notification['fname']), - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - break; - - default: - $formattedNotification = [ - 'label' => $default_item_label, - 'link' => $default_item_link, - 'image' => $default_item_image, - 'url' => $default_item_url, - 'text' => $default_item_text, - 'when' => $default_item_when, - 'ago' => $default_item_ago, - 'seen' => $notification['seen'] - ]; - } - - $formattedNotifications[] = $formattedNotification; - } - - return $formattedNotifications; - } - - /** - * Get network notifications - * - * @param bool $seen False => only include notifications into the query - * which aren't marked as "seen" - * @param int $start Start the query at this point - * @param int $limit Maximum number of query results - * - * @return array [string, array] - * string 'ident' => Notification identifier - * array 'notifications' => Network notifications - * - * @throws Exception - */ - public function getNetworkList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT) - { - $ident = self::NETWORK; - $notifications = []; - - $condition = ['wall' => false, 'uid' => local_user()]; - - if (!$seen) { - $condition['unseen'] = true; - } - - $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', - 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; - - $items = Item::selectForUser(local_user(), $fields, $condition, $params); - - if ($this->dba->isResult($items)) { - $notifications = $this->formatList(Item::inArray($items), $ident); - } - - $arr = [ - 'notifications' => $notifications, - 'ident' => $ident, - ]; - - return $arr; - } - - /** - * Get system notifications - * - * @param bool $seen False => only include notifications into the query - * which aren't marked as "seen" - * @param int $start Start the query at this point - * @param int $limit Maximum number of query results - * - * @return array [string, array] - * string 'ident' => Notification identifier - * array 'notifications' => System notifications - * - * @throws Exception - */ - public function getSystemList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT) - { - $ident = self::SYSTEM; - $notifications = []; - - $filter = ['uid' => local_user()]; - if (!$seen) { - $filter['seen'] = false; - } - - $params = []; - $params['order'] = ['date' => 'DESC']; - $params['limit'] = [$start, $limit]; - - $stmtNotifications = $this->dba->select('notify', - ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'], - $filter, - $params); - - if ($this->dba->isResult($stmtNotifications)) { - $notifications = $this->formatList($this->dba->toArray($stmtNotifications), $ident); - } - - $arr = [ - 'notifications' => $notifications, - 'ident' => $ident, - ]; - - return $arr; - } - - /** - * Get personal notifications - * - * @param bool $seen False => only include notifications into the query - * which aren't marked as "seen" - * @param int $start Start the query at this point - * @param int $limit Maximum number of query results - * - * @return array [string, array] - * string 'ident' => Notification identifier - * array 'notifications' => Personal notifications - * - * @throws Exception - */ - public function getPersonalList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT) - { - $ident = self::PERSONAL; - $notifications = []; - - $myurl = str_replace('http://', '', DI::app()->contact['nurl']); - $diasp_url = str_replace('/profile/', '/u/', $myurl); - - $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)", - local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]']; - - if (!$seen) { - $condition[0] .= " AND `unseen`"; - } - - $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', - 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; - - $items = Item::selectForUser(local_user(), $fields, $condition, $params); - - if ($this->dba->isResult($items)) { - $notifications = $this->formatList(Item::inArray($items), $ident); - } - - $arr = [ - 'notifications' => $notifications, - 'ident' => $ident, - ]; - - return $arr; - } - - /** - * Get home notifications - * - * @param bool $seen False => only include notifications into the query - * which aren't marked as "seen" - * @param int $start Start the query at this point - * @param int $limit Maximum number of query results - * - * @return array [string, array] - * string 'ident' => Notification identifier - * array 'notifications' => Home notifications - * - * @throws Exception - */ - public function getHomeList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT) - { - $ident = self::HOME; - $notifications = []; - - $condition = ['wall' => true, 'uid' => local_user()]; - - if (!$seen) { - $condition['unseen'] = true; - } - - $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar', - 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid']; - $params = ['order' => ['received' => true], 'limit' => [$start, $limit]]; - - $items = Item::selectForUser(local_user(), $fields, $condition, $params); - - if ($this->dba->isResult($items)) { - $notifications = $this->formatList(Item::inArray($items), $ident); - } - - $arr = [ - 'notifications' => $notifications, - 'ident' => $ident, - ]; - - return $arr; - } - - /** - * Get introductions - * - * @param bool $all If false only include introductions into the query - * which aren't marked as ignored - * @param int $start Start the query at this point - * @param int $limit Maximum number of query results - * @param int $id When set, only the introduction with this id is displayed - * - * @return array [string, array] - * string 'ident' => Notification identifier - * array 'notifications' => Introductions - * - * @throws ImagickException - * @throws Exception - */ - public function getIntroList(bool $all = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT, int $id = 0) - { - /// @todo sanitize wording according to SELF::INTRO - $ident = 'introductions'; - $notifications = []; - $sql_extra = ""; - - if (empty($id)) { - if (!$all) { - $sql_extra = " AND NOT `ignore` "; - } - - $sql_extra .= " AND NOT `intro`.`blocked` "; + if ($name != '') { + $pos = strpos($message, $name); } else { - $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id)); + $pos = false; } - /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact - $stmtNotifications = $this->dba->p( - "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, - `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`, - `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`, - `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`, - `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`, - `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr` - FROM `intro` - LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` - LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl` - LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` - WHERE `intro`.`uid` = ? $sql_extra - LIMIT ?, ?", - $_SESSION['uid'], - $start, - $limit - ); - if ($this->dba->isResult($stmtNotifications)) { - $notifications = $this->formatIntroList($this->dba->toArray($stmtNotifications)); + if ($pos !== false) { + $message = substr_replace($message, '{0}', $pos, strlen($name)); } - $arr = [ - 'ident' => $ident, - 'notifications' => $notifications, - ]; - - return $arr; - } - - /** - * Format the notification query in an usable array - * - * @param array $intros The array from the db query - * - * @return array with the introductions - * @throws HTTPException\InternalServerErrorException - * @throws ImagickException - */ - private function formatIntroList(array $intros) - { - $knowyou = ''; - - $formattedIntros = []; - - foreach ($intros as $intro) { - // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. - // We have to distinguish between these two because they use different data. - // Contact suggestions - if ($intro['fid']) { - $return_addr = bin2hex(DI::app()->user['nickname'] . '@' . - $this->baseUrl->getHostName() . - (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : '')); - - $intro = [ - 'label' => 'friend_suggestion', - 'str_type' => $this->l10n->t('Friend Suggestion'), - 'intro_id' => $intro['intro_id'], - 'madeby' => $intro['name'], - 'madeby_url' => $intro['url'], - 'madeby_zrl' => Contact::magicLink($intro['url']), - 'madeby_addr' => $intro['addr'], - 'contact_id' => $intro['contact-id'], - 'photo' => (!empty($intro['fphoto']) ? ProxyUtils::proxifyUrl($intro['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"), - 'name' => $intro['fname'], - 'url' => $intro['furl'], - 'zrl' => Contact::magicLink($intro['furl']), - 'hidden' => $intro['hidden'] == 1, - 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0), - 'knowyou' => $knowyou, - 'note' => $intro['note'], - 'request' => $intro['frequest'] . '?addr=' . $return_addr, - ]; - - // Normal connection requests - } else { - $intro = $this->getMissingIntroData($intro); - - if (empty($intro['url'])) { - continue; - } - - // Don't show these data until you are connected. Diaspora is doing the same. - if ($intro['gnetwork'] === Protocol::DIASPORA) { - $intro['glocation'] = ""; - $intro['gabout'] = ""; - $intro['ggender'] = ""; - } - $intro = [ - 'label' => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'), - 'str_type' => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')), - 'dfrn_id' => $intro['issued-id'], - 'uid' => $_SESSION['uid'], - 'intro_id' => $intro['intro_id'], - 'contact_id' => $intro['contact-id'], - 'photo' => (!empty($intro['photo']) ? ProxyUtils::proxifyUrl($intro['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"), - 'name' => $intro['name'], - 'location' => BBCode::convert($intro['glocation'], false), - 'about' => BBCode::convert($intro['gabout'], false), - 'keywords' => $intro['gkeywords'], - 'gender' => $intro['ggender'], - 'hidden' => $intro['hidden'] == 1, - 'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0), - 'url' => $intro['url'], - 'zrl' => Contact::magicLink($intro['url']), - 'addr' => $intro['gaddr'], - 'network' => $intro['gnetwork'], - 'knowyou' => $intro['knowyou'], - 'note' => $intro['note'], - ]; - } - - $formattedIntros[] = $intro; - } - - return $formattedIntros; - } - - /** - * Check for missing contact data and try to fetch the data from - * from other sources - * - * @param array $intro The input array with the intro data - * - * @return array The array with the intro data - * @throws HTTPException\InternalServerErrorException - */ - private function getMissingIntroData(array $intro) - { - // If the network and the addr isn't available from the gcontact - // table entry, take the one of the contact table entry - if (empty($intro['gnetwork']) && !empty($intro['network'])) { - $intro['gnetwork'] = $intro['network']; - } - if (empty($intro['gaddr']) && !empty($intro['addr'])) { - $intro['gaddr'] = $intro['addr']; - } - - // If the network and addr is still not available - // get the missing data data from other sources - if (empty($intro['gnetwork']) || empty($intro['gaddr'])) { - $ret = Contact::getDetailsByURL($intro['url']); - - if (empty($intro['gnetwork']) && !empty($ret['network'])) { - $intro['gnetwork'] = $ret['network']; - } - if (empty($intro['gaddr']) && !empty($ret['addr'])) { - $intro['gaddr'] = $ret['addr']; - } - } - - return $intro; + return $message; } } diff --git a/src/Module/BaseNotifications.php b/src/Module/BaseNotifications.php index 728a9d402..9a67bb566 100644 --- a/src/Module/BaseNotifications.php +++ b/src/Module/BaseNotifications.php @@ -8,8 +8,8 @@ use Friendica\Content\Pager; use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\DI; -use Friendica\Model\Notification; use Friendica\Network\HTTPException\ForbiddenException; +use Friendica\Object\Notification\Notification; /** * Base Module for each tab of the notification display @@ -47,6 +47,8 @@ abstract class BaseNotifications extends BaseModule /** @var int The default count of items per page */ const ITEMS_PER_PAGE = 20; + /** @var int The default limit of notifications per page */ + const DEFAULT_PAGE_LIMIT = 80; /** @var boolean True, if ALL entries should get shown */ protected static $showAll; @@ -104,7 +106,17 @@ abstract class BaseNotifications extends BaseModule return; } - System::jsonExit(static::getNotifications()['notifs'] ?? []); + // Set the pager + $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE); + + // Add additional informations (needed for json output) + $notifications = [ + 'notifications' => static::getNotifications(), + 'items_page' => $pager->getItemsPerPage(), + 'page' => $pager->getPage(), + ]; + + System::jsonExit($notifications); } /** diff --git a/src/Module/Notifications/Introductions.php b/src/Module/Notifications/Introductions.php index 99066bd3c..b0bdac0cb 100644 --- a/src/Module/Notifications/Introductions.php +++ b/src/Module/Notifications/Introductions.php @@ -9,6 +9,7 @@ use Friendica\Core\Renderer; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Module\BaseNotifications; +use Friendica\Object\Notification\Introduction; /** * Prints notifications about introduction @@ -23,7 +24,10 @@ class Introductions extends BaseNotifications $id = (int)DI::args()->get(2, 0); $all = DI::args()->get(2) == 'all'; - $notifications = DI::notification()->getIntroList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id); + $notifications = [ + 'ident' => 'introductions', + 'notifications' => DI::factNotIntro()->getIntroList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), + ]; return [ 'header' => DI::l10n()->t('Notifications'), @@ -50,11 +54,12 @@ class Introductions extends BaseNotifications // The link to switch between ignored and normal connection requests $notificationShowLink = [ 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros'), - 'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')) + 'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')), ]; // Loop through all introduction notifications.This creates an array with the output html for each // introduction + /** @var Introduction $notification */ foreach ($notifications['notifications'] as $notification) { // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. @@ -62,27 +67,27 @@ class Introductions extends BaseNotifications switch ($notification['label']) { case 'friend_suggestion': $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [ - '$type' => $notification['label'], + '$type' => $notification->getLabel(), 'str_notification_type' => DI::l10n()->t('Notification type:'), - 'str_type' => $notification['str_type'], - '$intro_id' => $notification['intro_id'], + 'str_type' => $notification->getType(), + '$intro_id' => $notification->getIntroId(), '$lbl_madeby' => DI::l10n()->t('Suggested by:'), - '$madeby' => $notification['madeby'], - '$madeby_url' => $notification['madeby_url'], - '$madeby_zrl' => $notification['madeby_zrl'], - '$madeby_addr' => $notification['madeby_addr'], - '$contact_id' => $notification['contact_id'], - '$photo' => $notification['photo'], - '$fullname' => $notification['name'], - '$url' => $notification['url'], - '$zrl' => $notification['zrl'], + '$madeby' => $notification->getMadeBy(), + '$madeby_url' => $notification->getMadeByUrl(), + '$madeby_zrl' => $notification->getMadeByZrl(), + '$madeby_addr' => $notification->getMadeByAddr(), + '$contact_id' => $notification->getContactId(), + '$photo' => $notification->getPhoto(), + '$fullname' => $notification->getName(), + '$url' => $notification->getUrl(), + '$zrl' => $notification->getZrl(), '$lbl_url' => DI::l10n()->t('Profile URL'), - '$addr' => $notification['addr'], - '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), ($notification['hidden'] == 1), ''], - '$knowyou' => $notification['knowyou'], + '$addr' => $notification->getAddr(), + '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''], + '$knowyou' => $notification->getKnowYou(), '$approve' => DI::l10n()->t('Approve'), - '$note' => $notification['note'], - '$request' => $notification['request'], + '$note' => $notification->getNote(), + '$request' => $notification->getRequest(), '$ignore' => DI::l10n()->t('Ignore'), '$discard' => DI::l10n()->t('Discard'), ]); @@ -90,8 +95,8 @@ class Introductions extends BaseNotifications // Normal connection requests default: - $friend_selected = (($notification['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled '); - $fan_selected = (($notification['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : ''); + $friend_selected = (($notification->getNetwork() !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled '); + $fan_selected = (($notification->getNetwork() === Protocol::OSTATUS) ? ' checked="checked" disabled ' : ''); $lbl_knowyou = ''; $knowyou = ''; @@ -99,13 +104,13 @@ class Introductions extends BaseNotifications $helptext2 = ''; $helptext3 = ''; - if ($notification['network'] === Protocol::DFRN) { + if ($notification->getNetwork() === Protocol::DFRN) { $lbl_knowyou = DI::l10n()->t('Claims to be known to you: '); - $knowyou = (($notification['knowyou']) ? DI::l10n()->t('yes') : DI::l10n()->t('no')); + $knowyou = ($notification->getKnowYou() ? DI::l10n()->t('yes') : DI::l10n()->t('no')); $helptext = DI::l10n()->t('Shall your connection be bidirectional or not?'); $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification['name'], $notification['name']); $helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification['name']); - } elseif ($notification['network'] === Protocol::DIASPORA) { + } elseif ($notification->getNetwork() === Protocol::DIASPORA) { $helptext = DI::l10n()->t('Shall your connection be bidirectional or not?'); $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification['name'], $notification['name']); $helptext3 = DI::l10n()->t('Accepting %s as a sharer allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification['name']); @@ -113,17 +118,17 @@ class Introductions extends BaseNotifications $dfrn_tpl = Renderer::getMarkupTemplate('notifications/netfriend.tpl'); $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [ - '$intro_id' => $notification['intro_id'], + '$intro_id' => $notification->getIntroId(), '$friend_selected' => $friend_selected, '$fan_selected' => $fan_selected, '$approve_as1' => $helptext, '$approve_as2' => $helptext2, '$approve_as3' => $helptext3, '$as_friend' => DI::l10n()->t('Friend'), - '$as_fan' => (($notification['network'] == Protocol::DIASPORA) ? DI::l10n()->t('Sharer') : DI::l10n()->t('Subscriber')) + '$as_fan' => (($notification->getNetwork() == Protocol::DIASPORA) ? DI::l10n()->t('Sharer') : DI::l10n()->t('Subscriber')), ]); - $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification['contact_id']]); + $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification->getContactId()]); if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) { $action = 'follow_confirm'; @@ -137,45 +142,45 @@ class Introductions extends BaseNotifications $header .= ' <' . $notification['addr'] . '>'; } - $header .= ' (' . ContactSelector::networkToName($notification['network'], $notification['url']) . ')'; + $header .= ' (' . ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()) . ')'; - if ($notification['network'] != Protocol::DIASPORA) { + if ($notification->getNetwork() != Protocol::DIASPORA) { $discard = DI::l10n()->t('Discard'); } else { $discard = ''; } $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [ - '$type' => $notification['label'], + '$type' => $notification->getLabel(), '$header' => $header, 'str_notification_type' => DI::l10n()->t('Notification type:'), - 'str_type' => $notification['notifytype'], + 'str_type' => $notification->getType(), '$dfrn_text' => $dfrn_text, - '$dfrn_id' => $notification['dfrn_id'], - '$uid' => $notification['uid'], - '$intro_id' => $notification['intro_id'], - '$contact_id' => $notification['contact_id'], - '$photo' => $notification['photo'], - '$fullname' => $notification['name'], - '$location' => $notification['location'], + '$dfrn_id' => $notification->getDfrnId(), + '$uid' => $notification->getUid(), + '$intro_id' => $notification->getIntroId(), + '$contact_id' => $notification->getContactId(), + '$photo' => $notification->getPhoto(), + '$fullname' => $notification->getName(), + '$location' => $notification->getLocation(), '$lbl_location' => DI::l10n()->t('Location:'), - '$about' => $notification['about'], + '$about' => $notification->getAbout(), '$lbl_about' => DI::l10n()->t('About:'), - '$keywords' => $notification['keywords'], + '$keywords' => $notification->getKeywords(), '$lbl_keywords' => DI::l10n()->t('Tags:'), - '$gender' => $notification['gender'], + '$gender' => $notification->getGender(), '$lbl_gender' => DI::l10n()->t('Gender:'), '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), ($notification['hidden'] == 1), ''], - '$url' => $notification['url'], - '$zrl' => $notification['zrl'], + '$url' => $notification->getUrl(), + '$zrl' => $notification->getZrl(), '$lbl_url' => DI::l10n()->t('Profile URL'), - '$addr' => $notification['addr'], + '$addr' => $notification->getAddr(), '$lbl_knowyou' => $lbl_knowyou, '$lbl_network' => DI::l10n()->t('Network:'), - '$network' => ContactSelector::networkToName($notification['network'], $notification['url']), + '$network' => ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()), '$knowyou' => $knowyou, '$approve' => DI::l10n()->t('Approve'), - '$note' => $notification['note'], + '$note' => $notification->getNote(), '$ignore' => DI::l10n()->t('Ignore'), '$discard' => $discard, '$action' => $action, diff --git a/src/Module/Notifications/Notification.php b/src/Module/Notifications/Notification.php index 2f3f927f9..f7fe6f190 100644 --- a/src/Module/Notifications/Notification.php +++ b/src/Module/Notifications/Notification.php @@ -23,7 +23,11 @@ class Notification extends BaseModule { // @TODO: Replace with parameter from router if (DI::args()->get(1) === 'mark' && DI::args()->get(2) === 'all') { - $success = DI::notification()->setAllSeen(); + try { + $success = DI::notification()->setAllSeen(); + }catch (\Exception $e) { + $success = false; + } header('Content-type: application/json; charset=utf-8'); echo json_encode([ @@ -43,14 +47,16 @@ class Notification extends BaseModule { // @TODO: Replace with parameter from router if (DI::args()->getArgc() > 2 && DI::args()->get(1) === 'view' && intval(DI::args()->get(2))) { - $notificationManager = DI::notification(); - // @TODO: Replace with parameter from router - $note = $notificationManager->getByID(DI::args()->get(2)); - if (!empty($note)) { - $notificationManager->setSeen($note); - if (!empty($note['link'])) { - System::externalRedirect($note['link']); + try { + $notification = DI::notification()->getByID(DI::args()->get(2)); + $notification->setSeen(); + + if (!empty($notification->link)) { + System::externalRedirect($notification->link); } + + } catch (HTTPException\NotFoundException $e) { + info(DI::l10n()->t('Invalid notification.')); } DI::baseUrl()->redirect(); diff --git a/src/Module/Notifications/Notifications.php b/src/Module/Notifications/Notifications.php index 3dffe90b5..bff5026df 100644 --- a/src/Module/Notifications/Notifications.php +++ b/src/Module/Notifications/Notifications.php @@ -3,10 +3,10 @@ namespace Friendica\Module\Notifications; use Friendica\Content\Nav; -use Friendica\Content\Pager; use Friendica\Core\Renderer; use Friendica\DI; use Friendica\Module\BaseNotifications; +use Friendica\Object\Notification\Notification; /** * Prints all notification types except introduction: @@ -22,41 +22,46 @@ class Notifications extends BaseNotifications */ public static function getNotifications() { - $nm = DI::notification(); - $notificationHeader = ''; + /** @var Notification[] $notifications */ + $notifications = []; // Get the network notifications if ((DI::args()->get(1) == 'network')) { $notificationHeader = DI::l10n()->t('Network Notifications'); - $notifications = $nm->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE); + $notifications = [ + 'ident' => Notification::NETWORK, + 'notifications' => DI::factNotification()->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + ]; // Get the system notifications } elseif ((DI::args()->get(1) == 'system')) { $notificationHeader = DI::l10n()->t('System Notifications'); - $notifications = $nm->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE); + $notifications = [ + 'ident' => Notification::SYSTEM, + 'notifications' => DI::factNotification()->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + ]; // Get the personal notifications } elseif ((DI::args()->get(1) == 'personal')) { $notificationHeader = DI::l10n()->t('Personal Notifications'); - $notifications = $nm->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE); + $notifications = [ + 'ident' => Notification::PERSONAL, + 'notifications' => DI::factNotification()->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + ]; // Get the home notifications } elseif ((DI::args()->get(1) == 'home')) { $notificationHeader = DI::l10n()->t('Home Notifications'); - $notifications = $nm->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE); + $notifications = [ + 'ident' => Notification::HOME, + 'notifications' => DI::factNotification()->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + ]; // fallback - redirect to main page } else { DI::baseUrl()->redirect('notifications'); } - // Set the pager - $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE); - - // Add additional informations (needed for json output) - $notifications['items_page'] = $pager->getItemsPerPage(); - $notifications['page'] = $pager->getPage(); - return [ 'header' => $notificationHeader, 'notifications' => $notifications, @@ -78,6 +83,7 @@ class Notifications extends BaseNotifications if (!empty($notifications['notifications'])) { // Loop trough ever notification This creates an array with the output html for each // notification and apply the correct template according to the notificationtype (label). + /** @var Notification $notification */ foreach ($notifications['notifications'] as $notification) { $notification_templates = [ 'like' => 'notifications/likes_item.tpl', @@ -91,17 +97,17 @@ class Notifications extends BaseNotifications 'notification' => 'notifications/notification.tpl', ]; - $notificationTemplate = Renderer::getMarkupTemplate($notification_templates[$notification['label']]); + $notificationTemplate = Renderer::getMarkupTemplate($notification_templates[$notification->getLabel()]); $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [ - '$item_label' => $notification['label'], - '$item_link' => $notification['link'], - '$item_image' => $notification['image'], - '$item_url' => $notification['url'], - '$item_text' => $notification['text'], - '$item_when' => $notification['when'], - '$item_ago' => $notification['ago'], - '$item_seen' => $notification['seen'], + '$item_label' => $notification->getLabel(), + '$item_link' => $notification->getLink(), + '$item_image' => $notification->getImage(), + '$item_url' => $notification->getUrl(), + '$item_text' => $notification->getText(), + '$item_when' => $notification->getWhen(), + '$item_ago' => $notification->getAgo(), + '$item_seen' => $notification->isSeen(), ]); } } else { diff --git a/src/Object/Notification/Introduction.php b/src/Object/Notification/Introduction.php new file mode 100644 index 000000000..56bc3ceba --- /dev/null +++ b/src/Object/Notification/Introduction.php @@ -0,0 +1,302 @@ +label; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return int + */ + public function getIntroId() + { + return $this->intro_id; + } + + /** + * @return string + */ + public function getMadeBy() + { + return $this->madeBy; + } + + /** + * @return string + */ + public function getMadeByUrl() + { + return $this->madeByUrl; + } + + /** + * @return string + */ + public function getMadeByZrl() + { + return $this->madeByZrl; + } + + /** + * @return string + */ + public function getMadeByAddr() + { + return $this->madeByAddr; + } + + /** + * @return int + */ + public function getContactId() + { + return $this->contactId; + } + + /** + * @return string + */ + public function getPhoto() + { + return $this->photo; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @return string + */ + public function getZrl() + { + return $this->zrl; + } + + /** + * @return bool + */ + public function isHidden() + { + return $this->hidden; + } + + /** + * @return int + */ + public function getPostNewFriend() + { + return $this->postNewFriend; + } + + /** + * @return string + */ + public function getKnowYou() + { + return $this->knowYou; + } + + /** + * @return string + */ + public function getNote() + { + return $this->note; + } + + /** + * @return string + */ + public function getRequest() + { + return $this->request; + } + + /** + * @return string + */ + public function getDfrnId() + { + return $this->dfrnId; + } + + /** + * @return string + */ + public function getAddr() + { + return $this->addr; + } + + /** + * @return string + */ + public function getNetwork() + { + return $this->network; + } + + /** + * @return int + */ + public function getUid() + { + return $this->uid; + } + + /** + * @return string + */ + public function getKeywords() + { + return $this->keywords; + } + + /** + * @return string + */ + public function getGender() + { + return $this->gender; + } + + /** + * @return string + */ + public function getLocation() + { + return $this->location; + } + + /** + * @return string + */ + public function getAbout() + { + return $this->about; + } + + public function __construct(array $data = []) + { + $this->label = $data['label'] ?? ''; + $this->type = $data['str_$type'] ?? ''; + $this->intro_id = $data['$intro_id'] ?? ''; + $this->madeBy = $data['$madeBy'] ?? ''; + $this->madeByUrl = $data['$madeByUrl'] ?? ''; + $this->madeByZrl = $data['$madeByZrl'] ?? ''; + $this->madeByAddr = $data['$madeByAddr'] ?? ''; + $this->contactId = $data['$contactId'] ?? ''; + $this->photo = $data['$photo'] ?? ''; + $this->name = $data['$name'] ?? ''; + $this->url = $data['$url'] ?? ''; + $this->zrl = $data['$zrl'] ?? ''; + $this->hidden = $data['$hidden'] ?? ''; + $this->postNewFriend = $data['$postNewFriend'] ?? ''; + $this->knowYou = $data['$knowYou'] ?? ''; + $this->note = $data['$note'] ?? ''; + $this->request = $data['$request'] ?? ''; + $this->dfrnId = $data['dfrn_id'] ?? ''; + $this->addr = $data['addr'] ?? ''; + $this->network = $data['network'] ?? ''; + $this->uid = $data['uid'] ?? ''; + $this->keywords = $data['keywords'] ?? ''; + $this->gender = $data['gender'] ?? ''; + $this->location = $data['location'] ?? ''; + $this->about = $data['about'] ?? ''; + } + + /** + * @inheritDoc + */ + public function jsonSerialize() + { + return $this->toArray(); + } + + /** + * @return array + */ + public function toArray() + { + return get_object_vars($this); + } +} diff --git a/src/Object/Notification/Notification.php b/src/Object/Notification/Notification.php new file mode 100644 index 000000000..2e853a649 --- /dev/null +++ b/src/Object/Notification/Notification.php @@ -0,0 +1,123 @@ +label; + } + + /** + * @return string + */ + public function getLink() + { + return $this->link; + } + + /** + * @return string + */ + public function getImage() + { + return $this->image; + } + + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @return string + */ + public function getText() + { + return $this->text; + } + + /** + * @return string + */ + public function getWhen() + { + return $this->when; + } + + /** + * @return string + */ + public function getAgo() + { + return $this->ago; + } + + /** + * @return bool + */ + public function isSeen() + { + return $this->seen; + } + + public function __construct(string $label = '', string $link = '', string $image = '', + string $url = '', string $text = '', + string $when = '', string $ago = '', bool $seen = false) + { + $this->label = $label; + $this->link = $link; + $this->image = $image; + $this->url = $url; + $this->text = $text; + $this->when = $when; + $this->ago = $ago; + $this->seen = $seen; + } + + /** + * @inheritDoc + */ + public function jsonSerialize() + { + return get_object_vars($this); + } + + /** + * @return array + */ + public function toArray() + { + return get_object_vars($this); + } +} diff --git a/src/Repository/Notification.php b/src/Repository/Notification.php new file mode 100644 index 000000000..a17d1223a --- /dev/null +++ b/src/Repository/Notification.php @@ -0,0 +1,91 @@ +dba, $this->logger, $this, $data); + } + + /** + * {@inheritDoc} + * + * @return Collection\Notifications + */ + public function select(array $condition = [], array $params = []) + { + $params['order'] = $params['order'] ?? ['date' => 'DESC']; + + $condition = array_merge($condition, ['uid' => local_user()]); + + return parent::select($condition, $params); + } + + /** + * {@inheritDoc} + * + * @return Model\Notification + * @throws NotFoundException + */ + public function getByID(int $id) + { + return $this->selectFirst(['id' => $id, 'uid' => local_user()]); + } + + /** + * {@inheritDoc} + * + * @return bool true on success, false on error + * @throws Exception + */ + public function setAllSeen(bool $seen = true) + { + return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]); + } + + /** + * @param array $fields + * + * @return Model\Notification + * + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws Exception + */ + public function insert(array $fields) + { + $fields['date'] = DateTimeFormat::utcNow(); + $fields['abort'] = false; + + Hook::callAll('enotify_store', $fields); + + if ($fields['abort']) { + $this->logger->debug('Abort adding notification entry', ['fields' => $fields]); + return null; + } + + $this->logger->debug('adding notification entry', ['fields' => $fields]); + + return parent::insert($fields); + } +} From 4c5856da2bc8851c90c62af01a432f71825718fd Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 25 Jan 2020 15:31:25 +0100 Subject: [PATCH 2/5] update PHPDoc --- src/Model/Notification.php | 5 ++++- src/Object/Notification/Introduction.php | 3 +++ src/Object/Notification/Notification.php | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Model/Notification.php b/src/Model/Notification.php index ab029f73b..11105d798 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -14,7 +14,10 @@ use Psr\Log\LoggerInterface; /** * Model for an entry in the notify table - * - Including additional calculated properties + * - Including additional, calculated properties + * + * Is used either for frontend interactions or for API-based interaction + * @see https://github.com/friendica/friendica/blob/develop/doc/API-Entities.md#notification * * @property string hash * @property integer type diff --git a/src/Object/Notification/Introduction.php b/src/Object/Notification/Introduction.php index 56bc3ceba..4972863ca 100644 --- a/src/Object/Notification/Introduction.php +++ b/src/Object/Notification/Introduction.php @@ -2,6 +2,9 @@ namespace Friendica\Object\Notification; +/** + * A view-only object for printing introduction notifications to the frontend + */ class Introduction implements \JsonSerializable { /** @var string */ diff --git a/src/Object/Notification/Notification.php b/src/Object/Notification/Notification.php index 2e853a649..925927d84 100644 --- a/src/Object/Notification/Notification.php +++ b/src/Object/Notification/Notification.php @@ -2,6 +2,9 @@ namespace Friendica\Object\Notification; +/** + * A view-only object for printing item notifications to the frontend + */ class Notification implements \JsonSerializable { const SYSTEM = 'system'; From 74f3a2f90c6b92aafdaa7be6dcfb672fa7757586 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 25 Jan 2020 20:00:58 +0100 Subject: [PATCH 3/5] Fix API result, add PHPDoc and cleanup object construction Fix missing getters fix data array fix missing "$" for template-variables Remove lazy-loaded parent notification instance (for now..) --- include/api.php | 11 +- .../Notification/IntroductionFactory.php | 6 + .../Notification/NotificationFactory.php | 162 +++++++++--------- src/Model/Notification.php | 24 --- src/Module/Notifications/Introductions.php | 130 +++++++------- src/Object/Notification/Introduction.php | 68 ++++---- src/Object/Notification/Notification.php | 20 +-- 7 files changed, 203 insertions(+), 218 deletions(-) diff --git a/include/api.php b/include/api.php index cb0599d9a..4a55c2b91 100644 --- a/include/api.php +++ b/include/api.php @@ -5909,16 +5909,21 @@ function api_friendica_notification($type) $notifications = DI::notification()->select([], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); if ($type == "xml") { - $xmlnotes = []; + $xmlnotes = false; if (!empty($notifications)) { foreach ($notifications as $notification) { $xmlnotes[] = ["@attributes" => $notification->toArray()]; } } - $notifications = $xmlnotes; + $result = $xmlnotes; + } elseif (count($notifications) > 0) { + $result = $notifications->getArrayCopy(); + } else { + $result = false; } - return api_format_data("notes", $type, ['note' => $notifications->getArrayCopy()]); + + return api_format_data("notes", $type, ['note' => $result]); } /** diff --git a/src/Factory/Notification/IntroductionFactory.php b/src/Factory/Notification/IntroductionFactory.php index 73f288326..3f2bb563a 100644 --- a/src/Factory/Notification/IntroductionFactory.php +++ b/src/Factory/Notification/IntroductionFactory.php @@ -19,6 +19,12 @@ use Friendica\Object\Notification\Introduction; use Friendica\Util\Proxy; use Psr\Log\LoggerInterface; +/** + * Factory for creating notification objects based on introductions + * Currently, there are two main types of introduction based notifications: + * - Friend suggestion + * - Friend/Follower request + */ class IntroductionFactory extends BaseFactory { /** @var Database */ diff --git a/src/Factory/Notification/NotificationFactory.php b/src/Factory/Notification/NotificationFactory.php index 2516916d1..05dd200ad 100644 --- a/src/Factory/Notification/NotificationFactory.php +++ b/src/Factory/Notification/NotificationFactory.php @@ -23,6 +23,14 @@ use Friendica\Util\Temporal; use Friendica\Util\XML; use Psr\Log\LoggerInterface; +/** + * Factory for creating notification objects based on items + * Currently, there are the following types of item based notifications: + * - network + * - system + * - home + * - personal + */ class NotificationFactory extends BaseFactory { /** @var Database */ @@ -93,97 +101,89 @@ class NotificationFactory extends BaseFactory // Transform the different types of notification in an usable array switch ($item['verb'] ?? '') { case Activity::LIKE: - return new \Friendica\Object\Notification\Notification( - 'like', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s liked %s's post", $item['author-name'], $item['parent-author-name']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'like', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s liked %s's post", $item['author-name'], $item['parent-author-name']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); case Activity::DISLIKE: - return new \Friendica\Object\Notification\Notification( - 'dislike', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s disliked %s's post", $item['author-name'], $item['parent-author-name']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'dislike', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s disliked %s's post", $item['author-name'], $item['parent-author-name']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); case Activity::ATTEND: - return new \Friendica\Object\Notification\Notification( - 'attend', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s is attending %s's event", $item['author-name'], $item['parent-author-name']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'attend', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s is attending %s's event", $item['author-name'], $item['parent-author-name']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); case Activity::ATTENDNO: - return new \Friendica\Object\Notification\Notification( - 'attendno', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s is not attending %s's event", $item['author-name'], $item['parent-author-name']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'attendno', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s is not attending %s's event", $item['author-name'], $item['parent-author-name']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); case Activity::ATTENDMAYBE: - return new \Friendica\Object\Notification\Notification( - 'attendmaybe', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s may attending %s's event", $item['author-name'], $item['parent-author-name']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'attendmaybe', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s may attending %s's event", $item['author-name'], $item['parent-author-name']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); case Activity::FRIEND: if (!isset($item['object'])) { - return new \Friendica\Object\Notification\Notification( - 'friend', - $item['link'], - $item['image'], - $item['url'], - $item['text'], - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'friend', + 'link' => $item['link'], + 'image' => $item['image'], + 'url' => $item['url'], + 'text' => $item['text'], + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); } $xmlHead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; $obj = XML::parseString($xmlHead . $item['object']); $item['fname'] = $obj->title; - return new \Friendica\Object\Notification\Notification( - 'friend', - $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], - Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), - $item['author-link'], - $this->l10n->t("%s is now friends with %s", $item['author-name'], $item['fname']), - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification([ + 'label' => 'friend', + 'link' => $this->baseUrl->get(true) . '/display/' . $item['parent-guid'], + 'image' => Proxy::proxifyUrl($item['author-avatar'], false, Proxy::SIZE_MICRO), + 'url' => $item['author-link'], + 'text' => $this->l10n->t("%s is now friends with %s", $item['author-name'], $item['fname']), + 'when' => $item['when'], + 'ago' => $item['ago'], + 'seen' => $item['seen']]); default: - return new \Friendica\Object\Notification\Notification( - $item['label'], - $item['link'], - $item['image'], - $item['url'], - $item['text'], - $item['when'] ?? '', - $item['ago'] ?? '', - $item['seen'] ?? false); + return new \Friendica\Object\Notification\Notification($item); break; } } @@ -215,15 +215,15 @@ class NotificationFactory extends BaseFactory $notifications = $this->notification->select($conditions, $params); foreach ($notifications as $notification) { - $formattedNotifications[] = new \Friendica\Object\Notification\Notification( - 'notification', - $this->baseUrl->get(true) . '/notification/view/' . $notification->id, - Proxy::proxifyUrl($notification->photo, false, Proxy::SIZE_MICRO), - $notification->url, - strip_tags(BBCode::convert($notification->msg)), - DateTimeFormat::local($notification->date, 'r'), - Temporal::getRelativeDate($notification->date), - $notification->seen); + $formattedNotifications[] = new \Friendica\Object\Notification\Notification([ + 'label' => 'notification', + 'link' => $this->baseUrl->get(true) . '/notification/view/' . $notification->id, + 'image' => Proxy::proxifyUrl($notification->photo, false, Proxy::SIZE_MICRO), + 'url' => $notification->url, + 'text' => strip_tags(BBCode::convert($notification->msg)), + 'when' => DateTimeFormat::local($notification->date, 'r'), + 'ago' => Temporal::getRelativeDate($notification->date), + 'seen' => $notification->seen]); } } catch (Exception $e) { $this->logger->warning('Select failed.', ['conditions' => $conditions, 'exception' => $e]); diff --git a/src/Model/Notification.php b/src/Model/Notification.php index 11105d798..254aaf5f6 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -46,8 +46,6 @@ class Notification extends BaseModel { /** @var \Friendica\Repository\Notification */ private $repo; - /** @var $this */ - private $parentInst; public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = []) { @@ -118,28 +116,6 @@ class Notification extends BaseModel } } - public function __get($name) - { - $this->checkValid(); - - $return = null; - - switch ($name) { - case 'parent': - if (!empty($this->parent)) { - $this->parentInst = $this->parentInst ?? $this->repo->getByID($this->parent); - - $return = $this->parentInst; - } - break; - default: - $return = parent::__get($name); - break; - } - - return $return; - } - public function __set($name, $value) { parent::__set($name, $value); diff --git a/src/Module/Notifications/Introductions.php b/src/Module/Notifications/Introductions.php index b0bdac0cb..8ded212c6 100644 --- a/src/Module/Notifications/Introductions.php +++ b/src/Module/Notifications/Introductions.php @@ -25,7 +25,7 @@ class Introductions extends BaseNotifications $all = DI::args()->get(2) == 'all'; $notifications = [ - 'ident' => 'introductions', + 'ident' => 'introductions', 'notifications' => DI::factNotIntro()->getIntroList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), ]; @@ -64,32 +64,32 @@ class Introductions extends BaseNotifications // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. // We have to distinguish between these two because they use different data. - switch ($notification['label']) { + switch ($notification->getLabel()) { case 'friend_suggestion': $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [ - '$type' => $notification->getLabel(), - 'str_notification_type' => DI::l10n()->t('Notification type:'), - 'str_type' => $notification->getType(), - '$intro_id' => $notification->getIntroId(), - '$lbl_madeby' => DI::l10n()->t('Suggested by:'), - '$madeby' => $notification->getMadeBy(), - '$madeby_url' => $notification->getMadeByUrl(), - '$madeby_zrl' => $notification->getMadeByZrl(), - '$madeby_addr' => $notification->getMadeByAddr(), - '$contact_id' => $notification->getContactId(), - '$photo' => $notification->getPhoto(), - '$fullname' => $notification->getName(), - '$url' => $notification->getUrl(), - '$zrl' => $notification->getZrl(), - '$lbl_url' => DI::l10n()->t('Profile URL'), - '$addr' => $notification->getAddr(), - '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''], - '$knowyou' => $notification->getKnowYou(), - '$approve' => DI::l10n()->t('Approve'), - '$note' => $notification->getNote(), - '$request' => $notification->getRequest(), - '$ignore' => DI::l10n()->t('Ignore'), - '$discard' => DI::l10n()->t('Discard'), + '$type' => $notification->getLabel(), + '$str_notification_type' => DI::l10n()->t('Notification type:'), + '$str_type' => $notification->getType(), + '$intro_id' => $notification->getIntroId(), + '$lbl_madeby' => DI::l10n()->t('Suggested by:'), + '$madeby' => $notification->getMadeBy(), + '$madeby_url' => $notification->getMadeByUrl(), + '$madeby_zrl' => $notification->getMadeByZrl(), + '$madeby_addr' => $notification->getMadeByAddr(), + '$contact_id' => $notification->getContactId(), + '$photo' => $notification->getPhoto(), + '$fullname' => $notification->getName(), + '$url' => $notification->getUrl(), + '$zrl' => $notification->getZrl(), + '$lbl_url' => DI::l10n()->t('Profile URL'), + '$addr' => $notification->getAddr(), + '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''], + '$knowyou' => $notification->getKnowYou(), + '$approve' => DI::l10n()->t('Approve'), + '$note' => $notification->getNote(), + '$request' => $notification->getRequest(), + '$ignore' => DI::l10n()->t('Ignore'), + '$discard' => DI::l10n()->t('Discard'), ]); break; @@ -108,12 +108,12 @@ class Introductions extends BaseNotifications $lbl_knowyou = DI::l10n()->t('Claims to be known to you: '); $knowyou = ($notification->getKnowYou() ? DI::l10n()->t('yes') : DI::l10n()->t('no')); $helptext = DI::l10n()->t('Shall your connection be bidirectional or not?'); - $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification['name'], $notification['name']); - $helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification['name']); + $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName()); + $helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName()); } elseif ($notification->getNetwork() === Protocol::DIASPORA) { $helptext = DI::l10n()->t('Shall your connection be bidirectional or not?'); - $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification['name'], $notification['name']); - $helptext3 = DI::l10n()->t('Accepting %s as a sharer allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification['name']); + $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName()); + $helptext3 = DI::l10n()->t('Accepting %s as a sharer allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName()); } $dfrn_tpl = Renderer::getMarkupTemplate('notifications/netfriend.tpl'); @@ -136,10 +136,10 @@ class Introductions extends BaseNotifications $action = 'dfrn_confirm'; } - $header = $notification['name']; + $header = $notification->getName(); - if ($notification['addr'] != '') { - $header .= ' <' . $notification['addr'] . '>'; + if ($notification->getAddr() != '') { + $header .= ' <' . $notification->getAddr() . '>'; } $header .= ' (' . ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()) . ')'; @@ -151,39 +151,39 @@ class Introductions extends BaseNotifications } $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [ - '$type' => $notification->getLabel(), - '$header' => $header, - 'str_notification_type' => DI::l10n()->t('Notification type:'), - 'str_type' => $notification->getType(), - '$dfrn_text' => $dfrn_text, - '$dfrn_id' => $notification->getDfrnId(), - '$uid' => $notification->getUid(), - '$intro_id' => $notification->getIntroId(), - '$contact_id' => $notification->getContactId(), - '$photo' => $notification->getPhoto(), - '$fullname' => $notification->getName(), - '$location' => $notification->getLocation(), - '$lbl_location' => DI::l10n()->t('Location:'), - '$about' => $notification->getAbout(), - '$lbl_about' => DI::l10n()->t('About:'), - '$keywords' => $notification->getKeywords(), - '$lbl_keywords' => DI::l10n()->t('Tags:'), - '$gender' => $notification->getGender(), - '$lbl_gender' => DI::l10n()->t('Gender:'), - '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), ($notification['hidden'] == 1), ''], - '$url' => $notification->getUrl(), - '$zrl' => $notification->getZrl(), - '$lbl_url' => DI::l10n()->t('Profile URL'), - '$addr' => $notification->getAddr(), - '$lbl_knowyou' => $lbl_knowyou, - '$lbl_network' => DI::l10n()->t('Network:'), - '$network' => ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()), - '$knowyou' => $knowyou, - '$approve' => DI::l10n()->t('Approve'), - '$note' => $notification->getNote(), - '$ignore' => DI::l10n()->t('Ignore'), - '$discard' => $discard, - '$action' => $action, + '$type' => $notification->getLabel(), + '$header' => $header, + '$str_notification_type' => DI::l10n()->t('Notification type:'), + '$str_type' => $notification->getType(), + '$dfrn_text' => $dfrn_text, + '$dfrn_id' => $notification->getDfrnId(), + '$uid' => $notification->getUid(), + '$intro_id' => $notification->getIntroId(), + '$contact_id' => $notification->getContactId(), + '$photo' => $notification->getPhoto(), + '$fullname' => $notification->getName(), + '$location' => $notification->getLocation(), + '$lbl_location' => DI::l10n()->t('Location:'), + '$about' => $notification->getAbout(), + '$lbl_about' => DI::l10n()->t('About:'), + '$keywords' => $notification->getKeywords(), + '$lbl_keywords' => DI::l10n()->t('Tags:'), + '$gender' => $notification->getGender(), + '$lbl_gender' => DI::l10n()->t('Gender:'), + '$hidden' => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''], + '$url' => $notification->getUrl(), + '$zrl' => $notification->getZrl(), + '$lbl_url' => DI::l10n()->t('Profile URL'), + '$addr' => $notification->getAddr(), + '$lbl_knowyou' => $lbl_knowyou, + '$lbl_network' => DI::l10n()->t('Network:'), + '$network' => ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()), + '$knowyou' => $knowyou, + '$approve' => DI::l10n()->t('Approve'), + '$note' => $notification->getNote(), + '$ignore' => DI::l10n()->t('Ignore'), + '$discard' => $discard, + '$action' => $action, ]); break; } diff --git a/src/Object/Notification/Introduction.php b/src/Object/Notification/Introduction.php index 4972863ca..19fda4c15 100644 --- a/src/Object/Notification/Introduction.php +++ b/src/Object/Notification/Introduction.php @@ -12,7 +12,7 @@ class Introduction implements \JsonSerializable /** @var string */ private $type = ''; /** @var integer */ - private $intro_id = 0; + private $intro_id = -1; /** @var string */ private $madeBy = ''; /** @var string */ @@ -22,7 +22,7 @@ class Introduction implements \JsonSerializable /** @var string */ private $madeByAddr = ''; /** @var integer */ - private $contactId = 0; + private $contactId = -1; /** @var string */ private $photo = ''; /** @var string */ @@ -34,29 +34,29 @@ class Introduction implements \JsonSerializable /** @var boolean */ private $hidden = false; /** @var integer */ - private $postNewFriend = 0; - /** @var string */ - private $knowYou = ''; + private $postNewFriend = -1; + /** @var boolean */ + private $knowYou = false; /** @var string */ private $note = ''; /** @var string */ private $request = ''; - /** @var string */ - private $dfrnId; - /** @var string */ - private $addr; - /** @var string */ - private $network; /** @var int */ - private $uid; + private $dfrnId = -1; /** @var string */ - private $keywords; + private $addr = ''; /** @var string */ - private $gender; + private $network = ''; + /** @var int */ + private $uid = -1; /** @var string */ - private $location; + private $keywords = ''; /** @var string */ - private $about; + private $gender = ''; + /** @var string */ + private $location = ''; + /** @var string */ + private $about = ''; /** * @return string @@ -261,26 +261,26 @@ class Introduction implements \JsonSerializable public function __construct(array $data = []) { $this->label = $data['label'] ?? ''; - $this->type = $data['str_$type'] ?? ''; - $this->intro_id = $data['$intro_id'] ?? ''; - $this->madeBy = $data['$madeBy'] ?? ''; - $this->madeByUrl = $data['$madeByUrl'] ?? ''; - $this->madeByZrl = $data['$madeByZrl'] ?? ''; - $this->madeByAddr = $data['$madeByAddr'] ?? ''; - $this->contactId = $data['$contactId'] ?? ''; - $this->photo = $data['$photo'] ?? ''; - $this->name = $data['$name'] ?? ''; - $this->url = $data['$url'] ?? ''; - $this->zrl = $data['$zrl'] ?? ''; - $this->hidden = $data['$hidden'] ?? ''; - $this->postNewFriend = $data['$postNewFriend'] ?? ''; - $this->knowYou = $data['$knowYou'] ?? ''; - $this->note = $data['$note'] ?? ''; - $this->request = $data['$request'] ?? ''; - $this->dfrnId = $data['dfrn_id'] ?? ''; + $this->type = $data['str_type'] ?? ''; + $this->intro_id = $data['$intro_id'] ?? -1; + $this->madeBy = $data['madeBy'] ?? ''; + $this->madeByUrl = $data['madeByUrl'] ?? ''; + $this->madeByZrl = $data['madeByZrl'] ?? ''; + $this->madeByAddr = $data['madeByAddr'] ?? ''; + $this->contactId = $data['contactId'] ?? ''; + $this->photo = $data['photo'] ?? ''; + $this->name = $data['name'] ?? ''; + $this->url = $data['url'] ?? ''; + $this->zrl = $data['zrl'] ?? ''; + $this->hidden = $data['hidden'] ?? false; + $this->postNewFriend = $data['postNewFriend'] ?? ''; + $this->knowYou = $data['knowYou'] ?? false; + $this->note = $data['note'] ?? ''; + $this->request = $data['request'] ?? ''; + $this->dfrnId = $data['dfrn_id'] ?? -1; $this->addr = $data['addr'] ?? ''; $this->network = $data['network'] ?? ''; - $this->uid = $data['uid'] ?? ''; + $this->uid = $data['uid'] ?? -1; $this->keywords = $data['keywords'] ?? ''; $this->gender = $data['gender'] ?? ''; $this->location = $data['location'] ?? ''; diff --git a/src/Object/Notification/Notification.php b/src/Object/Notification/Notification.php index 925927d84..bb29fd0af 100644 --- a/src/Object/Notification/Notification.php +++ b/src/Object/Notification/Notification.php @@ -94,18 +94,16 @@ class Notification implements \JsonSerializable return $this->seen; } - public function __construct(string $label = '', string $link = '', string $image = '', - string $url = '', string $text = '', - string $when = '', string $ago = '', bool $seen = false) + public function __construct(array $data) { - $this->label = $label; - $this->link = $link; - $this->image = $image; - $this->url = $url; - $this->text = $text; - $this->when = $when; - $this->ago = $ago; - $this->seen = $seen; + $this->label = $data['label'] ?? ''; + $this->link = $data['link'] ?? ''; + $this->image = $data['image'] ?? ''; + $this->url = $data['url'] ?? ''; + $this->text = $data['text'] ?? ''; + $this->when = $data['when'] ?? ''; + $this->ago = $data['ago'] ?? ''; + $this->seen = $data['seen'] ?? false; } /** From b85511b00db778030302255fd6901157c750a2f1 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 14:30:32 +0100 Subject: [PATCH 4/5] Improvements: - Fix API-select - Introduce OTYPE-constants - Rename `IntroductionFactory` methods --- include/api.php | 6 +++--- include/enotify.php | 3 ++- src/Factory/Notification/IntroductionFactory.php | 6 +++--- src/Model/Notification.php | 5 +++++ src/Module/Notifications/Introductions.php | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/api.php b/include/api.php index 4a55c2b91..0729c2a0f 100644 --- a/include/api.php +++ b/include/api.php @@ -23,6 +23,7 @@ use Friendica\Model\Contact; use Friendica\Model\Group; use Friendica\Model\Item; use Friendica\Model\Mail; +use Friendica\Model\Notification; use Friendica\Model\Photo; use Friendica\Model\Profile; use Friendica\Model\User; @@ -5906,7 +5907,7 @@ function api_friendica_notification($type) throw new BadRequestException("Invalid argument count"); } - $notifications = DI::notification()->select([], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); + $notifications = DI::notification()->select(['uid' => api_user()], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); if ($type == "xml") { $xmlnotes = false; @@ -5957,8 +5958,7 @@ function api_friendica_notification_seen($type) $notification = DI::notification()->getByID($id); $notification->setSeen(); - if ($notification->otype == 'item') { - // would be really better with an ItemsManager and $im->getByID() :-P + if ($notification->otype === Notification::OTYPE_ITEM) { $item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->iid, 'uid' => api_user()]); if (DBA::isResult($item)) { // we found the item, return it to the user diff --git a/include/enotify.php b/include/enotify.php index 32f32afec..f509367ef 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -12,6 +12,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\ItemContent; +use Friendica\Model\Notification; use Friendica\Model\User; use Friendica\Model\UserItem; use Friendica\Protocol\Activity; @@ -160,7 +161,7 @@ function notification($params) // if it's a post figure out who's post it is. $item = null; - if ($params['otype'] === 'item' && $parent_id) { + if ($params['otype'] === Notification::OTYPE_ITEM && $parent_id) { $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]); } diff --git a/src/Factory/Notification/IntroductionFactory.php b/src/Factory/Notification/IntroductionFactory.php index 3f2bb563a..098882115 100644 --- a/src/Factory/Notification/IntroductionFactory.php +++ b/src/Factory/Notification/IntroductionFactory.php @@ -63,7 +63,7 @@ class IntroductionFactory extends BaseFactory * * @return Introduction[] */ - public function getIntroList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0) + public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0) { $sql_extra = ""; @@ -128,7 +128,7 @@ class IntroductionFactory extends BaseFactory // Normal connection requests } else { - $notification = $this->getMissingIntroData($notification); + $notification = $this->getMissingData($notification); if (empty($notification['url'])) { continue; @@ -182,7 +182,7 @@ class IntroductionFactory extends BaseFactory * * @throws InternalServerErrorException */ - private function getMissingIntroData(array $intro) + private function getMissingData(array $intro) { // If the network and the addr isn't available from the gcontact // table entry, take the one of the contact table entry diff --git a/src/Model/Notification.php b/src/Model/Notification.php index 254aaf5f6..75fb95829 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notification.php @@ -44,6 +44,11 @@ use Psr\Log\LoggerInterface; */ class Notification extends BaseModel { + const OTYPE_ITEM = 'item'; + const OTYPE_INTRO = 'intro'; + const OTYPE_MAIL = 'mail'; + const OTYPE_PERSON = 'person'; + /** @var \Friendica\Repository\Notification */ private $repo; diff --git a/src/Module/Notifications/Introductions.php b/src/Module/Notifications/Introductions.php index 8ded212c6..69c776382 100644 --- a/src/Module/Notifications/Introductions.php +++ b/src/Module/Notifications/Introductions.php @@ -26,7 +26,7 @@ class Introductions extends BaseNotifications $notifications = [ 'ident' => 'introductions', - 'notifications' => DI::factNotIntro()->getIntroList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), + 'notifications' => DI::factNotIntro()->getList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), ]; return [ From 0840938dff3a2bdc8db836662fea2bbccff05e03 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:30:24 +0100 Subject: [PATCH 5/5] Rename classes - Repository/Model Notification => Notify - Factory/Object Notification => Notification --- include/api.php | 8 ++++---- include/enotify.php | 8 ++++---- mod/ping.php | 2 +- .../{Notifications.php => Notifies.php} | 4 ++-- src/DI.php | 18 +++++++++--------- ...ntroductionFactory.php => Introduction.php} | 6 +++--- ...otificationFactory.php => Notification.php} | 8 ++++---- src/Model/{Notification.php => Notify.php} | 6 +++--- src/Module/Notifications/Introductions.php | 2 +- src/Module/Notifications/Notification.php | 4 ++-- src/Module/Notifications/Notifications.php | 8 ++++---- .../{Notification.php => Notify.php} | 16 ++++++++-------- 12 files changed, 45 insertions(+), 45 deletions(-) rename src/Collection/{Notifications.php => Notifies.php} (69%) rename src/Factory/Notification/{IntroductionFactory.php => Introduction.php} (98%) rename src/Factory/Notification/{NotificationFactory.php => Notification.php} (98%) rename src/Model/{Notification.php => Notify.php} (96%) rename src/Repository/{Notification.php => Notify.php} (81%) diff --git a/include/api.php b/include/api.php index 0729c2a0f..f43ee2d1b 100644 --- a/include/api.php +++ b/include/api.php @@ -23,7 +23,7 @@ use Friendica\Model\Contact; use Friendica\Model\Group; use Friendica\Model\Item; use Friendica\Model\Mail; -use Friendica\Model\Notification; +use Friendica\Model\Notify; use Friendica\Model\Photo; use Friendica\Model\Profile; use Friendica\Model\User; @@ -5907,7 +5907,7 @@ function api_friendica_notification($type) throw new BadRequestException("Invalid argument count"); } - $notifications = DI::notification()->select(['uid' => api_user()], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); + $notifications = DI::notify()->select(['uid' => api_user()], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]); if ($type == "xml") { $xmlnotes = false; @@ -5955,10 +5955,10 @@ function api_friendica_notification_seen($type) $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0); try { - $notification = DI::notification()->getByID($id); + $notification = DI::notify()->getByID($id); $notification->setSeen(); - if ($notification->otype === Notification::OTYPE_ITEM) { + if ($notification->otype === Notify::OTYPE_ITEM) { $item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->iid, 'uid' => api_user()]); if (DBA::isResult($item)) { // we found the item, return it to the user diff --git a/include/enotify.php b/include/enotify.php index f509367ef..144460693 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -12,7 +12,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\ItemContent; -use Friendica\Model\Notification; +use Friendica\Model\Notify; use Friendica\Model\User; use Friendica\Model\UserItem; use Friendica\Protocol\Activity; @@ -161,7 +161,7 @@ function notification($params) // if it's a post figure out who's post it is. $item = null; - if ($params['otype'] === Notification::OTYPE_ITEM && $parent_id) { + if ($params['otype'] === Notify::OTYPE_ITEM && $parent_id) { $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]); } @@ -483,7 +483,7 @@ function notification($params) $notify_id = 0; if ($show_in_notification_page) { - $notification = DI::notification()->insert([ + $notification = DI::notify()->insert([ 'name' => $params['source_name'], 'url' => $params['source_link'], 'photo' => $params['source_photo'], @@ -498,7 +498,7 @@ function notification($params) $notification->link = DI::baseUrl() . '/notification/view/' . $notification->id; $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]); - DI::notification()->update($notification); + DI::notify()->update($notification); $itemlink = $notification->link; $notify_id = $notification->id; diff --git a/mod/ping.php b/mod/ping.php index a786d379f..dd57ba34d 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -433,7 +433,7 @@ function ping_get_notifications($uid) $notification["message"] = $notification["msg_cache"]; } else { $notification["name"] = strip_tags(BBCode::convert($notification["name"])); - $notification["message"] = Friendica\Model\Notification::formatMessage($notification["name"], strip_tags(BBCode::convert($notification["msg"]))); + $notification["message"] = Friendica\Model\Notify::formatMessage($notification["name"], strip_tags(BBCode::convert($notification["msg"]))); q( "UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d", diff --git a/src/Collection/Notifications.php b/src/Collection/Notifies.php similarity index 69% rename from src/Collection/Notifications.php rename to src/Collection/Notifies.php index 1c04bdf4d..0d64ee7e6 100644 --- a/src/Collection/Notifications.php +++ b/src/Collection/Notifies.php @@ -5,10 +5,10 @@ namespace Friendica\Collection; use Friendica\BaseCollection; use Friendica\Model; -class Notifications extends BaseCollection +class Notifies extends BaseCollection { /** - * @return Model\Notification + * @return Model\Notify */ public function current() { diff --git a/src/DI.php b/src/DI.php index 8f80bf68d..3034bd971 100644 --- a/src/DI.php +++ b/src/DI.php @@ -245,19 +245,19 @@ abstract class DI } /** - * @return \Friendica\Factory\Notification\NotificationFactory + * @return \Friendica\Factory\Notification\Notification */ - public static function factNotification() + public static function notification() { - return self::$dice->create(Factory\Notification\NotificationFactory::class); + return self::$dice->create(Factory\Notification\Notification::class); } /** - * @return \Friendica\Factory\Notification\IntroductionFactory + * @return \Friendica\Factory\Notification\Introduction */ - public static function factNotIntro() + public static function notificationIntro() { - return self::$dice->create(Factory\Notification\IntroductionFactory::class); + return self::$dice->create(Factory\Notification\Introduction::class); } // @@ -273,11 +273,11 @@ abstract class DI } /** - * @return Repository\Notification + * @return Repository\Notify */ - public static function notification() + public static function notify() { - return self::$dice->create(Repository\Notification::class); + return self::$dice->create(Repository\Notify::class); } /** diff --git a/src/Factory/Notification/IntroductionFactory.php b/src/Factory/Notification/Introduction.php similarity index 98% rename from src/Factory/Notification/IntroductionFactory.php rename to src/Factory/Notification/Introduction.php index 098882115..6b192ec1d 100644 --- a/src/Factory/Notification/IntroductionFactory.php +++ b/src/Factory/Notification/Introduction.php @@ -15,7 +15,7 @@ use Friendica\Database\Database; use Friendica\Model\Contact; use Friendica\Module\BaseNotifications; use Friendica\Network\HTTPException\InternalServerErrorException; -use Friendica\Object\Notification\Introduction; +use Friendica\Object; use Friendica\Util\Proxy; use Psr\Log\LoggerInterface; @@ -25,7 +25,7 @@ use Psr\Log\LoggerInterface; * - Friend suggestion * - Friend/Follower request */ -class IntroductionFactory extends BaseFactory +class Introduction extends BaseFactory { /** @var Database */ private $dba; @@ -61,7 +61,7 @@ class IntroductionFactory extends BaseFactory * @param int $limit Maximum number of query results * @param int $id When set, only the introduction with this id is displayed * - * @return Introduction[] + * @return Object\Notification\Introduction[] */ public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0) { diff --git a/src/Factory/Notification/NotificationFactory.php b/src/Factory/Notification/Notification.php similarity index 98% rename from src/Factory/Notification/NotificationFactory.php rename to src/Factory/Notification/Notification.php index 05dd200ad..5f2c2231b 100644 --- a/src/Factory/Notification/NotificationFactory.php +++ b/src/Factory/Notification/Notification.php @@ -16,7 +16,7 @@ use Friendica\Model\Item; use Friendica\Module\BaseNotifications; use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Protocol\Activity; -use Friendica\Repository\Notification; +use Friendica\Repository; use Friendica\Util\DateTimeFormat; use Friendica\Util\Proxy; use Friendica\Util\Temporal; @@ -31,11 +31,11 @@ use Psr\Log\LoggerInterface; * - home * - personal */ -class NotificationFactory extends BaseFactory +class Notification extends BaseFactory { /** @var Database */ private $dba; - /** @var Notification */ + /** @var Repository\Notify */ private $notification; /** @var BaseURL */ private $baseUrl; @@ -44,7 +44,7 @@ class NotificationFactory extends BaseFactory /** @var string */ private $nurl; - public function __construct(LoggerInterface $logger, Database $dba, Notification $notification, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session) + public function __construct(LoggerInterface $logger, Database $dba, Repository\Notify $notification, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session) { parent::__construct($logger); diff --git a/src/Model/Notification.php b/src/Model/Notify.php similarity index 96% rename from src/Model/Notification.php rename to src/Model/Notify.php index 75fb95829..1bb9f2904 100644 --- a/src/Model/Notification.php +++ b/src/Model/Notify.php @@ -42,17 +42,17 @@ use Psr\Log\LoggerInterface; * @property-read string $msg_html * @property-read string $msg_plain */ -class Notification extends BaseModel +class Notify extends BaseModel { const OTYPE_ITEM = 'item'; const OTYPE_INTRO = 'intro'; const OTYPE_MAIL = 'mail'; const OTYPE_PERSON = 'person'; - /** @var \Friendica\Repository\Notification */ + /** @var \Friendica\Repository\Notify */ private $repo; - public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notification $repo, array $data = []) + public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = []) { parent::__construct($dba, $logger, $data); diff --git a/src/Module/Notifications/Introductions.php b/src/Module/Notifications/Introductions.php index 69c776382..10919a13a 100644 --- a/src/Module/Notifications/Introductions.php +++ b/src/Module/Notifications/Introductions.php @@ -26,7 +26,7 @@ class Introductions extends BaseNotifications $notifications = [ 'ident' => 'introductions', - 'notifications' => DI::factNotIntro()->getList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), + 'notifications' => DI::notificationIntro()->getList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id), ]; return [ diff --git a/src/Module/Notifications/Notification.php b/src/Module/Notifications/Notification.php index f7fe6f190..9b252562c 100644 --- a/src/Module/Notifications/Notification.php +++ b/src/Module/Notifications/Notification.php @@ -24,7 +24,7 @@ class Notification extends BaseModule // @TODO: Replace with parameter from router if (DI::args()->get(1) === 'mark' && DI::args()->get(2) === 'all') { try { - $success = DI::notification()->setAllSeen(); + $success = DI::notify()->setAllSeen(); }catch (\Exception $e) { $success = false; } @@ -48,7 +48,7 @@ class Notification extends BaseModule // @TODO: Replace with parameter from router if (DI::args()->getArgc() > 2 && DI::args()->get(1) === 'view' && intval(DI::args()->get(2))) { try { - $notification = DI::notification()->getByID(DI::args()->get(2)); + $notification = DI::notify()->getByID(DI::args()->get(2)); $notification->setSeen(); if (!empty($notification->link)) { diff --git a/src/Module/Notifications/Notifications.php b/src/Module/Notifications/Notifications.php index bff5026df..37b234666 100644 --- a/src/Module/Notifications/Notifications.php +++ b/src/Module/Notifications/Notifications.php @@ -31,7 +31,7 @@ class Notifications extends BaseNotifications $notificationHeader = DI::l10n()->t('Network Notifications'); $notifications = [ 'ident' => Notification::NETWORK, - 'notifications' => DI::factNotification()->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + 'notifications' => DI::notification()->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), ]; // Get the system notifications @@ -39,7 +39,7 @@ class Notifications extends BaseNotifications $notificationHeader = DI::l10n()->t('System Notifications'); $notifications = [ 'ident' => Notification::SYSTEM, - 'notifications' => DI::factNotification()->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + 'notifications' => DI::notification()->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), ]; // Get the personal notifications @@ -47,7 +47,7 @@ class Notifications extends BaseNotifications $notificationHeader = DI::l10n()->t('Personal Notifications'); $notifications = [ 'ident' => Notification::PERSONAL, - 'notifications' => DI::factNotification()->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + 'notifications' => DI::notification()->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), ]; // Get the home notifications @@ -55,7 +55,7 @@ class Notifications extends BaseNotifications $notificationHeader = DI::l10n()->t('Home Notifications'); $notifications = [ 'ident' => Notification::HOME, - 'notifications' => DI::factNotification()->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), + 'notifications' => DI::notification()->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE), ]; // fallback - redirect to main page } else { diff --git a/src/Repository/Notification.php b/src/Repository/Notify.php similarity index 81% rename from src/Repository/Notification.php rename to src/Repository/Notify.php index a17d1223a..2ac14858f 100644 --- a/src/Repository/Notification.php +++ b/src/Repository/Notify.php @@ -10,28 +10,28 @@ use Friendica\Collection; use Friendica\Network\HTTPException\NotFoundException; use Friendica\Util\DateTimeFormat; -class Notification extends BaseRepository +class Notify extends BaseRepository { protected static $table_name = 'notify'; - protected static $model_class = Model\Notification::class; + protected static $model_class = Model\Notify::class; - protected static $collection_class = Collection\Notifications::class; + protected static $collection_class = Collection\Notifies::class; /** * {@inheritDoc} * - * @return Model\Notification + * @return Model\Notify */ protected function create(array $data) { - return new Model\Notification($this->dba, $this->logger, $this, $data); + return new Model\Notify($this->dba, $this->logger, $this, $data); } /** * {@inheritDoc} * - * @return Collection\Notifications + * @return Collection\Notifies */ public function select(array $condition = [], array $params = []) { @@ -45,7 +45,7 @@ class Notification extends BaseRepository /** * {@inheritDoc} * - * @return Model\Notification + * @return Model\Notify * @throws NotFoundException */ public function getByID(int $id) @@ -67,7 +67,7 @@ class Notification extends BaseRepository /** * @param array $fields * - * @return Model\Notification + * @return Model\Notify * * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws Exception