From ae56632c644c7cfec0ab145371fe6e89ee4d5460 Mon Sep 17 00:00:00 2001 From: armored-dragon Date: Thu, 19 Dec 2024 21:48:05 -0600 Subject: [PATCH] Housekeeping: data formatting --- scripts/system/domainChat/domainChat.js | 72 +++++++------------------ scripts/system/domainChat/formatting.js | 64 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 54 deletions(-) create mode 100644 scripts/system/domainChat/formatting.js diff --git a/scripts/system/domainChat/domainChat.js b/scripts/system/domainChat/domainChat.js index 411e6bd278..9428ef51fa 100644 --- a/scripts/system/domainChat/domainChat.js +++ b/scripts/system/domainChat/domainChat.js @@ -12,6 +12,10 @@ (() => { ("use strict"); + Script.include([ + "./formatting.js" + ]) + var appIsVisible = false; var settings = { external_window: false, @@ -94,22 +98,16 @@ // Is the message a chat message? channel = channel.toLowerCase(); if (channel !== "chat") return; - message = JSON.parse(message); - - // Get the message data - const currentTimestamp = _getTimestamp(); - const timeArray = _formatTimestamp(currentTimestamp); - + + if ((message = formatting.toJSON(message)) == null) return; // Make sure we are working with a JSON object we expect, otherwise kill + message = formatting.addTimeAndDateStringToPacket(message); + if (!message.channel) message.channel = "domain"; // We don't know where to put this message. Assume it is a domain wide message. message.channel = message.channel.toLowerCase(); // Only recognize channel names as lower case. if (!channels.includes(message.channel)) return; // Check the channel. If the channel is not one we have, do nothing. if (message.channel == "local" && isTooFar(message.position)) return; // If message is local, and if player is too far away from location, do nothing. - - // Format the timestamp - message.timeString = timeArray[0]; - message.dateString = timeArray[1]; - + let formattedMessagePacket = { ...message }; formattedMessagePacket.message = await _parseMessage(message.message) @@ -117,24 +115,16 @@ _notificationCoreMessage(message.displayName, message.message) // Show a new message on screen. // Create a new variable based on the message that will be saved. - let savedMessage = message; + let trimmedPacket = formatting.trimPacketToSave(message); + messageHistory.push(trimmedPacket); - // Remove unnecessary data. - delete savedMessage.position; - delete savedMessage.timeString; - delete savedMessage.dateString; - delete savedMessage.action; - - savedMessage.timestamp = currentTimestamp; - - messageHistory.push(savedMessage); while (messageHistory.length > settings.maximum_messages) { messageHistory.shift(); } Settings.setValue("ArmoredChat-Messages", messageHistory); - // Check to see if the message is close enough to the user function isTooFar(messagePosition) { + // Check to see if the message is close enough to the user return Vec3.distance(MyAvatar.position, messagePosition) > maxLocalDistance; } } @@ -219,10 +209,7 @@ } // Format the packet - let message = {}; - const timeArray = _formatTimestamp(_getTimestamp()); - message.timeString = timeArray[0]; - message.dateString = timeArray[1]; + let message = addTimeAndDateStringToPacket({}); message.message = `${displayName} ${type}`; // Show new message on screen @@ -243,43 +230,20 @@ if (messageHistory) { // Load message history for (message of messageHistory) { - const timeArray = _formatTimestamp(_getTimestamp()); - messagePacket = { ...message }; - messagePacket.timeString = timeArray[0]; - messagePacket.dateString = timeArray[1]; + messagePacket = { ...message }; // Create new variable + messagePacket = formatting.addTimeAndDateStringToPacket(messagePacket); // Add timestamp + messagePacket.message = await _parseMessage(messagePacket.message); // Parse the message for the UI - let formattedMessagePacket = {...messagePacket}; - formattedMessagePacket.message = await _parseMessage(messagePacket.message); - - _emitEvent({ type: "show_message", ...formattedMessagePacket }); + _emitEvent({ type: "show_message", ...messagePacket }); // Send message to UI } } - // Send current settings to the app - _emitEvent({ type: "initial_settings", settings: settings }); + _emitEvent({ type: "initial_settings", settings: settings }); // Send current settings to the app } function _saveSettings() { console.log("Saving config"); Settings.setValue("ArmoredChat-Config", settings); } - function _getTimestamp(){ - return Date.now(); - } - function _formatTimestamp(timestamp){ - let timeArray = []; - - timeArray.push(new Date().toLocaleTimeString(undefined, { - hour12: false, - })); - - timeArray.push(new Date(timestamp).toLocaleDateString(undefined, { - year: "numeric", - month: "long", - day: "numeric", - })); - - return timeArray; - } function _notificationCoreMessage(displayName, message){ Messages.sendLocalMessage( "Floof-Notif", diff --git a/scripts/system/domainChat/formatting.js b/scripts/system/domainChat/formatting.js new file mode 100644 index 0000000000..b523f30d2d --- /dev/null +++ b/scripts/system/domainChat/formatting.js @@ -0,0 +1,64 @@ +// +// formatting.js +// +// Created by Armored Dragon, 2024. +// Copyright 2024 Overte e.V. +// +// This just does some basic formatting and minor housekeeping for the domainChat.js application +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + +const formatting = { + toJSON: function(data) { + if (typeof data == "object") return data; // Already JSON + + try { + const parsedData = JSON.parse(data); + return parsedData; + } catch (e) { + console.log('Failed to convert data to JSON.') + return null; // Could not convert to json, some error; + } + }, + addTimeAndDateStringToPacket: function(packet) { + // Gets the current time and adds it to a given packet + const timeArray = formatting.helpers._timestampArray(packet.timestamp); + packet.timeString = timeArray[0]; + packet.dateString = timeArray[1]; + return packet; + }, + trimPacketToSave: function(packet) { + // Takes a packet, and returns a packet containing only what is needed to save. + let newPacket = { + channel: packet.channel || "", + displayName: packet.displayName || "", + message: packet.message || "", + timestamp: packet.timestamp || formatting.helpers.getTimestamp(), + }; + return newPacket; + }, + + helpers: { + // Small functions that are used often in the other functions. + _timestampArray: function(timestamp) { + const currentDate = timestamp || formatting.helpers.getTimestamp(); + let timeArray = []; + + timeArray.push(new Date(currentDate).toLocaleTimeString(undefined, { + hour12: false, + })); + + timeArray.push(new Date(currentDate).toLocaleDateString(undefined, { + year: "numeric", + month: "long", + day: "numeric", + })); + + return timeArray; + }, + getTimestamp: function(){ + return Date.now(); + } + } +}