Skip to content

Commit

Permalink
Merge pull request #7 from carandclassic/add-isreadby-isread-methods-…
Browse files Browse the repository at this point in the history
…to-message-model

Add isreadby isread methods to message model
  • Loading branch information
BinaryKitten committed Dec 7, 2022
2 parents fb2f95a + 2c9f51e commit 32ec6b6
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/Models/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@ public static function createManyFromArray(array $data): array
}
return $conversations;
}

public function unreadBy(): ?array
{
if ($this->lastMessage === null) {
return null;
}

$readBy = $this->lastMessage->readBy;
$readBy[] = $this->lastMessage->senderId;
$participants = array_keys($this->participants);

return array_diff($participants, $readBy);
}
}
16 changes: 16 additions & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ public function isSystemMessage(): bool
{
return $this->type == MessageType::SYSTEM;
}

public function isReadBy(string $userId): bool
{
if ($userId === $this->senderId) {
return true;
}

return in_array($userId, $this->readBy, true);
}

public function isRead(): bool
{
$unread = array_diff($this->readBy, [$this->senderId]);

return !empty($unread);
}
}
36 changes: 34 additions & 2 deletions tests/Feature/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use CarAndClassic\TalkJS\Events\ParticipationUpdated;
use CarAndClassic\TalkJS\Models\Conversation;
use CarAndClassic\TalkJS\Models\Message;
use CarAndClassic\TalkJS\Tests\TestCase;
use Symfony\Component\HttpClient\Response\MockResponse;

final class ConversationTest extends TestCase
{
private array $userIds;

private array $conversations;

public function setUp(): void
Expand Down Expand Up @@ -54,7 +54,7 @@ public function setUp(): void
'createdAt' => $createdAt,
]),
'participants' => [
$this->userIds[0] => [
$this->userIds[0] => [
'access' => 'ReadWrite',
'notify' => true
],
Expand Down Expand Up @@ -84,6 +84,38 @@ public function setUp(): void
]
],
'createdAt' => $createdAt
],
[
'id' => 'testConversationId3',
'subject' => 'Test Conversation 3',
'topicId' => 'Test Topic 3',
'photoUrl' => null,
'welcomeMessages' => ['Test Welcome Message'],
'custom' => ['test' => 'test'],
'lastMessage' => new Message([
'id' => "test",
'type' => "UserMessage",
'conversationId' => "dev_test",
'senderId' => $this->userIds[1],
'text' => "This is the message copy",
'readBy' => [],
'origin' => "rest",
'location' => null,
'custom' => [],
'attachment' => null,
'createdAt' => $createdAt,
]),
'participants' => [
$this->userIds[0] => [
'access' => 'ReadWrite',
'notify' => true
],
$this->userIds[1] => [
'access' => 'Read',
'notify' => false
]
],
'createdAt' => $createdAt
]
];
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use CarAndClassic\TalkJS\Events\MessageDeleted;
use CarAndClassic\TalkJS\Events\MessageEdited;
use CarAndClassic\TalkJS\Models\Message;
use CarAndClassic\TalkJS\Tests\TestCase;
use Symfony\Component\HttpClient\Response\MockResponse;

