Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/Events/NewMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Events;

use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $message;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->to);
}

public function broadcastWith()
{
$this->message->load('fromContact');

return ["message" => $this->message];
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Message;
use App\Events\NewMessage;

class ContactsController extends Controller
{
public function get()
{
$contacts = User::where('id', '!=', auth()->id())->get();
$unreadIds = Message::select(\DB::raw('`from` as sender_id, count(`from`) as messages_count'))
->where('to', auth()->id())
->where('read', false)
->groupBy('from')
->get();
$contacts = $contacts->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->id)->first();

$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;

return $contact;
});


return response()->json($contacts);
}

public function getMessagesFor($id)
{
// mark all messages with the selected contact as read
Message::where('from', $id)->where('to', auth()->id())->update(['read' => true]);

// get all messages between the authenticated user and the selected user
$messages = Message::where(function($q) use ($id) {
$q->where('from', auth()->id());
$q->where('to', $id);
})->orWhere(function($q) use ($id) {
$q->where('from', $id);
$q->where('to', auth()->id());
})
->get();
return response()->json($messages);
}

public function send(Request $request)
{
$message = Message::create([
'from' => auth()->id(),
'to' => $request->contact_id,
'text' => $request->text
]);
broadcast(new NewMessage($message));
return response()->json($message);
}
}
6 changes: 3 additions & 3 deletions app/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Message extends Model
*
* @var array
*/
protected $fillable = ['message'];
protected $guarded = [];

public function user()
public function fromContact()
{
return $this->belongsTo(User::class);
return $this->hasOne(User::class, 'id', 'from');
}
}
5 changes: 0 additions & 5 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,4 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

public function messages()
{
return $this->hasMany(Message::class);
}
}
Loading