(Update)[Chat 2.0] Max Message Limits

Now you are able to configure the max messages allowed in each room.
This commit is contained in:
Poppabear
2018-05-08 22:24:20 -04:00
parent 0fd8f5a9ef
commit 40ef5f6738
5 changed files with 48 additions and 10 deletions
@@ -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)
{
+17
View File
@@ -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);
+9 -3
View File
@@ -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
];
@@ -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)
})
}
+1
View File
@@ -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');