metamigo-api - fix linter errors in whatsapp service

This commit is contained in:
Abel Luck 2023-03-13 15:01:40 +00:00
parent ef216f7b1c
commit 8669b09224
7 changed files with 49 additions and 49 deletions

View file

@ -67,8 +67,7 @@ export const addTransactionCapability = (
}, {});
}
return state.get(type, ids);
return state.get(type, ids);
},
set(data) {
if (inTransaction) {

View file

@ -239,12 +239,11 @@ export const RequestCodeRoute = Helpers.withDefaults({
return h.response().code(402);
}
if (error.code) {
if (error.code) {
return h.response().code(error.code);
}
return h.response().code(500);
return h.response().code(500);
}
},
},

View file

@ -10,7 +10,8 @@ import * as Helpers from "../../helpers";
import workerUtils from "../../../../worker-utils";
import { SayLanguage, SayVoice } from "twilio/lib/twiml/VoiceResponse";
const queueRecording = async (meta) => workerUtils.addJob("twilio-recording", meta, { jobKey: meta.callSid });
const queueRecording = async (meta) =>
workerUtils.addJob("twilio-recording", meta, { jobKey: meta.callSid });
const twilioClientFor = (provider: SavedVoiceProvider): Twilio.Twilio => {
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;

View file

@ -1,4 +1,3 @@
import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice";
import {

View file

@ -8,6 +8,7 @@ import makeWASocket, {
MediaType,
AnyMessageContent,
WAProto,
UserFacingSocketConfig,
MiscMessageGenerationOptions,
} from "@adiwajshing/baileys";
import workerUtils from "../../worker-utils";
@ -47,13 +48,16 @@ export default class WhatsappService extends Service {
private async sleep(ms: number): Promise<void> {
console.log(`pausing ${ms}`);
return new Promise((resolve) => setTimeout(resolve, ms));
// eslint-disable-next-line no-new
new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
private async resetConnections() {
for (const connection of Object.values(this.connections)) {
try {
connection.end(null);
connection.end(undefined);
} catch (error) {
console.log(error);
}
@ -65,14 +69,14 @@ export default class WhatsappService extends Service {
private createConnection(
bot: Bot,
server: Server,
options: any,
authCompleteCallback?: any
options: Omit<UserFacingSocketConfig, "auth">,
authCompleteCallback?: () => void
) {
const { state, saveState } = useDatabaseAuthState(bot, server);
const connection = makeWASocket({ ...options, auth: state });
let pause = 5000;
connection.ev.on("connection.update", async (update) => {
console.log(`Connection updated ${JSON.stringify(update, null, 2)}`);
console.log(`Connection updated ${JSON.stringify(update, undefined, 2)}`);
const {
connection: connectionState,
lastDisconnect,
@ -88,6 +92,7 @@ export default class WhatsappService extends Service {
console.log("opened connection");
} else if (connectionState === "close") {
console.log("connection closed due to", lastDisconnect.error);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode;
if (disconnectStatusCode === DisconnectReason.restartRequired) {
@ -146,16 +151,17 @@ export default class WhatsappService extends Service {
}
}
private async queueMessage(bot: Bot, webMessageInfo: proto.WebMessageInfo) {
private async queueMessage(bot: Bot, webMessageInfo: proto.IWebMessageInfo) {
const { key, message, messageTimestamp } = webMessageInfo;
const { remoteJid } = key;
if (!key.fromMe && message && remoteJid !== "status@broadcast") {
const {fromMe, id: keyId} = key;
if (!fromMe && message && remoteJid !== "status@broadcast") {
const { audioMessage, documentMessage, imageMessage, videoMessage } =
message;
const isMediaMessage =
message.audioMessage ||
message.documentMessage ||
message.imageMessage ||
message.videoMessage;
audioMessage || documentMessage || imageMessage || videoMessage;
const messageContent = Object.values(message)[0];
let messageType: MediaType;
@ -163,25 +169,22 @@ export default class WhatsappService extends Service {
let filename: string;
let mimetype: string;
if (isMediaMessage) {
if (message.audioMessage) {
if (audioMessage) {
messageType = "audio";
filename =
key.id + "." + message.audioMessage.mimetype.split("/").pop();
mimetype = message.audioMessage.mimetype;
} else if (message.documentMessage) {
filename = keyId + "." + audioMessage.mimetype.split("/").pop();
mimetype = audioMessage.mimetype;
} else if (documentMessage) {
messageType = "document";
filename = message.documentMessage.fileName;
mimetype = message.documentMessage.mimetype;
} else if (message.imageMessage) {
filename = documentMessage.fileName;
mimetype = documentMessage.mimetype;
} else if (imageMessage) {
messageType = "image";
filename =
key.id + "." + message.imageMessage.mimetype.split("/").pop();
mimetype = message.imageMessage.mimetype;
} else if (message.videoMessage) {
filename = keyId + "." + imageMessage.mimetype.split("/").pop();
mimetype = imageMessage.mimetype;
} else if (videoMessage) {
messageType = "video";
filename =
key.id + "." + message.videoMessage.mimetype.split("/").pop();
mimetype = message.videoMessage.mimetype;
filename = keyId + "." + videoMessage.mimetype.split("/").pop();
mimetype = videoMessage.mimetype;
}
const stream = await downloadContentFromMessage(
@ -198,7 +201,7 @@ export default class WhatsappService extends Service {
if (messageContent || attachment) {
const receivedMessage = {
waMessageId: key.id,
waMessageId: keyId,
waMessage: JSON.stringify(webMessageInfo),
waTimestamp: new Date((messageTimestamp as number) * 1000),
attachment,
@ -209,13 +212,13 @@ export default class WhatsappService extends Service {
};
workerUtils.addJob("whatsapp-message", receivedMessage, {
jobKey: key.id,
jobKey: keyId,
});
}
}
}
private async queueUnreadMessages(bot: Bot, messages: any[]) {
private async queueUnreadMessages(bot: Bot, messages: proto.IWebMessageInfo[]) {
for await (const message of messages) {
await this.queueMessage(bot, message);
}
@ -274,7 +277,7 @@ export default class WhatsappService extends Service {
}
}
async receive(bot: Bot, lastReceivedDate: Date): Promise<any> {
async receive(bot: Bot, _lastReceivedDate: Date): Promise<proto.IWebMessageInfo[]> {
const connection = this.connections[bot.id];
// const messages = await connection.messagesReceivedAfter(
// lastReceivedDate,

View file

@ -1,7 +1,7 @@
export {default, loadConfig, loadConfigRaw, IAppConfig, IAppConvict} from "@digiresilience/metamigo-config";
export {
default,
loadConfig,
loadConfigRaw,
IAppConfig,
IAppConvict,
} from "@digiresilience/metamigo-config";

View file

@ -16,9 +16,8 @@ export const deployment = async (
return server;
};
export const stopDeployment = async (
server: Metamigo.Server
): Promise<void> => Metamigo.stopDeployment(server);
export const stopDeployment = async (server: Metamigo.Server): Promise<void> =>
Metamigo.stopDeployment(server);
const server = defState("server", {
start: () => deployment(config, true),