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

Commit

Permalink
Merge pull request #4 from shapintv/new-entrypoints
Browse files Browse the repository at this point in the history
New entrypoints
  • Loading branch information
Olivier Dolbeau authored Jan 16, 2020
2 parents 9adf628 + 31f96e3 commit c69fc61
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 12 deletions.
55 changes: 55 additions & 0 deletions src/Api/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,59 @@ public function leave(string $conversationId, string $userId)

return $this->hydrator->hydrate($response, Model\Conversation\ConversationLeft::class);
}

/**
* @throws Exception
*/
public function findMessages(string $conversationId, array $filters = []): Model\Conversation\MessageCollection
{
$response = $this->httpGet("conversations/$conversationId/messages", $filters);

if (200 !== $response->getStatusCode()) {
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, Model\Conversation\MessageCollection::class);
}

/**
* @throws Exception
*/
public function postSystemMessage(string $conversationId, string $text, array $custom = []): Model\Conversation\MessageCreated
{
$response = $this->httpPost("conversations/$conversationId/messages", [
[
'type' => 'SystemMessage',
'text' => $text,
'custom' => (object) $custom,
],
]);

if (200 !== $response->getStatusCode()) {
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, Model\Conversation\MessageCreated::class);
}

/**
* @throws Exception
*/
public function postUserMessage(string $conversationId, string $sender, string $text, array $custom = []): Model\Conversation\MessageCreated
{
$response = $this->httpPost("conversations/$conversationId/messages", [
[
'type' => 'UserMessage',
'sender' => $sender,
'text' => $text,
'custom' => (object) $custom,
],
]);

if (200 !== $response->getStatusCode()) {
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, Model\Conversation\MessageCreated::class);
}
}
12 changes: 1 addition & 11 deletions src/Exception/Domain/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ public function __construct(ResponseInterface $response)
$this->response = $response;
$content = json_decode($response->getContent(false), true);

if (!isset($content['reasons'])) {
parent::__construct('Bad Request: No reason.');

return;
}

$field = array_key_first($content['reasons']);
$reasons = reset($content['reasons']);
$reason = \is_array($reasons) ? $reasons[0] : $reasons;

parent::__construct("Bad request: Field $field: $reason.");
parent::__construct('Bad request. Content: '.json_encode($content));
}

public function getResponse(): ResponseInterface
Expand Down
156 changes: 156 additions & 0 deletions src/Model/Conversation/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);

namespace Shapin\TalkJS\Model\Conversation;

use Shapin\TalkJS\Model\CreatableFromArray;

class Message implements CreatableFromArray
{
const TYPE_SYSTEM_MESSAGE = 'SystemMessage';
const TYPE_USER_MESSAGE = 'UserMessage';

/**
* @var string
*/
private $type;

/**
* @var string
*/
private $text;

/**
* @var ?string
*/
private $senderId;

/**
* @var array
*/
private $readBy;

/**
* @var string
*/
private $origin;

/**
* @var string
*/
private $location;

/**
* @var string
*/
private $id;

/**
* @var array
*/
private $custom;

/**
* @var string
*/
private $conversationId;

/**
* @var \DateTimeImmutable
*/
private $createdAt;

/**
* @var string
*/
private $attachment;

/**
* {@inheritdoc}
*/
public static function createFromArray(array $data): self
{
$timestamp = round($data['createdAt'] / 1000, 0);

$message = new self();
$message->type = $data['type'];
$message->text = $data['text'];
$message->senderId = $data['senderId'];
$message->readBy = $data['readBy'];
$message->origin = $data['origin'];
$message->location = $data['location'];
$message->id = $data['id'];
$message->custom = $data['custom'];
$message->createdAt = new \DateTimeImmutable("@$timestamp");
$message->conversationId = $data['conversationId'];
$message->attachment = $data['attachment'];

return $message;
}

public function getType(): string
{
return $this->type;
}

public function isSystemMessage(): bool
{
return self::TYPE_SYSTEM_MESSAGE === $this->type;
}

public function isUserMessage(): bool
{
return self::TYPE_USER_MESSAGE === $this->type;
}

public function getText(): string
{
return $this->text;
}

public function getSenderId(): ?string
{
return $this->senderId;
}

public function getReadBy(): array
{
return $this->readBy;
}

public function getOrigin(): string
{
return $this->origin;
}

public function getLocation(): ?string
{
return $this->location;
}

public function getId(): string
{
return $this->id;
}

public function getCustom(): array
{
return $this->custom;
}

public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}

