Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from DZunke/feature/users-service
Browse files Browse the repository at this point in the history
Implement new users service to get detailed infos about the users on …
  • Loading branch information
DZunke committed Oct 28, 2015
2 parents 044058e + 3be8ff7 commit 1975052
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SlackBundle v1.3.0
==================
- Feature: File upload service now accepts an array of channels, string usage is deprecated
- Feature: Implement action to invite users to a channel
- Feature: Implement action and service to get details about users on the team
- Patch: File upload no longer need to lookup channel id due to api changes

SlackBundle v1.2.2
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ services:
dz.slack.channels:
class: DZunke\SlackBundle\Slack\Channels
arguments: [@dz.slack.client]

dz.slack.users:
class: DZunke\SlackBundle\Slack\Users
arguments: [@dz.slack.client]
12 changes: 12 additions & 0 deletions Resources/doc/actions-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ protected $parameter = [
'channels' => null # If no Channel is given the File will be private to the API-User
];
```

## users.list

[Slack Documentation](https://api.slack.com/methods/users.list)

Constant: \DZunke\SlackBundle\Slack\Client::ACTION_USERS_LIST

``` php
protected $parameter = [
'presence' => 1
];
```
22 changes: 22 additions & 0 deletions Resources/doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ $service->info($channelId);
$service->setTopic($channelId, $newTopic);
```

## Users

To get more information about the users in your team there is a service to read user specific things. In lack of an
api method to load single users every method of the service will load the complete list of users in your team. Beware
of using this service without caching informations you often need.

``` php
$service = $container->get('dz.slack.users');

# get the list of all users in your team
$service->getUsers();

# get all users that are not deleted
$service->getActiveUsers();

# get all deleted users
$service->getDeletedUsers();

# get the id of a single user - needed for some actions
$service->getId($username);

```
2 changes: 1 addition & 1 deletion Slack/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Connection $connection)
* @param array $parameter
* @return Response|bool
*/
public function send($action, array $parameter)
public function send($action, array $parameter = [])
{
if (!$this->connection->isValid()) {
return false;
Expand Down
4 changes: 3 additions & 1 deletion Slack/Client/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Actions
const ACTION_CHANNELS_INVITE = 'channels.invite';
const ACTION_CHANNELS_HISTORY = 'channels.history';
const ACTION_FILES_UPLOAD = 'files.upload';
const ACTION_USERS_LIST = 'users.list';

/**
* @var array
Expand All @@ -28,7 +29,8 @@ class Actions
self::ACTION_CHANNELS_INFO => 'ChannelsInfo',
self::ACTION_CHANNELS_INVITE => 'ChannelsInvite',
self::ACTION_CHANNELS_HISTORY => 'ChannelsHistory',
self::ACTION_FILES_UPLOAD => 'FilesUpload'
self::ACTION_FILES_UPLOAD => 'FilesUpload',
self::ACTION_USERS_LIST => 'UsersList'
];

/**
Expand Down
71 changes: 71 additions & 0 deletions Slack/Client/Actions/UsersList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace DZunke\SlackBundle\Slack\Client\Actions;

use DZunke\SlackBundle\Slack\Client\Actions;

/**
* @see https://api.slack.com/methods/users.list
*/
class UsersList implements ActionsInterface
{

/**
* @var array
*/
protected $parameter = [
'presence' => 1
];

/**
* @return array
*/
public function getRenderedRequestParams()
{
return $this->parameter;
}

/**
* @param array $parameter
* @return $this
*/
public function setParameter(array $parameter)
{
foreach ($parameter as $key => $value) {
if (array_key_exists($key, $this->parameter)) {
$this->parameter[$key] = $value;
}
}

return $this;
}

/**
* @return string
*/
public function getAction()
{
return Actions::ACTION_USERS_LIST;
}

/**
* @param array $response
* @return array
*/
public function parseResponse(array $response)
{
$users = [];
foreach ($response['members'] as $user) {
$users[$user['name']] = [
'id' => $user['id'],
'name' => $user['name'],
'deleted' => (bool)$user['deleted'],
'real_name' => $user['real_name'],
'is_bot' => (bool)$user['is_bot'],
'presence' => isset($user['presence']) ? $user['presence'] : null
];
}

return $users;
}
}
74 changes: 74 additions & 0 deletions Slack/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace DZunke\SlackBundle\Slack;

use DZunke\SlackBundle\Slack\Client\Actions;

class Users
{
/**
* @var Client
*/
protected $client;

/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @return Client
*/
public function getClient()
{
return $this->client;
}

/**
* @return array
*/
public function getUsers()
{
return $this->client->send(Actions::ACTION_USERS_LIST)->getData();
}

/**
* @return array
*/
public function getActiveUsers()
{
return array_filter(
$this->getUsers(),
function ($user) {
return $user['deleted'] === false;
}
);
}

/**
* @return array
*/
public function getDeletedUsers()
{
return array_filter(
$this->getUsers(),
function ($user) {
return $user['deleted'] === true;
}
);
}

/**
* @param string $name
* @return null|array
*/
public function getId($name)
{
$users = $this->getUsers();

return array_key_exists($name, $users) ? $users[$name]['id'] : null;
}
}

0 comments on commit 1975052

Please sign in to comment.