Skip to content

Commit

Permalink
Update_shaker
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaker-Al-Muzaini committed Nov 27, 2022
1 parent f3ab0a6 commit 4210af9
Show file tree
Hide file tree
Showing 10 changed files with 23,352 additions and 209 deletions.
26 changes: 17 additions & 9 deletions app/Http/Controllers/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
namespace App\Http\Controllers;

use App\Models\Conversation;
use App\Models\Participant;
use App\Models\Participant;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MessageController extends Controller
{
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
public function index($id =null)
{
$user = Auth::user();
$chats= $user->conversations()->with([
'lastMessage',
'participants'=>function($builder) use($user){
$builder->where('id','<>',$user->id);
}])->get();

$friends = User::where('id', '<>', $user->id)
->orderBy('name')
->paginate();
return view('messenger', [
$chats= $user->conversations()->with([
'lastMessage',
'participants'=>function($builder) use($user){
$builder->where('id','<>',$user->id);
}])->get();
$messages=[];
$activeChat=null;
if($id){
$activeChat=$chats->where('id',$id)->first();
$messages=$activeChat->messages()->with('user')->paginate();
}
return view('messenger',[
'friends' => $friends,
'chats'=>$chats,
'messages'=>$messages,
'activeChat'=>$activeChat,
]);
}

Expand Down
171 changes: 128 additions & 43 deletions app/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Http\Controllers;

use App\Events\MessageCreate;
//use App\Events\MessageCreated;
use App\Models\Conversation;
use App\Models\Message;
use App\Models\Recipient;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
Expand All @@ -14,44 +16,74 @@

class MessagesController extends Controller
{

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($id)
{
$user =Auth::user();
$conversation= $user->conversations()->findOrFail($id);
return $conversation->messages()->pagintate();
$user = Auth::user();
$conversation = $user->conversations()
->with(['participants' => function($builder) use ($user) {
$builder->where('id', '<>', $user->id);
}])
->findOrFail($id);

$messages = $conversation->messages()
->with('user')
->where(function($query) use ($user) {
$query
->where(function($query) use ($user) {
$query->where('user_id', $user->id)
->whereNull('deleted_at');
})
->orWhereRaw('id IN (
SELECT message_id FROM recipients
WHERE recipients.message_id = messages.id
AND recipients.user_id = ?
AND recipients.deleted_at IS NULL
)', [$user->id]);
})
->latest()
->paginate();

return [
'conversation' => $conversation,
'messages' => $messages,
];
}


/**
* @throws Throwable
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'message'=>[
'required',
'string'
],
'conversation_id'=>[
'int',
'exists:conversations,id',
Rule::requiredIf( function() use ($request){
$request->validate([
// 'message' => [Rule::requiredIf(function() use ($request) {
// return !$request->hasFile('attachment');
// }), 'string'],
// 'attachment' => ['file'],
'conversation_id' => [
Rule::requiredIf(function() use ($request) {
return !$request->input('user_id');
}),
],
'user_id'=>[
'int',
'exists:users,id',
Rule::requiredIf( function() use ($request){
'exists:conversations,id',
],
'user_id' => [
Rule::requiredIf(function() use ($request) {
return !$request->input('conversation_id');
}),
]

'int',
'exists:users,id',
],
]);

// $user = Auth::user();
$user =User::find(1);
$user = User::find(3);

$conversation_id = $request->post('conversation_id');
$user_id = $request->post('user_id');
Expand Down Expand Up @@ -83,26 +115,43 @@ public function store(Request $request)

}

$type = 'text';
$message = $request->post('message');
if ($request->hasFile('attachment')) {
$file = $request->file('attachment');
$message = [
'file_name' => $file->getClientOriginalName(),
'file_size' => $file->getSize(),
'mimetype' => $file->getMimeType(),
'file_path' => $file->store('attachments', [
'disk' => 'public'
]),
];
$type = 'attachment';
}

$message = $conversation->messages()->create([
'user_id'=>$user->id,
'body'=>$request->post('message')
'user_id' => $user->id,
'type' => $type,
'body' => $message,
]);

DB::statement('
INSERT INTO recipients (user_id,message_id)
SELECT user_id ,? FROM participants
where conversation_id= ?
',
[
$message->id,
$conversation->id
]);
INSERT INTO recipients (user_id, message_id)
SELECT user_id, ? FROM participants
WHERE conversation_id = ?
AND user_id <> ?
', [$message->id, $conversation->id, $user->id]);

$conversation->update([
'last_message_id'=>$message->id,
'last_message_id' => $message->id,
]);

DB::commit();
event(new MessageCreate($message));

$message->load('user');

// broadcast(new MessageCreated($message));

} catch (Throwable $e) {
DB::rollBack();
Expand All @@ -113,24 +162,60 @@ public function store(Request $request)
return $message;
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

public function destroy($id): array
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
Recipient::where([
'user_id'=>Auth::user(),
'message_id'=>$id
])->delete();
return[
'message'=>'deleted'
$user = Auth::user();

$user->sentMessages()
->where('id', '=', $id)
->update([
'deleted_at' => Carbon::now(),
]);

if ($request->target == 'me') {

Recipient::where([
'user_id' => $user->id,
'message_id' => $id,
])->delete();

} else {
Recipient::where([
'message_id' => $id,
])->delete();
}

return [
'message' => 'deleted',
];
}
}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Kernel extends HttpKernel
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
Expand Down
2 changes: 1 addition & 1 deletion artisan
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define('LARAVEL_START', microtime(true));

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$app = require __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 4210af9

Please sign in to comment.