Nachrichten-Plugin 1.0.0: Direktnachrichten, Gruppen, Anhänge, GIFs, Emojis
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
2844452002
55 changed files with 5448 additions and 0 deletions
40
resources/js/lib/api.js
Normal file
40
resources/js/lib/api.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { http } from '@inhaus/ui';
|
||||
|
||||
/** JSON-Schnittstelle der Chat-Oberfläche (routes/web.php, Präfix /chat/api) */
|
||||
const base = '/chat/api';
|
||||
|
||||
export const api = {
|
||||
conversations: () => http.get(`${base}/gespraeche`),
|
||||
direct: (userId) => http.post(`${base}/direkt`, { user_id: userId }),
|
||||
createGroup: (name, members) => http.post(`${base}/gruppen`, { name, members }),
|
||||
updateConversation: (id, data) => http.put(`${base}/gespraeche/${id}`, data),
|
||||
leave: (id) => http.delete(`${base}/gespraeche/${id}`),
|
||||
addMembers: (id, members) => http.post(`${base}/gespraeche/${id}/mitglieder`, { members }),
|
||||
removeMember: (id, userId) => http.delete(`${base}/gespraeche/${id}/mitglieder/${userId}`),
|
||||
setAdmin: (id, userId, admin) => http.put(`${base}/gespraeche/${id}/mitglieder/${userId}`, { admin }),
|
||||
|
||||
messages: (id, before) => http.get(`${base}/gespraeche/${id}/nachrichten${before ? `?vor=${before}` : ''}`),
|
||||
send(id, { body, gifUrl, replyTo, files }) {
|
||||
const data = new FormData();
|
||||
if (body) data.append('body', body);
|
||||
if (gifUrl) data.append('gif_url', gifUrl);
|
||||
if (replyTo) data.append('reply_to', replyTo);
|
||||
(files ?? []).forEach((file) => data.append('files[]', file));
|
||||
|
||||
return http.post(`${base}/gespraeche/${id}/nachrichten`, data);
|
||||
},
|
||||
edit: (messageId, body) => http.put(`${base}/nachrichten/${messageId}`, { body }),
|
||||
remove: (messageId) => http.delete(`${base}/nachrichten/${messageId}`),
|
||||
react: (messageId, emoji) => http.post(`${base}/nachrichten/${messageId}/reaktion`, { emoji }),
|
||||
read: (id, messageId) => http.post(`${base}/gespraeche/${id}/gelesen`, { message_id: messageId }),
|
||||
typing: (id) => http.post(`${base}/gespraeche/${id}/tippt`),
|
||||
updates: (params) => http.get(`${base}/neues?${new URLSearchParams(Object.entries(params).filter(([, v]) => v !== null && v !== undefined))}`),
|
||||
unread: () => http.get(`${base}/ungelesen`),
|
||||
gifs: (q) => http.get(`${base}/gifs?q=${encodeURIComponent(q ?? '')}`),
|
||||
people: (q) => http.get(`/personen/suche?q=${encodeURIComponent(q)}`),
|
||||
};
|
||||
|
||||
/** Ungelesene an das Symbol in der Kopfzeile melden */
|
||||
export function announceUnread(count) {
|
||||
window.dispatchEvent(new CustomEvent('inhaus-chat:unread', { detail: count }));
|
||||
}
|
||||
11
resources/js/lib/emojis.js
Normal file
11
resources/js/lib/emojis.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/** Emoji-Auswahl (Unicode) – kompakt, ohne externe Bibliothek */
|
||||
export const EMOJI_GROUPS = [
|
||||
{ label: 'Smileys', icon: '😀', emojis: '😀 😃 😄 😁 😆 😅 🤣 😂 🙂 🙃 😉 😊 😇 🥰 😍 🤩 😘 😗 😚 😋 😛 😜 🤪 😝 🤗 🤭 🤫 🤔 🤐 🤨 😐 😑 😶 😏 😒 🙄 😬 😌 😔 😪 😴 😷 🤒 🤕 🤢 🤧 🥵 🥶 🥴 😵 🤯 🤠 🥳 😎 🤓 🧐 😕 😟 🙁 😮 😯 😲 😳 🥺 😦 😧 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 🥱 😤 😡 😠 🤬'.split(' ') },
|
||||
{ label: 'Gesten', icon: '👍', emojis: '👍 👎 👌 🤌 ✌️ 🤞 🤟 🤘 🤙 👈 👉 👆 👇 ☝️ ✋ 🤚 🖐️ 🖖 👋 👏 🙌 👐 🤲 🤝 🙏 ✍️ 💪 🦾 👀 🧠 🫶 ❤️ 🧡 💛 💚 💙 💜 🖤 🤍 🤎 💔 ❣️ 💕 💞 💓 💗 💖 💘 💯 ✨ ⭐ 🌟 💥 🔥 🎉 🎊'.split(' ') },
|
||||
{ label: 'Menschen', icon: '🧑', emojis: '👶 🧒 👦 👧 🧑 👱 👨 🧔 👩 🧓 👴 👵 🙋 🙆 🙅 🤷 🤦 🙇 💁 🧑⚕️ 🧑🍳 🧑🔧 🧑🏫 🧑💻 🧑💼 🧑🔬 🧑🎨 🧑🚒 👮 🕵️ 💂 👷 🤴 👸 🧙 🧚 🦸 🏃 🚶 🧘 🏋️ 🚴 🤸 👯 💃 🕺 👪 🧑🤝🧑'.split(' ') },
|
||||
{ label: 'Natur', icon: '🌿', emojis: '🐶 🐱 🐭 🐹 🐰 🦊 🐻 🐼 🐨 🐯 🦁 🐮 🐷 🐸 🐵 🐔 🐧 🐦 🦆 🦉 🐴 🦄 🐝 🐞 🦋 🐢 🐍 🐙 🐠 🐬 🐳 🌵 🎄 🌲 🌳 🌴 🌱 🌿 🍀 🍁 🍂 🌷 🌹 🌻 🌼 🌸 🌞 🌝 🌙 ⭐ ☀️ 🌤️ ⛅ 🌧️ ⛈️ ❄️ ☃️ 🌈 🌊'.split(' ') },
|
||||
{ label: 'Essen', icon: '🍕', emojis: '🍏 🍎 🍐 🍊 🍋 🍌 🍉 🍇 🍓 🫐 🍒 🍑 🥭 🍍 🥥 🥝 🍅 🥑 🥦 🥕 🌽 🥔 🥐 🥖 🥨 🧀 🥚 🍳 🥞 🥓 🍔 🍟 🍕 🌭 🥪 🌮 🥗 🍝 🍜 🍲 🍣 🍰 🎂 🧁 🍩 🍪 🍫 🍬 🍭 🍦 ☕ 🍵 🧃 🥤 🍺 🍻 🥂 🍷'.split(' ') },
|
||||
{ label: 'Aktivitäten', icon: '⚽', emojis: '⚽ 🏀 🏈 ⚾ 🎾 🏐 🏓 🏸 🥅 ⛳ 🎣 🎽 🎿 ⛸️ 🥇 🥈 🥉 🏆 🏅 🎗️ 🎫 🎟️ 🎭 🎨 🎬 🎤 🎧 🎼 🎹 🥁 🎷 🎺 🎸 🎻 🎲 🧩 🎯 🎳 🎮 🎰'.split(' ') },
|
||||
{ label: 'Dinge', icon: '💡', emojis: '⌚ 📱 💻 ⌨️ 🖥️ 🖨️ 🖱️ 💾 📷 📞 ☎️ 📺 ⏰ ⏳ 💡 🔦 🕯️ 💰 💳 🧾 ✉️ 📧 📨 📦 📝 📁 📂 📅 📆 📌 📎 ✂️ 🔒 🔑 🔨 🛠️ 🧰 🩺 💊 💉 🩹 🧯 🛒 🎁 🎈 🏠 🏥 🏫 🚗 🚌 🚲 ✈️ 🚀 🕐'.split(' ') },
|
||||
{ label: 'Symbole', icon: '✅', emojis: '✅ ☑️ ✔️ ❌ ❎ ➕ ➖ ❓ ❗ ‼️ ⁉️ 💤 ⚠️ 🚫 ⛔ 🔴 🟠 🟡 🟢 🔵 🟣 ⚫ ⚪ 🔶 🔷 ▶️ ⏸️ ⏹️ 🔄 🔁 🆗 🆕 🆒 🆘 ℹ️ 🔔 🔕 📣 💬 💭 🗯️ ♻️ ⬆️ ⬇️ ⬅️ ➡️'.split(' ') },
|
||||
];
|
||||
62
resources/js/lib/format.js
Normal file
62
resources/js/lib/format.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
const time = new Intl.DateTimeFormat('de-DE', { hour: '2-digit', minute: '2-digit' });
|
||||
const weekday = new Intl.DateTimeFormat('de-DE', { weekday: 'short' });
|
||||
const short = new Intl.DateTimeFormat('de-DE', { day: '2-digit', month: '2-digit', year: '2-digit' });
|
||||
const long = new Intl.DateTimeFormat('de-DE', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' });
|
||||
|
||||
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
|
||||
/** Zeit in der Gesprächsliste: heute Uhrzeit, diese Woche Wochentag, sonst Datum */
|
||||
export function listTime(iso) {
|
||||
if (!iso) return '';
|
||||
|
||||
const date = new Date(iso);
|
||||
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
|
||||
|
||||
if (days === 0) return time.format(date);
|
||||
if (days === 1) return 'Gestern';
|
||||
if (days < 7) return weekday.format(date);
|
||||
|
||||
return short.format(date);
|
||||
}
|
||||
|
||||
export const messageTime = (iso) => time.format(new Date(iso));
|
||||
|
||||
/** Trenner zwischen Tagen im Verlauf */
|
||||
export function dayLabel(iso) {
|
||||
const date = new Date(iso);
|
||||
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
|
||||
|
||||
if (days === 0) return 'Heute';
|
||||
if (days === 1) return 'Gestern';
|
||||
|
||||
return long.format(date);
|
||||
}
|
||||
|
||||
export const sameDay = (a, b) => startOfDay(new Date(a)) === startOfDay(new Date(b));
|
||||
|
||||
export function fileSize(bytes) {
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
||||
|
||||
return `${(bytes / 1024 / 1024).toFixed(1).replace('.', ',')} MB`;
|
||||
}
|
||||
|
||||
/** Text in Stücke zerlegen, Links erkennen – ohne v-html */
|
||||
export function linkify(text) {
|
||||
const parts = [];
|
||||
const pattern = /(https?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)\]])/g;
|
||||
let last = 0;
|
||||
|
||||
for (const match of text.matchAll(pattern)) {
|
||||
if (match.index > last) parts.push({ text: text.slice(last, match.index) });
|
||||
parts.push({ text: match[0], href: match[0] });
|
||||
last = match.index + match[0].length;
|
||||
}
|
||||
|
||||
if (last < text.length) parts.push({ text: text.slice(last) });
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
/** Nur Emojis (bis zu 3)? Dann groß darstellen */
|
||||
export const onlyEmoji = (text) => !!text && /^(\p{Extended_Pictographic}|\p{Emoji_Component}||️|\s){1,12}$/u.test(text) && [...text.replace(/\s/g, '')].length <= 8;
|
||||
78
resources/js/lib/sync.js
Normal file
78
resources/js/lib/sync.js
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { api } from './api';
|
||||
|
||||
/**
|
||||
* Hält den Chat aktuell: fragt regelmäßig nach („Gibt es Neues?“) – häufig, solange die Seite
|
||||
* sichtbar ist, selten im Hintergrund. Ist Echtzeit eingerichtet, löst ein Signal des
|
||||
* Pusher-kompatiblen Dienstes die Abfrage sofort aus; das Nachfragen läuft dann nur noch als Rückfall.
|
||||
*/
|
||||
export function createSync({ realtime, userId, openId, lastId, onUpdate }) {
|
||||
let cursor = null;
|
||||
let timer = null;
|
||||
let running = false;
|
||||
let stopped = false;
|
||||
let socket = null;
|
||||
|
||||
const interval = () => {
|
||||
const visible = document.visibilityState === 'visible';
|
||||
|
||||
if (socket?.connection?.state === 'connected') return visible ? 20000 : 60000;
|
||||
|
||||
return visible ? (document.hasFocus() ? 3000 : 6000) : 30000;
|
||||
};
|
||||
|
||||
async function tick() {
|
||||
if (running || stopped) return;
|
||||
|
||||
running = true;
|
||||
clearTimeout(timer);
|
||||
|
||||
try {
|
||||
const data = await api.updates({ seit: cursor, offen: openId(), nach: lastId() });
|
||||
cursor = data.cursor;
|
||||
onUpdate(data);
|
||||
} catch {
|
||||
// Netzwerk kurz weg – nächster Versuch folgt
|
||||
} finally {
|
||||
running = false;
|
||||
|
||||
if (!stopped) timer = setTimeout(tick, interval());
|
||||
}
|
||||
}
|
||||
|
||||
const wake = () => document.visibilityState === 'visible' && tick();
|
||||
document.addEventListener('visibilitychange', wake);
|
||||
window.addEventListener('focus', wake);
|
||||
|
||||
if (realtime) {
|
||||
import('pusher-js').then(({ default: Pusher }) => {
|
||||
if (stopped) return;
|
||||
|
||||
socket = new Pusher(realtime.key, {
|
||||
cluster: realtime.cluster,
|
||||
...(realtime.host ? { wsHost: realtime.host, wsPort: realtime.port, wssPort: realtime.port, forceTLS: realtime.tls, enabledTransports: ['ws', 'wss'] } : {}),
|
||||
channelAuthorization: {
|
||||
endpoint: '/chat/api/echtzeit/anmelden',
|
||||
headers: { 'X-XSRF-TOKEN': decodeURIComponent(document.cookie.match(/(?:^|; )XSRF-TOKEN=([^;]*)/)?.[1] ?? ''), Accept: 'application/json' },
|
||||
},
|
||||
});
|
||||
socket.subscribe(`private-chat-user-${userId}`).bind('update', () => tick());
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
tick();
|
||||
|
||||
return {
|
||||
now: tick,
|
||||
reset() {
|
||||
cursor = null;
|
||||
tick();
|
||||
},
|
||||
stop() {
|
||||
stopped = true;
|
||||
clearTimeout(timer);
|
||||
document.removeEventListener('visibilitychange', wake);
|
||||
window.removeEventListener('focus', wake);
|
||||
socket?.disconnect();
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue