metamigo-api: fix linter errors

This commit is contained in:
Abel Luck 2023-03-13 16:40:04 +00:00
parent 594bc23583
commit f4a3031c5e
9 changed files with 181 additions and 108 deletions

View file

@ -9,13 +9,16 @@ import type { IAppConfig } from "../../config";
const CF_JWT_HEADER_NAME = "cf-access-jwt-assertion"; const CF_JWT_HEADER_NAME = "cf-access-jwt-assertion";
const CF_JWT_ALGOS = ["RS256"]; const CF_JWT_ALGOS = ["RS256"];
const verifyToken = (settings: any) => { type VerifyFn = (token: string) => Promise<void>;
const verifyToken = (settings) => {
const { audience, issuer } = settings; const { audience, issuer } = settings;
const client = jwksClient({ const client = jwksClient({
jwksUri: `${issuer}/cdn-cgi/access/certs`, jwksUri: `${issuer}/cdn-cgi/access/certs`,
}); });
return async (token: any) => { return async (token: string) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getKey = (header: any, callback: any) => { const getKey = (header: any, callback: any) => {
client.getSigningKey(header.kid, (err, key) => { client.getSigningKey(header.kid, (err, key) => {
if (err) if (err)
@ -37,7 +40,8 @@ const verifyToken = (settings: any) => {
}; };
const handleCfJwt = const handleCfJwt =
(verify: any) => async (request: Hapi.Request, h: Hapi.ResponseToolkit) => { (verify: VerifyFn) =>
async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const token = request.headers[CF_JWT_HEADER_NAME]; const token = request.headers[CF_JWT_HEADER_NAME];
if (token) { if (token) {
try { try {
@ -60,6 +64,7 @@ const defaultOpts = {
const cfJwtRegister = async ( const cfJwtRegister = async (
server: Hapi.Server, server: Hapi.Server,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any options: any
): Promise<void> => { ): Promise<void> => {
server.dependency(["hapi-auth-jwt2"]); server.dependency(["hapi-auth-jwt2"]);
@ -69,7 +74,11 @@ const cfJwtRegister = async (
const { validate, strategyName, audience, issuer } = settings; const { validate, strategyName, audience, issuer } = settings;
server.ext("onPreAuth", handleCfJwt(verify)); server.ext("onPreAuth", handleCfJwt(verify));
server.auth.strategy(strategyName!, "jwt", { if (!strategyName) {
throw new Error("Missing strategyName for cloudflare-jwt hapi plugin!");
}
server.auth.strategy(strategyName, "jwt", {
key: hapiJwt2KeyAsync({ key: hapiJwt2KeyAsync({
jwksUri: `${issuer}/cdn-cgi/access/certs`, jwksUri: `${issuer}/cdn-cgi/access/certs`,
}), }),
@ -102,6 +111,7 @@ export const registerCloudflareAccessJwt = async (
options: { options: {
issuer: `https://${domain}`, issuer: `https://${domain}`,
audience, audience,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validate(decoded: any, _request: any) { validate(decoded: any, _request: any) {
const { email, name } = decoded; const { email, name } = decoded;
return { return {

View file

@ -1,6 +1,5 @@
import type * as Hapi from "@hapi/hapi"; import type * as Hapi from "@hapi/hapi";
import { ServerRegisterPluginObjectDirect } from "@hapi/hapi/lib/types/plugin"; import type { IInitOptions } from "pg-promise";
import type { IInitOptions, IDatabase } from "pg-promise";
import Schmervice from "@hapipal/schmervice"; import Schmervice from "@hapipal/schmervice";
import { makePlugin } from "@digiresilience/hapi-pg-promise"; import { makePlugin } from "@digiresilience/hapi-pg-promise";

View file

@ -22,10 +22,14 @@ const jwtDefaults = {
const jwtRegister = async (server: Hapi.Server, options): Promise<void> => { const jwtRegister = async (server: Hapi.Server, options): Promise<void> => {
server.dependency(["hapi-auth-jwt2"]); server.dependency(["hapi-auth-jwt2"]);
const settings: any = Hoek.applyToDefaults(jwtDefaults, options); const settings = Hoek.applyToDefaults(jwtDefaults, options);
const key = settings.jwkeysB64.map((k) => jwkToHapiAuthJwt2(k)); const key = settings.jwkeysB64.map((k) => jwkToHapiAuthJwt2(k));
server.auth.strategy(settings.strategyName!, "jwt", { if (!settings.strategyName) {
throw new Error("Missing strategy name in nextauth-jwt pluginsettings!");
}
server.auth.strategy(settings.strategyName, "jwt", {
key, key,
cookieKey: false, cookieKey: false,
urlKey: false, urlKey: false,

View file

@ -161,7 +161,6 @@ export const UnverifyBotRoute = Helpers.withDefaults({
}, },
}); });
export const RefreshBotRoute = Helpers.withDefaults({ export const RefreshBotRoute = Helpers.withDefaults({
method: "get", method: "get",
path: "/api/whatsapp/bots/{id}/refresh", path: "/api/whatsapp/bots/{id}/refresh",

View file

@ -9,7 +9,6 @@ export const VoicemailUseTextPrompt = settingInfo<boolean>(
); );
export { ISettingsService } from "@digiresilience/metamigo-db"; export { ISettingsService } from "@digiresilience/metamigo-db";
// @ts-expect-error
const service = (server: Hapi.Server): Schmervice.ServiceFunctionalInterface => const service = (server: Hapi.Server): Schmervice.ServiceFunctionalInterface =>
SettingsService(server.db().settings); SettingsService(server.db().settings);

View file

@ -1,6 +1,6 @@
import { Server } from "@hapi/hapi"; import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice"; import { Service } from "@hapipal/schmervice";
import { promises as fs } from "fs"; import { promises as fs } from "node:fs";
import { import {
SignaldAPI, SignaldAPI,
SendResponsev1, SendResponsev1,
@ -185,7 +185,7 @@ export default class SignaldService extends Service {
await this.queueMessage(bot, message); await this.queueMessage(bot, message);
} }
private async getAttachmentInfo(dataMessage: any) { private async getAttachmentInfo(dataMessage: IncomingMessagev1) {
if (dataMessage.attachments?.length > 0) { if (dataMessage.attachments?.length > 0) {
const attachmentInfo = dataMessage.attachments[0]; const attachmentInfo = dataMessage.attachments[0];
const buffer = await fs.readFile(attachmentInfo.storedFilename); const buffer = await fs.readFile(attachmentInfo.storedFilename);
@ -193,14 +193,12 @@ export default class SignaldService extends Service {
const mimetype = attachmentInfo.contentType ?? "application/octet-stream"; const mimetype = attachmentInfo.contentType ?? "application/octet-stream";
const filename = attachmentInfo.customFilename ?? "unknown-filename"; const filename = attachmentInfo.customFilename ?? "unknown-filename";
return { attachment, mimetype, filename };
return { attachment, mimetype, filename }
} }
return { attachment: null, mimetype: null, filename: null }; return { attachment: undefined, mimetype: undefined, filename: undefined };
} }
private async queueMessage(bot: Bot, message: IncomingMessagev1) { private async queueMessage(bot: Bot, message: IncomingMessagev1) {
const { timestamp, account, data_message: dataMessage } = message; const { timestamp, account, data_message: dataMessage } = message;
if (!dataMessage?.body && !dataMessage?.attachments) { if (!dataMessage?.body && !dataMessage?.attachments) {
@ -212,7 +210,9 @@ export default class SignaldService extends Service {
this.server.logger.debug({ message }, "invalid message received"); this.server.logger.debug({ message }, "invalid message received");
} }
const { attachment, mimetype, filename } = await this.getAttachmentInfo(dataMessage); const { attachment, mimetype, filename } = await this.getAttachmentInfo(
dataMessage
);
const receivedMessage = { const receivedMessage = {
message, message,
@ -220,7 +220,7 @@ export default class SignaldService extends Service {
botPhoneNumber: bot.phoneNumber, botPhoneNumber: bot.phoneNumber,
attachment, attachment,
mimetype, mimetype,
filename filename,
}; };
workerUtils.addJob("signald-message", receivedMessage, { workerUtils.addJob("signald-message", receivedMessage, {

View file

@ -3,7 +3,17 @@
import { Server } from "@hapi/hapi"; import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice"; import { Service } from "@hapipal/schmervice";
import { SavedWhatsappBot as Bot } from "@digiresilience/metamigo-db"; import { SavedWhatsappBot as Bot } from "@digiresilience/metamigo-db";
import makeWASocket, { DisconnectReason, proto, downloadContentFromMessage, MediaType, fetchLatestBaileysVersion, isJidBroadcast, isJidStatusBroadcast, MessageRetryMap, useMultiFileAuthState } from "@adiwajshing/baileys"; import makeWASocket, {
DisconnectReason,
proto,
downloadContentFromMessage,
MediaType,
fetchLatestBaileysVersion,
isJidBroadcast,
isJidStatusBroadcast,
MessageRetryMap,
useMultiFileAuthState,
} from "@adiwajshing/baileys";
import fs from "fs"; import fs from "fs";
import workerUtils from "../../worker-utils"; import workerUtils from "../../worker-utils";
@ -36,14 +46,14 @@ export default class WhatsappService extends Service {
} }
private async sleep(ms: number): Promise<void> { private async sleep(ms: number): Promise<void> {
console.log(`pausing ${ms}`) console.log(`pausing ${ms}`);
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise((resolve) => setTimeout(resolve, ms));
} }
private async resetConnections() { private async resetConnections() {
for (const connection of Object.values(this.connections)) { for (const connection of Object.values(this.connections)) {
try { try {
connection.end(null) connection.end(null);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
@ -51,65 +61,74 @@ export default class WhatsappService extends Service {
this.connections = {}; this.connections = {};
} }
private async createConnection(
private async createConnection(bot: Bot, server: Server, options: any, authCompleteCallback?: any) { bot: Bot,
server: Server,
options: any,
authCompleteCallback?: any
) {
const directory = this.getAuthDirectory(bot); const directory = this.getAuthDirectory(bot);
const { state, saveCreds } = await useMultiFileAuthState(directory); const { state, saveCreds } = await useMultiFileAuthState(directory);
const msgRetryCounterMap: MessageRetryMap = {} const msgRetryCounterMap: MessageRetryMap = {};
const socket = makeWASocket({ const socket = makeWASocket({
...options, ...options,
auth: state, auth: state,
msgRetryCounterMap, msgRetryCounterMap,
shouldIgnoreJid: jid => isJidBroadcast(jid) || isJidStatusBroadcast(jid), shouldIgnoreJid: (jid) =>
isJidBroadcast(jid) || isJidStatusBroadcast(jid),
}); });
let pause = 5000; let pause = 5000;
socket.ev.process( socket.ev.process(async (events) => {
async (events) => { if (events["connection.update"]) {
if (events['connection.update']) { const update = events["connection.update"];
const update = events['connection.update'] const {
const { connection: connectionState, lastDisconnect, qr, isNewLogin } = update connection: connectionState,
if (qr) { lastDisconnect,
console.log('got qr code') qr,
await this.server.db().whatsappBots.updateQR(bot, qr); isNewLogin,
} else if (isNewLogin) { } = update;
console.log("got new login") if (qr) {
await this.server.db().whatsappBots.updateVerified(bot, true); console.log("got qr code");
} else if (connectionState === 'open') { await this.server.db().whatsappBots.updateQR(bot, qr);
console.log('opened connection') } else if (isNewLogin) {
} else if (connectionState === "close") { console.log("got new login");
console.log('connection closed due to ', lastDisconnect.error) await this.server.db().whatsappBots.updateVerified(bot, true);
const disconnectStatusCode = (lastDisconnect?.error as any)?.output?.statusCode } else if (connectionState === "open") {
console.log("opened connection");
} else if (connectionState === "close") {
console.log("connection closed due to ", lastDisconnect.error);
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode;
if (disconnectStatusCode === DisconnectReason.restartRequired) { if (disconnectStatusCode === DisconnectReason.restartRequired) {
console.log('reconnecting after got new login') console.log("reconnecting after got new login");
const updatedBot = await this.findById(bot.id); const updatedBot = await this.findById(bot.id);
await this.createConnection(updatedBot, server, options) await this.createConnection(updatedBot, server, options);
authCompleteCallback?.() authCompleteCallback?.();
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) { } else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
console.log('reconnecting') console.log("reconnecting");
await this.sleep(pause) await this.sleep(pause);
pause *= 2 pause *= 2;
this.createConnection(bot, server, options) this.createConnection(bot, server, options);
}
}
}
if (events['creds.update']) {
console.log("creds update")
await saveCreds()
}
if (events['messages.upsert']) {
console.log("messages upsert")
const upsert = events['messages.upsert']
const { messages } = upsert
if (messages) {
await this.queueUnreadMessages(bot, messages);
} }
} }
} }
)
if (events["creds.update"]) {
console.log("creds update");
await saveCreds();
}
if (events["messages.upsert"]) {
console.log("messages upsert");
const upsert = events["messages.upsert"];
const { messages } = upsert;
if (messages) {
await this.queueUnreadMessages(bot, messages);
}
}
});
this.connections[bot.id] = { socket, msgRetryCounterMap }; this.connections[bot.id] = { socket, msgRetryCounterMap };
} }
@ -120,31 +139,31 @@ export default class WhatsappService extends Service {
const bots = await this.server.db().whatsappBots.findAll(); const bots = await this.server.db().whatsappBots.findAll();
for await (const bot of bots) { for await (const bot of bots) {
if (bot.isVerified) { if (bot.isVerified) {
const { version, isLatest } = await fetchLatestBaileysVersion() const { version, isLatest } = await fetchLatestBaileysVersion();
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`) console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);
await this.createConnection( await this.createConnection(bot, this.server, {
bot, browser: WhatsappService.browserDescription,
this.server, printQRInTerminal: false,
{ version,
browser: WhatsappService.browserDescription, });
printQRInTerminal: false,
version
})
} }
} }
} }
private async queueMessage(bot: Bot, webMessageInfo: proto.WebMessageInfo) { private async queueMessage(bot: Bot, webMessageInfo: proto.IWebMessageInfo) {
const { key: { id, fromMe, remoteJid }, message, messageTimestamp } = webMessageInfo; const {
key: { id, fromMe, remoteJid },
message,
messageTimestamp,
} = webMessageInfo;
if (!fromMe && message && remoteJid !== "status@broadcast") { if (!fromMe && message && remoteJid !== "status@broadcast") {
const { audioMessage, documentMessage, imageMessage, videoMessage } = message; const { audioMessage, documentMessage, imageMessage, videoMessage } =
const isMediaMessage = audioMessage || message;
documentMessage || const isMediaMessage =
imageMessage || audioMessage || documentMessage || imageMessage || videoMessage;
videoMessage;
const messageContent = Object.values(message)[0] const messageContent = Object.values(message)[0];
let messageType: MediaType; let messageType: MediaType;
let attachment: string; let attachment: string;
let filename: string; let filename: string;
@ -152,8 +171,7 @@ export default class WhatsappService extends Service {
if (isMediaMessage) { if (isMediaMessage) {
if (audioMessage) { if (audioMessage) {
messageType = "audio"; messageType = "audio";
filename = filename = id + "." + audioMessage.mimetype.split("/").pop();
id + "." + audioMessage.mimetype.split("/").pop();
mimetype = audioMessage.mimetype; mimetype = audioMessage.mimetype;
} else if (documentMessage) { } else if (documentMessage) {
messageType = "document"; messageType = "document";
@ -161,20 +179,21 @@ export default class WhatsappService extends Service {
mimetype = documentMessage.mimetype; mimetype = documentMessage.mimetype;
} else if (imageMessage) { } else if (imageMessage) {
messageType = "image"; messageType = "image";
filename = filename = id + "." + imageMessage.mimetype.split("/").pop();
id + "." + imageMessage.mimetype.split("/").pop();
mimetype = imageMessage.mimetype; mimetype = imageMessage.mimetype;
} else if (videoMessage) { } else if (videoMessage) {
messageType = "video" messageType = "video";
filename = filename = id + "." + videoMessage.mimetype.split("/").pop();
id + "." + videoMessage.mimetype.split("/").pop();
mimetype = videoMessage.mimetype; mimetype = videoMessage.mimetype;
} }
const stream = await downloadContentFromMessage(messageContent, messageType) const stream = await downloadContentFromMessage(
let buffer = Buffer.from([]) messageContent,
messageType
);
let buffer = Buffer.from([]);
for await (const chunk of stream) { for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]) buffer = Buffer.concat([buffer, chunk]);
} }
attachment = buffer.toString("base64"); attachment = buffer.toString("base64");
} }
@ -198,7 +217,10 @@ export default class WhatsappService extends Service {
} }
} }
private async queueUnreadMessages(bot: Bot, messages: proto.IWebMessageInfo[]) { private async queueUnreadMessages(
bot: Bot,
messages: proto.IWebMessageInfo[]
) {
for await (const message of messages) { for await (const message of messages) {
await this.queueMessage(bot, message); await this.queueMessage(bot, message);
} }
@ -244,7 +266,7 @@ export default class WhatsappService extends Service {
} }
async register(bot: Bot, callback: AuthCompleteCallback): Promise<void> { async register(bot: Bot, callback: AuthCompleteCallback): Promise<void> {
const { version } = await fetchLatestBaileysVersion() const { version } = await fetchLatestBaileysVersion();
await this.createConnection(bot, this.server, { version }, callback); await this.createConnection(bot, this.server, { version }, callback);
} }
@ -265,7 +287,10 @@ export default class WhatsappService extends Service {
} }
} }
async receive(bot: Bot, _lastReceivedDate: Date): Promise<proto.IWebMessageInfo[]> { async receive(
bot: Bot,
_lastReceivedDate: Date
): Promise<proto.IWebMessageInfo[]> {
const connection = this.connections[bot.id]?.socket; const connection = this.connections[bot.id]?.socket;
const messages = await connection.loadAllUnreadMessages(); const messages = await connection.loadAllUnreadMessages();
return messages; return messages;

View file

@ -30,4 +30,5 @@ declare module "@hapipal/schmervice" {
interface SchmerviceDecorator { interface SchmerviceDecorator {
(namespace: "app"): AppServices; (namespace: "app"): AppServices;
} }
type ServiceFunctionalInterface = { name: string };
} }

View file

@ -6,18 +6,54 @@
"persistent": true "persistent": true
}, },
"build": { "build": {
"dependsOn": ["^build"], "dependsOn": [
"outputs": [".next/**"] "^build"
],
"outputs": [
".next/**"
]
}, },
"test": { "test": {
"dependsOn": ["build"], "dependsOn": [
"inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts", "test/**/*.tsx"] "build"
],
"inputs": [
"src/**/*.tsx",
"src/**/*.ts",
"test/**/*.ts",
"test/**/*.tsx"
]
},
"lint": {
"inputs": [
"src/**/*.tsx",
"src/**/*.ts",
"test/**/*.ts",
"test/**/*.tsx"
]
},
"fix:lint": {
"inputs": [
"src/**/*.tsx",
"src/**/*.ts",
"test/**/*.ts",
"test/**/*.tsx"
]
},
"fmt": {
"inputs": [
"src/**/*.tsx",
"src/**/*.ts",
"test/**/*.ts",
"test/**/*.tsx"
]
}, },
"lint": {},
"fix:lint": {},
"fmt": {},
"deploy": { "deploy": {
"dependsOn": ["build", "test", "lint"] "dependsOn": [
"build",
"test",
"lint"
]
} }
} }
} }