TwitterAction.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 TwitterAction 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_TWITTER)->getValue();
  25. }
  26. public function getAppId(): string {
  27. return 'core';
  28. }
  29. public function getId(): string {
  30. return IAccountManager::PROPERTY_TWITTER;
  31. }
  32. public function getDisplayId(): string {
  33. return $this->l10nFactory->get('lib')->t('Twitter');
  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 Twitter', [$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/twitter.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. return 'https://twitter.com/' . $username;
  51. }
  52. }