Benachrichtigung beim Veröffentlichen: Lücke geschlossen, besser sichtbar
- Fehler behoben: Meldungen, die als Entwurf mit älterem Datum angelegt und erst später veröffentlicht wurden, lösten keine Push-Nachricht aus (2-Tage-Grenze). Jetzt geht sie genau einmal raus, sobald die Meldung zum ersten Mal sichtbar ist - Editor: Hinweis unter „Benachrichtigung senden“, wann die Nachricht rausgeht (sobald veröffentlicht / am Datum mit Uhrzeit / sofort beim Speichern) – aktualisiert sich beim Ändern von Status und Datum - Meldungsliste: Glocke „geplant“ bzw. „gesendet“ - Seite „Benachrichtigungen“: Liste der geplanten Benachrichtigungen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a1e04773f4
commit
02171e4cfe
7 changed files with 145 additions and 17 deletions
92
src/lib/admin/NotifyField.svelte
Normal file
92
src/lib/admin/NotifyField.svelte
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<script lang="ts">
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import Bell from '@lucide/svelte/icons/bell';
|
||||
import BellRing from '@lucide/svelte/icons/bell-ring';
|
||||
import { fromLocalInput } from '$lib/dates';
|
||||
|
||||
/**
|
||||
* „Benachrichtigung senden“ einer Meldung – zeigt, wann die Push-Nachricht
|
||||
* rausgeht, abhängig von „Veröffentlicht“ und Datum im selben Formular.
|
||||
*/
|
||||
let { checked = false, notifiedAt = null, subscribers = 0 }: { checked?: boolean; notifiedAt?: string | null; subscribers?: number } = $props();
|
||||
|
||||
let on = $state(untrack(() => checked));
|
||||
let published = $state(true);
|
||||
let at = $state<Date | null>(null);
|
||||
let root: HTMLElement;
|
||||
|
||||
onMount(() => {
|
||||
const form = root.closest('form');
|
||||
if (!form) return;
|
||||
const read = () => {
|
||||
published = (form.querySelector('input[name="published"]') as HTMLInputElement | null)?.checked ?? true;
|
||||
at = fromLocalInput((form.querySelector('input[name="publishedAt"]') as HTMLInputElement | null)?.value ?? '');
|
||||
};
|
||||
read();
|
||||
form.addEventListener('input', read);
|
||||
form.addEventListener('change', read);
|
||||
return () => {
|
||||
form.removeEventListener('input', read);
|
||||
form.removeEventListener('change', read);
|
||||
};
|
||||
});
|
||||
|
||||
const fmt = (d: Date) => d.toLocaleString('de-DE', { day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Berlin' });
|
||||
const who = $derived(`${subscribers} ${subscribers === 1 ? 'Gerät' : 'Geräten'} mit abonnierten Neuigkeiten`);
|
||||
const when = $derived.by(() => {
|
||||
if (!published) return 'Wird gesendet, sobald du die Meldung veröffentlichst.';
|
||||
if (at && at.getTime() > Date.now()) return `Wird am ${fmt(at)} Uhr gesendet, wenn die Meldung erscheint.`;
|
||||
return 'Wird beim Speichern sofort gesendet.';
|
||||
});
|
||||
</script>
|
||||
|
||||
<div bind:this={root}>
|
||||
{#if notifiedAt}
|
||||
<p class="flex items-start gap-2 text-sm rounded-xl bg-surface-2 px-3 py-2">
|
||||
<BellRing class="size-4 mt-0.5 shrink-0" aria-hidden="true" />
|
||||
Benachrichtigung gesendet am {new Date(notifiedAt).toLocaleString('de-DE', { dateStyle: 'short', timeStyle: 'short' })}.
|
||||
</p>
|
||||
<input type="hidden" name="notify" value="on" />
|
||||
{:else}
|
||||
<label class="flex items-start gap-3 cursor-pointer">
|
||||
<input type="checkbox" name="notify" bind:checked={on} class="switch mt-0.5" aria-describedby="notify-hint" />
|
||||
<span>
|
||||
<span class="block font-semibold">Benachrichtigung senden</span>
|
||||
<span id="notify-hint" class="block text-sm text-ink-3">Push-Nachricht an {who}, sobald die Meldung erscheint.</span>
|
||||
</span>
|
||||
</label>
|
||||
{#if on}
|
||||
<p class="mt-2 flex items-start gap-2 text-sm rounded-xl bg-accent-soft text-accent-ink font-semibold px-3 py-2" role="status">
|
||||
<Bell class="size-4 mt-0.5 shrink-0" aria-hidden="true" />{when}
|
||||
</p>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.switch {
|
||||
appearance: none;
|
||||
flex-shrink: 0;
|
||||
width: 2.75rem;
|
||||
height: 1.6rem;
|
||||
border-radius: 999px;
|
||||
background: var(--line);
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.switch::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0.2rem;
|
||||
left: 0.2rem;
|
||||
width: 1.2rem;
|
||||
height: 1.2rem;
|
||||
border-radius: 999px;
|
||||
background: white;
|
||||
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
.switch:checked { background: var(--brand); }
|
||||
.switch:checked::after { transform: translateX(1.15rem); }
|
||||
</style>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import webpush from 'web-push';
|
||||
import { and, eq, gt, isNull, lte } from 'drizzle-orm';
|
||||
import { and, asc, eq, isNull, lte } from 'drizzle-orm';
|
||||
import { db } from './db';
|
||||
import { events, posts, pushMessages, pushSubscriptions, settings } from './db/schema';
|
||||
import { getSettings } from './settings';
|
||||
|
|
@ -130,7 +130,11 @@ export async function sendToTopic(
|
|||
|
||||
// ─── Automatische Benachrichtigungen ──────────────────────────────────────────
|
||||
|
||||
/** Meldungen mit „Benachrichtigung senden“, die jetzt erschienen sind */
|
||||
/**
|
||||
* Meldungen mit „Benachrichtigung senden“, die jetzt sichtbar sind. Jede Meldung
|
||||
* wird genau einmal gemeldet – auch wenn sie als Entwurf mit älterem Datum
|
||||
* angelegt und erst später veröffentlicht wurde.
|
||||
*/
|
||||
export async function sendDuePosts() {
|
||||
const due = db
|
||||
.select()
|
||||
|
|
@ -140,9 +144,7 @@ export async function sendDuePosts() {
|
|||
eq(posts.notify, true),
|
||||
isNull(posts.notifiedAt),
|
||||
eq(posts.published, true),
|
||||
lte(posts.publishedAt, new Date()),
|
||||
// ältere nicht mehr nachträglich verschicken
|
||||
gt(posts.publishedAt, new Date(Date.now() - 2 * 864e5))
|
||||
lte(posts.publishedAt, new Date())
|
||||
)
|
||||
)
|
||||
.all();
|
||||
|
|
@ -178,3 +180,14 @@ export function startPushScheduler() {
|
|||
run();
|
||||
setInterval(run, 5 * 60e3).unref();
|
||||
}
|
||||
|
||||
/** Geplante Benachrichtigungen (Meldungen mit Häkchen, noch nicht gesendet) */
|
||||
export function pendingPostNotifications() {
|
||||
return db
|
||||
.select({ id: posts.id, title: posts.title, published: posts.published, publishedAt: posts.publishedAt })
|
||||
.from(posts)
|
||||
.where(and(eq(posts.notify, true), isNull(posts.notifiedAt)))
|
||||
.orderBy(asc(posts.publishedAt))
|
||||
.all()
|
||||
.map((p) => ({ ...p, publishedAt: p.publishedAt.toISOString() }));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@ import { fail } from '@sveltejs/kit';
|
|||
import { desc, eq } from 'drizzle-orm';
|
||||
import { db } from '$lib/server/db';
|
||||
import { pushMessages, users } from '$lib/server/db/schema';
|
||||
import { sendToTopic, subscriberCounts } from '$lib/server/push';
|
||||
import { pendingPostNotifications, sendToTopic, subscriberCounts } from '$lib/server/push';
|
||||
import { fields } from '$lib/server/forms';
|
||||
import { allow } from '$lib/server/ratelimit';
|
||||
import { PUSH_TOPICS, type PushTopic } from '$lib/push';
|
||||
|
||||
export const load = () => ({
|
||||
counts: subscriberCounts(),
|
||||
pending: pendingPostNotifications(),
|
||||
history: db
|
||||
.select({ m: pushMessages, by: users.name })
|
||||
.from(pushMessages)
|
||||
|
|
|
|||
|
|
@ -96,6 +96,28 @@
|
|||
</aside>
|
||||
</div>
|
||||
|
||||
<section class="mt-10" aria-labelledby="geplant">
|
||||
<h2 id="geplant" class="text-lg font-bold mb-1">Geplant</h2>
|
||||
<p class="text-sm text-ink-3 mb-3">Meldungen mit „Benachrichtigung senden“ – die Nachricht geht raus, sobald die Meldung erscheint.</p>
|
||||
{#if data.pending.length}
|
||||
<ul class="card divide-y divide-line">
|
||||
{#each data.pending as m (m.id)}
|
||||
{@const future = new Date(m.publishedAt).getTime() > Date.now()}
|
||||
<li>
|
||||
<a href="/admin/meldungen/{m.id}" class="flex flex-wrap items-center gap-x-4 gap-y-1 px-4 py-3 hover:bg-surface-2/60">
|
||||
<span class="flex-1 min-w-48 font-semibold">{m.title}</span>
|
||||
<span class="text-sm text-ink-2">
|
||||
{#if !m.published}Entwurf – sobald veröffentlicht{:else if future}am {when(m.publishedAt)} Uhr{:else}in den nächsten Minuten{/if}
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{:else}
|
||||
<p class="card px-4 py-6 text-center text-ink-3">Keine geplanten Benachrichtigungen.</p>
|
||||
{/if}
|
||||
</section>
|
||||
|
||||
<section class="mt-10" aria-labelledby="verlauf">
|
||||
<h2 id="verlauf" class="text-lg font-bold mb-3">Verlauf</h2>
|
||||
<div class="card overflow-x-auto">
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ export const load = ({ url }) => {
|
|||
slug: r.slug,
|
||||
published: r.published,
|
||||
pinned: r.pinned,
|
||||
/** pending = geplant, sent = gesendet, null = keine Benachrichtigung */
|
||||
push: r.notifiedAt ? ('sent' as const) : r.notify ? ('pending' as const) : null,
|
||||
publishedAt: r.publishedAt.toISOString(),
|
||||
scheduled: r.publishedAt.getTime() > Date.now(),
|
||||
cover: r.coverId ? (covers.get(r.coverId)?.thumb ?? null) : null
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
import Newspaper from '@lucide/svelte/icons/newspaper';
|
||||
import Pin from '@lucide/svelte/icons/pin';
|
||||
import ExternalLink from '@lucide/svelte/icons/external-link';
|
||||
import Bell from '@lucide/svelte/icons/bell';
|
||||
import BellRing from '@lucide/svelte/icons/bell-ring';
|
||||
import PageHeader from '$lib/admin/PageHeader.svelte';
|
||||
import { formatShort } from '$lib/dates';
|
||||
|
||||
|
|
@ -36,6 +38,11 @@
|
|||
</span>
|
||||
<span class="text-sm text-ink-3">{formatShort(p.publishedAt)}</span>
|
||||
</a>
|
||||
{#if p.push === 'pending'}
|
||||
<span class="hidden sm:inline-flex items-center gap-1 text-xs font-bold text-accent-ink" title="Push-Benachrichtigung geplant"><Bell class="size-4" aria-hidden="true" />geplant<span class="sr-only">: Push-Benachrichtigung</span></span>
|
||||
{:else if p.push === 'sent'}
|
||||
<span class="hidden sm:inline-flex items-center gap-1 text-xs font-bold text-ink-3" title="Push-Benachrichtigung gesendet"><BellRing class="size-4" aria-hidden="true" />gesendet<span class="sr-only">: Push-Benachrichtigung</span></span>
|
||||
{/if}
|
||||
{#if !p.published}
|
||||
<span class="text-xs font-bold px-2.5 py-1 rounded-full bg-surface-2 text-ink-3">Entwurf</span>
|
||||
{:else if p.scheduled}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
import AttachmentsField from '$lib/admin/AttachmentsField.svelte';
|
||||
import ContactsField from '$lib/admin/ContactsField.svelte';
|
||||
import Switch from '$lib/admin/Switch.svelte';
|
||||
import NotifyField from '$lib/admin/NotifyField.svelte';
|
||||
import EasyLanguageField from '$lib/admin/EasyLanguageField.svelte';
|
||||
|
||||
let { data } = $props();
|
||||
|
|
@ -53,17 +54,7 @@
|
|||
<Panel title="Veröffentlichung">
|
||||
<Switch name="published" checked={p.published} label="Veröffentlicht" hint="Aus = Entwurf, nur hier sichtbar" />
|
||||
<Switch name="pinned" checked={p.pinned} label="Oben anheften" hint="Bleibt ganz oben in der Liste" />
|
||||
{#if p.notifiedAt}
|
||||
<p class="text-sm rounded-xl bg-surface-2 px-3 py-2">Benachrichtigung gesendet am {new Date(p.notifiedAt).toLocaleString('de-DE', { dateStyle: 'short', timeStyle: 'short' })}.</p>
|
||||
<input type="hidden" name="notify" value="on" />
|
||||
{:else}
|
||||
<Switch
|
||||
name="notify"
|
||||
checked={p.notify}
|
||||
label="Benachrichtigung senden"
|
||||
hint="Beim Erscheinen an {data.subscribers} {data.subscribers === 1 ? 'Person' : 'Personen'} mit abonnierten Neuigkeiten"
|
||||
/>
|
||||
{/if}
|
||||
<NotifyField checked={p.notify} notifiedAt={p.notifiedAt} subscribers={data.subscribers} />
|
||||
<div>
|
||||
<label class="label" for="publishedAt">Datum</label>
|
||||
<input id="publishedAt" name="publishedAt" type="datetime-local" class="field" value={p.publishedAt} />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue