diff options
author | Ashelyn Rose <git@ashen.earth> | 2025-05-11 00:33:08 -0600 |
---|---|---|
committer | Ashelyn Rose <git@ashen.earth> | 2025-05-11 00:33:08 -0600 |
commit | baef7baa0d4b725235b31ffd15cdf08a98b64708 (patch) | |
tree | 70ccdb6627310a93b974d866333a4eb604373216 /index.js | |
parent | 7bf526e6c44fb95894dae18ee7c2c70074fd1c8d (diff) |
Message timestamps
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 26 |
1 files changed, 15 insertions, 11 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 ` <div class="message${(currentUserId === eventUserId) ? ' currentUser' : ''}" data-user-id="${sanitizeText(eventUserId)}" data-message-id="${sanitizeText(args.messageId)}"> + ${timestamp ? `<span class="time" title="(all times in UTC)">${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}</span>`: ''} <span class="nick" title="${sanitizeText(eventUserId)}">${sanitizeText(args.nick)}</span> <span class="content"><span class="orig">${sanitizeText(args.content)}</span></span> ${(currentUserId === eventUserId) ? `<div class="actions"> @@ -327,19 +328,22 @@ function renderEvent({type, userId: eventUserId, args}, {userId: currentUserId, case 'nickChange': return ` <div class="nickChange"> - (<span class="nick" title="${sanitizeText(args.oldUserId)}">${sanitizeText(args.oldNick)}</span> changed name to <span class="new" title="${sanitizeText(args.newUserId)}">${sanitizeText(args.newNick)}</span>) + ${timestamp ? `<span class="time" title="(all times in UTC)">${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}</span>`: ''} + <span class="sysmessage">(<span class="nick" title="${sanitizeText(args.oldUserId)}">${sanitizeText(args.oldNick)}</span> changed name to <span class="new" title="${sanitizeText(args.newUserId)}">${sanitizeText(args.newNick)}</span>)</span> </div>` case 'join': return ` <div class="join"> - (<span class="nick" title="${sanitizeText(eventUserId)}">${sanitizeText(args.nick)}</span> joined the chat) + ${timestamp ? `<span class="time" title="(all times in UTC)">${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}</span>`: ''} + <span class="sysmessage">(<span class="nick" title="${sanitizeText(eventUserId)}">${sanitizeText(args.nick)}</span> joined the chat)</span> </div>` case 'part': return ` <div class="part"> - (<span class="nick" title="${sanitizeText(eventUserId)}">${sanitizeText(args.nick)}</span> left the chat) + ${timestamp ? `<span class="time" title="(all times in UTC)">${(new Date(timestamp)).toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false, hour: '2-digit', minute: '2-digit'})}</span>`: ''} + <span class="sysmessage">(<span class="nick" title="${sanitizeText(eventUserId)}">${sanitizeText(args.nick)}</span> left the chat)</span> </div>` } } |