Messages: Facebook-style Chats panel and docked chat windows
DevPlace CI / test (pull_request) Failing after 1h7m10s
DevPlace CI / test (pull_request) Failing after 1h7m10s
The topnav Messages icon opens a Chats panel (people search, recent conversations, New message, link to the full page). Picking a person opens a small chat window docked bottom-right; windows minimize to avatar heads with unread counts and survive navigation within the tab. Profile Send Message, the live message toast and message notifications on /notifications open the same window. Phones and /messages keep the full page. Each window is dp-chat in mode="window", sharing one WebSocket through chat/ChatSocketHub.js; chat/ConversationItem.js is the shared row builder. No server changes. The message input becomes [+] [text + emoji] [send] everywhere: + reveals the attach and GIF buttons, which tuck away while typing. Also fixes linked toasts never receiving clicks (pointer-events). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,13 @@ const MAX_BACKOFF_MS = 5000;
|
||||
const PING_MS = 25000;
|
||||
|
||||
export class ChatSocket {
|
||||
static resolveUrl(path) {
|
||||
if (/^wss?:\/\//i.test(path)) return path;
|
||||
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const normalized = path.startsWith("/") ? path : `/${path}`;
|
||||
return `${scheme}//${location.host}${normalized}`;
|
||||
}
|
||||
|
||||
constructor(url, handlers) {
|
||||
this.url = url;
|
||||
this.handlers = handlers || {};
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import { ChatSocket } from "./ChatSocket.js";
|
||||
|
||||
export class ChatSocketHub {
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.socket = null;
|
||||
this.ready = null;
|
||||
this.subscribers = new Set();
|
||||
}
|
||||
|
||||
subscribe(handlers) {
|
||||
this.subscribers.add(handlers);
|
||||
if (!this.socket) {
|
||||
this._connect();
|
||||
} else if (this.ready) {
|
||||
queueMicrotask(() => {
|
||||
if (this.ready && this.subscribers.has(handlers)) this._call(handlers, "onReady", this.ready);
|
||||
});
|
||||
}
|
||||
return {
|
||||
send: (frame) => this.send(frame),
|
||||
isOpen: () => this.isOpen(),
|
||||
close: () => this._unsubscribe(handlers),
|
||||
};
|
||||
}
|
||||
|
||||
send(frame) {
|
||||
return !!this.socket && this.socket.send(frame);
|
||||
}
|
||||
|
||||
isOpen() {
|
||||
return !!this.socket && this.socket.isOpen();
|
||||
}
|
||||
|
||||
_connect() {
|
||||
const socket = new ChatSocket(this.url, {
|
||||
onReady: (payload) => {
|
||||
if (this.socket !== socket) return;
|
||||
this.ready = payload;
|
||||
this._emit("onReady", payload);
|
||||
},
|
||||
onMessage: (frame) => {
|
||||
if (this.socket === socket) this._emit("onMessage", frame);
|
||||
},
|
||||
onClose: (event) => {
|
||||
if (this.socket !== socket) return;
|
||||
this.ready = null;
|
||||
this._emit("onClose", event);
|
||||
},
|
||||
});
|
||||
this.socket = socket;
|
||||
socket.connect();
|
||||
}
|
||||
|
||||
_unsubscribe(handlers) {
|
||||
this.subscribers.delete(handlers);
|
||||
if (this.subscribers.size || !this.socket) return;
|
||||
this.socket.close();
|
||||
this.socket = null;
|
||||
this.ready = null;
|
||||
}
|
||||
|
||||
_emit(name, payload) {
|
||||
for (const handlers of [...this.subscribers]) this._call(handlers, name, payload);
|
||||
}
|
||||
|
||||
_call(handlers, name, payload) {
|
||||
if (typeof handlers[name] === "function") handlers[name](payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import { Avatar } from "../Avatar.js";
|
||||
import { contentRenderer } from "../ContentRenderer.js";
|
||||
|
||||
const PREVIEW_CHARS = 60;
|
||||
|
||||
export class ConversationItem {
|
||||
static build(conv, activeUid = null) {
|
||||
const user = conv.other_user || {};
|
||||
const active = !!user.uid && user.uid === activeUid;
|
||||
const item = document.createElement("a");
|
||||
item.href = `/messages?with_uid=${user.uid || ""}`;
|
||||
item.className = `conversation-item${active ? " active" : ""}`;
|
||||
item.dataset.convUid = user.uid || "";
|
||||
item.dataset.convUsername = user.username || "";
|
||||
item.dataset.convAvatarSeed = user.avatar_seed || "";
|
||||
item.setAttribute("role", "listitem");
|
||||
if (active) item.setAttribute("aria-current", "true");
|
||||
|
||||
item.appendChild(Avatar.badgeElement(user));
|
||||
|
||||
const info = document.createElement("div");
|
||||
info.className = "conversation-info";
|
||||
const name = document.createElement("div");
|
||||
name.className = "conversation-name";
|
||||
name.textContent = user.username || "";
|
||||
const preview = document.createElement("div");
|
||||
preview.className = "conversation-preview";
|
||||
preview.textContent = contentRenderer.preview(conv.last_message, PREVIEW_CHARS);
|
||||
info.append(name, preview);
|
||||
item.appendChild(info);
|
||||
|
||||
const unread = document.createElement("span");
|
||||
unread.className = "conversation-unread-dot";
|
||||
unread.hidden = !conv.unread;
|
||||
const sr = document.createElement("span");
|
||||
sr.className = "sr-only";
|
||||
sr.textContent = "Unread messages";
|
||||
unread.appendChild(sr);
|
||||
item.appendChild(unread);
|
||||
|
||||
const time = document.createElement("span");
|
||||
time.className = "conversation-time";
|
||||
if (conv.last_message_at) {
|
||||
time.setAttribute("datetime", conv.last_message_at);
|
||||
time.dataset.dt = "";
|
||||
time.dataset.dtMode = "ago";
|
||||
if (window.app && window.app.localTime) window.app.localTime.apply(time);
|
||||
}
|
||||
item.appendChild(time);
|
||||
return item;
|
||||
}
|
||||
|
||||
static user(item) {
|
||||
return {
|
||||
uid: item.dataset.convUid || "",
|
||||
username: item.dataset.convUsername || "",
|
||||
avatar_seed: item.dataset.convAvatarSeed || "",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user