File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed
Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 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];
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments