StatusProvider.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\ContactsMenu;
  8. use OCA\UserStatus\Db\UserStatus;
  9. use OCA\UserStatus\Service\StatusService;
  10. use OCP\Contacts\ContactsMenu\IBulkProvider;
  11. use OCP\Contacts\ContactsMenu\IEntry;
  12. use function array_combine;
  13. use function array_filter;
  14. use function array_map;
  15. class StatusProvider implements IBulkProvider {
  16. public function __construct(private StatusService $statusService) {
  17. }
  18. public function process(array $entries): void {
  19. $uids = array_filter(
  20. array_map(fn (IEntry $entry): ?string => $entry->getProperty('UID'), $entries)
  21. );
  22. $statuses = $this->statusService->findByUserIds($uids);
  23. /** @var array<string, UserStatus> $indexed */
  24. $indexed = array_combine(
  25. array_map(fn (UserStatus $status) => $status->getUserId(), $statuses),
  26. $statuses
  27. );
  28. foreach ($entries as $entry) {
  29. $uid = $entry->getProperty('UID');
  30. if ($uid !== null && isset($indexed[$uid])) {
  31. $status = $indexed[$uid];
  32. $entry->setStatus(
  33. $status->getStatus(),
  34. $status->getCustomMessage(),
  35. $status->getStatusMessageTimestamp(),
  36. $status->getCustomIcon(),
  37. );
  38. }
  39. }
  40. }
  41. }