Delete webhook messages too by looking up their id
A message sent through the webhook has no stored id, but it belongs to the same Slack app, so the bot token may remove it. The delete button now looks the message up in the channel by its text and time before deleting.
This commit is contained in:
@@ -8,7 +8,8 @@ import {
|
||||
} from '~/data/repositories/settings'
|
||||
import { invalid, isUuid, readField, readFlag, type AdminFormState } from './admin-forms'
|
||||
import { runWeeklyReport } from './report-settings'
|
||||
import { deleteFromSlack } from './slack'
|
||||
import { deleteFromSlack, findMessageTs } from './slack'
|
||||
import { reportText } from './weekly-report'
|
||||
import type { Viewer } from './auth-access'
|
||||
|
||||
function looksLikeWebhook(value: string): boolean {
|
||||
@@ -108,18 +109,34 @@ export async function performDeleteMessage(viewer: Viewer, formData: FormData):
|
||||
return invalid('runUnknown')
|
||||
}
|
||||
|
||||
if (!run.channel || !run.messageTs) {
|
||||
return invalid('runNotDeletable')
|
||||
if (!run.sent) {
|
||||
return invalid('runNotSent')
|
||||
}
|
||||
|
||||
const values = await readSettings(['slack.botToken'])
|
||||
const values = await readSettings(['slack.botToken', 'slack.channel'])
|
||||
const token = values.get('slack.botToken')?.trim()
|
||||
const channel = run.channel ?? values.get('slack.channel')?.trim()
|
||||
|
||||
if (!token) {
|
||||
return invalid('runNoToken')
|
||||
}
|
||||
|
||||
const result = await deleteFromSlack(token, run.channel, run.messageTs)
|
||||
if (!channel) {
|
||||
return invalid('runNoChannel')
|
||||
}
|
||||
|
||||
const messageTs = run.messageTs ?? await findMessageTs({
|
||||
token,
|
||||
channel,
|
||||
text: reportText({ from: run.fromAt, to: run.toAt, total: run.total }),
|
||||
around: run.createdAt,
|
||||
})
|
||||
|
||||
if (!messageTs) {
|
||||
return invalid('runNotFound')
|
||||
}
|
||||
|
||||
const result = await deleteFromSlack(token, channel, messageTs)
|
||||
|
||||
if (!result.ok) {
|
||||
return invalid('runDeleteFailed')
|
||||
@@ -133,7 +150,7 @@ export async function performDeleteMessage(viewer: Viewer, formData: FormData):
|
||||
action: 'settings.reportDelete',
|
||||
entity: 'setting',
|
||||
entityId: run.id,
|
||||
data: { channel: run.channel, messageTs: run.messageTs },
|
||||
data: { channel, messageTs },
|
||||
})
|
||||
|
||||
return { status: 'ok', message: 'messageDeleted' }
|
||||
|
||||
Reference in New Issue
Block a user