From 7120044b87cc55b0a9b30ffaa593f3692fcefbfe Mon Sep 17 00:00:00 2001 From: outaTiME Date: Thu, 22 Feb 2024 12:01:56 -0300 Subject: [PATCH] refactor: remove legacy whatsapp social --- .../fluent-ffmpeg-npm-2.1.2-692c218f68.patch | 7 - .../whatsapp-web.js-https-a2405ef4ed.patch | 79 ----------- package.json | 3 +- packages/api/package.json | 5 +- .../api/src/libs/social/whatsapp-legacy.js | 133 ------------------ sst.config.js | 3 - stacks/MainStack.js | 37 +---- 7 files changed, 7 insertions(+), 260 deletions(-) delete mode 100644 .yarn/patches/fluent-ffmpeg-npm-2.1.2-692c218f68.patch delete mode 100644 .yarn/patches/whatsapp-web.js-https-a2405ef4ed.patch delete mode 100644 packages/api/src/libs/social/whatsapp-legacy.js diff --git a/.yarn/patches/fluent-ffmpeg-npm-2.1.2-692c218f68.patch b/.yarn/patches/fluent-ffmpeg-npm-2.1.2-692c218f68.patch deleted file mode 100644 index d0adaec..0000000 --- a/.yarn/patches/fluent-ffmpeg-npm-2.1.2-692c218f68.patch +++ /dev/null @@ -1,7 +0,0 @@ -diff --git a/index.js b/index.js -index 04e594c12fcf56fe767f0ec71910ab797b90491f..68a1522e052e72e70ce862c2ad50acca25bd9570 100644 ---- a/index.js -+++ b/index.js -@@ -1 +1 @@ --module.exports = process.env.FLUENTFFMPEG_COV ? require('./lib-cov/fluent-ffmpeg') : require('./lib/fluent-ffmpeg'); -+module.exports = require('./lib/fluent-ffmpeg'); diff --git a/.yarn/patches/whatsapp-web.js-https-a2405ef4ed.patch b/.yarn/patches/whatsapp-web.js-https-a2405ef4ed.patch deleted file mode 100644 index d1b09da..0000000 --- a/.yarn/patches/whatsapp-web.js-https-a2405ef4ed.patch +++ /dev/null @@ -1,79 +0,0 @@ -diff --git a/package.json b/package.json -index ac4c60e2cd783d686ac0a18132b4dcd8a5a0e8aa..14d18fc7585670119e8e18458d1ccb7676c6b27b 100644 ---- a/package.json -+++ b/package.json -@@ -52,8 +52,8 @@ - "node": ">=12.0.0" - }, - "optionalDependencies": { -+ "adm-zip": "^0.5.10", - "archiver": "^5.3.1", -- "fs-extra": "^10.1.0", -- "unzipper": "^0.10.11" -+ "fs-extra": "^10.1.0" - } - } -diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js -index 99e688ba2fd33a661c54d9a11447a8061accaa34..0e6585325296b29f4fe0de23eef5ed95c81b515f 100644 ---- a/src/authStrategies/RemoteAuth.js -+++ b/src/authStrategies/RemoteAuth.js -@@ -3,11 +3,11 @@ - /* Require Optional Dependencies */ - try { - var fs = require('fs-extra'); -- var unzipper = require('unzipper'); -+ var AdmZip = require('adm-zip'); - var archiver = require('archiver'); - } catch { - fs = undefined; -- unzipper = undefined; -+ AdmZip = undefined; - archiver = undefined; - } - -@@ -20,12 +20,12 @@ const BaseAuthStrategy = require('./BaseAuthStrategy'); - * @param {object} options - options - * @param {object} options.store - Remote database store instance - * @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance -- * @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/" -+ * @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/" - * @param {number} options.backupSyncIntervalMs - Sets the time interval for periodic session backups. Accepts values starting from 60000ms {1 minute} - */ - class RemoteAuth extends BaseAuthStrategy { - constructor({ clientId, dataPath, store, backupSyncIntervalMs } = {}) { -- if (!fs && !unzipper && !archiver) throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag'); -+ if (!fs && !AdmZip && !archiver) throw new Error('Optional Dependencies [fs-extra, adm-zip, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag'); - super(); - - const idRegex = /^[-_\w]+$/i; -@@ -154,13 +154,15 @@ class RemoteAuth extends BaseAuthStrategy { - } - - async unCompressSession(compressedSessionPath) { -- var stream = fs.createReadStream(compressedSessionPath); - await new Promise((resolve, reject) => { -- stream.pipe(unzipper.Extract({ -- path: this.userDataDir -- })) -- .on('error', err => reject(err)) -- .on('finish', () => resolve()); -+ var zip = new AdmZip(compressedSessionPath); -+ zip.extractAllToAsync(this.userDataDir, false, false, (err) => { -+ if (err) { -+ reject(err); -+ } else { -+ resolve(); -+ } -+ }); - }); - await fs.promises.unlink(compressedSessionPath); - } -@@ -173,7 +175,7 @@ class RemoteAuth extends BaseAuthStrategy { - if (!this.requiredDirs.includes(element)) { - const dirElement = path.join(dir, element); - const stats = await fs.promises.lstat(dirElement); -- -+ - if (stats.isDirectory()) { - await fs.promises.rm(dirElement, { - recursive: true, diff --git a/package.json b/package.json index 3dcfcce..86507ed 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ }, "packageManager": "yarn@4.1.0", "resolutions": { - "babel-plugin-lodash/@babel/types": "~7.20.0", - "fluent-ffmpeg@npm:2.1.2": "patch:fluent-ffmpeg@npm%3A2.1.2#~/.yarn/patches/fluent-ffmpeg-npm-2.1.2-692c218f68.patch" + "babel-plugin-lodash/@babel/types": "~7.20.0" } } diff --git a/packages/api/package.json b/packages/api/package.json index 4928501..0c09607 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -24,12 +24,9 @@ "object-hash": "^3.0.0", "pretty-ms": "^9.0.0", "puppeteer-core": "~21.9.0", - "qrcode-terminal": "^0.12.0", "reddit": "^1.2.1", "semver": "^7.6.0", "sharp": "^0.32.6", - "twitter-api-v2": "^1.16.0", - "whatsapp-web.js": "patch:whatsapp-web.js@https%3A//github.com/alechkos/whatsapp-web.js.git%23commit=bc250c7e99ec36f98865240413c5d50da8d512ae#~/.yarn/patches/whatsapp-web.js-https-a2405ef4ed.patch", - "wwebjs-aws-s3": "^1.0.2" + "twitter-api-v2": "^1.16.0" } } diff --git a/packages/api/src/libs/social/whatsapp-legacy.js b/packages/api/src/libs/social/whatsapp-legacy.js deleted file mode 100644 index 274d331..0000000 --- a/packages/api/src/libs/social/whatsapp-legacy.js +++ /dev/null @@ -1,133 +0,0 @@ -import { - PutObjectCommand as putObjectCommand, - HeadObjectCommand as headObjectCommand, - GetObjectCommand as getObjectCommand, - DeleteObjectCommand as deleteObjectCommand, -} from '@aws-sdk/client-s3'; -import chromium from '@sparticuz/chromium'; -import prettyMilliseconds from 'pretty-ms'; -import puppeteer from 'puppeteer-core'; -import qrcode from 'qrcode-terminal'; -import { AwsS3Store } from 'wwebjs-aws-s3'; - -import Shared, { S3_BUCKET } from '../shared'; - -// cjs format required by aws lambda layer -const { Client, RemoteAuth, MessageMedia } = require('whatsapp-web.js'); - -const getClient = async () => { - // write permissions required for restore auth - !process.env.IS_LOCAL && process.chdir('/tmp'); - // wwebjs with remote auth - const s3Client = Shared.getS3Client(); - const store = new AwsS3Store({ - bucketName: S3_BUCKET, - remoteDataPath: 'wwebjs', - s3Client, - putObjectCommand, - headObjectCommand, - getObjectCommand, - deleteObjectCommand, - }); - return new Client({ - puppeteer: { - args: process.env.IS_LOCAL ? puppeteer.defaultArgs() : chromium.args, - defaultViewport: chromium.defaultViewport, - executablePath: process.env.IS_LOCAL - ? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' - : await chromium.executablePath(), - headless: process.env.IS_LOCAL ? false : chromium.headless, - }, - authStrategy: new RemoteAuth({ - // clientId: 'wwebjs-auth-session', - // store on ephemeral storage - dataPath: '/tmp/.wwebjs_auth', - store, - backupSyncIntervalMs: 1 * 60 * 60 * 1000, // 1 hour - }), - webVersionCache: { - type: 'none', - }, - }); -}; - -// available in local mode to create and persist a session -export const handler = () => - new Promise(async (resolve) => { - const start_time = Date.now(); - const client = await getClient(); - client.on('qr', (qr) => { - console.info('QR code received', JSON.stringify({ qr })); - qrcode.generate(qr, { small: true }); - }); - client.on('authenticated', () => { - console.info('Authenticated'); - }); - client.on('auth_failure', (msg) => { - // fired if session restore was unsuccessful - console.error( - 'Authentication failed', - JSON.stringify({ - error: msg, - }), - ); - resolve( - Shared.serviceResponse(null, 400, { - error: msg, - }), - ); - }); - client.on('remote_session_saved', () => { - console.info('Session saved remotely on S3'); - }); - client.on('ready', () => { - console.info('Initialization completed'); - const duration = Date.now() - start_time; - resolve( - Shared.serviceResponse(null, 200, { - duration: prettyMilliseconds(duration), - }), - ); - }); - client.initialize(); - }); - -export const publish = (caption, image_url) => - new Promise(async (resolve, reject) => { - // const start_time = Date.now(); - const client = await getClient(); - client.on('authenticated', () => { - console.info('[WHATSAPP] Authenticated'); - }); - client.on('auth_failure', (msg) => { - // fired if session restore was unsuccessful - console.error( - '[WHATSAPP] Authentication failed', - JSON.stringify({ - error: msg, - }), - ); - reject(new Error(msg)); - }); - client.on('ready', async () => { - console.info('[WHATSAPP] Initialization completed'); - const channelId = process.env.WHATSAPP_CHANNEL_ID; - try { - const media = image_url && (await MessageMedia.fromUrl(image_url)); - const { id: { id } = {}, type } = await client.sendMessage( - channelId, - caption, - { ...(media && { media }) }, - ); - // const duration = Date.now() - start_time; - resolve({ - id, - type, - // duration: prettyMilliseconds(duration), - }); - } catch (error) { - reject(error); - } - }); - client.initialize(); - }); diff --git a/sst.config.js b/sst.config.js index 8309396..ed995d2 100644 --- a/sst.config.js +++ b/sst.config.js @@ -44,9 +44,6 @@ export default { BSKY_PASSWORD: process.env.BSKY_PASSWORD, WHATSAPP_CHANNEL_ID: process.env.WHATSAPP_CHANNEL_ID, WHAPI_TOKEN: process.env.WHAPI_TOKEN, - // https://pptr.dev/api/puppeteer.configuration - // PUPPETEER_SKIP_DOWNLOAD: 'true', - STORE_DEBUG: 'true', // https://aws.amazon.com/es/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/ // NODE_OPTIONS: '--no-experimental-fetch', // prevents experimental warnings from buffer.File diff --git a/stacks/MainStack.js b/stacks/MainStack.js index e72946c..f9068aa 100644 --- a/stacks/MainStack.js +++ b/stacks/MainStack.js @@ -4,9 +4,8 @@ import * as s3 from 'aws-cdk-lib/aws-s3'; import { SubscriptionFilter } from 'aws-cdk-lib/aws-sns'; import { Api, Function, StaticSite, Topic, Cron } from 'sst/constructs'; -export function MainStack({ stack /*, app */ }) { +export function MainStack({ stack }) { const IS_PRODUCTION = stack.stage === 'prod'; - // const IS_LOCAL = app.local === true; // existing resources const bucket = s3.Bucket.fromBucketName( stack, @@ -227,22 +226,14 @@ export function MainStack({ stack /*, app */ }) { SOCIAL_SCREENSHOT_URL: screenshotSite.url, }, handler: 'packages/api/src/subscribers/social-notify.handler', - layers: [ - process.env.CHROME_LAYER_ARN, - process.env.SHARP_LAYER_ARN, - // process.env.WWEBJS_LAYER_ARN, - ], + layers: [process.env.CHROME_LAYER_ARN, process.env.SHARP_LAYER_ARN], nodejs: { esbuild: { - external: [ - '@sparticuz/chromium', - 'sharp', - // 'whatsapp-web.js' - ], + external: ['@sparticuz/chromium', 'sharp'], }, }, - // ~2m - timeout: '4 minutes', + // ~60s + timeout: '2 minutes', }, cdk: { subscription: { @@ -313,21 +304,6 @@ export function MainStack({ stack /*, app */ }) { }), }, }); - /* const createWhatsAppSessionFn = - IS_LOCAL && - new Function(stack, 'CreateWhatsAppSession', { - handler: 'packages/api/src/libs/social/whatsapp.handler', - layers: [process.env.CHROME_LAYER_ARN, process.env.WWEBJS_LAYER_ARN], - nodejs: { - esbuild: { - external: ['@sparticuz/chromium', 'whatsapp-web.js'], - }, - }, - permissions: [bucket], - // ~2m - timeout: '4 minutes', - url: true, - }); */ // trace stack stack.addOutputs({ ApiUrl: api.url, @@ -340,8 +316,5 @@ export function MainStack({ stack /*, app */ }) { ...(legacyApi.cdk.domainName && { LegacyApiRegionalDomainName: legacyApi.cdk.domainName.regionalDomainName, }), - /* ...(createWhatsAppSessionFn && { - CreateWhatsAppSession: createWhatsAppSessionFn.url, - }), */ }); }