Skip to content

Commit 32ec6b6

Browse files
authored
Merge pull request #7 from carandclassic/add-isreadby-isread-methods-to-message-model
Add isreadby isread methods to message model
2 parents fb2f95a + 2c9f51e commit 32ec6b6

8 files changed

+297
-8
lines changed

src/Models/Conversation.php

+13
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,17 @@ public static function createManyFromArray(array $data): array
4747
}
4848
return $conversations;
4949
}
50+
51+
public function unreadBy(): ?array
52+
{
53+
if ($this->lastMessage === null) {
54+
return null;
55+
}
56+
57+
$readBy = $this->lastMessage->readBy;
58+
$readBy[] = $this->lastMessage->senderId;
59+
$participants = array_keys($this->participants);
60+
61+
return array_diff($participants, $readBy);
62+
}
5063
}

src/Models/Message.php

+16
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ public function isSystemMessage(): bool
5353
{
5454
return $this->type == MessageType::SYSTEM;
5555
}
56+
57+
public function isReadBy(string $userId): bool
58+
{
59+
if ($userId === $this->senderId) {
60+
return true;
61+
}
62+
63+
return in_array($userId, $this->readBy, true);
64+
}
65+
66+
public function isRead(): bool
67+
{
68+
$unread = array_diff($this->readBy, [$this->senderId]);
69+
70+
return !empty($unread);
71+
}
5672
}

tests/Feature/ConversationTest.php

+34-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use CarAndClassic\TalkJS\Events\ParticipationUpdated;
1515
use CarAndClassic\TalkJS\Models\Conversation;
1616
use CarAndClassic\TalkJS\Models\Message;
17+
use CarAndClassic\TalkJS\Tests\TestCase;
1718
use Symfony\Component\HttpClient\Response\MockResponse;
1819

1920
final class ConversationTest extends TestCase
2021
{
2122
private array $userIds;
22-
2323
private array $conversations;
2424

2525
public function setUp(): void
@@ -54,7 +54,7 @@ public function setUp(): void
5454
'createdAt' => $createdAt,
5555
]),
5656
'participants' => [
57-
$this->userIds[0] => [
57+
$this->userIds[0] => [
5858
'access' => 'ReadWrite',
5959
'notify' => true
6060
],
@@ -84,6 +84,38 @@ public function setUp(): void
8484
]
8585
],
8686
'createdAt' => $createdAt
87+
],
88+
[
89+
'id' => 'testConversationId3',
90+
'subject' => 'Test Conversation 3',
91+
'topicId' => 'Test Topic 3',
92+
'photoUrl' => null,
93+
'welcomeMessages' => ['Test Welcome Message'],
94+
'custom' => ['test' => 'test'],
95+
'lastMessage' => new Message([
96+
'id' => "test",
97+
'type' => "UserMessage",
98+
'conversationId' => "dev_test",
99+
'senderId' => $this->userIds[1],
100+
'text' => "This is the message copy",
101+
'readBy' => [],
102+
'origin' => "rest",
103+
'location' => null,
104+
'custom' => [],
105+
'attachment' => null,
106+
'createdAt' => $createdAt,
107+
]),
108+
'participants' => [
109+
$this->userIds[0] => [
110+
'access' => 'ReadWrite',
111+
'notify' => true
112+
],
113+
$this->userIds[1] => [
114+
'access' => 'Read',
115+
'notify' => false
116+
]
117+
],
118+
'createdAt' => $createdAt
87119
]
88120
];
89121
}

tests/Feature/MessageTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
use CarAndClassic\TalkJS\Events\MessageDeleted;
1111
use CarAndClassic\TalkJS\Events\MessageEdited;
1212
use CarAndClassic\TalkJS\Models\Message;
13+
use CarAndClassic\TalkJS\Tests\TestCase;
1314
use Symfony\Component\HttpClient\Response\MockResponse;
1415

