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

Commit c69fc61

Browse files
author
Olivier Dolbeau
authored
Merge pull request #4 from shapintv/new-entrypoints
New entrypoints
2 parents 9adf628 + 31f96e3 commit c69fc61

File tree

6 files changed

+294
-12
lines changed

6 files changed

+294
-12
lines changed

src/Api/Conversation.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,59 @@ public function leave(string $conversationId, string $userId)
101101

102102
return $this->hydrator->hydrate($response, Model\Conversation\ConversationLeft::class);
103103
}
104+
105+
/**
106+
* @throws Exception
107+
*/
108+
public function findMessages(string $conversationId, array $filters = []): Model\Conversation\MessageCollection
109+
{
110+
$response = $this->httpGet("conversations/$conversationId/messages", $filters);
111+
112+
if (200 !== $response->getStatusCode()) {
113+
$this->handleErrors($response);
114+
}
115+
116+
return $this->hydrator->hydrate($response, Model\Conversation\MessageCollection::class);
117+
}
118+
119+
/**
120+
* @throws Exception
121+
*/
122+
public function postSystemMessage(string $conversationId, string $text, array $custom = []): Model\Conversation\MessageCreated
123+
{
124+
$response = $this->httpPost("conversations/$conversationId/messages", [
125+
[
126+
'type' => 'SystemMessage',
127+
'text' => $text,
128+
'custom' => (object) $custom,
129+
],
130+
]);
131+
132+
if (200 !== $response->getStatusCode()) {
133+
$this->handleErrors($response);
134+
}
135+
136+
return $this->hydrator->hydrate($response, Model\Conversation\MessageCreated::class);
137+
}
138+
139+
/**
140+
* @throws Exception
141+
*/
142+
public function postUserMessage(string $conversationId, string $sender, string $text, array $custom = []): Model\Conversation\MessageCreated
143+
{
144+
$response = $this->httpPost("conversations/$conversationId/messages", [
145+
[
146+
'type' => 'UserMessage',
147+
'sender' => $sender,
148+
'text' => $text,
149+
'custom' => (object) $custom,
150+
],
151+
]);
152+
153+
if (200 !== $response->getStatusCode()) {
154+
$this->handleErrors($response);
155+
}
156+
157+
return $this->hydrator->hydrate($response, Model\Conversation\MessageCreated::class);
158+
}
104159
}

src/Exception/Domain/BadRequestException.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ public function __construct(ResponseInterface $response)
2121
$this->response = $response;
2222
$content = json_decode($response->getContent(false), true);
2323

24-
if (!isset($content['reasons'])) {
25-
parent::__construct('Bad Request: No reason.');
26-
27-
return;
28-
}
29-
30-
$field = array_key_first($content['reasons']);
31-
$reasons = reset($content['reasons']);
32-
$reason = \is_array($reasons) ? $reasons[0] : $reasons;
33-
34-
parent::__construct("Bad request: Field $field: $reason.");
24+
parent::__construct('Bad request. Content: '.json_encode($content));
3525
}
3626

3727
public function getResponse(): ResponseInterface

