Skip to content
Draft
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
56 changes: 25 additions & 31 deletions app/Bots/NerdBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,40 +279,34 @@ public function pm(): true|\Illuminate\Http\Response

if ($type === 'message' || $type === 'private') {
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$target->id,
3600,
fn () => UserEcho::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $this->bot->id)) {
$echoes->push(UserEcho::create([
'user_id' => $target->id,
'bot_id' => $this->bot->id,
]));

cache()->put('user-echoes'.$target->id, $echoes, 3600);

Chatter::dispatch('echo', $target->id, UserEchoResource::collection($echoes));
$affected = UserEcho::query()->upsert([[
'user_id' => $target->id,
'bot_id' => $this->bot->id,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('echo', $target->id, UserEchoResource::collection(
UserEcho::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $target->id)
->get()
));
}

// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$target->id,
3600,
fn () => UserAudible::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $this->bot->id)) {
$audibles->push(UserAudible::create([
'user_id' => $target->id,
'bot_id' => $this->bot->id,
'status' => false,
]));

cache()->put('user-audibles'.$target->id, $audibles, 3600);

Chatter::dispatch('audible', $target->id, UserAudibleResource::collection($audibles));
$affected = UserAudible::query()->upsert([[
'user_id' => $target->id,
'bot_id' => $this->bot->id,
'status' => false,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('audible', $target->id, UserAudibleResource::collection(
UserAudible::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $target->id)
->get()
));
}

// Create message
Expand Down
56 changes: 25 additions & 31 deletions app/Bots/SystemBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,40 +165,34 @@ public function pm(): \Illuminate\Http\Response|true

if ($type === 'message' || $type === 'private') {
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$target->id,
3600,
fn () => UserEcho::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $this->bot->id)) {
$echoes->push(UserEcho::create([
'user_id' => $target->id,
'bot_id' => $this->bot->id,
]));

cache()->put('user-echoes'.$target->id, $echoes, 3600);

Chatter::dispatch('echo', $target->id, UserEchoResource::collection($echoes));
$affected = UserEcho::query()->upsert([[
'user_id' => $target->id,
'bot_id' => $this->bot->id,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('echo', $target->id, UserEchoResource::collection(
UserEcho::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $target->id)
->get()
));
}

// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$target->id,
3600,
fn () => UserAudible::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $this->bot->id)) {
$audibles->push(UserAudible::create([
'user_id' => $target->id,
'bot_id' => $this->bot->id,
'status' => false,
]));

cache()->put('user-audibles'.$target->id, $audibles, 3600);

Chatter::dispatch('audible', $target->id, UserAudibleResource::collection($audibles));
$affected = UserAudible::query()->upsert([[
'user_id' => $target->id,
'bot_id' => $this->bot->id,
'status' => false,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('audible', $target->id, UserAudibleResource::collection(
UserAudible::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $target->id)
->get()
));
}

// Create message
Expand Down
151 changes: 62 additions & 89 deletions app/Http/Controllers/API/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,34 @@
$user = $request->user();

// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$user->id,
3600,
fn () => UserEcho::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user->id)->get()
);

if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $bot->id)) {
$echoes->push(UserEcho::create([
'user_id' => $user->id,
'bot_id' => $bot->id,
]));

cache()->put('user-echoes'.$user->id, $echoes, 3600);

Chatter::dispatch('echo', $user->id, UserEchoResource::collection($echoes));
$affected = UserEcho::query()->upsert([[
'user_id' => $user->id,
'bot_id' => $bot->id,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('echo', $user->id, UserEchoResource::collection(
UserEcho::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $user->id)
->get()
));
}

// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$user->id,
3600,
fn () => UserAudible::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user->id)->get()
);

if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $bot->id)) {
$audibles->push(UserAudible::create([
'user_id' => $user->id,
'bot_id' => $bot->id,
'status' => false,
]));

cache()->put('user-audibles'.$user->id, $audibles, 3600);

Chatter::dispatch('audible', $user->id, UserAudibleResource::collection($audibles));
$affected = UserAudible::query()->upsert([[
'user_id' => $user->id,
'bot_id' => $bot->id,
'status' => false,
]], ['user_id', 'bot_id']);

if ($affected === 1) {
Chatter::dispatch('audible', $user->id, UserAudibleResource::collection(
UserAudible::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $user->id)
->get()
));
}

return ChatMessageResource::collection($this->chatRepository->botMessages($request->user()->id, $bot->id));
Expand Down Expand Up @@ -222,42 +216,36 @@
if ($receiverId && $receiverId > 0) {
// 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(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get()
);

