You are here

class Users in Rocket.Chat 8.2

Hierarchy

Expanded class hierarchy of Users

4 files declare their use of Users
moduleHelper.php in modules/rocket_chat_group/src/RocketChat/moduleHelper.php
RocketChatGroupHelper.php in src/Form/RocketChatGroupHelper.php
RocketChatSettingsForm.php in src/Form/RocketChatSettingsForm.php
Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.
User.php in modules/rocket_chat_api/src/RocketChat/Element/User.php

File

modules/rocket_chat_api/src/RocketChat/Collection/Users.php, line 11

Namespace

Drupal\rocket_chat_api\RocketChat\Collection
View source
class Users implements CollectionInterface {

  /**
   * @var \Drupal\rocket_chat_api\RocketChat\RocketChatStateinterface
   */
  private $state;

  /**
   * @var \Drupal\rocket_chat_api\RocketChat\ApiClient
   */
  private $apiClient;

  /**
   * Cache Names (or stub name).
   */
  public const LIST = "rocket.chat.user.list";
  public const UPDATE = "rocket.chat.user.lastUpdate";
  public const USER = "rocket.chat.user.";

  /**
   * @var \Psr\Log\LoggerInterface
   */
  protected $Logger;
  public function __construct(StateInterface $state, ApiClient $apiClient) {
    $this->state = $state;
    $this->apiClient = $apiClient;
    $this->Logger = Drupal::logger("Rocket Chat API: Users");

    //Todo Decouple this
  }

  /**
   * @param bool $forceReload
   * @return array
   */
  public function getCache($forceReload = FALSE) {
    $this
      ->refreshCache(FALSE);
    $idList = $this->state
      ->get(self::LIST, []);
    foreach ($idList as $index => $id) {
      $idList[$index] = self::USER . $id;
    }
    $users = $this->state
      ->getMultiple($idList);
    if (empty($users)) {
      $users = [];
    }
    return $users;
  }
  public function refreshCache($forceReload = FALSE) {
    $lastUpdate = $this->state
      ->get(self::UPDATE, 0);
    $now = time();
    if ($now - $lastUpdate >= 3600 * 24 * 7) {
      $this->Logger
        ->info("Refreshing Users Cache due to stale Cache  (timeout)");
      $forceReload = TRUE;
    }
    if ($forceReload) {
      $users = [];
      $found = self::getAllUsersBatched($this->apiClient, $users);
      $userIds = [];
      foreach ($users as $user) {
        $userIds[] = $user['_id'];
        $this->state
          ->set(self::USER . $user['_id'], $user);
      }
      $this->state
        ->set(self::LIST, $userIds);
      $this->state
        ->set(self::UPDATE, $now);
    }
  }

  /**
   * Retrieve Chat User List. (in batch size)
   * @param \Drupal\rocket_chat_api\RocketChat\ApiClient $api
   * @param array $users
   * @param int $offset
   * @param int $size
   * @return float|int
   * @return float|int
   * @todo needs better Error checking / missing detection.
   */
  public static function getAllUsersBatched(ApiClient &$api, array &$users, $offset = 0, $size = 500) {
    $ret = $api
      ->getFromRocketChat("users.list", [
      "query" => [
        "offset" => $offset,
        "count" => $size,
      ],
    ]);
    foreach ($ret['body']['users'] as $index => $user) {
      $users[] = $user;
    }
    $total = $ret['body']['total'];
    $count = $ret['body']['count'];
    $retOffset = $ret['body']['offset'];
    $subTotal = $count * (1 + $retOffset);
    $usersLeft = $total - $subTotal;
    if ($usersLeft > 0) {
      self::getAllUsersBatched($api, $users, ++$offset, $size);
    }
    return $subTotal;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Users::$apiClient private property
Users::$Logger protected property
Users::$state private property
Users::getAllUsersBatched public static function Retrieve Chat User List. (in batch size)
Users::getCache public function Overrides CollectionInterface::getCache
Users::LIST public constant Cache Names (or stub name).
Users::refreshCache public function Overrides CollectionInterface::refreshCache
Users::UPDATE public constant
Users::USER public constant
Users::__construct public function