diff --git a/app/Http/Controllers/API/ChatController.php b/app/Http/Controllers/API/ChatController.php index 806822759..c946b27b0 100644 --- a/app/Http/Controllers/API/ChatController.php +++ b/app/Http/Controllers/API/ChatController.php @@ -40,6 +40,13 @@ class ChatController extends Controller return ChatRoomResource::collection($this->chat->rooms()); } + public function roomLimits($room_id) + { + return response([ + 'max_messages' => config('chat.message_limit') + ], 200); + } + /* MESSAGES */ public function createMessage(Request $request) { diff --git a/app/Repositories/ChatRepository.php b/app/Repositories/ChatRepository.php index d7f111a05..675405da6 100644 --- a/app/Repositories/ChatRepository.php +++ b/app/Repositories/ChatRepository.php @@ -52,11 +52,28 @@ class ChatRepository 'message' => $message ]); + $this->checkMessageLimits($room_id); + broadcast(new MessageSent($message)); return $message; } + public function messages($room_id) { + return $this->message->where('chatroom_id', $room_id)->get(); + } + + public function checkMessageLimits($room_id) + { + $messages = $this->messages($room_id); + $limit = config('chat.message_limit'); + $count = $messages->count(); + + if ($count > $limit) { + $messages->first()->delete(); + } + } + public function systemMessage($message) { $this->message(1, $this->systemChatroom(), $message); diff --git a/config/chat.php b/config/chat.php index 844d2e037..64603bd16 100644 --- a/config/chat.php +++ b/config/chat.php @@ -7,10 +7,16 @@ return [ * * Note: can use the id or name of the chatroom * - * id (integer) example: 3 - * name (string) example: 'System' + * id (integer) example: 1 + * name (string) example: 'General' */ - 'system_chatroom' => 'System', + 'system_chatroom' => 'General', + + /** + * Total Messages to keep in history (per chatroom) + */ + 'message_limit' => 100 + ]; \ No newline at end of file diff --git a/resources/assets/js/components/chat/Chatbox.vue b/resources/assets/js/components/chat/Chatbox.vue index dfbf5d67c..34e9c5db7 100644 --- a/resources/assets/js/components/chat/Chatbox.vue +++ b/resources/assets/js/components/chat/Chatbox.vue @@ -110,6 +110,7 @@ currentRoom: 0, scroll: true, channel: null, + limits: {} } }, watch: { @@ -159,7 +160,11 @@ axios.get('/api/chat/rooms').then(response => { this.chatrooms = response.data.data - this.changeRoom(this.auth.chatroom.id) + axios.get(`/api/chat/rooms/${this.auth.chatroom.id}/limits`).then(response => { + this.limits = response.data + + this.changeRoom(this.auth.chatroom.id) + }) }) }, @@ -200,8 +205,7 @@ /* Add system message */ this.createMessage( - `[url=/${this.auth.username}.${this.auth.id}]${this.auth.username}[/url] has updated their status to [b]${this.auth.chat_status.name}[/b]`, - true + `[url=/${this.auth.username}.${this.auth.id}]${this.auth.username}[/url] has updated their status to [b]${this.auth.chat_status.name}[/b]` ) }) @@ -209,13 +213,18 @@ }, /* User defaults to System user */ - createMessage (message, save = false, user_id = 1) { + createMessage (message, save = true, user_id = 1) { axios.post('/api/chat/messages', { 'user_id': user_id, 'chatroom_id': this.currentRoom, 'message': message, - 'save': save, // if you want to save the system message to the database + 'save': save, + }).then((response) => { + let count = this.chatrooms[this.room_index].messages.length; + if (count > this.limits.max_messages) { + this.chatrooms[this.room_index].messages.splice(0, 1); + } }) }, @@ -251,8 +260,6 @@ this.createMessage(`${user.username} has LEFT the chat ...`) }) .listen('.new.message', e => { - console.log(e) - //push the new message on to the array this.chatrooms[this.room_index].messages.push(e.message) }) } diff --git a/routes/api.php b/routes/api.php index 887bbde31..9db9e4dd3 100755 --- a/routes/api.php +++ b/routes/api.php @@ -31,6 +31,7 @@ Route::namespace('API')->group(function () { /* Rooms */ Route::get('/rooms', 'ChatController@rooms'); + Route::get('/rooms/{room_id}/limits', 'ChatController@roomLimits'); /* Messages */ Route::post('/messages', 'ChatController@createMessage');