src/Model/Conversation/Message.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapin\TalkJS\Model\Conversation;
6+
7+
use Shapin\TalkJS\Model\CreatableFromArray;
8+
9+
class Message implements CreatableFromArray
10+
{
11+
const TYPE_SYSTEM_MESSAGE = 'SystemMessage';
12+
const TYPE_USER_MESSAGE = 'UserMessage';
13+
14+
/**
15+
* @var string
16+
*/
17+
private $type;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $text;
23+
24+
/**
25+
* @var ?string
26+
*/
27+
private $senderId;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $readBy;
33+
34+
/**
35+
* @var string
36+
*/
37+
private $origin;
38+
39+
/**
40+
* @var string
41+
*/
42+
private $location;
43+
44+
/**
45+
* @var string
46+
*/
47+
private $id;
48+
49+
/**
50+
* @var array
51+
*/
52+
private $custom;
53+
54+
/**
55+
* @var string
56+
*/
57+
private $conversationId;
58+
59+
/**
60+
* @var \DateTimeImmutable
61+
*/
62+
private $createdAt;
63+
64+
/**
65+
* @var string
66+
*/
67+
private $attachment;
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public static function createFromArray(array $data): self
73+
{
74+
$timestamp = round($data['createdAt'] / 1000, 0);
75+
76+
$message = new self();
77+
$message->type = $data['type'];
78+
$message->text = $data['text'];
79+
$message->senderId = $data['senderId'];
80+
$message->readBy = $data['readBy'];
81+
$message->origin = $data['origin'];
82+
$message->location = $data['location'];
83+
$message->id = $data['id'];
84+
$message->custom = $data['custom'];
85+
$message->createdAt = new \DateTimeImmutable("@$timestamp");
86+
$message->conversationId = $data['conversationId'];
87+
$message->attachment = $data['attachment'];
88+
89+
return $message;
90+
}
91+
92+
public function getType(): string
93+
{
94+
return $this->type;
95+
}
96+
97+
public function isSystemMessage(): bool
98+
{
99+
return self::TYPE_SYSTEM_MESSAGE === $this->type;
100+
}
101+
102+
public function isUserMessage(): bool
103+
{
104+
return self::TYPE_USER_MESSAGE === $this->type;
105+
}
106+
107+
public function getText(): string
108+
{
109+
return $this->text;
110+
}
111+
112+
public function getSenderId(): ?string
113+
{
114+
return $this->senderId;
115+
}
116+
117+
public function getReadBy(): array
118+
{
119+
return $this->readBy;
120+
}
121+
122+
public function getOrigin(): string
123+
{
124+
return $this->origin;
125+
}
126+
127+
public function getLocation(): ?string
128+
{
129+
return $this->location;
130+
}
131+
132+
public function getId(): string
133+
{
134+
return $this->id;
135+
}
136+
137+
public function getCustom(): array
138+
{
139+
return $this->custom;
140+
}
141+
142+
public function getCreatedAt(): \DateTimeImmutable
143+
{
144+
return $this->createdAt;
145+
}
146+
147+
public function getConversationId(): string
148+
{
149+
return $this->conversationId;
150+
}
151+
152+
public function getAttachment(): ?string
153+
{
154+
return $this->attachment;
155+
}
156+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapin\TalkJS\Model\Conversation;
6+
7+
use Shapin\TalkJS\Model\Collection;
8+
9+
class MessageCollection extends Collection
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public static function createFromArray(array $data)
15+
{
16+
$messages = [];
17+
18+
foreach ($data['data'] as $message) {
19+
$messages[$message['id']] = Message::createFromArray($message);
20+
}
21+
22+
return new self($messages);
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapin\TalkJS\Model\Conversation;
6+
7+
class MessageCreated
8+
{
9+
}

tests/FunctionalTests/ConversationTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Shapin\TalkJS\Model\Conversation\ConversationCreatedOrUpdated;
1515
use Shapin\TalkJS\Model\Conversation\ConversationJoined;
1616
use Shapin\TalkJS\Model\Conversation\ConversationLeft;
17+
use Shapin\TalkJS\Model\Conversation\Message;
18+
use Shapin\TalkJS\Model\Conversation\MessageCollection;
19+
use Shapin\TalkJS\Model\Conversation\MessageCreated;
1720
use Shapin\TalkJS\Model\Conversation\ParticipationUpdated;
1821

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

28-
public function testCreateOrUpdate()
31+
public function testAll()
2932
{
3033
$randomTestString = bin2hex(random_bytes(10));
3134
$conversationId = "conversation_$randomTestString";
@@ -96,5 +99,50 @@ public function testCreateOrUpdate()
9699
$conversation = $this->api->get($conversationId);
97100
$this->assertInstanceOf(Conversation::class, $conversation);
98101
$this->assertSame(['my_user' => ['notify' => false, 'access' => 'Read']], $conversation->getParticipants());
102+
103+
// Find messages: none should be found.
104+
$messages = $this->api->findMessages($conversationId);
105+
$this->assertInstanceOf(MessageCollection::class, $messages);
106+
$this->assertCount(0, $messages);
107+
108+
// Post a new system message.
109+
$messageCreated = $this->api->postSystemMessage($conversationId, 'An amazing system message', ['foo' => 'bar']);
110+
$this->assertInstanceOf(MessageCreated::class, $messageCreated);
111+
112+
// We now have 1 available system message
113+
$messages = $this->api->findMessages($conversationId);
114+
$this->assertInstanceOf(MessageCollection::class, $messages);
115+
$this->assertCount(1, $messages);
116+
$message = $messages->getIterator()->current();
117+
$this->assertInstanceOf(Message::class, $message);
118+
$this->assertTrue($message->isSystemMessage());
119+
$this->assertSame('An amazing system message', $message->getText());
120+
$this->assertNull($message->getSenderId());
121+
$this->assertCount(0, $message->getReadBy());
122+
$this->assertSame('rest', $message->getOrigin());
123+
$this->assertNull($message->getLocation());
124+
$this->assertSame(['foo' => 'bar'], $message->getCustom());
125+
$this->assertSame($conversationId, $message->getConversationId());
126+
$this->assertNull($message->getAttachment());
127+
128+
// Post a new user message.
129+
$messageCreated = $this->api->postUserMessage($conversationId, 'my_user', 'An amazing user message');
130+
$this->assertInstanceOf(MessageCreated::class, $messageCreated);
131+
132+
// We now have 2 available messages
133+
$messages = $this->api->findMessages($conversationId);
134+
$this->assertInstanceOf(MessageCollection::class, $messages);
135+
$this->assertCount(2, $messages);
136+
$message = $messages->getIterator()->current();
137+
$this->assertInstanceOf(Message::class, $message);
138+
$this->assertTrue($message->isUserMessage());
139+
$this->assertSame('An amazing user message', $message->getText());
140+
$this->assertSame('my_user', $message->getSenderId());
141+
$this->assertCount(0, $message->getReadBy());
142+
$this->assertSame('rest', $message->getOrigin());
143+
$this->assertNull($message->getLocation());
144+
$this->assertSame([], $message->getCustom());
145+
$this->assertSame($conversationId, $message->getConversationId());
146+
$this->assertNull($message->getAttachment());
99147
}
100148
}

0 commit comments

Comments
 (0)