1
0

StatusProvider.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(
  17. private StatusService $statusService,
  18. ) {
  19. }
  20. public function process(array $entries): void {
  21. $uids = array_filter(
  22. array_map(fn (IEntry $entry): ?string => $entry->getProperty('UID'), $entries)
  23. );
  24. $statuses = $this->statusService->findByUserIds($uids);
  25. /** @var array<string, UserStatus> $indexed */
  26. $indexed = array_combine(
  27. array_map(fn (UserStatus $status) => $status->getUserId(), $statuses),
  28. $statuses
  29. );
  30. foreach ($entries as $entry) {
  31. $uid = $entry->getProperty('UID');
  32. if ($uid !== null && isset($indexed[$uid])) {
  33. $status = $indexed[$uid];
  34. $entry->setStatus(
  35. $status->getStatus(),
  36. $status->getCustomMessage(),
  37. $status->getStatusMessageTimestamp(),
  38. $status->getCustomIcon(),
  39. );
  40. }
  41. }
  42. }
  43. }