FediverseAction.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Profile\Actions;
  8. use OCP\Accounts\IAccountManager;
  9. use OCP\IURLGenerator;
  10. use OCP\IUser;
  11. use OCP\L10N\IFactory;
  12. use OCP\Profile\ILinkAction;
  13. use function substr;
  14. class FediverseAction implements ILinkAction {
  15. private string $value = '';
  16. public function __construct(
  17. private IAccountManager $accountManager,
  18. private IFactory $l10nFactory,
  19. private IURLGenerator $urlGenerator,
  20. ) {
  21. }
  22. public function preload(IUser $targetUser): void {
  23. $account = $this->accountManager->getAccount($targetUser);
  24. $this->value = $account->getProperty(IAccountManager::PROPERTY_FEDIVERSE)->getValue();
  25. }
  26. public function getAppId(): string {
  27. return 'core';
  28. }
  29. public function getId(): string {
  30. return IAccountManager::PROPERTY_FEDIVERSE;
  31. }
  32. public function getDisplayId(): string {
  33. return $this->l10nFactory->get('lib')->t('Fediverse');
  34. }
  35. public function getTitle(): string {
  36. $displayUsername = $this->value[0] === '@' ? $this->value : '@' . $this->value;
  37. return $this->l10nFactory->get('lib')->t('View %s on the fediverse', [$displayUsername]);
  38. }
  39. public function getPriority(): int {
  40. return 50;
  41. }
  42. public function getIcon(): string {
  43. return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/mastodon.svg'));
  44. }
  45. public function getTarget(): ?string {
  46. if (empty($this->value)) {
  47. return null;
  48. }
  49. $username = $this->value[0] === '@' ? substr($this->value, 1) : $this->value;
  50. [$username, $instance] = explode('@', $username);
  51. return 'https://' . $instance . '/@' . $username;
  52. }
  53. }