forked from eatvio/eatvio-infra
same at 22.05.2023
This commit is contained in:
197
app/Http/Controllers/ChatController.php
Normal file
197
app/Http/Controllers/ChatController.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ChatMessageUpdated;
|
||||
use App\Models\Chat;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Faker\Factory as FakerFaktory;
|
||||
|
||||
class ChatMessage
|
||||
{
|
||||
|
||||
public $id;
|
||||
public $payload;
|
||||
|
||||
public function __construct($id, $payload)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public function someRandomFillFunction()
|
||||
{
|
||||
|
||||
$users1 = User::all()->pluck('id');
|
||||
$users2 = User::all()->pluck('id');
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$users1->each(
|
||||
function ($user1, $key) use ($faker, $users2) {
|
||||
$users2->each(
|
||||
function ($user2) use ($faker, $user1) {
|
||||
if ($user1 == $user2) {
|
||||
return;
|
||||
}
|
||||
$data1 = [
|
||||
'from_user_id' => $user2,
|
||||
'to_user_id' => $user1,
|
||||
'data' => $faker->text()
|
||||
];
|
||||
|
||||
$data2 = [
|
||||
'from_user_id' => $user1,
|
||||
'to_user_id' => $user2,
|
||||
'data' => $faker->text()
|
||||
];
|
||||
|
||||
$newMessage = (new Chat())->forceFill($data1);
|
||||
$newMessage->save();
|
||||
$newMessage = (new Chat())->forceFill($data2);
|
||||
$newMessage->save();
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// $_ = "from_user_id to_user_id";
|
||||
$curUserId = Auth()->user()->id;
|
||||
$lastChatUsers = Chat::select(['from_user_id', 'to_user_id'])
|
||||
->where('from_user_id', $curUserId)
|
||||
->orWhere('to_user_id', $curUserId)
|
||||
->get()
|
||||
->map(fn ($e) => [$e['from_user_id'], $e['to_user_id']])
|
||||
->flatten()
|
||||
->unique()
|
||||
->filter(fn ($e) => $e != $curUserId)
|
||||
->values()
|
||||
->map(fn ($id) => User::find($id));
|
||||
|
||||
$lastChatMessage = Chat::withLastUserMessage($curUserId);
|
||||
$lastChatRecipientUserId = $lastChatMessage['from_user_id'];
|
||||
|
||||
if ($lastChatMessage['from_user_id'] === $curUserId) {
|
||||
$lastChatRecipientUserId = $lastChatMessage['to_user_id'];
|
||||
}
|
||||
|
||||
return view(
|
||||
'chat.index',
|
||||
[
|
||||
'lastChatRecipientUserId' => $lastChatRecipientUserId,
|
||||
'lastChatUsers' => $lastChatUsers,
|
||||
'curUserId' => $curUserId,
|
||||
'chanId' => $this->makeChanName($curUserId, $lastChatRecipientUserId)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function makeChanName($from, $to)
|
||||
{
|
||||
return (int) $from < (int) $to
|
||||
? $from. "_" . $to
|
||||
: $to . "_" . $from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
ChatMessageUpdated::dispatch(new ChatMessage(time(), 'some text ' . md5(time())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\Models\Chat $chat
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(int $lastChatUser)
|
||||
{
|
||||
$lastChatUser = User::find($lastChatUser);
|
||||
// $_ = "from_user_id to_user_id";
|
||||
$curUserId = Auth()->user()->id;
|
||||
|
||||
$lastChatUsers = Chat::select(['from_user_id', 'to_user_id'])
|
||||
->where('from_user_id', $curUserId)
|
||||
->orWhere('to_user_id', $curUserId)
|
||||
->get()
|
||||
->map(fn ($e) => [$e['from_user_id'], $e['to_user_id']])
|
||||
->flatten()
|
||||
->unique()
|
||||
->filter(fn ($e) => $e != $curUserId)
|
||||
->values()
|
||||
->map(fn ($id) => User::find($id));
|
||||
|
||||
return view(
|
||||
'chat.index',
|
||||
[
|
||||
'lastChatRecipientUserId' => $lastChatUser->id,
|
||||
'lastChatUsers' => $lastChatUsers,
|
||||
'curUserId' => $curUserId,
|
||||
'chanId' => $this->makeChanName($curUserId, $lastChatUser->id)
|
||||
]
|
||||
);
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param \App\Models\chat $chat
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(chat $chat)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Models\chat $chat
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, chat $chat)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\Models\chat $chat
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(chat $chat)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user