Send through a bot token so messages can be deleted again

Incoming webhooks return no message id, so nothing can be removed later.
Settings now take a bot token plus channel, chat.postMessage records channel
and timestamp on the run, and the history carries a delete button per
message. The webhook stays as a fallback and is marked as not deletable.
This commit is contained in:
Matthias G
2026-08-03 12:45:04 +02:00
parent 6c78acdd6d
commit 81d0489c78
16 changed files with 2450 additions and 42 deletions
+67 -1
View File
@@ -3,6 +3,8 @@ import { db } from '~/data/db'
import { appSettings } from '~/data/schema'
import { listReportRuns, writeSetting } from '~/data/repositories/settings'
import { readSlackConfig, reportScope, runWeeklyReport } from '~/lib/report-settings'
import { makeBrand, makeProject } from '../support/admin'
import { makePost } from '../support/entries'
const origin = 'https://logbuch.nyo.de'
@@ -19,7 +21,8 @@ describe('readSlackConfig', () => {
const config = await readSlackConfig()
expect(config.webhook).toContain('verwaltung')
expect(config.target).toMatchObject({ kind: 'webhook' })
expect(config.target && 'url' in config.target ? config.target.url : '').toContain('verwaltung')
expect(config.fromEnv).toBe(false)
})
@@ -55,3 +58,66 @@ describe('runWeeklyReport', () => {
expect(runs[0]).toMatchObject({ sent: false, total: 0, trigger: 'test', detail: 'nothing_published' })
})
})
describe('Bot-Token', () => {
it('nimmt Bot-Token und Kanal vor dem Webhook', async () => {
await writeSetting('slack.webhook', 'https://hooks.slack.com/services/a/b/c')
await writeSetting('slack.botToken', 'xoxb-1234')
await writeSetting('slack.channel', 'logbuch')
const config = await readSlackConfig()
expect(config.target).toEqual({ kind: 'bot', token: 'xoxb-1234', channel: 'logbuch' })
expect(config.hasBot).toBe(true)
})
it('braucht zum Bot-Token einen Kanal', async () => {
await writeSetting('slack.botToken', 'xoxb-1234')
const config = await readSlackConfig()
expect(config.target).toBeUndefined()
expect(config.hasBot).toBe(true)
})
it('merkt sich Kanal und Nachrichtenkennung eines Versands', async () => {
const brand = await makeBrand()
const project = await makeProject(brand.id)
const post = await makePost(project.id, { number: 1, slug: 'eins', status: 'published', publishAt: new Date() })
expect(post.id).toBeDefined()
await writeSetting('slack.botToken', 'xoxb-1234')
await writeSetting('slack.channel', 'logbuch')
vi.stubGlobal('fetch', vi.fn(async () => new Response(
JSON.stringify({ ok: true, channel: 'C999', ts: '1785600000.000100' }),
{ headers: { 'content-type': 'application/json' } },
)))
const outcome = await runWeeklyReport({ origin, trigger: 'test' })
expect(outcome.reason).toBe('sent')
expect(outcome.run).toMatchObject({ sent: true, channel: 'C999', messageTs: '1785600000.000100' })
})
it('vermerkt eine Ablehnung mit dem Grund von Slack', async () => {
const brand = await makeBrand()
const project = await makeProject(brand.id)
await makePost(project.id, { number: 1, slug: 'eins', status: 'published', publishAt: new Date() })
await writeSetting('slack.botToken', 'xoxb-1234')
await writeSetting('slack.channel', 'logbuch')
vi.stubGlobal('fetch', vi.fn(async () => new Response(
JSON.stringify({ ok: false, error: 'channel_not_found' }),
{ headers: { 'content-type': 'application/json' } },
)))
const outcome = await runWeeklyReport({ origin, trigger: 'test' })
expect(outcome.reason).toBe('rejected')
expect(outcome.run).toMatchObject({ sent: false, detail: 'channel_not_found' })
})
})