From f65aeb00199b1792838c8ab60ff607ad3eb90023 Mon Sep 17 00:00:00 2001 From: Aster Fialla Date: Thu, 19 Feb 2026 13:49:37 -0500 Subject: [PATCH] add methods to messageHelper to parse through attachment URLs --- package.json | 1 + src/helpers/messageHelper.js | 56 +++++++++++++++++++++++++++-- tests/helpers/messageHelper.test.js | 8 ++--- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c8f1a20..2ddb35e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@fluxerjs/core": "^1.1.5", "dotenv": "^17.3.1", + "node-fetch": "^3.3.2", "pg": "^8.18.0", "pg-hstore": "^2.3.4", "pm2": "^6.0.14", diff --git a/src/helpers/messageHelper.js b/src/helpers/messageHelper.js index b80676d..56b6b60 100644 --- a/src/helpers/messageHelper.js +++ b/src/helpers/messageHelper.js @@ -1,8 +1,6 @@ import {memberHelper} from "./memberHelper.js"; -import {enums} from "../enums.js"; import tmp, {setGracefulCleanup} from "tmp"; -import fs from 'fs'; -import {Message} from "@fluxerjs/core"; +import fetch from 'node-fetch'; const msgh = {}; @@ -86,4 +84,56 @@ msgh.returnBufferFromText = function (text) { return {text: text, file: undefined} } +/** + * Returns an ArrayBuffer from an attachment URL. + * + * @param {string} attachmentUrl + * @returns {ArrayBuffer} The buffer from the image. + * + */ +msgh.returnBufferFromUrl = async function (attachmentUrl) { + retryPromise(() => fetch(attachmentUrl),{ + retryIf: (response) => !response.ok, + retries: 5 + }).then(async(res) => { + return await res.arrayBuffer().catch((err) => { + throw new Error(`Error loading attachment into buffer: ${err.message}`); + }) + }) + +} + +// Source - https://stackoverflow.com/a/70687149 - Arturo Hernandez +function retryPromise(promise, options) { + const { retryIf, retryCatchIf, retries } = { retryIf: () => false, retryCatchIf: () => true, retries: 5, ...options}; + let _promise = promise(); + + for (let i = 1; i < retries; i++) + _promise = _promise.catch((value) => retryCatchIf(value) ? promise() : Promise.reject(value)) + .then((value) => retryIf(value) ? promise() : Promise.reject(value)); + + return _promise; +} + + +/** + * Returns an ArrayBuffer from an attachment URL. + * + * @param {Map} attachments - A collection of attachments from the message object + * @returns {[{string, ArrayBuffer}]} An array of file objects + * + */ +msgh.createFileObjectFromAttachments = async function (attachments) { + if (attachments.size === 0) { + return []; + } + const attachmentsObj = []; + attachments.forEach(async (attachment) => { + await msgh.returnBufferFromUrl(attachment.url).then((res) => { + attachmentsObj.push({name: attachment.filename, data: res}); + }); + }); + return attachmentsObj; +} + export const messageHelper = msgh; diff --git a/tests/helpers/messageHelper.test.js b/tests/helpers/messageHelper.test.js index 9088c46..5496d64 100644 --- a/tests/helpers/messageHelper.test.js +++ b/tests/helpers/messageHelper.test.js @@ -2,10 +2,8 @@ const env = require('dotenv'); env.config(); const {memberHelper} = require("../../src/helpers/memberHelper.js"); -const {Message} = require("@fluxerjs/core"); -const {fs} = require('fs'); const {enums} = require('../../src/enums'); -const {tmp, setGracefulCleanup} = require('tmp'); +const fetch = require('node-fetch'); jest.mock('../../src/helpers/memberHelper.js', () => { return {memberHelper: { @@ -13,7 +11,7 @@ jest.mock('../../src/helpers/memberHelper.js', () => { }} }) -jest.mock('tmp'); +jest.mock('node-fetch'); jest.mock('fs'); jest.mock('@fluxerjs/core'); @@ -48,6 +46,7 @@ describe('messageHelper', () => { , {name: "someOtherPerson", proxy: "?text}"}, {name: "someLastPerson", proxy: "{text}"}, {name: "someEmojiPerson", proxy: "⭐text"}, + {name: "someSpacePerson", proxy: "-- text"}, ] const membersFor2 = [] @@ -75,6 +74,7 @@ describe('messageHelper', () => { ['1', '?hello}', null, {member: membersFor1[3], message: 'hello', hasAttachment: false}], ['1', '{hello}', null, {member: membersFor1[4], message: 'hello', hasAttachment: false}], ['1', '⭐hello', null, {member: membersFor1[5], message: 'hello', hasAttachment: false}], + ['1', '-- hello', null, {member: membersFor1[5], message: 'hello', hasAttachment: false}] ['2', 'hello', null, undefined], ['2', '--hello', null, undefined], ['2', 'hello', attachmentUrl, undefined],