From 97d1295ff28066eb4faeb36995afa9d46c66579e Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 26 Mar 2025 21:03:53 +0100 Subject: [PATCH] feat: Implement getSeenUsers to iterate over users This method uses an iterator. This is lighter on resources and gives more control to the caller Signed-off-by: Louis Chemineau --- lib/private/User/Manager.php | 26 ++++++++++++++++++++++++++ lib/public/IUserManager.php | 11 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 2ca0359e4d4..c51a074bb22 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -819,4 +819,30 @@ class Manager extends PublicEmitter implements IUserManager { public function getDisplayNameCache(): DisplayNameCache { return $this->displayNameCache; } + + /** + * Gets the list of users sorted by lastLogin, from most recent to least recent + * + * @param int $offset from which offset to fetch + * @return \Iterator list of user IDs + * @since 30.0.0 + */ + public function getSeenUsers(int $offset = 0): \Iterator { + $limit = 1000; + + do { + $userIds = $this->getSeenUserIds($limit, $offset); + $offset += $limit; + + foreach ($userIds as $userId) { + foreach ($this->backends as $backend) { + if ($backend->userExists($userId)) { + $user = $this->getUserObject($userId, $backend, false); + yield $user; + break; + } + } + } + } while (count($userIds) === $limit); + } } diff --git a/lib/public/IUserManager.php b/lib/public/IUserManager.php index 091ccd89048..75549a953e3 100644 --- a/lib/public/IUserManager.php +++ b/lib/public/IUserManager.php @@ -221,4 +221,15 @@ interface IUserManager { * @since 30.0.0 */ public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array; + + /** + * Gets the list of users. + * An iterator is returned allowing the caller to stop the iteration at any time. + * The offset argument allows the caller to continue the iteration at a specific offset. + * + * @param int $offset from which offset to fetch + * @return \Iterator list of IUser object + * @since 32.0.0 + */ + public function getSeenUsers(int $offset = 0): \Iterator; }