Files
stpweb/vue/src/components/util/Chat.vue
T
adamfandClaude Opus 5.5 65fddfcfa2
Test and deploy / deploy (push) Successful in 3m48s
Test and deploy / test (push) Successful in 37s
Give uploads one path end to end, and serve /uploads/ from Garage first
Frontend: src/uploads.js is the one way to store or show a file
(uploadFile, ACCEPT, kindOf, isUploadUrl, tagFor, mediaSize). Chat,
the box editor, canvas drops and the background editor all use it;
views/home3/upload.js and the chat store's own axios call are gone.

Backend: POST /api/uploads (was /messages/upload) is a thin wrapper over
services.Uploads.Save, which owns validation, naming and storage. The
three per-extension maps are now one UploadTypes table, and a refused
file is an *UploadError whose message is safe to show.

nginx now fetches /uploads/ from Garage first and falls back to the
volume on a 404 or any 5xx, so the bucket is the source of truth and
deleting a file in garage-webui unpublishes it, while a Garage outage
still only affects bucket-only files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 21:04:37 +01:00

337 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup>
/**
* The chat widget: message list, composer, file attach, admin delete and an
* admins-only "private" channel. Drives stores/messages.js and is mounted in
* the right-hand sidebar of views/home/Home.vue.
*
* Admins see two tabs. The socket carries both channels in one stream (the
* server only sends private messages to admin connections), so the tab is a
* view over `messages` and also decides whether what you send is private —
* you cannot post to a channel you are not looking at.
*
* Owns the socket lifecycle — connect() on mount, disconnect() on unmount — so
* the socket exists only while the widget is on screen.
*/
import { ref, computed, watch, nextTick, onMounted, onUnmounted } from "vue";
import Button from "@/components/input/Button.vue";
import { useMessagesStore } from "@/stores/messages";
import { useAuthStore } from "@/stores/auth";
import Header from "@/components/text/Header.vue";
import Link from "@/components/text/Link.vue";
import { ACCEPT, isUploadUrl, kindOf } from "@/uploads";
const messagesStore = useMessagesStore();
const authStore = useAuthStore();
const messageInput = ref("");
const messagesContainer = ref(null);
const messagesInner = ref(null);
const fileInput = ref(null);
const isAdmin = computed(() => !!authStore.user.admin);
const activeTab = ref("public");
const isPrivateTab = computed(
() => isAdmin.value && activeTab.value === "private",
);
// The private tab is the only place private messages are shown, so the public
// tab stays public even for an admin.
const messages = computed(() =>
messagesStore.messages.filter((m) => !!m.private === isPrivateTab.value),
);
// Autoscroll only when the reader is already at the bottom: scrolling up to
// read history must not be yanked back down by an incoming message.
const isNearBottom = ref(true);
const SCROLL_THRESHOLD = 100;
let resizeObserver = null;
function scrollToBottom() {
if (messagesContainer.value) {
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight;
}
}
function scrollToBottomIfNear() {
if (isNearBottom.value) {
scrollToBottom();
}
}
function onScroll() {
if (!messagesContainer.value) return;
const { scrollHeight, scrollTop, clientHeight } = messagesContainer.value;
isNearBottom.value =
scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD;
}
function goToBottom() {
isNearBottom.value = true;
scrollToBottom();
}
// nextTick: scrollHeight is only correct once Vue has patched the new row in.
watch(
() => messages.value.length,
() => {
nextTick(scrollToBottomIfNear);
},
);
// A tab switch swaps the whole list, so start the new one at its newest message
// rather than at whatever scroll offset the old one left behind.
watch(activeTab, () => {
isNearBottom.value = true;
nextTick(scrollToBottom);
});
function sendMessage() {
const text = messageInput.value.trim();
if (!text) return;
isNearBottom.value = true;
messagesStore.sendMessage(text, isPrivateTab.value);
messageInput.value = "";
}
function deleteMessage(id) {
messagesStore.deleteMessage(id);
}
// Admin status is fixed when the socket connects (from the auth cookies), so
// reconnect after a login/logout to pick up or drop private messages. A
// demoted admin must also be moved off the private tab, which no longer has
// anything to show.
watch(isAdmin, (admin) => {
if (!admin) activeTab.value = "public";
if (!messagesStore.isConnected) return;
messagesStore.disconnect();
messagesStore.connect();
});
async function onFileSelected(e) {
const file = e.target.files[0];
if (!file) return;
isNearBottom.value = true;
// The private flag both routes the upload to /uploads/private/ and marks the
// message itself private; the two must agree.
await messagesStore.uploadAndSendFile(file, isPrivateTab.value);
fileInput.value.value = "";
}
// Attachments are rendered only if isUploadUrl() accepts them: the fileUrl
// arrives over the socket and is otherwise attacker-controlled.
// NOTE: a module-level /g regex carries `lastIndex` between calls. It is safe
// here only because parseMessageParts() below always runs exec() to completion
// (exec returns null, resetting lastIndex) before returning. An early return
// from that loop would make the next message parse from the wrong offset.
const urlRegex = /(https?:\/\/[^\s<]+)/g;
/** Split message text into alternating {type:'text'|'link', value} parts so
* URLs can be rendered as anchors without using v-html. */
function parseMessageParts(text) {
const parts = [];
let lastIndex = 0;
let match;
while ((match = urlRegex.exec(text)) !== null) {
if (match.index > lastIndex) {
parts.push({
type: "text",
value: text.slice(lastIndex, match.index),
});
}
parts.push({ type: "link", value: match[1] });
lastIndex = urlRegex.lastIndex;
}
if (lastIndex < text.length) {
parts.push({ type: "text", value: text.slice(lastIndex) });
}
return parts;
}
onMounted(() => {
messagesStore.connect();
if (messagesContainer.value) {
messagesContainer.value.addEventListener("scroll", onScroll, {
passive: true,
});
}
// Images and videos load after their row is inserted and change its height,
// which the length watcher above has already missed. Observing the inner
// wrapper catches those late reflows.
if (messagesInner.value) {
resizeObserver = new ResizeObserver(scrollToBottomIfNear);
resizeObserver.observe(messagesInner.value);
}
scrollToBottom();
});
onUnmounted(() => {
messagesStore.disconnect();
if (messagesContainer.value) {
messagesContainer.value.removeEventListener("scroll", onScroll);
}
if (resizeObserver) {
resizeObserver.disconnect();
resizeObserver = null;
}
});
</script>
<template>
<div class="chat-root flex-col flex min-h-0">
<Header>{{ isPrivateTab ? "Private Chat" : "Chat" }}</Header>
<div v-if="isAdmin" class="flex gap-1 pt-1" role="tablist">
<button
type="button"
class="chat-tab flex-1"
role="tab"
:aria-selected="activeTab === 'public'"
:class="{ 'is-active': activeTab === 'public' }"
@click="activeTab = 'public'"
>
Public
</button>
<button
type="button"
class="chat-tab flex-1"
role="tab"
:aria-selected="activeTab === 'private'"
:class="{ 'is-active': activeTab === 'private' }"
@click="activeTab = 'private'"
>
🔒 Private
</button>
</div>
<div
ref="messagesContainer"
class="flex flex-col flex-1 min-h-0 overflow-y-auto overflow-x-hidden p-2 min-w-0"
>
<div ref="messagesInner">
<p
v-for="message in messages"
:key="message.id"
class="break-words min-w-0 w-full"
:class="{ 'text-secondary italic': message.private }"
>
<button
v-if="isAdmin && message.id"
type="button"
class="text-tertiary hover:text-primary cursor-pointer mr-1"
aria-label="Delete message"
title="Delete message"
@click="deleteMessage(message.id)"
>
×
</button>
<span class="text-tertiary">{{ message.authorId }}:</span>
<template
v-for="(part, i) in parseMessageParts(message.text || '')"
:key="i"
>
<Link
v-if="part.type === 'link'"
bare
:href="part.value"
target="_blank"
class="text-primary underline break-all"
>{{ part.value }}</Link
>
<span v-else>{{ part.value }}</span>
</template>
<template v-if="isUploadUrl(message.fileUrl)">
<img
v-if="kindOf(message.fileUrl) === 'image'"
:src="message.fileUrl"
alt="Uploaded image"
loading="lazy"
class="w-full max-w-full rounded block"
/>
<video
v-else-if="kindOf(message.fileUrl) === 'video'"
:src="message.fileUrl"
controls
preload="none"
class="w-full max-w-full max-h-48 rounded block"
/>
<audio
v-else-if="kindOf(message.fileUrl) === 'audio'"
:src="message.fileUrl"
controls
preload="none"
class="w-full max-w-full block"
/>
<Link
v-else
bare
:href="message.fileUrl"
target="_blank"
class="underline break-all"
>{{ message.fileUrl.split("/").pop() }}</Link
>
</template>
</p>
</div>
</div>
<div>
<input
v-model="messageInput"
@keyup.enter="sendMessage"
:aria-label="isPrivateTab ? 'Private chat message' : 'Chat message'"
:placeholder="isPrivateTab ? 'Admins only' : ''"
/>
<input
ref="fileInput"
type="file"
:accept="ACCEPT.any"
class="hidden"
@change="onFileSelected"
/>
<div class="flex gap-2">
<Button class="flex-1" @click="sendMessage">Send</Button>
<Button v-if="isAdmin" class="flex-1" @click="fileInput.click()"
>Attach</Button
>
<Button v-if="!isNearBottom" class="flex-1" @click="goToBottom"
>Bottom</Button
>
</div>
</div>
</div>
</template>
<style scoped>
.chat-tab {
padding: 2px 6px;
color: var(--color-primary);
background-color: var(--color-link-bg);
border: 1px solid transparent;
font-family: var(--font-heading);
font-size: 0.875rem;
cursor: pointer;
transition:
background-color 120ms ease,
border-color 120ms ease,
color 120ms ease;
}
.chat-tab:hover {
border-color: var(--color-primary);
}
.chat-tab.is-active {
border-color: var(--color-primary);
color: var(--color-tertiary);
background-color: var(--color-surface-tint);
}
@media (max-width: 850px) {
.chat-root {
max-height: none;
height: 100%;
}
}
</style>