Oh man that's a lot of changes.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// coms/client/public/data.js
|
||||
// Data module: holds the in‐memory threads, notices and reply state
|
||||
|
||||
// Map of all messages and notices by ID
|
||||
// Map of all messages and notices by ID (string IDs)
|
||||
// Client’s own session ID
|
||||
let myId = null;
|
||||
const threads = new Map();
|
||||
@@ -9,28 +9,28 @@ const threads = new Map();
|
||||
// Map of connected users by unique ID
|
||||
const users = new Map();
|
||||
|
||||
// Ordered list of root‐level IDs (messages and notices)
|
||||
// Ordered list of root‐level IDs (messages and notices, stored as strings)
|
||||
const rootIds = [];
|
||||
|
||||
// Negative counter to generate unique IDs for notices
|
||||
let noticeCounter = -1;
|
||||
|
||||
// ID of the message we’re currently replying to (or null)
|
||||
// ID of the message we’re currently replying to (string or null)
|
||||
let replyTo = null;
|
||||
|
||||
// ID of the message currently focused for navigation (or null)
|
||||
// ID of the message currently focused for navigation (string or null)
|
||||
let focusedId = null;
|
||||
|
||||
/**
|
||||
* Set the focused message ID (or null to clear).
|
||||
* @param {number|null} id
|
||||
* @param {string|null} id
|
||||
*/
|
||||
function setFocused(id) {
|
||||
focusedId = id;
|
||||
}
|
||||
/**
|
||||
* Get the currently focused message ID.
|
||||
* @returns {number|null}
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function getFocused() {
|
||||
return focusedId;
|
||||
@@ -46,7 +46,7 @@ function clearFocused() {
|
||||
|
||||
/**
|
||||
* Register a user with their unique ID.
|
||||
* @param {number} id
|
||||
* @param {string} id
|
||||
* @param {string} name
|
||||
*/
|
||||
function addUser(id, name) {
|
||||
@@ -55,7 +55,7 @@ function addUser(id, name) {
|
||||
|
||||
/**
|
||||
* Remove a user by their ID.
|
||||
* @param {number} id
|
||||
* @param {string} id
|
||||
*/
|
||||
function removeUser(id) {
|
||||
users.delete(id);
|
||||
@@ -63,7 +63,7 @@ function removeUser(id) {
|
||||
|
||||
/**
|
||||
* Update a user’s name.
|
||||
* @param {number} id
|
||||
* @param {string} id
|
||||
* @param {string} name
|
||||
*/
|
||||
function updateUser(id, name) {
|
||||
@@ -72,7 +72,7 @@ function updateUser(id, name) {
|
||||
|
||||
/**
|
||||
* Get a list of {id, name} objects for all users.
|
||||
* @returns {{id:number, name:string}[]}
|
||||
* @returns {{id:string, name:string}[]}
|
||||
*/
|
||||
function getUsers() {
|
||||
return Array.from(users.entries()).map(([id, name]) => ({ id, name }));
|
||||
@@ -95,14 +95,31 @@ function getGroupedUsers() {
|
||||
|
||||
/**
|
||||
* Add a new chat message to the thread structure.
|
||||
* @param {{id: number, username: string, content: string, ts: number, parent?: number}} msg
|
||||
* @param {{id: string|number, username: string, content: string, ts: number, parent?: string|number|null, authorId?: string|number|null}} msg
|
||||
*/
|
||||
function addMessage({ id, username, content, ts, parent = null }) {
|
||||
threads.set(id, { id, username, content, ts, parent, children: [] });
|
||||
if (parent !== null && threads.has(parent)) {
|
||||
threads.get(parent).children.push(id);
|
||||
function addMessage({
|
||||
id,
|
||||
username,
|
||||
content,
|
||||
ts,
|
||||
parent = null,
|
||||
authorId = null,
|
||||
}) {
|
||||
const sid = String(id);
|
||||
const sparent = parent === null ? null : String(parent);
|
||||
threads.set(sid, {
|
||||
id: sid,
|
||||
username,
|
||||
authorId: authorId === null ? null : String(authorId),
|
||||
content,
|
||||
ts,
|
||||
parent: sparent,
|
||||
children: [],
|
||||
});
|
||||
if (sparent !== null && threads.has(sparent)) {
|
||||
threads.get(sparent).children.push(sid);
|
||||
} else {
|
||||
rootIds.push(id);
|
||||
rootIds.push(sid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +129,7 @@ function addMessage({ id, username, content, ts, parent = null }) {
|
||||
*/
|
||||
function addNotice(content) {
|
||||
const ts = Math.floor(Date.now() / 1000);
|
||||
const id = noticeCounter--;
|
||||
const id = String(noticeCounter--);
|
||||
threads.set(id, {
|
||||
id,
|
||||
username: "",
|
||||
@@ -132,40 +149,41 @@ function clearData() {
|
||||
rootIds.length = 0;
|
||||
noticeCounter = -1;
|
||||
replyTo = null;
|
||||
users.clear();
|
||||
}
|
||||
|
||||
/** @returns {Map<number,object>} The threads map. */
|
||||
/** @returns {Map<string,object>} The threads map. */
|
||||
function getThreads() {
|
||||
return threads;
|
||||
}
|
||||
|
||||
/** @returns {number[]} The ordered list of root‐level IDs. */
|
||||
/** @returns {string[]} The ordered list of root‐level IDs. */
|
||||
function getRootIds() {
|
||||
return rootIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this client’s own session ID.
|
||||
* @param {number} id
|
||||
* @param {string|number} id
|
||||
*/
|
||||
function setMyId(id) {
|
||||
myId = id;
|
||||
myId = String(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this client’s own session ID.
|
||||
* @returns {number|null}
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function getMyId() {
|
||||
return myId;
|
||||
}
|
||||
|
||||
/** @returns {number|null} The current reply‐to ID. */
|
||||
/** @returns {string|null} The current reply‐to ID. */
|
||||
function getReplyTo() {
|
||||
return replyTo;
|
||||
}
|
||||
|
||||
/** @param {number|null} id — Set the current reply‐to ID. */
|
||||
/** @param {string|null} id — Set the current reply‐to ID. */
|
||||
function setReplyTo(id) {
|
||||
replyTo = id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user