mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-14 20:15:28 +10:00
add methods to messageHelper to parse through attachment URLs
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user