if ($echoes->doesntContain(fn ($echo) => $echo->target_id == $user2Id)) {
$echoes->push(UserEcho::create([
'user_id' => $user1Id,
'target_id' => $user2Id,
]));

cache()->put('user-echoes'.$user1Id, $echoes, 3600);

Chatter::dispatch('echo', $user1Id, UserEchoResource::collection($echoes));
$affected = UserEcho::query()->upsert([[
'user_id' => $user->id,
'target_id' => $bot->id,

Check failure on line 221 in app/Http/Controllers/API/ChatController.php

View workflow job for this annotation

GitHub Actions / php 8.4 on ubuntu-22.04

Variable $bot might not be defined.
]], ['user_id', 'target_id']);

if ($affected === 1) {
Chatter::dispatch('echo', $user->id, UserEchoResource::collection(
UserEcho::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $user->id)
->get()
));
}
}

// 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(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user1Id)->get()
);

if ($audibles->doesntContain(fn ($audible) => $audible->target_id == $user2Id)) {
$audibles->push(UserAudible::create([
'user_id' => $user1Id,
'target_id' => $user2Id,
'status' => true,
]));

cache()->put('user-audibles'.$user1Id, $audibles, 3600);

Chatter::dispatch('audible', $user1Id, UserAudibleResource::collection($audibles));
$affected = UserAudible::query()->upsert([[
'user_id' => $user->id,
'target_id' => $bot->id,

Check failure on line 238 in app/Http/Controllers/API/ChatController.php

View workflow job for this annotation

GitHub Actions / php 8.4 on ubuntu-22.04

Variable $bot might not be defined.
'status' => true,
]], ['user_id', 'target_id']);

if ($affected === 1) {
Chatter::dispatch('audible', $user->id, UserAudibleResource::collection(
UserAudible::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $user->id)
->get()
));
}
}

Expand Down Expand Up @@ -307,8 +295,6 @@

$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$user->id, $senderEchoes, $expiresAt);
event(new Chatter('echo', $user->id, UserEchoResource::collection($senderEchoes)));

/**
Expand All @@ -326,8 +312,6 @@
$user->load(['chatStatus', 'chatroom', 'group', 'echoes']);
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$user->id, $senderEchoes, $expiresAt);
event(new Chatter('echo', $user->id, UserEchoResource::collection($senderEchoes)));

/**
Expand All @@ -345,8 +329,6 @@
$user->load(['chatStatus', 'chatroom', 'group', 'echoes']);
$senderEchoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$user->id, $senderEchoes, $expiresAt);
event(new Chatter('echo', $user->id, UserEchoResource::collection($senderEchoes)));

return response()->json($user);
Expand All @@ -362,8 +344,6 @@
$user->load(['chatStatus', 'chatroom', 'group', 'audibles', 'audibles']);
$senderAudibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$user->id, $senderAudibles, $expiresAt);
event(new Chatter('audible', $user->id, UserAudibleResource::collection($senderAudibles)));

return response()->json($user);
Expand All @@ -379,8 +359,6 @@
$user->load(['chatStatus', 'chatroom', 'group', 'audibles', 'audibles']);
$senderAudibles = UserAudible::with(['target', 'room', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$user->id, $senderAudibles, $expiresAt);
event(new Chatter('audible', $user->id, UserAudibleResource::collection($senderAudibles)));

return response()->json($user);
Expand All @@ -396,8 +374,6 @@
$user->load(['chatStatus', 'chatroom', 'group', 'audibles', 'audibles'])->findOrFail($user->id);
$senderAudibles = UserAudible::with(['bot', 'room', 'bot'])->where('user_id', $user->id)->get();

$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$user->id, $senderAudibles, $expiresAt);
event(new Chatter('audible', $user->id, UserAudibleResource::collection($senderAudibles)));

return response()->json($user);
Expand Down Expand Up @@ -429,21 +405,18 @@
$user->save();

// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$user->id,
3600,
fn () => UserEcho::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user->id)->get(),
);

if ($echoes->doesntContain(fn ($echo) => $echo->room_id == $room->id)) {
$echoes->push(UserEcho::create([
'user_id' => $user->id,
'room_id' => $room->id,
]));

cache()->put('user-echoes'.$user->id, $echoes, 3600);

Chatter::dispatch('echo', $user->id, UserEchoResource::collection($echoes));
$affected = UserEcho::query()->upsert([[
'user_id' => $user->id,
'room_id' => $room->id,
]], ['user_id', 'room_id']);

if ($affected === 1) {
Chatter::dispatch('echo', $user->id, UserEchoResource::collection(
UserEcho::query()
->with(['user', 'room', 'target', 'bot'])
->where('user_id', '=', $user->id)
->get()
));
}

return response()->json($user);
Expand Down
Loading