You are here

public function Channel::removeMember in Rocket.Chat 8.2

File

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

Class

Channel

Namespace

Drupal\rocket_chat_api\RocketChat\Element

Code

public function removeMember(ApiClient $apiClient, User $user) {
  $members = [];
  if (empty($this->ChannelMembers)) {
    $this
      ->getAllChannelMembersBatched($apiClient, $members);
  }
  $found = FALSE;
  foreach ($this->ChannelMembers as $member) {
    if (strcmp($user
      ->getName(), $member['name']) === 0) {
      if (strcmp($user
        ->getUsername(), $member['username']) === 0) {
        $found = TRUE;
        break;
      }
    }
  }
  if ($found) {
    $user
      ->getUserProxy($apiClient);
    $method = $methodBase = $this
      ->getChannelTypeName();
    $membersJson = [];
    $membersJson["json"] = [];
    $membersJson["json"]['roomId'] = $this->Channel['_id'];
    $membersJson["json"]['userId'] = $user
      ->getUser()['_id'];
    $ret = $apiClient
      ->postToRocketChat($method . ".kick", $membersJson);
    $this
      ->getAllChannelMembersBatched($apiClient, $members);
    return $ret['body']['status'];
  }
  return false;
}