Use public contact ID in Model\Post\UserNotification::insertNotification

- Add localRelationship dependency to Notification factory
- Remove dependencies from Factory\Notification->getMessageFromNotification method
This commit is contained in:
Hypolite Petovan 2022-03-03 08:49:07 -05:00
parent 7ce97459d4
commit deafdcde95
8 changed files with 105 additions and 70 deletions

View File

@ -487,6 +487,11 @@ abstract class DI
return self::$dice->create(Contact\Introduction\Factory\Introduction::class); return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
} }
public static function localRelationship(): Contact\LocalRelationship\Repository\LocalRelationship
{
return self::$dice->create(Contact\LocalRelationship\Repository\LocalRelationship::class);
}
public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet
{ {
return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class); return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class);

View File

@ -2665,7 +2665,7 @@ class Contact
// Ensure to always have the correct network type, independent from the connection request method // Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact['id']); self::updateFromProbe($contact['id']);
Post\UserNotification::insertNotification($contact['id'], Activity::FOLLOW, $importer['uid']); Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
return true; return true;
} else { } else {
@ -2696,7 +2696,7 @@ class Contact
self::updateAvatar($contact_id, $photo, true); self::updateAvatar($contact_id, $photo, true);
Post\UserNotification::insertNotification($contact_id, Activity::FOLLOW, $importer['uid']); Post\UserNotification::insertNotification($pub_contact['id'], Activity::FOLLOW, $importer['uid']);
$contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]); $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
@ -2716,9 +2716,7 @@ class Contact
Group::addMember(User::getDefaultGroup($importer['uid']), $contact_record['id']); Group::addMember(User::getDefaultGroup($importer['uid']), $contact_record['id']);
if (($user['notify-flags'] & Notification\Type::INTRO) && if (($user['notify-flags'] & Notification\Type::INTRO) && $user['page-flags'] == User::PAGE_FLAGS_NORMAL) {
in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL])) {
DI::notify()->createFromArray([ DI::notify()->createFromArray([
'type' => Notification\Type::INTRO, 'type' => Notification\Type::INTRO,
'otype' => Notification\ObjectType::INTRO, 'otype' => Notification\ObjectType::INTRO,

View File

@ -308,7 +308,7 @@ class UserNotification
return; return;
} }
$notification = (new Notifications\Factory\Notification(DI::logger()))->createForUser( $notification = (new Notifications\Factory\Notification(DI::baseUrl(), DI::l10n(), DI::localRelationship(), DI::logger()))->createForUser(
$uid, $uid,
$item['vid'], $item['vid'],
$type, $type,
@ -328,7 +328,7 @@ class UserNotification
/** /**
* Add a notification entry * Add a notification entry
* *
* @param int $actor Contact ID of the actor * @param int $actor Public contact ID of the actor
* @param string $verb One of the Activity verb constant values * @param string $verb One of the Activity verb constant values
* @param int $uid User ID * @param int $uid User ID
* @return boolean * @return boolean
@ -336,7 +336,7 @@ class UserNotification
*/ */
public static function insertNotification(int $actor, string $verb, int $uid): bool public static function insertNotification(int $actor, string $verb, int $uid): bool
{ {
$notification = (new Notifications\Factory\Notification(DI::logger()))->createForRelationship( $notification = (new Notifications\Factory\Notification(DI::baseUrl(), DI::l10n(), DI::localRelationship(), DI::logger()))->createForRelationship(
$uid, $uid,
$actor, $actor,
$verb $verb

View File

@ -34,6 +34,7 @@ use Friendica\BaseEntity;
* @property-read $parentUriId * @property-read $parentUriId
* @property-read $created * @property-read $created
* @property-read $seen * @property-read $seen
* @property-read $dismissed
*/ */
class Notification extends BaseEntity class Notification extends BaseEntity
{ {
@ -72,11 +73,11 @@ class Notification extends BaseEntity
* @param int|null $parentUriId * @param int|null $parentUriId
* @param DateTime|null $created * @param DateTime|null $created
* @param bool $seen * @param bool $seen
* @param int|null $id
* @param bool $dismissed * @param bool $dismissed
* @param int|null $id
* @see \Friendica\Navigation\Notifications\Factory\Notification * @see \Friendica\Navigation\Notifications\Factory\Notification
*/ */
public function __construct(int $uid, string $verb, int $type, int $actorId, int $targetUriId = null, int $parentUriId = null, DateTime $created = null, bool $seen = false, int $id = null, bool $dismissed = false) public function __construct(int $uid, string $verb, int $type, int $actorId, int $targetUriId = null, int $parentUriId = null, DateTime $created = null, bool $seen = false, bool $dismissed = false, int $id = null)
{ {
$this->uid = $uid; $this->uid = $uid;
$this->verb = $verb; $this->verb = $verb;
@ -86,8 +87,9 @@ class Notification extends BaseEntity
$this->parentUriId = $parentUriId ?: $targetUriId; $this->parentUriId = $parentUriId ?: $targetUriId;
$this->created = $created; $this->created = $created;
$this->seen = $seen; $this->seen = $seen;
$this->id = $id;
$this->dismissed = $dismissed; $this->dismissed = $dismissed;
$this->id = $id;
} }
public function setSeen() public function setSeen()

View File

@ -24,16 +24,35 @@ namespace Friendica\Navigation\Notifications\Factory;
use Friendica\App\BaseURL; use Friendica\App\BaseURL;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
use Friendica\Content\Text\Plaintext; use Friendica\Content\Text\Plaintext;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\Verb; use Friendica\Model\Verb;
use Friendica\Navigation\Notifications\Entity; use Friendica\Navigation\Notifications\Entity;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Psr\Log\LoggerInterface;
class Notification extends BaseFactory implements ICanCreateFromTableRow 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 public function createFromTableRow(array $row): Entity\Notification
{ {
return new Entity\Notification( return new Entity\Notification(
@ -45,7 +64,8 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
$row['parent-uri-id'], $row['parent-uri-id'],
new \DateTime($row['created'], new \DateTimeZone('UTC')), new \DateTime($row['created'], new \DateTimeZone('UTC')),
$row['seen'], $row['seen'],
$row['id'] $row['dismissed'],
$row['id'],
); );
} }
@ -61,6 +81,12 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
); );
} }
/**
* @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 public function createForRelationship(int $uid, int $contactId, string $verb): Entity\Notification
{ {
return new Entity\Notification( return new Entity\Notification(
@ -73,12 +99,11 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
/** /**
* @param Entity\Notification $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 * @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 = []; $message = [];
@ -89,37 +114,41 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
} }
if ($Notification->type === Post\UserNotification::TYPE_NONE) { if ($Notification->type === Post\UserNotification::TYPE_NONE) {
if ($causer['pending']) { $localRelationship = $this->localRelationshipRepo->getForUserContact($Notification->uid, $Notification->actorId);
$msg = $userL10n->t('%1$s wants to follow you'); if ($localRelationship->pending) {
$msg = $this->l10n->t('%1$s wants to follow you');
} else { } else {
$msg = $userL10n->t('%1$s had started following you'); $msg = $this->l10n->t('%1$s had started following you');
} }
$title = $causer['name']; $title = $causer['name'];
$link = $baseUrl . '/contact/' . $causer['id']; $link = $this->baseUrl . '/contact/' . $causer['id'];
} else { } else {
if (!$Notification->targetUriId) { if (!$Notification->targetUriId) {
return $message; 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->targetUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
$item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); if (empty($item)) {
if (empty($item)) { $this->logger->info('Post not found', ['uri-id' => $Notification->targetUriId]);
$this->logger->info('Parent post not found', ['uri-id' => $Notification->parentUriId]); return $message;
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) || ($Notification->type === Post\UserNotification::TYPE_SHARED)) { if ($Notification->type == Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION) {
$item = Post::selectFirst([], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); $thrParentId = $item['thr-parent-id'];
if (empty($item)) { $item = Post::selectFirst([], ['uri-id' => $thrParentId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
$this->logger->info('Thread parent post not found', ['uri-id' => $item['thr-parent-id']]); if (empty($item)) {
return $message; $this->logger->info('Thread parent post not found', ['uri-id' => $thrParentId]);
} 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;
} }
} }
@ -131,9 +160,9 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
} }
} }
$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'])) { if (!empty($content['text'])) {
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"'; $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
} else { } else {
@ -146,40 +175,40 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
case Activity::LIKE: case Activity::LIKE:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_COMMENT: 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; break;
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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;
} }
break; break;
case Activity::DISLIKE: case Activity::DISLIKE:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_COMMENT: 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; break;
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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;
} }
break; break;
case Activity::ANNOUNCE: case Activity::ANNOUNCE:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_COMMENT: 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; break;
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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; break;
case Post\UserNotification::TYPE_SHARED: case Post\UserNotification::TYPE_SHARED:
if (($causer['id'] != $author['id']) && ($title != '')) { 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']) { } 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 != '') { } elseif ($title != '') {
$msg = $userL10n->t('%1$s shared the post %2$s'); $msg = $this->l10n->t('%1$s shared the post %2$s');
} else { } else {
$msg = $userL10n->t('%1$s shared a post'); $msg = $this->l10n->t('%1$s shared a post');
} }
break; break;
} }
@ -187,68 +216,68 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
case Activity::ATTEND: case Activity::ATTEND:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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;
} }
break; break;
case Activity::ATTENDNO: case Activity::ATTENDNO:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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;
} }
break; break;
case Activity::ATTENDMAYBE: case Activity::ATTENDMAYBE:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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;
} }
break; break;
case Activity::POST: case Activity::POST:
switch ($Notification->type) { switch ($Notification->type) {
case Post\UserNotification::TYPE_EXPLICIT_TAGGED: 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; break;
case Post\UserNotification::TYPE_IMPLICIT_TAGGED: 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; break;
case Post\UserNotification::TYPE_THREAD_COMMENT: 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; break;
case Post\UserNotification::TYPE_DIRECT_COMMENT: 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; break;
case Post\UserNotification::TYPE_COMMENT_PARTICIPATION: case Post\UserNotification::TYPE_COMMENT_PARTICIPATION:
case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION: case Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION:
if (($causer['id'] == $author['id']) && ($title != '')) { 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']) { } 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 != '') { } 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 { } 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; break;
case Post\UserNotification::TYPE_DIRECT_THREAD_COMMENT: 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; break;
case Post\UserNotification::TYPE_SHARED: case Post\UserNotification::TYPE_SHARED:
if (($causer['id'] != $author['id']) && ($title != '')) { 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']) { } 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 != '') { } elseif ($title != '') {
$msg = $userL10n->t('%1$s shared the post %2$s'); $msg = $this->l10n->t('%1$s shared the post %2$s');
} else { } else {
$msg = $userL10n->t('%1$s shared a post'); $msg = $this->l10n->t('%1$s shared a post');
} }
break; break;
} }
@ -268,6 +297,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
'[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]', '[url=' . $causer['url'] . ']' . $causer['name'] . '[/url]',
'[url=' . $link . ']' . $title . '[/url]', '[url=' . $link . ']' . $title . '[/url]',
'[url=' . $author['url'] . ']' . $author['name'] . '[/url]'); '[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
$message['link'] = $link;
} }
return $message; return $message;

View File

@ -41,9 +41,9 @@ class Notification extends BaseRepository
protected static $table_name = 'notification'; protected static $table_name = 'notification';
public function __construct(Database $database, LoggerInterface $logger, Factory\Notification $factory = null) public function __construct(Database $database, LoggerInterface $logger, Factory\Notification $factory)
{ {
parent::__construct($database, $logger, $factory ?? new Factory\Notification($logger)); parent::__construct($database, $logger, $factory);
} }
/** /**

View File

@ -732,7 +732,7 @@ class Notify extends BaseRepository
$subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $item['parent'], $contact['name']); $subject = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $item['parent'], $contact['name']);
} }
$msg = $this->notification->getMessageFromNotification($Notification, $this->baseUrl, $l10n); $msg = $this->notification->getMessageFromNotification($Notification);
if (empty($msg)) { if (empty($msg)) {
$this->logger->info('No notification message, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]); $this->logger->info('No notification message, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
return false; return false;

View File

@ -82,7 +82,7 @@ class PushSubscription
} }
} }
$message = DI::notificationFactory()->getMessageFromNotification($Notification, DI::baseUrl(), $l10n); $message = DI::notificationFactory()->getMessageFromNotification($Notification);
$title = $message['plain'] ?: ''; $title = $message['plain'] ?: '';
$push = Subscription::create([ $push = Subscription::create([