From baef7baa0d4b725235b31ffd15cdf08a98b64708 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Sun, 11 May 2025 00:33:08 -0600 Subject: Message timestamps --- index.js | 26 +++++++++++++++----------- static/index.html | 2 +- static/style.css | 52 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index dae7762..d5de68d 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const {promises: {readFile}, readFileSync, createReadStream, createWriteStream} const {createServer} = require('node:http') const {createHash, randomBytes} = require('node:crypto') const Readline = require('node:readline'); -const { stdin, stdout } = require('node:process'); +const {stdin, stdout} = require('node:process'); const {EventEmitter} = require('node:events') const readline = Readline.createInterface({ @@ -176,7 +176,7 @@ async function handleGetPage(req, res) { currentUsers[userId] = nonce setTimeout(() => { if (currentUsers[userId] === nonce) { - chatEvents.emit('event', {type: 'part', userId, args: {nick}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'part', userId, args: {nick}}) delete currentUsers[userId] } }, 3000) @@ -187,7 +187,7 @@ async function handleGetPage(req, res) { res.on('finish', onDisconnect) if (!(userId in currentUsers)) { - chatEvents.emit('event', {type: 'join', userId, args: {nick}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'join', userId, args: {nick}}) } currentUsers[userId] = true } @@ -219,7 +219,7 @@ async function handleMessagePost(req, res) { const messageId = randomBytes(20).toString('hex').slice(0, 7) if (content) - chatEvents.emit('event', {type: 'message', userId, args: {messageId, content, nick}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'message', userId, args: {messageId, content, nick}}) res.writeHead(303, {location: '/'}).end() } @@ -231,7 +231,7 @@ async function handleDeletePost(req, res) { const userId = getUserId(pass) if (messageId) - chatEvents.emit('event', {type: 'delete', userId, args: {messageId}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'delete', userId, args: {messageId}}) res.writeHead(303, {location: '/'}).end() } @@ -244,7 +244,7 @@ async function handleEditPost(req, res) { const userId = getUserId(pass) if (messageId && content) - chatEvents.emit('event', {type: 'edit', userId, args: {messageId, content}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'edit', userId, args: {messageId, content}}) res.writeHead(303, {location: '/'}).end() } @@ -262,16 +262,17 @@ async function handleSavePost(req, res) { writeCookie(res, 'semi-pass', newPass) if ((oldNick && oldNick !== newNick)) - chatEvents.emit('event', {type: 'nickChange', userId: oldUserId, args: {oldNick, newNick, oldUserId, newUserId}}) + chatEvents.emit('event', {timestamp: Date.now(), type: 'nickChange', userId: oldUserId, args: {oldNick, newNick, oldUserId, newUserId}}) res.writeHead(303, {location: '/'}).end() } -function renderEvent({type, userId: eventUserId, args}, {userId: currentUserId, live}) { +function renderEvent({type, timestamp, userId: eventUserId, args}, {userId: currentUserId, live}) { switch (type) { case 'message': return `
+ ${timestamp ? `${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}`: ''} ${sanitizeText(args.nick)} ${sanitizeText(args.content)} ${(currentUserId === eventUserId) ? `
@@ -327,19 +328,22 @@ function renderEvent({type, userId: eventUserId, args}, {userId: currentUserId, case 'nickChange': return `
- (${sanitizeText(args.oldNick)} changed name to ${sanitizeText(args.newNick)}) + ${timestamp ? `${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}`: ''} + (${sanitizeText(args.oldNick)} changed name to ${sanitizeText(args.newNick)})
` case 'join': return `
- (${sanitizeText(args.nick)} joined the chat) + ${timestamp ? `${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}`: ''} + (${sanitizeText(args.nick)} joined the chat)
` case 'part': return `
- (${sanitizeText(args.nick)} left the chat) + ${timestamp ? `${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}`: ''} + (${sanitizeText(args.nick)} left the chat)
` } } diff --git a/static/index.html b/static/index.html index b411bb2..77718dc 100644 --- a/static/index.html +++ b/static/index.html @@ -16,7 +16,7 @@