Migrate changes from other Metamigo repo

This commit is contained in:
Darren Clarke 2023-03-13 15:53:21 +00:00
parent 8669b09224
commit 27810142b3
11 changed files with 615 additions and 235 deletions

View file

@ -1,5 +1,6 @@
import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice";
import { promises as fs } from "fs";
import {
SignaldAPI,
IncomingMessagev1,
@ -179,6 +180,22 @@ export default class SignaldService extends Service {
await this.queueMessage(bot, message);
}
private async getAttachmentInfo(dataMessage: any) {
if (dataMessage.attachments?.length > 0) {
const attachmentInfo = dataMessage.attachments[0];
const buffer = await fs.readFile(attachmentInfo.storedFilename);
const attachment = buffer.toString("base64");
const mimetype = attachmentInfo.contentType ?? "application/octet-stream";
const filename = attachmentInfo.customFilename ?? "unknown-filename";
return { attachment, mimetype, filename }
}
return { attachment: null, mimetype: null, filename: null };
}
private async queueMessage(bot: Bot, message: IncomingMessagev1) {
const { timestamp, account, data_message: dataMessage } = message;
if (!dataMessage?.body && !dataMessage?.attachments) {
@ -190,10 +207,15 @@ export default class SignaldService extends Service {
this.server.logger.debug({ message }, "invalid message received");
}
const { attachment, mimetype, filename } = await this.getAttachmentInfo(dataMessage);
const receivedMessage = {
message,
botId: bot.id,
botPhoneNumber: bot.phoneNumber,
attachment,
mimetype,
filename
};
workerUtils.addJob("signald-message", receivedMessage, {