final class MessageTest extends TestCase
{
private string $conversationId;

private string $senderId;

private array $messages;

protected function setUp(): void
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
use CarAndClassic\TalkJS\Events\UserCreatedOrUpdated;
use CarAndClassic\TalkJS\Models\Conversation;
use CarAndClassic\TalkJS\Models\User;
use CarAndClassic\TalkJS\Tests\TestCase;
use Symfony\Component\HttpClient\Response\MockResponse;

final class UserTest extends TestCase
{
private string $userId;

private array $userDetails;

private array $userConversations;

protected function setUp(): void
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/TestCase.php → tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace CarAndClassic\TalkJS\Tests\Feature;
namespace CarAndClassic\TalkJS\Tests;

use CarAndClassic\TalkJS\Api\TalkJSApi;
use PHPUnit\Framework\TestCase as BaseTestCase;
Expand All @@ -12,7 +12,6 @@
abstract class TestCase extends BaseTestCase
{
protected array $defaultMockResponseHeaders;

protected array $defaultFilters;

protected function setUp(): void
Expand Down
133 changes: 133 additions & 0 deletions tests/Unit/ConversationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace CarAndClassic\TalkJS\Tests\Unit;

use CarAndClassic\TalkJS\Models\Conversation;
use CarAndClassic\TalkJS\Tests\TestCase;

final class ConversationTest extends TestCase
{
private array $conversations;

public function setUp(): void
{
parent::setUp();
$createdAt = time() * 1000;
$userIds = [
'TestConversationUserId1',
'TestConversationUserId2'
];
$this->conversations = [
[
'id' => 'testConversationId1',
'subject' => 'Test Conversation 1',
'topicId' => 'Test Topic 1',
'photoUrl' => null,
'welcomeMessages' => ['Test Welcome Message'],
'custom' => ['test' => 'test'],
'lastMessage' => [
'id' => "test",
'type' => "UserMessage",
'conversationId' => "dev_test",
'senderId' => $userIds[1],
'text' => "This is the message copy",
'readBy' => [
$userIds[0],
],
'origin' => "rest",
'location' => null,
'custom' => [],
'attachment' => null,
'createdAt' => $createdAt,
],
'participants' => [
$userIds[0] => [
'access' => 'ReadWrite',
'notify' => true
],
$userIds[1] => [
'access' => 'Read',
'notify' => false
]
],
'createdAt' => $createdAt
],
[
'id' => 'testConversationId2',
'subject' => 'Test Conversation 2',
'topicId' => 'Test Topic 2',
'photoUrl' => null,
'welcomeMessages' => ['Test Welcome Message'],
'custom' => ['test' => 'test'],
'lastMessage' => null,
'participants' => [
$userIds[0] => [
'access' => 'ReadWrite',
'notify' => true
],
$userIds[1] => [
'access' => 'Read',
'notify' => false
]
],
'createdAt' => $createdAt
],
[
'id' => 'testConversationId3',
'subject' => 'Test Conversation 3',
'topicId' => 'Test Topic 3',
'photoUrl' => null,
'welcomeMessages' => ['Test Welcome Message'],
'custom' => ['test' => 'test'],
'lastMessage' => [
'id' => "test",
'type' => "UserMessage",
'conversationId' => "dev_test",
'senderId' => $userIds[1],
'text' => "This is the message copy",
'readBy' => [],
'origin' => "rest",
'location' => null,
'custom' => [],
'attachment' => null,
'createdAt' => $createdAt,
],
'participants' => [
$userIds[0] => [
'access' => 'ReadWrite',
'notify' => true
],
$userIds[1] => [
'access' => 'Read',
'notify' => false
]
],
'createdAt' => $createdAt
]
];
}

public function testCreateManyFromArray(): void
{
$conversations = Conversation::createManyFromArray($this->conversations);

$this->assertIsArray($conversations);

foreach ($conversations as $conversation) {
$this->assertInstanceOf(Conversation::class, $conversation);
}
}

public function testUnreadBy(): void
{
$conversation1 = new Conversation($this->conversations[0]);
$conversation2 = new Conversation($this->conversations[1]);
$conversation3 = new Conversation($this->conversations[2]);

$this->assertEmpty($conversation1->unreadBy());
$this->assertNull($conversation2->unreadBy());
$this->assertEquals($conversation3->unreadBy(), ['TestConversationUserId1']);
}
}
98 changes: 98 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace CarAndClassic\TalkJS\Tests\Unit;

use CarAndClassic\TalkJS\Enumerations\MessageType;
use CarAndClassic\TalkJS\Models\Message;
use CarAndClassic\TalkJS\Tests\TestCase;

final class MessageTest extends TestCase
{
private string $senderId;
private array $messages;
private Message $message1;
private Message $message2;

protected function setUp(): void
{
parent::setUp();

$conversationId = 'testConversationId';
$this->senderId = 'testSenderId';
$this->userIds = [
'TestConversationUserId1',
'TestConversationUserId2'
];
$this->messages = [
[
'id' => '1', // At time of writing results are returned descending
'type' => MessageType::USER,
'conversationId' => $conversationId,
'senderId' => $this->senderId,
'text' => 'Test User Message',
'readBy' => [
$this->userIds[0]
],
'origin' => 'rest',
'location' => null,
'custom' => ['test' => 'test'],
'createdAt' => (time() + 1) * 1000, // At time of writing TalkJS returns timestamp in milliseconds
'attachment' => null
],
[
'id' => '2',
'type' => MessageType::SYSTEM,
'conversationId' => $conversationId,
'senderId' => null,
'text' => 'Test System Message',
'readBy' => [],
'origin' => 'rest',
'location' => null,
'custom' => ['test' => 'test'],
'createdAt' => time() * 1000,
'attachment' => null
]
];
$this->message1 = new Message($this->messages[0]);
$this->message2 = new Message($this->messages[1]);
}

public function testCreateManyFromArray(): void
{
$messages = Message::createManyFromArray($this->messages);

$this->assertIsArray($messages);

foreach ($messages as $message) {
$this->assertInstanceOf(Message::class, $message);
}
}

public function testIsUserMessage(): void
{
$this->assertSame($this->message1->isUserMessage(), true);
$this->assertSame($this->message2->isUserMessage(), false);
}

public function testIsSystemMessage(): void
{
$this->assertSame($this->message1->isSystemMessage(), false);
$this->assertSame($this->message2->isSystemMessage(), true);
}

public function testIsReadBy(): void
{
$this->assertSame($this->message1->isReadBy($this->userIds[0]), true);
$this->assertSame($this->message1->isReadBy($this->userIds[1]), false);
$this->assertSame($this->message1->isReadBy($this->senderId), true);
$this->assertSame($this->message2->isReadBy($this->userIds[0]), false);
}

public function testIsRead(): void
{
$this->assertSame($this->message1->isRead(), true);
$this->assertSame($this->message2->isRead(), false);
}
}

0 comments on commit 32ec6b6

Please sign in to comment.