Chat Channels part 2

This commit is contained in:
HF 2020-04-29 18:30:06 +02:00
parent c979df911f
commit c051dc9ac5
5 changed files with 40 additions and 21 deletions

View File

@ -18,15 +18,17 @@ const Chat = ({ chatMessages, chatChannel }) => {
initialScroll: Infinity, initialScroll: Infinity,
}); });
const channelMessages = chatMessages[chatChannel];
useLayoutEffect(() => { useLayoutEffect(() => {
stayScrolled(); stayScrolled();
}, [chatMessages.length]); }, [channelMessages.length]);
return ( return (
<div style={{ height: '100%' }}> <div style={{ height: '100%' }}>
<ul className="chatarea" ref={listRef}> <ul className="chatarea" ref={listRef}>
{ {
chatMessages[chatChannel].map((message) => ( channelMessages.map((message) => (
<p className="chatmsg"> <p className="chatmsg">
{(message[0] === 'info') {(message[0] === 'info')
? <span style={{ color: '#cc0000' }}>{message[1]}</span> ? <span style={{ color: '#cc0000' }}>{message[1]}</span>

View File

@ -47,22 +47,32 @@ class ChatInput extends React.Component {
if (name) { if (name) {
return ( return (
<div className="chatinput"> <div className="chatinput">
<form onSubmit={(e) => { this.handleSubmit(e, chatChannel); }}> <form
onSubmit={(e) => { this.handleSubmit(e, chatChannel); }}
style={{ display: 'inline' }}
>
<input <input
style={{ maxWidth: '80%', width: '240px' }} style={{ maxWidth: '58%', width: '240px' }}
value={message} value={message}
onChange={(evt) => this.setState({ message: evt.target.value })} onChange={(evt) => this.setState({ message: evt.target.value })}
type="text" type="text"
placeholder="Chat here" placeholder="Chat here"
/> />
<button id="chatmsginput" type="submit">Send</button> <button
id="chatmsginput"
type="submit"
style={{ height: 22 }}
>
</button>
</form> </form>
<select <select
onChange={(evt) => setChannel(evt.target.selectedIndex)} onChange={(evt) => setChannel(evt.target.selectedIndex)}
style={{ height: 22 }}
> >
{ {
CHAT_CHANNELS.map((ch) => ( CHAT_CHANNELS.map((ch) => (
<option selected={ch === selectedChannel}>ch</option> <option selected={ch === selectedChannel}>{ch}</option>
)) ))
} }
</select> </select>

View File

@ -6,6 +6,8 @@ import redis from '../data/redis';
import User from '../data/models/User'; import User from '../data/models/User';
import webSockets from '../socket/websockets'; import webSockets from '../socket/websockets';
import { CHAT_CHANNELS } from '../core/constants';
class ChatProvider { class ChatProvider {
/* /*
@ -16,12 +18,19 @@ class ChatProvider {
constructor() { constructor() {
this.history = []; this.history = [];
for (let i = 0; i < CHAT_CHANNELS.length; i += 1) {
this.history.push([]);
}
this.caseCheck = /^[A-Z !.]*$/; this.caseCheck = /^[A-Z !.]*$/;
this.filters = [ this.filters = [
{ {
regexp: /ADMIN/gi, regexp: /ADMIN/gi,
matches: 2, matches: 2,
}, },
{
regexp: /JEBAĆ/gi,
matches: 2,
},
{ {
regexp: /FUCK/gi, regexp: /FUCK/gi,
matches: 3, matches: 3,
@ -41,10 +50,11 @@ class ChatProvider {
} }
addMessage(name, message, country, channelId = 0) { addMessage(name, message, country, channelId = 0) {
if (this.history.length > 20) { const channelHistory = this.history[channelId];
this.history.shift(); if (channelHistory.length > 20) {
channelHistory.shift();
} }
this.history.push([name, message, country, channelId]); channelHistory.push([name, message, country]);
} }
async sendMessage(user, message, channelId: number = 0) { async sendMessage(user, message, channelId: number = 0) {
@ -53,6 +63,7 @@ class ChatProvider {
? 'il' ? 'il'
: (user.country || 'xx'); : (user.country || 'xx');
if (name.startsWith('popi')) return null;
if (!name) { if (!name) {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
return 'Couldn\'t send your message, pls log out and back in again.'; return 'Couldn\'t send your message, pls log out and back in again.';

View File

@ -45,8 +45,8 @@ const initialState: UserState = {
totalRanking: {}, totalRanking: {},
totalDailyRanking: {}, totalDailyRanking: {},
chatMessages: [ chatMessages: [
['info', 'Welcome to the PixelPlanet Chat', 'il'], [['info', 'Welcome to the PixelPlanet Chat', 'il']],
['info', 'Welcome to the PixelPlanet Chat', 'il'], [['info', 'Welcome to the PixelPlanet Chat', 'il']],
], ],
minecraftname: null, minecraftname: null,
isOnMobile: false, isOnMobile: false,
@ -125,19 +125,15 @@ export default function user(
const { const {
name, text, country, channel, name, text, country, channel,
} = action; } = action;
let { chatMessages } = state; const chatMessages = state.chatMessages.slice();
let channelMessages = chatMessages[channel]; let channelMessages = chatMessages[channel];
if (channelMessages.length > 50) { if (channelMessages.length > 50) {
channelMessages = channelMessages.slice(-50); channelMessages = channelMessages.slice(-50);
} }
channelMessages = channelMessages.concat([ channelMessages = channelMessages.concat([
[name, text, country, channel], [name, text, country]
]); ]);
chatMessages = Object.assign( chatMessages[channel] = channelMessages;
[],
chatMessages,
{ channel: channelMessages },
);
return { return {
...state, ...state,
chatMessages, chatMessages,

View File

@ -223,8 +223,8 @@ class SocketServer extends WebSocketEvents {
} }
static async onTextMessage(text, ws) { static async onTextMessage(text, ws) {
let message; let let message;
channelId; let channelId;
try { try {
const data = JSON.parse(text); const data = JSON.parse(text);
[message, channelId] = data; [message, channelId] = data;
@ -257,7 +257,7 @@ class SocketServer extends WebSocketEvents {
channelId, channelId,
); );
if (errorMsg) { if (errorMsg) {
ws.send(JSON.stringify(['info', errorMsg, 'il'])); ws.send(JSON.stringify(['info', errorMsg, 'il', channelId]));
} }
if (ws.last_message && ws.last_message === message) { if (ws.last_message && ws.last_message === message) {
ws.message_repeat += 1; ws.message_repeat += 1;