All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m5s
- Agregar endpoint oldest.get.ts para obtener mensaje mas antiguo de un chat - Agregar boton 'Cargar historial de WhatsApp' en vista de mensajes - Mejorar HistorySection.vue con selector de chats y auto-deteccion
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
/**
|
|
* GET /api/messages/:instanceId/:chatId/oldest
|
|
* Get the oldest message from a chat for use with fetchMessageHistory
|
|
*/
|
|
import { query } from '../../../../utils/database'
|
|
|
|
interface ChatRow {
|
|
jid: string
|
|
}
|
|
|
|
interface MessageRow {
|
|
message_id: string
|
|
from_me: boolean
|
|
timestamp: Date
|
|
}
|
|
|
|
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')
|
|
const chatId = getRouterParam(event, 'chatId')
|
|
|
|
// Get chat info (jid)
|
|
const chatResult = await query<ChatRow>(
|
|
'SELECT jid FROM chats WHERE id = $1 AND instance_id = $2',
|
|
[chatId, instanceId]
|
|
)
|
|
|
|
if (!chatResult.rows.length) {
|
|
throw createError({ statusCode: 404, message: 'Chat not found' })
|
|
}
|
|
|
|
// Get oldest message
|
|
const oldestResult = await query<MessageRow>(
|
|
`SELECT message_id, from_me, timestamp
|
|
FROM messages
|
|
WHERE chat_id = $1
|
|
ORDER BY timestamp ASC
|
|
LIMIT 1`,
|
|
[chatId]
|
|
)
|
|
|
|
if (!oldestResult.rows.length) {
|
|
return {
|
|
hasMessages: false,
|
|
messageKey: null,
|
|
timestamp: null
|
|
}
|
|
}
|
|
|
|
const oldest = oldestResult.rows[0]
|
|
|
|
return {
|
|
hasMessages: true,
|
|
messageKey: {
|
|
remoteJid: chatResult.rows[0].jid,
|
|
id: oldest.message_id,
|
|
fromMe: oldest.from_me
|
|
},
|
|
timestamp: Math.floor(new Date(oldest.timestamp).getTime() / 1000)
|
|
}
|
|
})
|