From 3be8ff7cc02cc218e2fadb7e27b3d61a7c45cf67 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 10:29:01 +0100 Subject: [PATCH] Implement new users service to get detailed infos about the users on a team --- CHANGELOG | 1 + Resources/config/services.yml | 4 ++ Resources/doc/actions-list.md | 12 +++++ Resources/doc/services.md | 22 +++++++++ Slack/Client.php | 2 +- Slack/Client/Actions.php | 4 +- Slack/Client/Actions/UsersList.php | 71 ++++++++++++++++++++++++++++ Slack/Users.php | 74 ++++++++++++++++++++++++++++++ 8 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 Slack/Client/Actions/UsersList.php create mode 100644 Slack/Users.php diff --git a/CHANGELOG b/CHANGELOG index c45eabd..1603558 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 7111d19..85a0e42 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -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] diff --git a/Resources/doc/actions-list.md b/Resources/doc/actions-list.md index 3848ec3..2e7eea1 100644 --- a/Resources/doc/actions-list.md +++ b/Resources/doc/actions-list.md @@ -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 +]; +``` diff --git a/Resources/doc/services.md b/Resources/doc/services.md index 16bd40a..5999f8d 100644 --- a/Resources/doc/services.md +++ b/Resources/doc/services.md @@ -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); + +``` diff --git a/Slack/Client.php b/Slack/Client.php index 1bd6753..866f40e 100644 --- a/Slack/Client.php +++ b/Slack/Client.php @@ -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; diff --git a/Slack/Client/Actions.php b/Slack/Client/Actions.php index 9e88c03..1100ed5 100644 --- a/Slack/Client/Actions.php +++ b/Slack/Client/Actions.php @@ -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 @@ -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' ]; /** diff --git a/Slack/Client/Actions/UsersList.php b/Slack/Client/Actions/UsersList.php new file mode 100644 index 0000000..4de7771 --- /dev/null +++ b/Slack/Client/Actions/UsersList.php @@ -0,0 +1,71 @@ + 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; + } +} diff --git a/Slack/Users.php b/Slack/Users.php new file mode 100644 index 0000000..c117571 --- /dev/null +++ b/Slack/Users.php @@ -0,0 +1,74 @@ +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; + } +}