From 41bff51cc5a87b169ddb895c744c6a21a12f6eb9 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sat, 29 May 2021 06:38:01 +0000 Subject: [PATCH] fix #160 --- server/room.ts | 28 ++++++++++++++++---- src/components/App/App.tsx | 7 +++++ src/components/Settings/SettingsTab.tsx | 34 +++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/server/room.ts b/server/room.ts index 34eac0911f..addc893987 100644 --- a/server/room.ts +++ b/server/room.ts @@ -126,6 +126,7 @@ export class Room { socket.on('CMD:seek', (data) => this.seekVideo(socket, data)); socket.on('CMD:ts', (data) => this.setTimestamp(socket, data)); socket.on('CMD:chat', (data) => this.sendChatMessage(socket, data)); + socket.on('CMD:clearchat', (data) => this.clearChat(socket, data)); socket.on('CMD:joinVideo', () => this.joinVideo(socket)); socket.on('CMD:leaveVideo', () => this.leaveVideo(socket)); socket.on('CMD:joinScreenShare', (data) => @@ -471,16 +472,33 @@ export class Room { if (data && data.length > 10000) { return; } - if (config.NODE_ENV === 'development' && data === '/clear') { - this.chat.length = 0; - this.io.of(this.roomId).emit('chatinit', this.chat); - return; - } redisCount('chatMessages'); const chatMsg = { id: socket.id, msg: data }; this.addChatMessage(socket, chatMsg); }; + private clearChat = async ( + socket: Socket, + data: { uid: string; token: string } + ) => { + const decoded = await validateUserToken( + data?.uid as string, + data?.token as string + ); + if (!decoded) { + socket.emit('errorMessage', 'Failed to authenticate user'); + return; + } + const isOwner = await this.validateOwner(decoded.uid); + if (!isOwner) { + socket.emit('errorMessage', 'Not current room owner'); + return; + } + this.chat.length = 0; + this.io.of(this.roomId).emit('chatinit', this.chat); + return; + }; + private joinVideo = (socket: Socket) => { const match = this.roster.find((user) => user.id === socket.id); if (match) { diff --git a/src/components/App/App.tsx b/src/components/App/App.tsx index b67afaa16c..80a23d7c33 100644 --- a/src/components/App/App.tsx +++ b/src/components/App/App.tsx @@ -1309,6 +1309,12 @@ export default class App extends React.Component { setIsChatDisabled = (val: boolean) => this.setState({ isChatDisabled: val }); + clearChat = async () => { + const uid = this.props.user?.uid; + const token = await this.props.user?.getIdToken(); + this.socket.emit('CMD:clearchat', { uid, token }); + }; + getLeaderTime = () => { return calculateMedian(Object.values(this.state.tsMap)); }; @@ -1453,6 +1459,7 @@ export default class App extends React.Component { roomLink={this.state.roomLink} password={this.state.password} setPassword={this.setPassword} + clearChat={this.clearChat} /> ); diff --git a/src/components/Settings/SettingsTab.tsx b/src/components/Settings/SettingsTab.tsx index 614a3d2d25..12cf3e19c8 100644 --- a/src/components/Settings/SettingsTab.tsx +++ b/src/components/Settings/SettingsTab.tsx @@ -32,6 +32,7 @@ interface SettingsTabProps { setPassword: Function; isChatDisabled: boolean; setIsChatDisabled: Function; + clearChat: Function; } export const SettingsTab = ({ @@ -49,6 +50,7 @@ export const SettingsTab = ({ setPassword, isChatDisabled, setIsChatDisabled, + clearChat, }: SettingsTabProps) => { const [updateTS, setUpdateTS] = useState(0); const [permModalOpen, setPermModalOpen] = useState(false); @@ -103,7 +105,14 @@ export const SettingsTab = ({ !Boolean(user) || Boolean(owner && owner !== user?.uid); return ( -
+
{permModalOpen && ( setPermModalOpen(false)} @@ -170,6 +179,25 @@ export const SettingsTab = ({ onChange={(_e, data) => setIsChatDisabled(Boolean(data.checked))} /> )} + {owner && owner === user?.uid && ( + clearChat()} + > + + Delete Messages + + } + /> + )} {owner && owner === user?.uid && ( )}
-
{description}
+
+ {description} +
{content}