Change to timestamp instead of saving timestrings.

This commit is contained in:
armored-dragon 2024-11-16 18:47:33 -06:00
parent ad5b6d0db7
commit 83373d0efb
No known key found for this signature in database
GPG key ID: C7207ACC3382AD8B
2 changed files with 54 additions and 24 deletions

View file

@ -97,20 +97,26 @@
if (channel !== "chat") return; if (channel !== "chat") return;
message = JSON.parse(message); message = JSON.parse(message);
// Get the message data
const currentTimestamp = _getTimestamp();
const timeArray = _formatTimestamp(currentTimestamp);
if (!message.channel) message.channel = "domain"; // We don't know where to put this message. Assume it is a domain wide message. if (!message.channel) message.channel = "domain"; // We don't know where to put this message. Assume it is a domain wide message.
if (message.forApp) return; // Floofchat if (message.forApp) return; // Floofchat
// Floofchat compatibility hook // Floofchat compatibility hook
message = floofChatCompatibilityConversion(message); message = floofChatCompatibilityConversion(message);
message.channel = message.channel.toLowerCase(); // Make sure the "local", "domain", etc. is formatted consistently message.channel = message.channel.toLowerCase();
if (!channels.includes(message.channel)) return; // Check the channel // Check the channel. If the channel is not one we have, do nothing.
if ( if (!channels.includes(message.channel)) return;
message.channel == "local" &&
Vec3.distance(MyAvatar.position, message.position) > // If message is local, and if player is too far away from location, do nothing.
maxLocalDistance if (message.channel == "local" && isCloseEnough(message.position)) return;
)
return; // If message is local, and if player is too far away from location, don't do anything // Format the timestamp
message.timeString = timeArray[0];
message.dateString = timeArray[1];
// Update qml view of to new message // Update qml view of to new message
_emitEvent({ type: "show_message", ...message }); _emitEvent({ type: "show_message", ...message });
@ -125,20 +131,25 @@
// Save message to history // Save message to history
let savedMessage = message; let savedMessage = message;
// Remove unnecessary data.
delete savedMessage.position; delete savedMessage.position;
savedMessage.timeString = new Date().toLocaleTimeString(undefined, { delete savedMessage.timeString;
hour12: false, delete savedMessage.dateString;
}); delete savedMessage.action;
savedMessage.dateString = new Date().toLocaleDateString(undefined, {
year: "numeric", savedMessage.timestamp = currentTimestamp;
month: "long",
day: "numeric",
});
messageHistory.push(savedMessage); messageHistory.push(savedMessage);
while (messageHistory.length > settings.maximum_messages) { while (messageHistory.length > settings.maximum_messages) {
messageHistory.shift(); messageHistory.shift();
} }
Settings.setValue("ArmoredChat-Messages", messageHistory); Settings.setValue("ArmoredChat-Messages", messageHistory);
// Check to see if the message is close enough to the user
function isCloseEnough(messagePosition) {
return Vec3.distance(MyAvatar.position, messagePosition) > maxLocalDistance;
}
} }
function fromQML(event) { function fromQML(event) {
switch (event.type) { switch (event.type) {
@ -214,9 +225,7 @@
// Get the display name of the user // Get the display name of the user
let displayName = ""; let displayName = "";
displayName = displayName = AvatarManager.getPalData([sessionId])?.data[0]?.sessionDisplayName || null;
AvatarManager.getPalData([sessionId])?.data[0]
?.sessionDisplayName || null;
if (displayName == null) { if (displayName == null) {
for (let i = 0; i < palData.length; i++) { for (let i = 0; i < palData.length; i++) {
if (palData[i].sessionUUID == sessionId) { if (palData[i].sessionUUID == sessionId) {
@ -227,6 +236,9 @@
// Format the packet // Format the packet
let message = {}; let message = {};
const timeArray = _formatTimestamp(_getTimestamp());
message.timeString = timeArray[0];
message.dateString = timeArray[1];
message.message = `${displayName} ${type}`; message.message = `${displayName} ${type}`;
_emitEvent({ type: "notification", ...message }); _emitEvent({ type: "notification", ...message });
@ -238,7 +250,9 @@
if (messageHistory) { if (messageHistory) {
// Load message history // Load message history
messageHistory.forEach((message) => { messageHistory.forEach((message) => {
delete message.action; const timeArray = _formatTimestamp(_getTimestamp());
message.timeString = timeArray[0];
message.dateString = timeArray[1];
_emitEvent({ type: "show_message", ...message }); _emitEvent({ type: "show_message", ...message });
}); });
} }
@ -250,6 +264,24 @@
console.log("Saving config"); console.log("Saving config");
Settings.setValue("ArmoredChat-Config", settings); 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;
}
/** /**
* Emit a packet to the HTML front end. Easy communication! * Emit a packet to the HTML front end. Easy communication!

View file

@ -562,15 +562,13 @@ Rectangle {
// Messages from script // Messages from script
function fromScript(message) { function fromScript(message) {
let time = new Date().toLocaleTimeString(undefined, { hour12: false });
let date = new Date().toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric", });
switch (message.type){ switch (message.type){
case "show_message": case "show_message":
addMessage(message.displayName, message.message, `[ ${message.timeString || time} - ${message.dateString || date} ]`, message.channel, "chat"); addMessage(message.displayName, message.message, `[ ${message.timeString} - ${message.dateString} ]`, message.channel, "chat");
break; break;
case "notification": case "notification":
addMessage("SYSTEM", message.message, `[ ${time} - ${date} ]`, "domain", "notification"); addMessage("SYSTEM", message.message, `[ ${message.timeString} - ${message.dateString} ]`, "domain", "notification");
break; break;
case "clear_messages": case "clear_messages":
local.clear(); local.clear();