From 6aba097c853cc176d03bd93a462bd0a515c9732f Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 7 Jan 2021 22:40:05 +0000 Subject: [PATCH] Add support for sending chat messages --- src/api.ts | 39 ++++++++++++++++ src/components/ChatBox.vue | 91 ++++++++++++++++++++++++++++++++++++- src/errors/ChatError.ts | 22 +++++++++ src/scss/_placeholders.scss | 1 + src/scss/style.scss | 84 +++++++++------------------------- src/store/action-types.ts | 1 + src/store/actions.ts | 8 ++++ src/store/state.ts | 4 ++ 8 files changed, 186 insertions(+), 64 deletions(-) create mode 100644 src/errors/ChatError.ts diff --git a/src/api.ts b/src/api.ts index 156d77e..425f3a9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -35,6 +35,7 @@ import { DynmapWorldMap } from "@/dynmap"; import {useStore} from "@/store"; +import ChatError from "@/errors/ChatError"; function buildServerConfig(response: any): DynmapServerConfig { return { @@ -669,5 +670,43 @@ export default { return sets; }); + }, + + sendChatMessage(message: string) { + const store = useStore(); + + if(!store.state.components.chatSending) { + return Promise.reject(new ChatError("Chat is not enabled")); + } + + return fetch(window.config.url.sendmessage, { + method: 'POST', + body: JSON.stringify({ + name: null, + message: message, + }) + }).then((response) => { + if(response.status === 403) { //Rate limited + throw new ChatError(store.state.messages.chatCooldown + .replace('%interval%', store.state.components.chatSending!.cooldown.toString())); + } + + if (!response.ok) { + throw new Error('Network request failed'); + } + + return response.json(); + }).then(response => { + if (response.error !== 'none') { + throw new ChatError(store.state.messages.chatNotAllowed); + } + }).catch(e => { + if(!(e instanceof ChatError)) { + console.error('Unexpected error while sending chat message'); + console.trace(e); + } + + throw e; + }); } } diff --git a/src/components/ChatBox.vue b/src/components/ChatBox.vue index f8c4411..78d72ff 100644 --- a/src/components/ChatBox.vue +++ b/src/components/ChatBox.vue @@ -20,13 +20,21 @@
  • No chat messages yet...
  • +
    +
    {{ sendingError }}
    + + +