Erste Version der Chats auf Basis der Schlüsselverwaltung: Gespräche zu
zweit, nur Text. Ziel ist, dass der Betreiber den Inhalt nicht kennt;
der Server speichert und verteilt nur OpenPGP-Chiffrate.
- Profil: „Ansprechen“, wenn beide einen Schlüssel haben (für Nutzer
ohne Schlüssel unsichtbar).
- Entsperren einmal pro Tab mit der Passphrase; der Schlüsselbund bleibt
im Speicher (keyring.ts) und umfasst auch frühere eigene Schlüssel.
- Nachrichten an Empfänger und Absender verschlüsselt und signiert; das
offene Gespräch fragt alle 5 s nach, die Übersicht alle 30 s.
- Server: Tabelle message, /api/chats, /api/chats/{username};
pgpkey.CheckMessage weist Klartext ab; internal/chat löscht stündlich,
was älter als 15 Tage ist. Konto löschen nimmt die Nachrichten mit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
150 lines
5.7 KiB
TypeScript
150 lines
5.7 KiB
TypeScript
import { useState } from "react";
|
|
import { Link, useParams } from "react-router";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { api, errMsg } from "../api";
|
|
import { useAuthChanged, useMe } from "../auth";
|
|
import { avatarUrl, linkify } from "../format";
|
|
import { useUserKey } from "../useKey";
|
|
import { useFormAction } from "../useFormAction";
|
|
import { Feed } from "../components/Feed";
|
|
import type { Profile } from "../types";
|
|
|
|
// Muss zu maxBioRunes im Server passen (internal/httpapi/users.go).
|
|
const maxBio = 300;
|
|
|
|
// Beim Wechsel zu einem anderen Profil frisch anfangen (Meldungen, Formulare).
|
|
export function UserPage() {
|
|
const { username = "" } = useParams();
|
|
return <ProfileView key={username} username={username} />;
|
|
}
|
|
|
|
// ProfileView: Header-Bild (ohne Bild ein schwarzer Balken), darauf
|
|
// überlappend der Avatar, darunter Name, Beschreibung, Meta-Zeile und die
|
|
// Beiträge. Der Eigentümer bearbeitet Bilder und Beschreibung direkt hier;
|
|
// Name und Konto stehen unter /settings.
|
|
function ProfileView({ username }: { username: string }) {
|
|
const profile = useQuery({
|
|
queryKey: ["profile", username],
|
|
queryFn: () => api.get<Profile>(`/u/${encodeURIComponent(username)}/info`),
|
|
});
|
|
const me = useMe().data;
|
|
const [editing, setEditing] = useState(false);
|
|
|
|
const info = profile.data;
|
|
let name = "";
|
|
if (profile.isError) name = errMsg(profile.error, "Profil konnte nicht geladen werden.");
|
|
else if (info) name = info.username;
|
|
|
|
const isOwner = !!me && !!info && me.username === info.username;
|
|
|
|
return (
|
|
<section id="profile">
|
|
<div className="profile-header">
|
|
{info?.header && <img src={`/${info.header}`} alt="" />}
|
|
</div>
|
|
<div className="profile-body">
|
|
<div className="profile-top">
|
|
<img id="profile-avatar" className="avatar avatar-xl" src={avatarUrl(info?.avatar ?? "")} alt="" />
|
|
{isOwner && (
|
|
<button onClick={() => setEditing(!editing)} aria-expanded={editing}>
|
|
{editing ? "Fertig" : "Profil bearbeiten"}
|
|
</button>
|
|
)}
|
|
{me && info && !isOwner && <Contact me={me.username} other={info.username} />}
|
|
</div>
|
|
<h2 id="profile-name">{name}</h2>
|
|
{info?.bio && <p className="bio">{linkify(info.bio)}</p>}
|
|
<p id="profile-info" className="muted">
|
|
{info &&
|
|
`Mitglied seit ${new Date(info.created_at * 1000).toLocaleDateString("de-DE")} · ` +
|
|
`${info.entries} ${info.entries === 1 ? "Beitrag" : "Beiträge"}`}
|
|
</p>
|
|
{isOwner && editing && info && <ProfileEdit info={info} />}
|
|
</div>
|
|
|
|
{info && (
|
|
<Feed
|
|
as="div"
|
|
queryKey={["feed", "user", username]}
|
|
urlFor={(page) => `/u/${encodeURIComponent(username)}/feed/${page}`}
|
|
/>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
// Contact führt zum Chat mit dem Profil -- nur, wenn beide einen Schlüssel
|
|
// haben; wer selbst keinen hat, sieht den Knopf gar nicht.
|
|
function Contact({ me, other }: { me: string; other: string }) {
|
|
const own = useUserKey(me);
|
|
const theirs = useUserKey(other);
|
|
if (!own.data?.key || !theirs.data?.key) return null;
|
|
return (
|
|
<Link className="button" to={`/chats/${encodeURIComponent(other)}`}>
|
|
Ansprechen
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
// ProfileEdit: Header-Bild, Profilbild und Beschreibung, nur für den
|
|
// Eigentümer.
|
|
function ProfileEdit({ info }: { info: Profile }) {
|
|
const qc = useQueryClient();
|
|
const authChanged = useAuthChanged();
|
|
const [bio, setBio] = useState(info.bio);
|
|
|
|
const refreshProfile = () => qc.invalidateQueries({ queryKey: ["profile"] });
|
|
// Das Profilbild taucht auch in den Beitragskarten und im eigenen Konto auf.
|
|
const refreshAvatar = () => {
|
|
qc.invalidateQueries({ queryKey: ["feed"] });
|
|
qc.invalidateQueries({ queryKey: ["thread"] });
|
|
refreshProfile();
|
|
return authChanged();
|
|
};
|
|
|
|
const header = useFormAction("/user/header", "Hochladen fehlgeschlagen.", (form) => {
|
|
form.reset();
|
|
refreshProfile();
|
|
});
|
|
const removeHeader = useFormAction("/user/header/remove", "Entfernen fehlgeschlagen.", refreshProfile);
|
|
const avatar = useFormAction("/user/avatar", "Hochladen fehlgeschlagen.", (form) => {
|
|
form.reset();
|
|
refreshAvatar();
|
|
});
|
|
const saveBio = useFormAction("/user/bio", "Speichern fehlgeschlagen.", refreshProfile);
|
|
|
|
return (
|
|
<div className="profile-edit">
|
|
<h3>Header-Bild</h3>
|
|
<form id="header-form" onSubmit={header.onSubmit}>
|
|
<input name="header" type="file" accept="image/*" />{" "}
|
|
<button type="submit" disabled={header.busy}>Bild hochladen</button>
|
|
</form>
|
|
{info.header && (
|
|
<form onSubmit={removeHeader.onSubmit}>
|
|
<button type="submit" disabled={removeHeader.busy}>Header-Bild entfernen</button>
|
|
</form>
|
|
)}
|
|
<p className="msg">{header.msg || removeHeader.msg}</p>
|
|
|
|
<h3>Profilbild</h3>
|
|
<form id="avatar-form" onSubmit={avatar.onSubmit}>
|
|
<input name="avatar" type="file" accept="image/*" />{" "}
|
|
<button type="submit" disabled={avatar.busy}>Bild hochladen</button>
|
|
</form>
|
|
<p className="msg" id="avatar-msg">{avatar.msg}</p>
|
|
|
|
<h3>Beschreibung</h3>
|
|
<form id="bio-form" onSubmit={saveBio.onSubmit}>
|
|
<textarea name="bio" rows={4} maxLength={maxBio} value={bio} onChange={(e) => setBio(e.target.value)} />
|
|
<div className="row">
|
|
<button type="submit" disabled={saveBio.busy}>Text speichern</button>
|
|
{saveBio.ok && bio === info.bio && <span className="muted">Gespeichert.</span>}
|
|
<span className="muted counter">{[...bio].length}/{maxBio}</span>
|
|
</div>
|
|
</form>
|
|
<p className="msg">{saveBio.msg}</p>
|
|
</div>
|
|
);
|
|
}
|