Skip to content

Commit c4bc1c6

Browse files
authored
Specificy sender fields (#191)
* Allow specifying sender fields for queries * Apply fixes from StyleCI (#190)
1 parent 1180f39 commit c4bc1c6

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

config/musonza_chat.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@
2626
* Automatically convert conversations with more than two users to public
2727
*/
2828
'make_three_or_more_users_public' => true,
29+
30+
/*
31+
* Specify the fields that you want to return each time for the sender.
32+
* If not set or empty, all the columns for the sender will be returned
33+
*/
34+
'sender_fields_whitelist' => [],
2935
];

src/Chat.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@ public static function makeThreeOrMoreUsersPublic()
130130
{
131131
return config('musonza_chat.make_three_or_more_users_public', true);
132132
}
133+
134+
public static function senderFieldsWhitelist()
135+
{
136+
$fields = config('musonza_chat.sender_fields_whitelist', []);
137+
138+
return (is_array($fields) && !empty($fields)) ? $fields : null;
139+
}
133140
}

src/Models/Message.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class Message extends BaseModel
3030

3131
public function sender()
3232
{
33+
$fields = Chat::senderFieldsWhitelist();
34+
35+
if ($fields) {
36+
return $this->belongsTo(Chat::userModel(), 'user_id')->select($fields);
37+
}
38+
3339
return $this->belongsTo(Chat::userModel(), 'user_id');
3440
}
3541

tests/Unit/ConversationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,15 @@ public function it_filters_conversations_by_type()
304304
$publicConversations = Chat::conversations()->setUser($this->users[0])->isPrivate(false)->get();
305305
$this->assertCount(1, $publicConversations);
306306
}
307+
308+
/** @test */
309+
public function it_specifies_fields_to_return_for_sender()
310+
{
311+
$this->app['config']->set('musonza_chat.sender_fields_whitelist', ['uid', 'email']);
312+
313+
$conversation = Chat::createConversation([$this->users[0]->getKey(), $this->users[1]->getKey()]);
314+
$message = Chat::message('Hello')->from($this->users[0])->to($conversation)->send();
315+
316+
$this->assertSame(['uid', 'email'], array_keys($message->sender->attributesToArray()));
317+
}
307318
}

0 commit comments

Comments
 (0)