use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Capabilities\ICanCreateFromTableRow;
+use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\L10n;
use Friendica\Model\Contact;
use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Navigation\Notifications\Entity;
+use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
+use Psr\Log\LoggerInterface;
class Notification extends BaseFactory implements ICanCreateFromTableRow
{
+ /** @var BaseURL */
+ private $baseUrl;
+ /** @var L10n */
+ private $l10n;
+ /** @var LocalRelationship */
+ private $localRelationshipRepo;
+
+ public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger)
+ {
+ parent::__construct($logger);
+
+ $this->baseUrl = $baseUrl;
+ $this->l10n = $l10n;
+ $this->localRelationshipRepo = $localRelationshipRepo;
+ }
+
public function createFromTableRow(array $row): Entity\Notification
{
return new Entity\Notification(
$row['parent-uri-id'],
new \DateTime($row['created'], new \DateTimeZone('UTC')),
$row['seen'],
- $row['id']
+ $row['dismissed'],
+ $row['id'],
);
}
);
}
+ /**
+ * @param int $uid
+ * @param int $contactId Public contact id
+ * @param string $verb
+ * @return Entity\Notification
+ */
public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
{
return new Entity\Notification(
/**
* @param Entity\Notification $Notification
- * @param BaseURL $baseUrl
- * @param L10n $userL10n Seeded with the language of the user we mean the notification for
* @return array
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws HTTPException\InternalServerErrorException
+ * @throws HTTPException\NotFoundException
*/
- public function getMessageFromNotification(Entity\Notification $Notification, BaseURL $baseUrl, L10n $userL10n)
+ public function getMessageFromNotification(Entity\Notification $Notification): array
{
$message = [];
}
if ($Notification->type === Post\UserNotification::TYPE_NONE) {
- if ($causer['pending']) {
- $msg = $userL10n->t('%1$s wants to follow you');
+ $localRelationship = $this->localRelationshipRepo->getForUserContact($Notification->uid, $Notification->actorId);
+ if ($localRelationship->pending) {
+ $msg = $this->l10n->t('%1$s wants to follow you');
} else {
- $msg = $userL10n->t('%1$s had started following you');
+ $msg = $this->l10n->t('%1$s had started following you');
}
+
$title = $causer['name'];
- $link = $baseUrl . '/contact/' . $causer['id'];
+ $link = $this->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]]);
+ $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->type == Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION) {
+ $thrParentId = $item['thr-parent-id'];
+ $item = Post::selectFirst([], ['uri-id' => $thrParentId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
if (empty($item)) {
- $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
+ $this->logger->info('Thread parent post not found', ['uri-id' => $thrParentId]);
return $message;
}
+ }
- if (($Notification->verb == Activity::POST) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) {
- $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;
- }
+ $parent = $item;
+ if ($Notification->targetUriId != $Notification->parentUriId) {
+ $parent = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
+ if (empty($parent)) {
+ $this->logger->info('Top level post not found', ['uri-id' => $Notification->parentUriId]);
+ return $message;
}
}
}
}
- $link = $baseUrl . '/display/' . urlencode($item['guid']);
+ $link = $this->baseUrl . '/display/' . urlencode($item['guid']);
- $content = Plaintext::getPost($item, 70);
+ $content = Plaintext::getPost($parent, 70);
if (!empty($content['text'])) {
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
} else {
case Activity::LIKE:
switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_COMMENT:
- $msg = $userL10n->t('%1$s liked your comment %2$s');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->t('%1$s shared your post %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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->t('%1$s shared a post from %3$s');
} elseif ($title != '') {
- $msg = $userL10n->t('%1$s shared the post %2$s');
+ $msg = $this->l10n->t('%1$s shared the post %2$s');
} else {
- $msg = $userL10n->t('%1$s shared a post');
+ $msg = $this->l10n->t('%1$s shared a post');
}
break;
}
case Activity::ATTEND:
switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
- $msg = $userL10n->t('%1$s wants to attend your event %2$s');
+ $msg = $this->l10n->t('%1$s wants to attend your event %2$s');
break;
}
break;
case Activity::ATTENDNO:
switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
- $msg = $userL10n->t('%1$s does not want to attend your event %2$s');
+ $msg = $this->l10n->t('%1$s does not want to attend your event %2$s');
break;
}
break;
case Activity::ATTENDMAYBE:
switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT:
- $msg = $userL10n->t('%1$s maybe wants to attend your event %2$s');
+ $msg = $this->l10n->t('%1$s maybe wants to attend your event %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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->t('%1$s commented in their thread %2$s');
} elseif ($causer['id'] == $author['id']) {
- $msg = $userL10n->t('%1$s commented in their thread');
+ $msg = $this->l10n->t('%1$s commented in their thread');
} elseif ($title != '') {
- $msg = $userL10n->t('%1$s commented in the thread %2$s from %3$s');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->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');
+ $msg = $this->l10n->t('%1$s shared a post from %3$s');
} elseif ($title != '') {
- $msg = $userL10n->t('%1$s shared the post %2$s');
+ $msg = $this->l10n->t('%1$s shared the post %2$s');
} else {
- $msg = $userL10n->t('%1$s shared a post');
+ $msg = $this->l10n->t('%1$s shared a post');
}
break;
}
'[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
'[url=' . $link . ']' . $title . '[/url]',
'[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
+ $message['link'] = $link;
}
return $message;