diff --git a/src/BaseDepository.php b/src/BaseDepository.php index 00d3bcfdfd..18cca9d30e 100644 --- a/src/BaseDepository.php +++ b/src/BaseDepository.php @@ -5,6 +5,7 @@ namespace Friendica; use Exception; use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Database\Database; +use Friendica\Database\DBA; use Friendica\Network\HTTPException\NotFoundException; use Psr\Log\LoggerInterface; @@ -43,6 +44,67 @@ abstract class BaseDepository $this->factory = $factory; } + /** + * Populates the collection according to the condition. Retrieves a limited subset of entities depending on the + * boundaries and the limit. The total count of rows matching the condition is stored in the collection. + * + * Depends on the corresponding table featuring a numerical auto incremented column called `id`. + * + * max_id and min_id are susceptible to the query order: + * - min_id alone only reliably works with ASC order + * - max_id alone only reliably works with DESC order + * If the wrong order is detected in either case, we reverse the query order and the entity list order after the query + * + * Chainable. + * + * @param array $condition + * @param array $params + * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible + * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible + * @param int $limit + * @return BaseCollection + * @throws \Exception + */ + protected function _selectByBoundaries( + array $condition = [], + array $params = [], + int $min_id = null, + int $max_id = null, + int $limit = self::LIMIT + ): BaseCollection { + $totalCount = $this->count($condition); + + $boundCondition = $condition; + + $reverseOrder = false; + + if (isset($min_id)) { + $boundCondition = DBA::mergeConditions($boundCondition, ['`id` > ?', $min_id]); + if (!isset($max_id) && isset($params['order']['id']) && ($params['order']['id'] === true || $params['order']['id'] === 'DESC')) { + $reverseOrder = true; + + $params['order']['id'] = 'ASC'; + } + } + + if (isset($max_id)) { + $boundCondition = DBA::mergeConditions($boundCondition, ['`id` < ?', $max_id]); + if (!isset($min_id) && (!isset($params['order']['id']) || $params['order']['id'] === false || $params['order']['id'] === 'ASC')) { + $reverseOrder = true; + + $params['order']['id'] = 'DESC'; + } + } + + $params['limit'] = $limit; + + $Entities = $this->_select($boundCondition, $params); + if ($reverseOrder) { + $Entities->reverse(); + } + + return new BaseCollection($Entities->getArrayCopy(), $totalCount); + } /** * @param array $condition diff --git a/src/Navigation/Notifications/Collection/Notifications.php b/src/Navigation/Notifications/Collection/Notifications.php new file mode 100644 index 0000000000..f383b4ccb0 --- /dev/null +++ b/src/Navigation/Notifications/Collection/Notifications.php @@ -0,0 +1,43 @@ +. + * + */ + +namespace Friendica\Navigation\Notifications\Collection; + +use Friendica\BaseCollection; +use Friendica\Navigation\Notifications\Entity; + +class Notifications extends BaseCollection +{ + /** + * @return Entity\Notification + */ + public function current(): Entity\Notification + { + return parent::current(); + } + + public function setSeen(): Notifications + { + return $this->map(function (Entity\Notification $Notification) { + $Notification->setSeen(); + }); + } +} diff --git a/src/Navigation/Notifications/Depository/Notification.php b/src/Navigation/Notifications/Depository/Notification.php new file mode 100644 index 0000000000..a93bce6657 --- /dev/null +++ b/src/Navigation/Notifications/Depository/Notification.php @@ -0,0 +1,141 @@ +getArrayCopy()); + } + + public function countForUser($uid, array $condition, array $params = []): int + { + $condition = DBA::mergeConditions($condition, ['uid' => $uid]); + + return $this->count($condition, $params); + } + + public function existsForUser($uid, array $condition): bool + { + $condition = DBA::mergeConditions($condition, ['uid' => $uid]); + + return $this->exists($condition); + } + + /** + * @param int $id + * @return Entity\Notification + * @throws NotFoundException + */ + public function selectOneById(int $id): Entity\Notification + { + return $this->selectOne(['id' => $id]); + } + + public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification + { + $condition = DBA::mergeConditions($condition, ['uid' => $uid]); + + return $this->selectOne($condition, $params); + } + + public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications + { + $condition = DBA::mergeConditions($condition, ['uid' => $uid]); + + return $this->select($condition, $params); + } + + public function selectAllForUser(int $uid): Collection\Notifications + { + return $this->selectForUser($uid); + } + + /** + * @param array $condition + * @param array $params + * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible + * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible + * @param int $limit + * @return BaseCollection + * @throws Exception + * @see _selectByBoundaries + */ + public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) + { + $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit); + + return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount()); + } + + public function setAllSeenForUser(int $uid, array $condition = []): bool + { + $condition = DBA::mergeConditions($condition, ['uid' => $uid]); + + return $this->db->update(self::$table_name, ['seen' => true], $condition); + } + + /** + * @param Entity\Notification $Notification + * @return Entity\Notification + * @throws Exception + */ + public function save(Entity\Notification $Notification): Entity\Notification + { + $fields = [ + 'uid' => $Notification->uid, + 'vid' => Verb::getID($Notification->verb), + 'type' => $Notification->type, + 'actor-id' => $Notification->actorId, + 'target-uri-id' => $Notification->targetUriId, + 'parent-uri-id' => $Notification->parentUriId, + 'seen' => $Notification->seen, + ]; + + if ($Notification->id) { + $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]); + } else { + $fields['created'] = DateTimeFormat::utcNow(); + $this->db->insert(self::$table_name, $fields); + + $Notification = $this->selectOneById($this->db->lastInsertId()); + } + + return $Notification; + } +} diff --git a/src/Navigation/Notifications/Entity/Notification.php b/src/Navigation/Notifications/Entity/Notification.php new file mode 100644 index 0000000000..3f491f98c0 --- /dev/null +++ b/src/Navigation/Notifications/Entity/Notification.php @@ -0,0 +1,74 @@ +uid = $uid; + $this->verb = $verb; + $this->type = $type; + $this->actorId = $actorId; + $this->targetUriId = $targetUriId; + $this->parentUriId = $parentUriId ?: $targetUriId; + $this->created = $created; + $this->seen = $seen; + $this->id = $id; + } + + public function setSeen() + { + $this->seen = true; + } +} diff --git a/src/Navigation/Notifications/Exception/UnexpectedNotificationTypeException.php b/src/Navigation/Notifications/Exception/UnexpectedNotificationTypeException.php new file mode 100644 index 0000000000..e370608af5 --- /dev/null +++ b/src/Navigation/Notifications/Exception/UnexpectedNotificationTypeException.php @@ -0,0 +1,7 @@ +. + * + */ + +namespace Friendica\Navigation\Notifications\Factory; + +use Exception; +use Friendica\App; +use Friendica\App\BaseURL; +use Friendica\BaseFactory; +use Friendica\Content\Text\BBCode; +use Friendica\Core\L10n; +use Friendica\Core\PConfig\IPConfig; +use Friendica\Core\Protocol; +use Friendica\Core\Session\ISession; +use Friendica\Database\Database; +use Friendica\Model\Contact; +use Friendica\Module\BaseNotifications; +use Friendica\Navigation\Notifications\ValueObject; +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 Introduction extends BaseFactory +{ + /** @var Database */ + private $dba; + /** @var BaseURL */ + private $baseUrl; + /** @var L10n */ + private $l10n; + /** @var IPConfig */ + private $pConfig; + /** @var ISession */ + private $session; + /** @var string */ + private $nick; + + public function __construct(LoggerInterface $logger, Database $dba, BaseURL $baseUrl, L10n $l10n, App $app, IPConfig $pConfig, ISession $session) + { + parent::__construct($logger); + + $this->dba = $dba; + $this->baseUrl = $baseUrl; + $this->l10n = $l10n; + $this->pConfig = $pConfig; + $this->session = $session; + $this->nick = $app->getLoggedInUserNickname() ?? ''; + } + + /** + * 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 ValueObject\Introduction[] + */ + public function getList(bool $all = false, int $start = 0, int $limit = BaseNotifications::DEFAULT_PAGE_LIMIT, int $id = 0): array + { + $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 ", $id); + } + + $formattedIntroductions = []; + + try { + /// @todo Fetch contact details by "Contact::getByUrl" instead of queries to contact and fcontact + $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` + FROM `intro` + LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` + LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` + WHERE `intro`.`uid` = ? $sql_extra + LIMIT ?, ?", + $_SESSION['uid'], + $start, + $limit + ); + + while ($intro = $this->dba->fetch($stmtNotifications)) { + if (empty($intro['url'])) { + continue; + } + + // 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'] ?? '') { + if (empty($intro['furl'])) { + continue; + } + $return_addr = bin2hex($this->nick . '@' . + $this->baseUrl->getHostname() . + (($this->baseUrl->getUrlPath()) ? '/' . $this->baseUrl->getUrlPath() : '')); + + $formattedIntroductions[] = new ValueObject\Introduction([ + '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' => Contact::getAvatarUrlForUrl($intro['furl'], 0, Proxy::SIZE_SMALL), + '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), + 'note' => $intro['note'], + 'request' => $intro['frequest'] . '?addr=' . $return_addr]); + + // Normal connection requests + } else { + // Don't show these data until you are connected. Diaspora is doing the same. + if ($intro['network'] === Protocol::DIASPORA) { + $intro['location'] = ""; + $intro['about'] = ""; + } + + $formattedIntroductions[] = new ValueObject\Introduction([ + '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' => $this->session->get('uid'), + 'intro_id' => $intro['intro_id'], + 'contact_id' => $intro['contact-id'], + 'photo' => Contact::getPhoto($intro), + 'name' => $intro['name'], + 'location' => BBCode::convert($intro['location'], false), + 'about' => BBCode::convert($intro['about'], false), + 'keywords' => $intro['keywords'], + '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['addr'], + 'network' => $intro['network'], + 'knowyou' => $intro['knowyou'], + 'note' => $intro['note'], + ]); + } + } + } catch (Exception $e) { + $this->logger->warning('Select failed.', ['uid' => $_SESSION['uid'], 'exception' => $e]); + } + + return $formattedIntroductions; + } +} diff --git a/src/Navigation/Notifications/Factory/Notification.php b/src/Navigation/Notifications/Factory/Notification.php new file mode 100644 index 0000000000..e8ff9ea30f --- /dev/null +++ b/src/Navigation/Notifications/Factory/Notification.php @@ -0,0 +1,235 @@ +actorId, ['id', 'name', 'url', 'pending']); + if (empty($causer)) { + $this->logger->info('Causer not found', ['contact' => $Notification->actorId]); + return $message; + } + + if ($Notification->type === Post\UserNotification::TYPE_NONE) { + if ($causer['pending']) { + $msg = $userL10n->t('%1$s wants to follow you'); + } else { + $msg = $userL10n->t('%1$s had started following you'); + } + $title = $causer['name']; + $link = $baseUrl . '/contact/' . $causer['id']; + } else { + if (!$Notification->targetUriId) { + return $message; + } + + if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) { + $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); + if (empty($item)) { + $this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]); + return $message; + } + } else { + $item = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); + if (empty($item)) { + $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]); + return $message; + } + + if ($Notification->verb == Activity::POST) { + $item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); + if (empty($item)) { + $this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]); + return $message; + } + } + } + + if ($item['owner-id'] != $item['author-id']) { + $cid = $item['owner-id']; + } + if (!empty($item['causer-id']) && ($item['causer-id'] != $item['author-id'])) { + $cid = $item['causer-id']; + } + + if (($Notification->type === Post\UserNotification::TYPE_SHARED) && !empty($cid)) { + $causer = Contact::getById($cid, ['id', 'name', 'url']); + if (empty($causer)) { + $this->logger->info('Causer not found', ['causer' => $cid]); + return $message; + } + } elseif (in_array($Notification->type, [Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION])) { + $author = Contact::getById($item['author-id'], ['id', 'name', 'url']); + if (empty($author)) { + $this->logger->info('Author not found', ['author' => $item['author-id']]); + return $message; + } + } + + $link = $baseUrl . '/display/' . urlencode($item['guid']); + + $content = Plaintext::getPost($item, 70); + if (!empty($content['text'])) { + $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"'; + } else { + $title = ''; + } + + switch ($Notification->verb) { + case Activity::LIKE: + switch ($Notification->type) { + case Post\UserNotification::TYPE_DIRECT_COMMENT: + $msg = $userL10n->t('%1$s liked your comment %2$s'); + break; + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: + $msg = $userL10n->t('%1$s liked your post %2$s'); + break; + } + break; + case Activity::DISLIKE: + switch ($Notification->type) { + case Post\UserNotification::TYPE_DIRECT_COMMENT: + $msg = $userL10n->t('%1$s disliked your comment %2$s'); + break; + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: + $msg = $userL10n->t('%1$s disliked your post %2$s'); + break; + } + break; + case Activity::ANNOUNCE: + switch ($Notification->type) { + case Post\UserNotification::TYPE_DIRECT_COMMENT: + $msg = $userL10n->t('%1$s shared your comment %2$s'); + break; + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: + $msg = $userL10n->t('%1$s shared your post %2$s'); + break; + } + break; + case Activity::POST: + switch ($Notification->type) { + case Post\UserNotification::TYPE_EXPLICIT_TAGGED: + $msg = $userL10n->t('%1$s tagged you on %2$s'); + break; + + case Post\UserNotification::TYPE_IMPLICIT_TAGGED: + $msg = $userL10n->t('%1$s replied to you on %2$s'); + break; + + case Post\UserNotification::TYPE_THREAD_COMMENT: + $msg = $userL10n->t('%1$s commented in your thread %2$s'); + break; + + case Post\UserNotification::TYPE_DIRECT_COMMENT: + $msg = $userL10n->t('%1$s commented on your comment %2$s'); + break; + + case Post\UserNotification::TYPE_COMMENT_PARTICIPATION: + case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION: + if (($causer['id'] == $author['id']) && ($title != '')) { + $msg = $userL10n->t('%1$s commented in their thread %2$s'); + } elseif ($causer['id'] == $author['id']) { + $msg = $userL10n->t('%1$s commented in their thread'); + } elseif ($title != '') { + $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s'); + } else { + $msg = $userL10n->t('%1$s commented in the thread from %3$s'); + } + break; + + case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: + $msg = $userL10n->t('%1$s commented on your thread %2$s'); + break; + + case Post\UserNotification::TYPE_SHARED: + if (($causer['id'] != $author['id']) && ($title != '')) { + $msg = $userL10n->t('%1$s shared the post %2$s from %3$s'); + } elseif ($causer['id'] != $author['id']) { + $msg = $userL10n->t('%1$s shared a post from %3$s'); + } elseif ($title != '') { + $msg = $userL10n->t('%1$s shared the post %2$s'); + } else { + $msg = $userL10n->t('%1$s shared a post'); + } + break; + } + break; + } + } + + if (!empty($msg)) { + // Name of the notification's causer + $message['causer'] = $causer['name']; + // Format for the "ping" mechanism + $message['notification'] = sprintf($msg, '{0}', $title, $author['name']); + // Plain text for the web push api + $message['plain'] = sprintf($msg, $causer['name'], $title, $author['name']); + // Rich text for other purposes + $message['rich'] = sprintf($msg, + '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]', + '[url=' . $link . ']' . $title . '[/url]', + '[url=' . $author['url'] . ']' . $author['name'] . '[/url]'); + } + + return $message; + } +} diff --git a/src/Navigation/Notifications/ValueObject/Introduction.php b/src/Navigation/Notifications/ValueObject/Introduction.php new file mode 100644 index 0000000000..332f6ccf60 --- /dev/null +++ b/src/Navigation/Notifications/ValueObject/Introduction.php @@ -0,0 +1,241 @@ +. + * + */ + +namespace Friendica\Navigation\Notifications\ValueObject; + +/** + * A view-only object for printing introduction notifications to the frontend + */ +class Introduction implements \JsonSerializable +{ + /** @var string */ + private $label; + /** @var string */ + private $type; + /** @var int */ + private $intro_id; + /** @var string */ + private $madeBy; + /** @var string */ + private $madeByUrl; + /** @var string */ + private $madeByZrl; + /** @var string */ + private $madeByAddr; + /** @var int */ + private $contactId; + /** @var string */ + private $photo; + /** @var string */ + private $name; + /** @var string */ + private $url; + /** @var string */ + private $zrl; + /** @var boolean */ + private $hidden; + /** @var int */ + private $postNewFriend; + /** @var boolean */ + private $knowYou; + /** @var string */ + private $note; + /** @var string */ + private $request; + /** @var int */ + private $dfrnId; + /** @var string */ + private $addr; + /** @var string */ + private $network; + /** @var int */ + private $uid; + /** @var string */ + private $keywords; + /** @var string */ + private $location; + /** @var string */ + private $about; + + public function getLabel(): string + { + return $this->label; + } + + public function getType(): string + { + return $this->type; + } + + public function getIntroId(): int + { + return $this->intro_id; + } + + public function getMadeBy(): string + { + return $this->madeBy; + } + + public function getMadeByUrl(): string + { + return $this->madeByUrl; + } + + public function getMadeByZrl(): string + { + return $this->madeByZrl; + } + + public function getMadeByAddr(): string + { + return $this->madeByAddr; + } + + public function getContactId(): int + { + return $this->contactId; + } + + public function getPhoto(): string + { + return $this->photo; + } + + public function getName(): string + { + return $this->name; + } + + public function getUrl(): string + { + return $this->url; + } + + public function getZrl(): string + { + return $this->zrl; + } + + public function isHidden(): bool + { + return $this->hidden; + } + + public function getPostNewFriend(): int + { + return $this->postNewFriend; + } + + public function getKnowYou(): string + { + return $this->knowYou; + } + + public function getNote(): string + { + return $this->note; + } + + public function getRequest(): string + { + return $this->request; + } + + public function getDfrnId(): int + { + return $this->dfrnId; + } + + public function getAddr(): string + { + return $this->addr; + } + + public function getNetwork(): string + { + return $this->network; + } + + public function getUid(): int + { + return $this->uid; + } + + public function getKeywords(): string + { + return $this->keywords; + } + + public function getLocation(): string + { + return $this->location; + } + + public function getAbout(): string + { + return $this->about; + } + + public function __construct(array $data = []) + { + $this->label = $data['label'] ?? ''; + $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'] ?? -1; + $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 = -1; + $this->addr = $data['addr'] ?? ''; + $this->network = $data['network'] ?? ''; + $this->uid = $data['uid'] ?? -1; + $this->keywords = $data['keywords'] ?? ''; + $this->location = $data['location'] ?? ''; + $this->about = $data['about'] ?? ''; + } + + /** + * @inheritDoc + */ + public function jsonSerialize() + { + return $this->toArray(); + } + + /** + * @return array + */ + public function toArray(): array + { + return get_object_vars($this); + } +}