Skip to content

Commit

Permalink
Merge pull request #570 from HDInnovations/chat-changes
Browse files Browse the repository at this point in the history
(Update) Chat/Shoutbox
  • Loading branch information
HDVinnie authored Feb 11, 2019
2 parents efe6d51 + 0a56068 commit 8d1fb6c
Show file tree
Hide file tree
Showing 10 changed files with 973 additions and 910 deletions.
30 changes: 20 additions & 10 deletions app/Http/Controllers/API/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,18 @@ public function createMessage(Request $request)
return response('error', 401);
}

$bots = Bot::where('active', '=', 1)->orderBy('position', 'asc')->get();
$bot_dirty = 0;
$bots = cache()->get('bots');
if (! $bots || ! is_array($bots) || count($bots) < 1) {
$bots = Bot::where('active', '=', 1)->orderBy('position', 'asc')->get();
$bot_dirty = 1;
}

if ($bot_dirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('bots', $bots, $expiresAt);
}

$which = null;
$target = null;
$runbot = null;
Expand Down Expand Up @@ -185,15 +196,12 @@ public function createMessage(Request $request)
foreach ($bots as $bot) {
if ($message && substr($message, 0, 1 + (strlen($bot->command))) == '/'.$bot->command) {
$which = 'echo';
}
if ($message && substr($message, 0, 1 + (strlen($bot->command))) == '!'.$bot->command) {
} elseif ($message && substr($message, 0, 1 + (strlen($bot->command))) == '!'.$bot->command) {
$which = 'public';
}
if ($message && substr($message, 0, 1 + (strlen($bot->command))) == '@'.$bot->command) {
} elseif ($message && substr($message, 0, 1 + (strlen($bot->command))) == '@'.$bot->command) {
$message = substr($message, 1 + strlen($bot->command), strlen($message));
$which = 'private';
}
if ($message && $receiver_id == 1 && $bot->id == $bot_id) {
} elseif ($message && $receiver_id == 1 && $bot->id == $bot_id) {
if ($message && substr($message, 0, 1 + (strlen($bot->command))) == '/'.$bot->command) {
$message = substr($message, 1 + strlen($bot->command), strlen($message));
}
Expand Down Expand Up @@ -326,17 +334,19 @@ public function createMessage(Request $request)
event(new Chatter('audible', $receiver_id, UserAudibleResource::collection($receiver_audibles)));
}

$ignore = false;
$room_id = 0;
if ($bot_id > 0) {
if ($bot_id > 0 && receiver_id == 1) {
$ignore = true;
} else {
$ignore = null;
}
$save = true;
$echo = true;
$message = $this->chat->privateMessage($user_id, $room_id, $message, $receiver_id, null, $ignore);
} else {
$receiver_id = null;
$bot_id = null;
$message = $this->chat->message($user_id, $room_id, $message, $receiver_id, $bot_id);
$this->chat->ping('room', $room_id);
}

if (! $save) {
Expand Down
40 changes: 27 additions & 13 deletions app/Repositories/ChatRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function echoes($user_id)
])->where(function ($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
})
->latest()
->orderBy('id', 'asc')
->get();
}

Expand Down Expand Up @@ -185,19 +185,30 @@ public function botMessage($bot_id, $room_id, $message, $receiver = null)
$message->delete();
}

public function privateMessage($user_id, $room_id, $message, $receiver = null, $bot = null, $ignore = false)
public function privateMessage($user_id, $room_id, $message, $receiver = null, $bot = null, $ignore = null)
{
if ($this->user->find($user_id)->censor) {
$message = $this->censorMessage($message);
}
$message = $this->htmlifyMessage($message);
$save = $this->message->create([
'user_id' => $user_id,
'chatroom_id' => 0,
'message' => $message,
'receiver_id' => $receiver,
'bot_id' => $bot,
]);

if ($bot != null) {
$save = $this->message->create([
'user_id' => $user_id,
'chatroom_id' => 0,
'message' => $message,
'receiver_id' => $receiver,
'bot_id' => $bot,
]);
} else {
$save = $this->message->create([
'user_id' => $user_id,
'chatroom_id' => 0,
'message' => $message,
'receiver_id' => $receiver,
'bot_id' => $bot,
]);
}

$message = Message::with([
'bot',
Expand All @@ -207,11 +218,14 @@ public function privateMessage($user_id, $room_id, $message, $receiver = null, $
'receiver.chatStatus',
])->find($save->id);

if ($ignore != true) {
if ($ignore != null) {
event(new Chatter('new.message', $user_id, new ChatMessageResource($message)));
}
event(new Chatter('new.message', $receiver, new ChatMessageResource($message)));
event(new Chatter('new.ping', $receiver, ['type' => 'target', 'id' => $user_id]));

if ($receiver != 1) {
event(new Chatter('new.ping', $receiver, ['type' => 'target', 'id' => $user_id]));
}

return $message;
}
Expand Down Expand Up @@ -252,8 +266,8 @@ public function botMessages($sender_id, $bot_id)
'receiver.group',
'receiver.chatStatus',
])->where(function ($query) use ($sender_id,$bot_id) {
$query->whereRaw('(user_id = ? and bot_id = ?)', [$sender_id, $bot_id])->orWhereRaw('(receiver_id = ? and bot_id = ?)', [$sender_id, $bot_id]);
})
$query->whereRaw('(user_id = ? and receiver_id = ?)', [$sender_id, 1])->orWhereRaw('(user_id = ? and receiver_id = ?)', [1, $sender_id]);
})->where('bot_id', '=', $bot_id)
->orderBy('id', 'desc')
->limit(config('chat.message_limit'))
->get();
Expand Down
Loading

0 comments on commit 8d1fb6c

Please sign in to comment.