Skip to content

Commit

Permalink
Improve readme and add user resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Oct 23, 2016
1 parent 32af66b commit 6df8b61
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 25 deletions.
155 changes: 148 additions & 7 deletions README.md
Expand Up @@ -46,9 +46,11 @@ $client->connect(); // Locks current runtime and starts an EventLoop

## Resources

1) `$client->resource->action(...)`
Example: `$client->resource->action(...)` or `$client->resource->fetchType->action(...)`

Where `resource` are one of `"rooms"`, `"users"`, `"groups"` or `"messages"`; `action` are specify for every resource.
- `resource` are one of `"rooms"`, `"users"`, `"groups"` or `"messages"`
- `fetchType` are one of `"sync"`, `"async"` or `"stream"`
- `action` are specify for every resource

```php
$response = $client->rooms->all(); // "rooms" - resource, "all" - action
Expand All @@ -62,10 +64,6 @@ foreach ($response as $room) {
$client->connect();
```

2) `$client->resource->fetchType->action(...)`

Where `fetchType` are one of `"sync"`, `"async"` or `"stream"`.

### Sync

Sync requests are block event loop "tick"
Expand Down Expand Up @@ -114,6 +112,128 @@ $observer->subscribe(function($response) {
$client->connect();
```

## Available resources

### Groups

List groups the current user is in.

- `$client->groups->all()`

List of rooms nested under the specified group.

- `$client->groups->rooms(string $roomId)`

### Messages

List of messages in a room in historical reversed order. **Only synchronous driver**

- `$client->messages->all(string $roomId[, string $query]): \Generator`

There is also a way to retrieve a single message using its id.

- `$client->messages->find(string $roomId, string $messageId)`

Send a message to a room.

- `$client->messages->create(string $roomId, string $content)`

Update a message.

- `$client->messages->update(string $roomId, string $messageId, string $content)`

Delete a message.

- `$client->messages->delete(string $roomId, string $messageId)`

### Rooms

List rooms the current user is in.

- `$client->rooms->all([string $query])`

Join user into a room.

- `$client->rooms->joinUser(string $roomId, string $userId)`

Join current user into a room.

- `$client->rooms->join(string $roomId)`

Join current user into a room by room name (URI).

- `$client->rooms->joinByName(string $name)`

Find room by room name (URI).

- `$client->rooms->findByName(string $name)`

Kick user from target room.

- `$client->rooms->kick(string $roomId, string $userId)`

This can be self-inflicted to leave the the room and remove room from your left menu.

- `$client->rooms->leave(string $roomId)`

Sets up a new topic of target room.

- `$client->rooms->topic(string $roomId, string $topic)`

Sets the room is indexed by search engines.

- `$client->rooms->searchIndex(string $roomId, bool $enabled)`

Sets the tags that define the room

- `$client->rooms->tags(string $roomId, array $tags)`

If you hate one of the rooms - you can destroy it!

- `$client->rooms->delete(string $roomId)`

List of users currently in the room. **Only synchronous driver**

- `$client->rooms->users(string $roomId[, string $query]: \Generator`

Use the streaming API to listen events. **Only streaming driver**

- `$client->rooms->events(string $roomId): Observer`

Use the streaming API to listen messages. **Only streaming driver**

- `$client->rooms->messages(string $roomId): Observer`

### Users

Returns the current user logged in.

- `$client->users->current(): array`

List of Rooms the user is part of.

- `$client->users->rooms([string $userId])`

You can retrieve unread items and mentions using the following endpoint.

- `$client->users->unreadItems(string $roomId[, string $userId])`

There is an additional endpoint nested under rooms that you can use to mark chat messages as read

- `$client->users->markAsRead(string $roomId, array $messageIds[, string $userId])`

List of the user's GitHub Organisations and their respective Room if available.

- `$client->users->orgs([string $userId])`

List of the user's GitHub Repositories and their respective Room if available.

- `$client->users->repos([string $userId])`

List of Gitter channels nested under the user.

- `$client->users->channels([string $userId])`

## Custom routing

```php
Expand All @@ -129,4 +249,25 @@ $client->request->stream->to($route)->subscribe(function($message) {
});

$client->connect();
```
```

Available route methods:

- `Route::get(string $route)` - GET http method
- `Route::post(string $route)` - POST http method
- `Route::put(string $route)` - PUT http method
- `Route::patch(string $route)` - PATCH http method
- `Route::delete(string $route)` - DELETE http method
- `Route::options(string $route)` - OPTIONS http method
- `Route::head(string $route)` - HEAD http method
- `Route::connect(string $route)` - CONNECT http method
- `Route::trace(string $route)` - TRACE http method

Route arguments:

- `$route->with(string $key, string $value)` - Add route or GET query parameter
- `$route->withMany(array $parameters)` - Add route or GET query parameters
- `$route->withBody(string $key, string $value)` - Add POST, PUT, DELETE, etc body parameter


See more info about API into [Documentation](https://developer.gitter.im/docs/welcome)
9 changes: 9 additions & 0 deletions src/Resources/AbstractResource.php
Expand Up @@ -69,6 +69,15 @@ protected function currentUser(): array
return self::$currentUserId[$this->client->token];
}

/**
* @return string
* @throws \InvalidArgumentException
*/
protected function currentUserId(): string
{
return $this->currentUser()['id'];
}

/**
* @return $this
* @throws \InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Messages.php
Expand Up @@ -41,7 +41,7 @@ class Messages extends AbstractResource
* @return \Generator
* @throws \InvalidArgumentException
*/
public function all(string $roomId, string $query = null)
public function all(string $roomId, string $query = null): \Generator
{
$adapter = $this->using(AdapterInterface::TYPE_SYNC);

Expand Down
37 changes: 21 additions & 16 deletions src/Resources/Users.php
Expand Up @@ -44,53 +44,56 @@ public function rooms(string $userId = null)
{
return $this->fetch(
Route::get('user/{userId}/rooms')
->with('userId', $userId ?? $this->current()['id'])
->with('userId', $userId ?? $this->currentUserId())
);
}

/**
* You can retrieve unread items and mentions using the following endpoint.
*
* @param string $userId
* @param string $roomId
* @param string|null $userId
* @return mixed
* @throws \InvalidArgumentException
*/
public function unreadItems(string $userId, string $roomId)
public function unreadItems(string $roomId, string $userId = null)
{
return $this->fetch(
Route::get('user/{userId}/rooms/{roomId}/unreadItems')
->withMany(['userId' => $userId, 'roomId' => $roomId])
->withMany(['userId' => $userId ?? $this->currentUserId(), 'roomId' => $roomId])
);
}

/**
* There is an additional endpoint nested under rooms that you can use to mark chat messages as read
*
* @param string $userId
* @param string $roomId
* @param array $messageIds
* @param string|null $userId
* @return mixed
* @throws \InvalidArgumentException
*/
public function markAsRead(string $userId, string $roomId, array $messageIds)
public function markAsRead(string $roomId, array $messageIds, string $userId = null)
{
return $this->fetch(
Route::post('user/{userId}/rooms/{roomId}/unreadItems')
->withMany(['userId' => $userId, 'roomId' => $roomId])
->withMany(['userId' => $userId ?? $this->currentUserId(), 'roomId' => $roomId])
->withBody('chat', $messageIds)
);
}

/**
* List of the user's GitHub Organisations and their respective Room if available.
*
* @param string $userId
* @param string|null $userId
* @return mixed
* @throws \InvalidArgumentException
*/
public function orgs(string $userId)
public function orgs(string $userId = null)
{
return $this->fetch(
Route::get('user/{userId}/orgs')
->with('userId', $userId)
->with('userId', $userId ?? $this->currentUserId())
);
}

Expand All @@ -99,28 +102,30 @@ public function orgs(string $userId)
*
* Note: It'll return private repositories if the current user has granted Gitter privileges to access them.
*
* @param string $userId
* @param string|null $userId
* @return mixed
* @throws \InvalidArgumentException
*/
public function repos(string $userId)
public function repos(string $userId = null)
{
return $this->fetch(
Route::get('user/{userId}/repos')
->with('userId', $userId)
->with('userId', $userId ?? $this->currentUserId())
);
}

/**
* List of Gitter channels nested under the current user.
*
* @param string $userId
* @param string|null $userId
* @return mixed
* @throws \InvalidArgumentException
*/
public function channels(string $userId)
public function channels(string $userId = null)
{
return $this->fetch(
Route::get('user/{userId}/channels')
->with('userId', $userId)
->with('userId', $userId ?? $this->currentUserId())
);
}
}
51 changes: 50 additions & 1 deletion tests/ResourceUsersTest.php
Expand Up @@ -7,6 +7,7 @@
*/
namespace Gitter\Tests;

use Clue\React\Buzz\Message\ResponseException;
use Gitter\ClientAdapter\AdapterInterface;

/**
Expand All @@ -15,5 +16,53 @@
*/
class ResourceUsersTest extends TestCase
{

public function testCurrentUser()
{
$this->assertInternalType('array', $this->client()->users->current());
}

public function testCurrentUserRooms()
{
try {
$this->assertInternalType('array', $this->client()->users->rooms());
} catch (ResponseException $e) {
$this->assertContains('404', $e->getMessage());
}
}

public function testCurrentUserUnreadItems()
{
try {
$this->assertInternalType('array', $this->client()->users->unreadItems($this->debugRoomId()));
} catch (ResponseException $e) {
$this->assertContains('404', $e->getMessage());
}
}

public function testCurrentUserOrgs()
{
try {
$this->assertInternalType('array', $this->client()->users->orgs());
} catch (ResponseException $e) {
$this->assertContains('404', $e->getMessage());
}
}

public function testCurrentUserRepos()
{
try {
$this->assertInternalType('array', $this->client()->users->repos());
} catch (ResponseException $e) {
$this->assertContains('404', $e->getMessage());
}
}

public function testCurrentUserChannels()
{
try {
$this->assertInternalType('array', $this->client()->users->channels());
} catch (ResponseException $e) {
$this->assertContains('404', $e->getMessage());
}
}
}

0 comments on commit 6df8b61

Please sign in to comment.