1516
final class MessageTest extends TestCase
1617
{
1718
private string $conversationId;
18-
1919
private string $senderId;
20-
2120
private array $messages;
2221

2322
protected function setUp(): void

tests/Feature/UserTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
use CarAndClassic\TalkJS\Events\UserCreatedOrUpdated;
99
use CarAndClassic\TalkJS\Models\Conversation;
1010
use CarAndClassic\TalkJS\Models\User;
11+
use CarAndClassic\TalkJS\Tests\TestCase;
1112
use Symfony\Component\HttpClient\Response\MockResponse;
1213

1314
final class UserTest extends TestCase
1415
{
1516
private string $userId;
16-
1717
private array $userDetails;
18-
1918
private array $userConversations;
2019

2120
protected function setUp(): void

tests/Feature/TestCase.php tests/TestCase.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace CarAndClassic\TalkJS\Tests\Feature;
5+
namespace CarAndClassic\TalkJS\Tests;
66

77
use CarAndClassic\TalkJS\Api\TalkJSApi;
88
use PHPUnit\Framework\TestCase as BaseTestCase;
@@ -12,7 +12,6 @@
1212
abstract class TestCase extends BaseTestCase
1313
{
1414
protected array $defaultMockResponseHeaders;
15-
1615
protected array $defaultFilters;
1716

1817
protected function setUp(): void

tests/Unit/ConversationTest.php

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CarAndClassic\TalkJS\Tests\Unit;
6+
7+
use CarAndClassic\TalkJS\Models\Conversation;
8+
use CarAndClassic\TalkJS\Tests\TestCase;
9+
10+
final class ConversationTest extends TestCase
11+
{
12+
private array $conversations;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
$createdAt = time() * 1000;
18+
$userIds = [
19+
'TestConversationUserId1',
20+
'TestConversationUserId2'
21+
];
22+
$this->conversations = [
23+
[
24+
'id' => 'testConversationId1',
25+
'subject' => 'Test Conversation 1',
26+
'topicId' => 'Test Topic 1',
27+
'photoUrl' => null,
28+
'welcomeMessages' => ['Test Welcome Message'],
29+
'custom' => ['test' => 'test'],
30+
'lastMessage' => [
31+
'id' => "test",
32+
'type' => "UserMessage",
33+
'conversationId' => "dev_test",
34+
'senderId' => $userIds[1],
35+
'text' => "This is the message copy",
36+
'readBy' => [
37+
$userIds[0],
38+
],
39+
'origin' => "rest",
40+
'location' => null,
41+
'custom' => [],
42+
'attachment' => null,
43+
'createdAt' => $createdAt,
44+
],
45+
'participants' => [
46+
$userIds[0] => [
47+
'access' => 'ReadWrite',
48+
'notify' => true
49+
],
50+
$userIds[1] => [
51+
'access' => 'Read',
52+
'notify' => false
53+
]
54+
],
55+
'createdAt' => $createdAt
56+
],
57+
[
58+
'id' => 'testConversationId2',
59+
'subject' => 'Test Conversation 2',
60+
'topicId' => 'Test Topic 2',
61+
'photoUrl' => null,
62+
'welcomeMessages' => ['Test Welcome Message'],
63+
'custom' => ['test' => 'test'],
64+
'lastMessage' => null,
65+
'participants' => [
66+
$userIds[0] => [
67+
'access' => 'ReadWrite',
68+
'notify' => true
69+
],
70+
$userIds[1] => [
71+
'access' => 'Read',
72+
'notify' => false
73+
]
74+
],
75+
'createdAt' => $createdAt
76+
],
77+
[
78+
'id' => 'testConversationId3',
79+
'subject' => 'Test Conversation 3',
80+
'topicId' => 'Test Topic 3',
81+
'photoUrl' => null,
82+
'welcomeMessages' => ['Test Welcome Message'],
83+
'custom' => ['test' => 'test'],
84+
'lastMessage' => [
85+
'id' => "test",
86+
'type' => "UserMessage",
87+
'conversationId' => "dev_test",
88+
'senderId' => $userIds[1],
89+
'text' => "This is the message copy",
90+
'readBy' => [],
91+
'origin' => "rest",
92+
'location' => null,
93+
'custom' => [],
94+
'attachment' => null,
95+
'createdAt' => $createdAt,
96+
],
97+
'participants' => [
98+
$userIds[0] => [
99+
'access' => 'ReadWrite',
100+
'notify' => true
101+
],
102+
$userIds[1] => [
103+
'access' => 'Read',
104+
'notify' => false
105+
]
106+
],
107+
'createdAt' => $createdAt
108+
]
109+
];
110+
}
111+
112+
public function testCreateManyFromArray(): void
113+
{
114+
$conversations = Conversation::createManyFromArray($this->conversations);
115+
116+
$this->assertIsArray($conversations);
117+
118+
foreach ($conversations as $conversation) {
119+
$this->assertInstanceOf(Conversation::class, $conversation);
120+
}
121+
}
122+
123+
public function testUnreadBy(): void
124+
{
125+
$conversation1 = new Conversation($this->conversations[0]);
126+
$conversation2 = new Conversation($this->conversations[1]);
127+
$conversation3 = new Conversation($this->conversations[2]);
128+
129+
$this->assertEmpty($conversation1->unreadBy());
130+
$this->assertNull($conversation2->unreadBy());
131+
$this->assertEquals($conversation3->unreadBy(), ['TestConversationUserId1']);
132+
}
133+
}

tests/Unit/MessageTest.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CarAndClassic\TalkJS\Tests\Unit;
6+
7+
use CarAndClassic\TalkJS\Enumerations\MessageType;
8+
use CarAndClassic\TalkJS\Models\Message;
9+
use CarAndClassic\TalkJS\Tests\TestCase;
10+
11+
final class MessageTest extends TestCase
12+
{
13+
private string $senderId;
14+
private array $messages;
15+
private Message $message1;
16+
private Message $message2;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$conversationId = 'testConversationId';
23+
$this->senderId = 'testSenderId';
24+
$this->userIds = [
25+
'TestConversationUserId1',
26+
'TestConversationUserId2'
27+
];
28+
$this->messages = [
29+
[
30+
'id' => '1', // At time of writing results are returned descending
31+
'type' => MessageType::USER,
32+
'conversationId' => $conversationId,
33+
'senderId' => $this->senderId,
34+
'text' => 'Test User Message',
35+
'readBy' => [
36+
$this->userIds[0]
37+
],
38+
'origin' => 'rest',
39+
'location' => null,
40+
'custom' => ['test' => 'test'],
41+
'createdAt' => (time() + 1) * 1000, // At time of writing TalkJS returns timestamp in milliseconds
42+
'attachment' => null
43+
],
44+
[
45+
'id' => '2',
46+
'type' => MessageType::SYSTEM,
47+
'conversationId' => $conversationId,
48+
'senderId' => null,
49+
'text' => 'Test System Message',
50+
'readBy' => [],
51+
'origin' => 'rest',
52+
'location' => null,
53+
'custom' => ['test' => 'test'],
54+
'createdAt' => time() * 1000,
55+
'attachment' => null
56+
]
57+
];
58+
$this->message1 = new Message($this->messages[0]);
59+
$this->message2 = new Message($this->messages[1]);
60+
}
61+
62+
public function testCreateManyFromArray(): void
63+
{
64+
$messages = Message::createManyFromArray($this->messages);
65+
66+
$this->assertIsArray($messages);
67+
68+
foreach ($messages as $message) {
69+
$this->assertInstanceOf(Message::class, $message);
70+
}
71+
}
72+
73+
public function testIsUserMessage(): void
74+
{
75+
$this->assertSame($this->message1->isUserMessage(), true);
76+
$this->assertSame($this->message2->isUserMessage(), false);
77+
}
78+
79+
public function testIsSystemMessage(): void
80+
{
81+
$this->assertSame($this->message1->isSystemMessage(), false);
82+
$this->assertSame($this->message2->isSystemMessage(), true);
83+
}
84+
85+
public function testIsReadBy(): void
86+
{
87+
$this->assertSame($this->message1->isReadBy($this->userIds[0]), true);
88+
$this->assertSame($this->message1->isReadBy($this->userIds[1]), false);
89+
$this->assertSame($this->message1->isReadBy($this->senderId), true);
90+
$this->assertSame($this->message2->isReadBy($this->userIds[0]), false);
91+
}
92+
93+
public function testIsRead(): void
94+
{
95+
$this->assertSame($this->message1->isRead(), true);
96+
$this->assertSame($this->message2->isRead(), false);
97+
}
98+
}

0 commit comments

Comments
 (0)