forked from eatvio/eatvio-infra
139 lines
4.1 KiB
PHP
139 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Events\ChatMessageUpdated;
|
|
use App\Models\Chat;
|
|
use App\Models\User;
|
|
use Illuminate\View\View;
|
|
|
|
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
|
|
{
|
|
public function index(): View
|
|
{
|
|
// $_ = "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->count() ? $lastChatMessage['from_user_id'] : null;
|
|
|
|
if (! is_null($lastChatRecipientUserId) && $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),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
ChatMessageUpdated::dispatch(new ChatMessage(time(), 'some text '.md5(time())));
|
|
}
|
|
|
|
public function show(int $lastChatUser): View
|
|
{
|
|
$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),
|
|
]
|
|
);
|
|
}
|
|
|
|
private function makeChanName($from, $to)
|
|
{
|
|
return (int) $from < (int) $to
|
|
? $from.'_'.$to
|
|
: $to.'_'.$from;
|
|
}
|
|
}
|