All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m4s
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
/**
|
|
* POST /api/presence/:instanceId/send
|
|
* Send presence update (composing, recording, available, unavailable, paused)
|
|
*/
|
|
import { baileysManager } from '../../../services/baileys/manager'
|
|
|
|
type PresenceType = 'composing' | 'recording' | 'available' | 'unavailable' | 'paused'
|
|
|
|
const VALID_PRESENCES: PresenceType[] = ['composing', 'recording', 'available', 'unavailable', 'paused']
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const username = getHeader(event, 'x-authentik-username')
|
|
if (!username) {
|
|
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
|
}
|
|
|
|
const instanceId = getRouterParam(event, 'instanceId')
|
|
if (!instanceId) {
|
|
throw createError({ statusCode: 400, message: 'Missing instanceId' })
|
|
}
|
|
|
|
const body = await readBody<{ jid: string; presence: PresenceType }>(event)
|
|
if (!body?.jid) {
|
|
throw createError({ statusCode: 400, message: 'Missing jid in request body' })
|
|
}
|
|
|
|
if (!body?.presence || !VALID_PRESENCES.includes(body.presence)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: `Invalid presence. Must be one of: ${VALID_PRESENCES.join(', ')}`
|
|
})
|
|
}
|
|
|
|
try {
|
|
await baileysManager.sendPresence(instanceId, body.jid, body.presence)
|
|
return { success: true, jid: body.jid, presence: body.presence }
|
|
} catch (error: any) {
|
|
console.error('[Presence API] Error sending presence:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: error.message || 'Error sending presence'
|
|
})
|
|
}
|
|
})
|