/** * */ 'use strict'; const { objectMapper, pick } = require('../../utils/commons.util'); const mediaMsg = { contentToSend: msg => { const { msgtype, msgcontent, ...body } = msg; return { // ...body, to: body.to, externalId: body.actionId, // type WAMediaUpload = Buffer | WAMediaPayloadStream | WAMediaPayloadURL; [msgtype]: { url: msgcontent[msgtype].link, }, ...(msgcontent[msgtype].caption ? { caption: msgcontent[msgtype].caption } : {}), // image, video, document // mimetype: 'audio/mp4', ...(msgtype === 'document' ? { filename: msgcontent[msgtype].filename } : {}), // document ...(msgcontent.context ? { quoted: { key: msgcontent.context.message_id } } : {}), // seconds?: number; // audio // isAnimated?: boolean; // sticker // jpegThumbnail?: string; // image, video }; }, dataToDB: msg => { const { msgtype, msgcontent, ...body } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, IVADS_link: msgcontent[msgtype].link, IVADS_caption: msgcontent[msgtype].caption || '', IVADS_filename: msgcontent[msgtype].filename || '', ...(msgcontent.context ? { context_id: msgcontent.context.message_id, context_from: msgcontent.message_origin.from, } : {}), message_origin: JSON.stringify(msg), }; }, }; const apiSendMsgTypeMapped = { text: { type: 'text', contentToSend: msg => ({ // ...msg, to: msg.to, externalId: msg.actionId, text: msg.msgcontent.body, // linkPreview: true, // {} ...(msg.msgcontent.context ? { quoted: { key: msg.msgcontent.context.message_id } } : {}), }), dataToDB: msg => { const { msgcontent } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, text_body: msgcontent.body, text_preview_url: msgcontent.preview_url, ...(msgcontent.context ? { context_id: msgcontent.context.message_id, context_from: msgcontent.message_origin.from, } : {}), message_origin: JSON.stringify(msg), }; }, }, image: { type: 'image', contentToSend: msg => ({ ...mediaMsg.contentToSend({ ...msg }), }), dataToDB: msg => mediaMsg.dataToDB(msg), }, sticker: { type: 'sticker', contentToSend: msg => ({ ...mediaMsg.contentToSend({ ...msg }), }), dataToDB: msg => mediaMsg.dataToDB(msg), }, audio: { type: 'audio', contentToSend: msg => ({ ...mediaMsg.contentToSend({ ...msg }), }), dataToDB: msg => mediaMsg.dataToDB(msg), }, video: { type: 'video', // todo: gif contentToSend: msg => ({ ...mediaMsg.contentToSend({ ...msg }), }), dataToDB: msg => mediaMsg.dataToDB(msg), }, document: { type: 'document', contentToSend: msg => ({ ...mediaMsg.contentToSend({ ...msg }), }), dataToDB: msg => mediaMsg.dataToDB(msg), }, // 不支持 react: { type: 'react', contentToSend: msg => ({ // ...msg, to: msg.to, externalId: msg.actionId, react: { text: msg.msgcontent.body, // emoji | use an empty string to remove the reaction key: msg.msgcontent.context.message_id, }, }), dataToDB: msg => { const { msgtype, msgcontent, ...body } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, reaction_message_id: '', reaction_emoji: '', message_origin: JSON.stringify(msg), }; }, }, // 不支持 location: { type: 'location', contentToSend: msg => ({ // ...msg, to: msg.to, externalId: msg.actionId, // location: { degreesLatitude: msg.latitude, degreesLongitude: msg.longitude }, // todo: }), dataToDB: msg => { const { msgtype, msgcontent, ...body } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, message_origin: JSON.stringify(msg), }; }, }, // 不支持 contacts: { type: 'contacts', contentToSend: msg => ({ // ...msg, to: msg.to, externalId: msg.actionId, // contacts: { displayName: '', contacts: msg.contacts }, // todo: }), dataToDB: msg => { const { msgtype, msgcontent, ...body } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, message_origin: JSON.stringify(msg), }; }, }, template: { type: 'template', contentToSend: msg => ({ // ...msg, to: msg.to, externalId: msg.actionId, // msgcontent: { // ...msg.template, // components: [ // ...msg.template.components.filter(com => !['footer', 'buttons'].includes(com.type.toLowerCase())), // ], // }, }), dataToDB: msg => { const { msgtype, msgcontent, ...body } = msg; const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']); return { direction: 'outbound', msg_status: 'ready', createTime: Date.now(), id: msg.actionId, ...record, message_origin: JSON.stringify(msg), }; }, }, }; /** * API Payload to Send */ exports.ctxToSendBuilder = ctxContent => { const { type, contentToSend } = apiSendMsgTypeMapped[ctxContent.msgtype]; const msgReady = contentToSend(ctxContent); return msgReady; }; /** * API Payload to DB */ exports.ctxToDB = ctxContent => { const { type, dataToDB } = apiSendMsgTypeMapped[ctxContent.msgtype]; const msgReady = dataToDB(ctxContent); return msgReady; };