refactor: simplify echo/audible creation logic

Refactor the code to be more idiomatic code.
This commit is contained in:
Roardom
2024-05-01 07:38:24 +00:00
parent d59ef3ba8d
commit 7a71cc7935
6 changed files with 132 additions and 269 deletions
+35 -57
View File
@@ -282,70 +282,48 @@ class NerdBot
$message = $this->message;
if ($type === 'message' || $type === 'private') {
$receiverDirty = false;
$receiverEchoes = cache()->get('user-echoes'.$target->id);
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$target->id,
3600,
fn () => UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);
if (!$receiverEchoes || !\is_array($receiverEchoes)) {
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $this->bot->id)) {
UserEcho::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
]);
$echoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
cache()->put('user-echoes'.$target->id, $echoes, 3600);
Chatter::dispatch('echo', $target->id, UserEchoResource::collection($echoes));
}
$receiverListening = false;
// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$target->id,
3600,
fn () => UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);
foreach ($receiverEchoes as $receiverEcho) {
if ($receiverEcho->bot_id === $this->bot->id) {
$receiverListening = true;
if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $this->bot->id)) {
UserAudible::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
'status' => false,
]);
break;
}
}
if (!$receiverListening) {
$receiverPort = new UserEcho();
$receiverPort->user_id = $target->id;
$receiverPort->bot_id = $this->bot->id;
$receiverPort->save();
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
$receiverDirty = true;
}
if ($receiverDirty) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$target->id, $receiverEchoes, $expiresAt);
event(new Chatter('echo', $target->id, UserEchoResource::collection($receiverEchoes)));
}
$receiverDirty = false;
$receiverAudibles = cache()->get('user-audibles'.$target->id);
if (!$receiverAudibles || !\is_array($receiverAudibles)) {
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
}
$receiverListening = false;
foreach ($receiverAudibles as $receiverEcho) {
if ($receiverEcho->bot_id === $this->bot->id) {
$receiverListening = true;
break;
}
}
if (!$receiverListening) {
$receiverPort = new UserAudible();
$receiverPort->user_id = $target->id;
$receiverPort->bot_id = $this->bot->id;
$receiverPort->save();
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
$receiverDirty = true;
}
if ($receiverDirty) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$target->id, $receiverAudibles, $expiresAt);
event(new Chatter('audible', $target->id, UserAudibleResource::collection($receiverAudibles)));
$audibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
cache()->put('user-audibles'.$target->id, $audibles, 3600);
Chatter::dispatch('audible', $target->id, UserAudibleResource::collection($audibles));
}
// Create message
if ($txt !== '') {
$roomId = 0;
$this->chatRepository->privateMessage($target->id, $roomId, $message, 1, $this->bot->id);
+35 -58
View File
@@ -23,7 +23,6 @@ use App\Models\UserAudible;
use App\Models\UserEcho;
use App\Notifications\NewBon;
use App\Repositories\ChatRepository;
use Illuminate\Support\Carbon;
class SystemBot
{
@@ -178,70 +177,48 @@ class SystemBot
$message = $this->message;
if ($type === 'message' || $type === 'private') {
$receiverDirty = false;
$receiverEchoes = cache()->get('user-echoes'.$target->id);
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$target->id,
3600,
fn () => UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);
if (!$receiverEchoes || !\is_array($receiverEchoes)) {
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $this->bot->id)) {
UserEcho::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
]);
$echoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
cache()->put('user-echoes'.$target->id, $echoes, 3600);
Chatter::dispatch('echo', $target->id, UserEchoResource::collection($echoes));
}
$receiverListening = false;
// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$target->id,
3600,
fn () => UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);
foreach ($receiverEchoes as $receiverEcho) {
if ($receiverEcho->bot_id === $this->bot->id) {
$receiverListening = true;
if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $this->bot->id)) {
UserAudible::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
'status' => false,
]);
break;
}
}
if (!$receiverListening) {
$receiverPort = new UserEcho();
$receiverPort->user_id = $target->id;
$receiverPort->bot_id = $this->bot->id;
$receiverPort->save();
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
$receiverDirty = true;
}
if ($receiverDirty) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$target->id, $receiverEchoes, $expiresAt);
event(new Chatter('echo', $target->id, UserEchoResource::collection($receiverEchoes)));
}
$receiverDirty = false;
$receiverAudibles = cache()->get('user-audibles'.$target->id);
if (!$receiverAudibles || !\is_array($receiverAudibles)) {
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
}
$receiverListening = false;
foreach ($receiverAudibles as $se => $receiverEcho) {
if ($receiverEcho->bot_id === $this->bot->id) {
$receiverListening = true;
break;
}
}
if (!$receiverListening) {
$receiverPort = new UserAudible();
$receiverPort->user_id = $target->id;
$receiverPort->bot_id = $this->bot->id;
$receiverPort->save();
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
$receiverDirty = true;
}
if ($receiverDirty) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$target->id, $receiverAudibles, $expiresAt);
event(new Chatter('audible', $target->id, UserAudibleResource::collection($receiverAudibles)));
$audibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();
cache()->put('user-audibles'.$target->id, $audibles, 3600);
Chatter::dispatch('audible', $target->id, UserAudibleResource::collection($audibles));
}
// Create message
if ($txt !== '') {
$roomId = 0;
$this->chatRepository->privateMessage($target->id, $roomId, $message, 1, $this->bot->id);
+48 -144
View File
@@ -148,18 +148,7 @@ class ChatController extends Controller
return response('error', 401);
}
$botDirty = 0;
$bots = cache()->get('bots');
if (!$bots || !\is_array($bots) || \count($bots) < 1) {
$bots = Bot::where('active', '=', 1)->oldest('position')->get();
$botDirty = 1;
}
if ($botDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('bots', $bots, $expiresAt);
}
$bots = cache()->remember('bots', 3600, fn () => Bot::where('active', '=', 1)->orderByDesc('position')->get());
$which = null;
$target = null;
@@ -239,126 +228,51 @@ class ChatController extends Controller
$echo = false;
if ($receiverId && $receiverId > 0) {
$senderDirty = 0;
$receiverDirty = 0;
$senderEchoes = cache()->get('user-echoes'.$userId);
$receiverEchoes = cache()->get('user-echoes'.$receiverId);
// Create echo for both users if missing
foreach ([[$userId, $receiverId], [$receiverId, $userId]] as [$user1Id, $user2Id]) {
$echoes = cache()->remember(
'user-echoes'.$user1Id,
3600,
fn () => UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get()
);
if (!$senderEchoes || !\is_array($senderEchoes) || \count($senderEchoes) < 1) {
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', $userId)->get();
}
if ($echoes->doesntContain(fn ($echo) => $echo->target_id == $user2Id)) {
UserEcho::create([
'user_id' => $user1Id,
'target_id' => $user2Id,
]);
if (!$receiverEchoes || !\is_array($receiverEchoes) || \count($receiverEchoes) < 1) {
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$receiverId])->get();
}
$echoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get();
$senderListening = false;
cache()->put('user-echoes'.$user1Id, $echoes, 3600);
foreach ($senderEchoes as $senderEcho) {
if ($senderEcho['target_id'] == $receiverId) {
$senderListening = true;
Chatter::dispatch('echo', $user1Id, UserEchoResource::collection($echoes));
}
}
if (!$senderListening) {
$senderPort = new UserEcho();
$senderPort->user_id = $userId;
$senderPort->target_id = $receiverId;
$senderPort->save();
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', $userId)->get();
$senderDirty = 1;
}
// Create audible for both users if missing
foreach ([[$userId, $receiverId], [$receiverId, $userId]] as [$user1Id, $user2Id]) {
$audibles = cache()->remember(
'user-audibles'.$user1Id,
3600,
fn () => UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get()
);
$receiverListening = false;
if ($audibles->doesntContain(fn ($audible) => $audible->target_id == $user2Id)) {
UserAudible::create([
'user_id' => $user1Id,
'target_id' => $user2Id,
'status' => false,
]);
foreach ($receiverEchoes as $receiverEcho) {
if ($receiverEcho['target_id'] == $userId) {
$receiverListening = true;
$audibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get();
cache()->put('user-audibles'.$user1Id, $audibles, 3600);
Chatter::dispatch('audible', $user1Id, UserAudibleResource::collection($audibles));
}
}
if (!$receiverListening) {
$receiverPort = new UserEcho();
$receiverPort->user_id = $receiverId;
$receiverPort->target_id = $userId;
$receiverPort->save();
$receiverEchoes = UserEcho::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$receiverId])->get();
$receiverDirty = 1;
}
if ($senderDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$userId, $senderEchoes, $expiresAt);
event(new Chatter('echo', $userId, UserEchoResource::collection($senderEchoes)));
}
if ($receiverDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$receiverId, $receiverEchoes, $expiresAt);
event(new Chatter('echo', $receiverId, UserEchoResource::collection($receiverEchoes)));
}
$senderDirty = 0;
$receiverDirty = 0;
$senderAudibles = cache()->get('user-audibles'.$userId);
$receiverAudibles = cache()->get('user-audibles'.$receiverId);
if (!$senderAudibles || !\is_array($senderAudibles) || \count($senderAudibles) < 1) {
$senderAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', $userId)->get();
}
if (!$receiverAudibles || !\is_array($receiverAudibles) || \count($receiverAudibles) < 1) {
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$receiverId])->get();
}
$senderListening = false;
foreach ($senderAudibles as $senderEcho) {
if ($senderEcho['target_id'] == $receiverId) {
$senderListening = true;
}
}
if (!$senderListening) {
$senderPort = new UserAudible();
$senderPort->user_id = $userId;
$senderPort->target_id = $receiverId;
$senderPort->status = false;
$senderPort->save();
$senderAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', $userId)->get();
$senderDirty = 1;
}
$receiverListening = false;
foreach ($receiverAudibles as $receiverEcho) {
if ($receiverEcho['target_id'] == $userId) {
$receiverListening = true;
}
}
if (!$receiverListening) {
$receiverPort = new UserAudible();
$receiverPort->user_id = $receiverId;
$receiverPort->target_id = $userId;
$receiverPort->status = false;
$receiverPort->save();
$receiverAudibles = UserAudible::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$receiverId])->get();
$receiverDirty = 1;
}
if ($senderDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$userId, $senderAudibles, $expiresAt);
event(new Chatter('audible', $userId, UserAudibleResource::collection($senderAudibles)));
}
if ($receiverDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$receiverId, $receiverAudibles, $expiresAt);
event(new Chatter('audible', $receiverId, UserAudibleResource::collection($receiverAudibles)));
}
$roomId = 0;
$ignore = $botId > 0 && $receiverId == 1 ? true : null;
$save = true;
@@ -528,34 +442,24 @@ class ChatController extends Controller
$user->save();
$senderDirty = 0;
$senderEchoes = cache()->get('user-echoes'.$user->id);
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$user->id,
3600,
fn () => UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user->id)->get(),
);
if (!$senderEchoes || !\is_array($senderEchoes) || \count($senderEchoes) < 1) {
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user->id)->get();
}
if ($echoes->doesntContain(fn ($echo) => $echo->room_id == $room->id)) {
UserEcho::create([
'user_id' => $user->id,
'room_id' => $room->id,
]);
$senderListening = false;
$echoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user->id)->get();
foreach ($senderEchoes as $senderEcho) {
if ($senderEcho['room_id'] == $room->id) {
$senderListening = true;
}
}
cache()->put('user-echoes'.$user->id, $echoes, 3600);
if (!$senderListening) {
$userEcho = new UserEcho();
$userEcho->user_id = $user->id;
$userEcho->room_id = $room->id;
$userEcho->save();
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $user->id)->get();
$senderDirty = 1;
}
if ($senderDirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$user->id, $senderEchoes, $expiresAt);
event(new Chatter('echo', $user->id, UserEchoResource::collection($senderEchoes)));
Chatter::dispatch('echo', $user->id, UserEchoResource::collection($echoes));
}
return response($user);
+7
View File
@@ -32,6 +32,13 @@ class UserAudible extends Model
{
use HasFactory;
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];
/**
* Belongs To A User.
*
+7
View File
@@ -31,6 +31,13 @@ class UserEcho extends Model
{
use HasFactory;
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];
/**
* Belongs To A User.
*
-10
View File
@@ -520,11 +520,6 @@ parameters:
count: 2
path: app/Http/Controllers/API/ChatController.php
-
message: "#^Comparison operation \"\\<\" between int\\<1, max\\> and 1 is always false\\.$#"
count: 6
path: app/Http/Controllers/API/ChatController.php
-
message: "#^Method App\\\\Http\\\\Controllers\\\\API\\\\ChatController\\:\\:botMessages\\(\\) has parameter \\$botId with no type specified\\.$#"
count: 1
@@ -555,11 +550,6 @@ parameters:
count: 3
path: app/Http/Controllers/API/ChatController.php
-
message: "#^Property App\\\\Models\\\\UserAudible\\:\\:\\$status \\(int\\) does not accept false\\.$#"
count: 2
path: app/Http/Controllers/API/ChatController.php
-
message: "#^Property App\\\\Models\\\\UserAudible\\:\\:\\$status \\(int\\) does not accept true\\.$#"
count: 1