You are here

private function Channel::fetchChannel in Rocket.Chat 8.2

1 call to Channel::fetchChannel()
Channel::getChannelProxy in modules/rocket_chat_api/src/RocketChat/Element/Channel.php
Retrieve the Proxy, create the Channel / Group if needed.

File

modules/rocket_chat_api/src/RocketChat/Element/Channel.php, line 428

Class

Channel

Namespace

Drupal\rocket_chat_api\RocketChat\Element

Code

private function fetchChannel(array &$Channels, ApiClient &$apiClient, bool $writeChannel, string $methodBase) {
  if (!$this
    ->isInList($Channels)) {
    if (!empty($this->owner)) {
      $this->owner
        ->getUserProxy($apiClient);
      $members[] = $this->owner
        ->getUsername();
    }
    $options = [];
    $options['json'] = [];
    $options['json']['name'] = $this
      ->getSafeChannelName();
    $options['json']['readOnly'] = !$writeChannel;
    $myProxy = $apiClient
      ->whoAmI();
    $foundMemberInList = FALSE;
    if (!empty($this->owner)) {
      foreach ($members as $member) {
        if (strcmp($member, $this->owner
          ->getUsername()) === 0) {
          $foundMemberInList = TRUE;
          break;
        }
      }
    }
    else {
      foreach ($members as $member) {
        if (strcmp($member, $myProxy['body']['username']) === 0) {
          $foundMemberInList = TRUE;
          break;
        }
      }
    }
    if (!$foundMemberInList) {
      if (!empty($this->owner)) {
        $members[] = $this->owner
          ->getUsername();
      }
      else {
        $members[] = $myProxy['body']['username'];
      }
    }
    $options['json']['members'] = $members;

    //Member names to add...
    $ret = $apiClient
      ->postToRocketChat("{$methodBase}.create", $options);

    //todo implement error check
    if ($ret['body']['status'] === "failed") {
      $this->Logger
        ->error($ret['status']);
      return NULL;

      //FAILED!
    }
    $myId = $myProxy['body']['_id'];
    if (!empty($this->owner)) {
      if (strcmp($myId, $this->owner
        ->getUser()['_id']) !== 0) {
        $ownerJson = [];
        $ownerJson["json"] = [];
        $ownerJson['json']['roomId'] = $ret['body'][rtrim($methodBase, "s")]['_id'];
        $ownerJson['json']["userId"] = $this->owner
          ->getUser()['_id'];
        $ownerJson['json']["userId"] = $this->owner
          ->getUser()['_id'];

        //Group Owner
        $own = $apiClient
          ->postToRocketChat("{$methodBase}.addOwner", $ownerJson);
        $ownerJson['json']["userId"] = $myId;

        //Current User
        $remOwn = $apiClient
          ->postToRocketChat("{$methodBase}.removeOwner", $ownerJson);

        //todo implement better error check
      }
    }
    else {
      $logger = drupal::logger("Rocket Chat API");
      $logger
        ->warning("Can not set a channle owner that we do not know.");

      //Can not set Owner if we do not know owner.
    }
  }
  else {
    $ret = [];
    $ret['body'] = [];
    $ret['body'][rtrim($methodBase, "s")] = $this
      ->getFromList($Channels);
    $ret2 = $apiClient
      ->getFromRocketChat("{$methodBase}.info", [
      "query" => [
        "roomName" => $this
          ->getSafeChannelName(),
      ],
    ]);
    $ret3 = $ret;
  }
  return $ret;
}