public function getConversationId(): string
{
return $this->conversationId;
}

public function getAttachment(): ?string
{
return $this->attachment;
}
}
24 changes: 24 additions & 0 deletions src/Model/Conversation/MessageCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Shapin\TalkJS\Model\Conversation;

use Shapin\TalkJS\Model\Collection;

class MessageCollection extends Collection
{
/**
* {@inheritdoc}
*/
public static function createFromArray(array $data)
{
$messages = [];

foreach ($data['data'] as $message) {
$messages[$message['id']] = Message::createFromArray($message);
}

return new self($messages);
}
}
9 changes: 9 additions & 0 deletions src/Model/Conversation/MessageCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Shapin\TalkJS\Model\Conversation;

class MessageCreated
{
}
50 changes: 49 additions & 1 deletion tests/FunctionalTests/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Shapin\TalkJS\Model\Conversation\ConversationCreatedOrUpdated;
use Shapin\TalkJS\Model\Conversation\ConversationJoined;
use Shapin\TalkJS\Model\Conversation\ConversationLeft;
use Shapin\TalkJS\Model\Conversation\Message;
use Shapin\TalkJS\Model\Conversation\MessageCollection;
use Shapin\TalkJS\Model\Conversation\MessageCreated;
use Shapin\TalkJS\Model\Conversation\ParticipationUpdated;

final class ConversationTest extends TestCase
Expand All @@ -25,7 +28,7 @@ public function setUp(): void
$this->api = $this->getTalkJSClient()->conversations();
}

public function testCreateOrUpdate()
public function testAll()
{
$randomTestString = bin2hex(random_bytes(10));
$conversationId = "conversation_$randomTestString";
Expand Down Expand Up @@ -96,5 +99,50 @@ public function testCreateOrUpdate()
$conversation = $this->api->get($conversationId);
$this->assertInstanceOf(Conversation::class, $conversation);
$this->assertSame(['my_user' => ['notify' => false, 'access' => 'Read']], $conversation->getParticipants());

// Find messages: none should be found.
$messages = $this->api->findMessages($conversationId);
$this->assertInstanceOf(MessageCollection::class, $messages);
$this->assertCount(0, $messages);

// Post a new system message.
$messageCreated = $this->api->postSystemMessage($conversationId, 'An amazing system message', ['foo' => 'bar']);
$this->assertInstanceOf(MessageCreated::class, $messageCreated);

// We now have 1 available system message
$messages = $this->api->findMessages($conversationId);
$this->assertInstanceOf(MessageCollection::class, $messages);
$this->assertCount(1, $messages);
$message = $messages->getIterator()->current();
$this->assertInstanceOf(Message::class, $message);
$this->assertTrue($message->isSystemMessage());
$this->assertSame('An amazing system message', $message->getText());
$this->assertNull($message->getSenderId());
$this->assertCount(0, $message->getReadBy());
$this->assertSame('rest', $message->getOrigin());
$this->assertNull($message->getLocation());
$this->assertSame(['foo' => 'bar'], $message->getCustom());
$this->assertSame($conversationId, $message->getConversationId());
$this->assertNull($message->getAttachment());

// Post a new user message.
$messageCreated = $this->api->postUserMessage($conversationId, 'my_user', 'An amazing user message');
$this->assertInstanceOf(MessageCreated::class, $messageCreated);

// We now have 2 available messages
$messages = $this->api->findMessages($conversationId);
$this->assertInstanceOf(MessageCollection::class, $messages);
$this->assertCount(2, $messages);
$message = $messages->getIterator()->current();
$this->assertInstanceOf(Message::class, $message);
$this->assertTrue($message->isUserMessage());
$this->assertSame('An amazing user message', $message->getText());
$this->assertSame('my_user', $message->getSenderId());
$this->assertCount(0, $message->getReadBy());
$this->assertSame('rest', $message->getOrigin());
$this->assertNull($message->getLocation());
$this->assertSame([], $message->getCustom());
$this->assertSame($conversationId, $message->getConversationId());
$this->assertNull($message->getAttachment());
}
}

0 comments on commit c69fc61

Please sign in to comment.