Flatten arbitrary contact structure in constructor of FormattedNavNotification

This commit is contained in:
Hypolite Petovan 2022-11-10 10:40:37 -05:00
parent 87bb4d44a2
commit 71ab9e13f2
3 changed files with 40 additions and 27 deletions

View file

@ -233,23 +233,19 @@ class Ping extends BaseModule
} }
if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) { if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
foreach ($registrations as $reg) { foreach ($registrations as $registration) {
$navNotifications[] = $this->formattedNavNotification->createFromParams( $navNotifications[] = $this->formattedNavNotification->createFromParams(
[ $registration['name'],
'name' => $reg['name'], $registration['url'],
'url' => $reg['url'],
],
$this->l10n->t('{0} requested registration'), $this->l10n->t('{0} requested registration'),
new \DateTime($reg['created'], new \DateTimeZone('UTC')), new \DateTime($registration['created'], new \DateTimeZone('UTC')),
new Uri($this->baseUrl->get(true) . '/moderation/users/pending') new Uri($this->baseUrl->get(true) . '/moderation/users/pending')
); );
} }
} elseif (count($registrations) > 1) { } elseif (count($registrations) > 1) {
$navNotifications[] = $this->formattedNavNotification->createFromParams( $navNotifications[] = $this->formattedNavNotification->createFromParams(
[ $registrations[0]['name'],
'name' => $registrations[0]['name'], $registrations[0]['url'],
'url' => $registrations[0]['url'],
],
$this->l10n->t('{0} and %d others requested registration', count($registrations) - 1), $this->l10n->t('{0} and %d others requested registration', count($registrations) - 1),
new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')), new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')),
new Uri($this->baseUrl->get(true) . '/moderation/users/pending') new Uri($this->baseUrl->get(true) . '/moderation/users/pending')

View file

@ -28,6 +28,7 @@ use Friendica\Model\Contact;
use Friendica\Navigation\Notifications\Entity; use Friendica\Navigation\Notifications\Entity;
use Friendica\Navigation\Notifications\Exception\NoMessageException; use Friendica\Navigation\Notifications\Exception\NoMessageException;
use Friendica\Navigation\Notifications\ValueObject; use Friendica\Navigation\Notifications\ValueObject;
use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
use Friendica\Util\Temporal; use Friendica\Util\Temporal;
@ -65,32 +66,39 @@ class FormattedNavNotification extends BaseFactory
} }
/** /**
* @param array $contact A contact array with the following keys: name, url * @param string $contact_name
* @param string $contact_url
* @param string $message A notification message with the {0} placeholder for the contact name * @param string $message A notification message with the {0} placeholder for the contact name
* @param \DateTime $date * @param \DateTime $date
* @param Uri $href * @param Uri $href
* @param bool $seen * @param bool $seen
* @return ValueObject\FormattedNavNotification * @return ValueObject\FormattedNavNotification
* @throws \Friendica\Network\HTTPException\ServiceUnavailableException * @throws ServiceUnavailableException
*/ */
public function createFromParams(array $contact, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification public function createFromParams(string $contact_name, string $contact_url, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
{ {
$contact['photo'] = Contact::getAvatarUrlForUrl($contact['url'], $this->userSession->getLocalUserId(), Proxy::SIZE_MICRO); $contact_photo = Contact::getAvatarUrlForUrl($contact_url, $this->userSession->getLocalUserId(), Proxy::SIZE_MICRO);
$dateMySQL = $date->format(DateTimeFormat::MYSQL); $dateMySQL = $date->format(DateTimeFormat::MYSQL);
$templateNotify = [ $templateNotify = [
'contact' => $contact, 'contact' => [
'name' => $contact_name,
'url' => $contact_url,
'photo' => $contact_photo,
],
'href' => $href->__toString(), 'href' => $href->__toString(),
'message' => $message, 'message' => $message,
'seen' => $seen, 'seen' => $seen,
'localdate' => DateTimeFormat::local($dateMySQL), 'localdate' => DateTimeFormat::local($dateMySQL),
'ago' => Temporal::getRelativeDate($dateMySQL), 'ago' => Temporal::getRelativeDate($dateMySQL),
'richtext' => Entity\Notify::formatMessage($contact['name'], $message), 'richtext' => Entity\Notify::formatMessage($contact_name, $message),
]; ];
return new ValueObject\FormattedNavNotification( return new ValueObject\FormattedNavNotification(
$contact, $contact_name,
$contact_url,
$contact_photo,
$date->getTimestamp(), $date->getTimestamp(),
strip_tags($templateNotify['richtext']), strip_tags($templateNotify['richtext']),
Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]), Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
@ -120,7 +128,8 @@ class FormattedNavNotification extends BaseFactory
} }
return $this->createFromParams( return $this->createFromParams(
self::$contacts[$notification->actorId], self::$contacts[$notification->actorId]['name'],
self::$contacts[$notification->actorId]['url'],
$message['notification'], $message['notification'],
$notification->created, $notification->created,
new Uri($this->baseUrl->get() . '/notification/' . $notification->id), new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
@ -141,7 +150,8 @@ class FormattedNavNotification extends BaseFactory
} }
return $this->createFromParams( return $this->createFromParams(
self::$contacts[$intro->cid], self::$contacts[$intro->cid]['name'],
self::$contacts[$intro->cid]['url'],
$msg, $msg,
$intro->datetime, $intro->datetime,
new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id) new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)

View file

@ -42,16 +42,23 @@ class FormattedNavNotification extends BaseEntity
protected $seen; protected $seen;
/** /**
* @param array $contact Contact array with the following keys: name, url, photo * @param string $contact_name Contact display name
* @param string $timestamp Unix timestamp * @param string $contact_url Contact profile URL
* @param string $plaintext Localized notification message with the placeholder replaced by the contact name * @param string $contact_photo Contact picture URL
* @param string $html Full HTML string of the notification menu element * @param string $timestamp Unix timestamp
* @param string $href Absolute URL this notification should send the user to when interacted with * @param string $plaintext Localized notification message with the placeholder replaced by the contact name
* @param bool $seen Whether the user interacted with this notification once * @param string $html Full HTML string of the notification menu element
* @param string $href Absolute URL this notification should send the user to when interacted with
* @param bool $seen Whether the user interacted with this notification once
*/ */
public function __construct(array $contact, string $timestamp, string $plaintext, string $html, string $href, bool $seen) public function __construct(string $contact_name, string $contact_url, string $contact_photo, string $timestamp, string $plaintext, string $html, string $href, bool $seen)
{ {
$this->contact = $contact; // Properties differ from constructor because this structure is used in the "nav-update" Javascript event listener
$this->contact = [
'name' => $contact_name,
'url' => $contact_url,
'photo' => $contact_photo,
];
$this->timestamp = $timestamp; $this->timestamp = $timestamp;
$this->plaintext = $plaintext; $this->plaintext = $plaintext;
$this->html = $html; $this->html = $html;