Merge branch 'main' into web-chat-create

# Conflicts:
#	web/PARITY.md
#	web/src/app.js
#	web/test/chat.test.mjs
#	web/test/ui-smoke.mjs
This commit is contained in:
2026-10-19 14:31:08 +01:00
11 changed files with 2339 additions and 80 deletions
+262
View File
@@ -37,6 +37,10 @@
// ui/chat/ChatFormat.kt attachmentError, attachmentFiles,
// MAX_ATTACHMENT_BYTES, attachmentDisplayName
// ui/chat/ChatAttachments.kt the attachment display name/error rules
// ui/chat/ChatOverrides.kt ChatSessionOverrides: local titles + hidden ids,
// applied over every server list refresh
// ui/chat/ChatSeen.kt ChatSeenState: the seen watermark and explicit
// unseen marks behind the row's "New" badge
//
// Nothing here touches the DOM or imports the app: every wording and decision is
// unit-tested without a browser. The caller sets each server string with
@@ -584,6 +588,264 @@ export function closeChatConfirmation(title) {
);
}
// ---------------------------------------------------------------------------
// Local overrides and seen state (ChatOverrides.kt / ChatSeen.kt)
// ---------------------------------------------------------------------------
/** The row menu and the rename dialog, verbatim from ChatListPane.kt. */
export const CHAT_RENAME_ACTION = "Rename…";
export const CHAT_HIDE_ACTION = "Hide on this device";
export const CHAT_ACTIONS_LABEL = "Chat actions";
export const CHAT_REMOVED_MESSAGE = "Hidden on this device; the chat stays on the server.";
export const CHAT_UNDO_ACTION = "Undo";
export const CHAT_RENAME_TITLE = "Rename chat";
export const CHAT_RENAME_FIELD_LABEL = "Name";
export const CHAT_RENAME_HELPER =
"The server has no rename for a chat, so this name is kept on this device.";
export const CHAT_RENAME_SAVE = "Save";
export const CHAT_RENAME_DISMISS = "Cancel";
/** The empty override model: no local titles, nothing hidden. */
export function emptyChatOverrides() {
return { titles: {}, hidden: [] };
}
/** The empty seen model: no watermarks, no explicit unseen marks. */
export function emptyChatSeen() {
return { seenAt: {}, unseen: [] };
}
/** A positive finite epoch-millisecond value, else 0 (never NaN). */
function millis(value) {
const number = Number(value);
return Number.isFinite(number) && number > 0 ? number : 0;
}
/**
* Decode a stored overrides value. Blank text, `null`, corrupt JSON, an array and
* wrong-typed fields all fall back to the empty model; nothing here throws, so a
* bad preference can never break the list.
*/
export function decodeChatOverrides(stored) {
const empty = emptyChatOverrides();
if (stored == null || stored === "") return empty;
let parsed = stored;
if (typeof stored === "string") {
if (stored.trim() === "") return empty;
try {
parsed = JSON.parse(stored);
} catch {
return empty;
}
}
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) return empty;
const titles = {};
const rawTitles = parsed.titles;
if (rawTitles != null) {
if (typeof rawTitles !== "object" || Array.isArray(rawTitles)) return empty;
for (const [id, title] of Object.entries(rawTitles)) {
if (isBlank(id) || typeof title !== "string" || isBlank(title)) continue;
titles[id] = title.trim();
}
}
const hidden = [];
const rawHidden = parsed.hidden;
if (rawHidden != null) {
if (!Array.isArray(rawHidden)) return empty;
for (const id of rawHidden) {
if (typeof id !== "string" || isBlank(id) || hidden.includes(id)) continue;
hidden.push(id);
}
}
return { titles, hidden };
}
/** The one-line JSON an overrides value is stored as. */
export function encodeChatOverrides(overrides) {
return JSON.stringify({
titles: { ...(overrides?.titles ?? {}) },
hidden: [...(overrides?.hidden ?? [])],
});
}
/**
* Decode a stored seen value with the same tolerance as the overrides: blank,
* corrupt or wrong-typed input is nothing seen, never an exception. Unseen ids
* are de-duplicated.
*/
export function decodeChatSeen(stored) {
const empty = emptyChatSeen();
if (stored == null || stored === "") return empty;
let parsed = stored;
if (typeof stored === "string") {
if (stored.trim() === "") return empty;
try {
parsed = JSON.parse(stored);
} catch {
return empty;
}
}
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) return empty;
const seenAt = {};
const rawSeenAt = parsed.seenAt;
if (rawSeenAt != null) {
if (typeof rawSeenAt !== "object" || Array.isArray(rawSeenAt)) return empty;
for (const [id, value] of Object.entries(rawSeenAt)) {
const at = millis(value);
if (isBlank(id) || at <= 0) continue;
seenAt[id] = at;
}
}
const unseen = [];
const rawUnseen = parsed.unseen;
if (rawUnseen != null) {
if (!Array.isArray(rawUnseen)) return empty;
for (const id of rawUnseen) {
if (typeof id !== "string" || isBlank(id) || unseen.includes(id)) continue;
unseen.push(id);
}
}
return { seenAt, unseen };
}
/** The one-line JSON a seen value is stored as. */
export function encodeChatSeen(seen) {
return JSON.stringify({
seenAt: { ...(seen?.seenAt ?? {}) },
unseen: [...(seen?.unseen ?? [])],
});
}
/**
* Record a local rename. A blank id or a title that is blank after trimming
* changes nothing; a recorded title is trimmed, and it wins over the server's
* own on every later refresh.
*/
export function renameChatOverride(overrides, id, title) {
const current = overrides ?? emptyChatOverrides();
const trimmed = str(title).trim();
const key = str(id);
if (isBlank(key) || trimmed === "") return current;
if (current.titles?.[key] === trimmed) return current;
return { ...current, titles: { ...(current.titles ?? {}), [key]: trimmed } };
}
/**
* Hide a session on this device. A blank id or an already-hidden id changes
* nothing, and an id is never appended twice.
*/
export function hideChatOverride(overrides, id) {
const current = overrides ?? emptyChatOverrides();
const key = str(id);
const hidden = current.hidden ?? [];
if (isBlank(key) || hidden.includes(key)) return current;
return { ...current, hidden: [...hidden, key] };
}
/** Restore a hidden session, undoing [hideChatOverride]. */
export function restoreChatOverride(overrides, id) {
const current = overrides ?? emptyChatOverrides();
const key = str(id);
const hidden = current.hidden ?? [];
if (!hidden.includes(key)) return current;
return { ...current, hidden: hidden.filter((entry) => entry !== key) };
}
/**
* The server's sessions with this device's overrides applied: hidden ids are
* removed (the set is built once, so each row is O(1)) and a local title replaces
* the server's. A blank/absent server title still falls through to
* `sessionTitle` at render time.
*/
export function applyChatOverrides(sessions, overrides) {
const list = Array.isArray(sessions) ? sessions : [];
const titles = overrides?.titles ?? {};
const hiddenIds = new Set(overrides?.hidden ?? []);
if (hiddenIds.size === 0 && Object.keys(titles).length === 0) return list;
return list
.filter((session) => !hiddenIds.has(str(session?.id)))
.map((session) => {
const local = titles[str(session?.id)];
return typeof local === "string" && local.trim() !== ""
? { ...session, title: local.trim() }
: session;
});
}
/** The newest recency the operator has seen for [id], or 0 when never seen. */
export function chatSeenWatermark(seen, id) {
const value = seen?.seenAt?.[str(id)];
return millis(value);
}
/**
* Record the first sight of every listed session, so a pre-existing chat is not
* reported as new activity. The active session is read (it is on screen); every
* other session is seeded only when it has no watermark yet and its recency is a
* real timestamp. An explicit unseen mark is never cleared by seeding.
*/
export function seedChatSeen(seen, sessions, activeId) {
let next = seen ?? emptyChatSeen();
for (const session of Array.isArray(sessions) ? sessions : []) {
const id = str(session?.id);
const updatedAt = millis(session?.updated_at);
if (isBlank(id) || updatedAt <= 0) continue;
if (id === str(activeId)) {
next = readChatSeen(next, id, updatedAt);
} else if (chatSeenWatermark(next, id) === 0) {
next = { ...next, seenAt: { ...(next.seenAt ?? {}), [id]: updatedAt } };
}
}
return next;
}
/**
* Read [id] up to [updatedAt]: the watermark advances only when the value is
* strictly newer (a stale read can never rewind it) and any explicit unseen mark
* is cleared. A blank id changes nothing.
*/
export function readChatSeen(seen, id, updatedAt) {
const current = seen ?? emptyChatSeen();
const key = str(id);
if (isBlank(key)) return current;
const at = millis(updatedAt);
const advanced = at > chatSeenWatermark(current, key);
const unseen = current.unseen ?? [];
const marked = unseen.includes(key);
if (!advanced && !marked) return current;
return {
seenAt: advanced ? { ...(current.seenAt ?? {}), [key]: at } : { ...(current.seenAt ?? {}) },
unseen: marked ? unseen.filter((entry) => entry !== key) : [...unseen],
};
}
/** Mark [id] as moved on while it was not on screen; blank ids and duplicates no-op. */
export function markChatUnseen(seen, id) {
const current = seen ?? emptyChatSeen();
const key = str(id);
const unseen = current.unseen ?? [];
if (isBlank(key) || unseen.includes(key)) return current;
return { ...current, seenAt: { ...(current.seenAt ?? {}) }, unseen: [...unseen, key] };
}
/**
* The exact `ChatState.isUnseen` rule: a session is new only when it is not the
* active one and either an explicit unseen mark exists or it has never been
* opened here and its `updated_at` is strictly newer than the stored watermark.
*/
export function isChatSessionUnseen(session, { activeId = null, seen = null, opened = false } = {}) {
const id = str(session?.id);
if (isBlank(id) || id === str(activeId)) return false;
if ((seen?.unseen ?? []).includes(id)) return true;
if (opened) return false;
return millis(session?.updated_at) > chatSeenWatermark(seen, id);
}
// ---------------------------------------------------------------------------
// Session narrowing and grouping (ChatListFilter.kt)
// ---------------------------------------------------------------------------