Skip to content

Commit 94cce0a

Browse files
authored
Direct convo updates (#213)
* direct conversation updates * Apply fixes from StyleCI (#212)
1 parent 693e247 commit 94cce0a

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ $conversation = Chat::createConversation($participants)->makePrivate();
141141
$conversation = Chat::createConversation($participants)->makePrivate(false);
142142

143143
// Create a direct message
144+
145+
// Make direct conversation after creation
144146
$conversation = Chat::createConversation($participants)->makeDirect();
147+
148+
// Specify intent for direct conversation before creation
149+
$conversation = Chat::makeDirect()->createConversation($participants);
145150
```
146151

147152
> **Note:** You will not be able to add additional participants to a direct conversation.

src/Chat.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ public function __construct(
4949
*/
5050
public function createConversation(array $participants, array $data = [])
5151
{
52-
return $this->conversationService->start($participants, $data);
52+
$payload = [
53+
'participants' => $participants,
54+
'data' => $data,
55+
'direct_message' => $this->conversationService->directMessage,
56+
];
57+
58+
return $this->conversationService->start($payload);
59+
}
60+
61+
public function makeDirect()
62+
{
63+
$this->conversationService->directMessage = true;
64+
65+
return $this;
5366
}
5467

5568
/**

src/ChatServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function register()
4545
*/
4646
private function registerChat()
4747
{
48-
$this->app->bind('chat', function () {
49-
return $this->app->make(\Musonza\Chat\Chat::class);
48+
$this->app->bind('\Musonza\Chat\Chat', function () {
49+
return $this->app->make(Chat::class);
5050
});
5151
}
5252

src/Facades/ChatFacade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Musonza\Chat\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Musonza\Chat\Chat;
67

78
class ChatFacade extends Facade
89
{
@@ -14,6 +15,8 @@ class ChatFacade extends Facade
1415
*/
1516
protected static function getFacadeAccessor()
1617
{
17-
return 'chat';
18+
self::clearResolvedInstance(Chat::class);
19+
20+
return Chat::class;
1821
}
1922
}

src/Models/Conversation.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class Conversation extends BaseModel
2121
{
2222
protected $table = ConfigurationManager::CONVERSATIONS_TABLE;
23-
protected $fillable = ['data'];
23+
protected $fillable = ['data', 'direct_message'];
2424
protected $casts = [
2525
'data' => 'array',
2626
'direct_message' => 'boolean',
@@ -148,18 +148,23 @@ public function removeParticipant($participants)
148148
/**
149149
* Starts a new conversation.
150150
*
151-
* @param array $participants
152-
* @param array $data
151+
* @param array $payload
152+
*
153+
* @throws InvalidDirectMessageNumberOfParticipants
153154
*
154155
* @return Conversation
155156
*/
156-
public function start(array $participants, $data = []): self
157+
public function start(array $payload): self
157158
{
159+
if ($payload['direct_message'] && count($payload['participants']) > 2) {
160+
throw new InvalidDirectMessageNumberOfParticipants();
161+
}
162+
158163
/** @var Conversation $conversation */
159-
$conversation = $this->create(['data' => $data]);
164+
$conversation = $this->create(['data' => $payload['data'], 'direct_message' => (bool) $payload['direct_message']]);
160165

161-
if ($participants) {
162-
$conversation->addParticipants($participants);
166+
if ($payload['participants']) {
167+
$conversation->addParticipants($payload['participants']);
163168
}
164169

165170
return $conversation;
@@ -210,6 +215,7 @@ public function makeDirect($isDirect = true)
210215
private function ensureNoDirectMessagingExist()
211216
{
212217
$participants = $this->participants()->get()->pluck('messageable');
218+
/** @var Conversation $common */
213219
$common = Chat::conversations()->between($participants[0], $participants[1]);
214220

215221
if (!is_null($common)) {

src/Services/ConversationService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ class ConversationService
2121
*/
2222
public $conversation;
2323

24+
public $directMessage = false;
25+
2426
public function __construct(Conversation $conversation)
2527
{
2628
$this->conversation = $conversation;
2729
}
2830

29-
public function start($participants, $data = [])
31+
public function start(array $payload)
3032
{
31-
$conversation = $this->conversation->start($participants, $data);
33+
$conversation = $this->conversation->start($payload);
3234

3335
event(new ConversationStarted($conversation));
3436

tests/Unit/ConversationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ public function it_prevents_additional_participants_to_direct_conversation()
274274
/** @test */
275275
public function it_can_return_a_conversation_between_users()
276276
{
277-
$conversation = Chat::createConversation([$this->alpha, $this->bravo])->makeDirect();
278-
$conversation2 = Chat::createConversation([$this->alpha, $this->charlie]);
277+
$conversation = Chat::makeDirect()->createConversation([$this->alpha, $this->bravo]);
278+
Chat::createConversation([$this->alpha, $this->charlie]);
279279
$conversation3 = Chat::createConversation([$this->alpha, $this->delta])->makeDirect();
280280

281281
$c1 = Chat::conversations()->between($this->alpha, $this->bravo);

0 commit comments

Comments
 (0)