diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index 2dfeaaa4..124c1049 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -331,12 +331,50 @@ export const getRootDiff = async (storeId, root1, root2) => { }; const _addMirror = async (storeId, url) => { + const mirrors = await getMirrors(storeId); + + // Dont add the mirror if it already exists. + if ( + mirrors.find( + (mirror) => mirror.launcher_id === storeId && mirror.urls.includes(url), + ).length === 0 + ) { + return true; + } + + try { + const options = { + url: `${rpcUrl}/add_mirror`, + body: JSON.stringify({ + id: storeId, + urls: [url], + amount: 1, + }), + }; + + const response = await request( + Object.assign({}, getBaseOptions(), options), + ); + + const data = JSON.parse(response); + + if (data.success) { + logger.info(`Adding mirror ${storeId} at ${url}`); + return true; + } + + logger.error(`FAILED ADDING MIRROR FOR ${storeId}`); + return false; + } catch (error) { + return false; + } +}; + +const getMirrors = async (storeId) => { const options = { - url: `${rpcUrl}/add_mirror`, + url: `${rpcUrl}/get_mirrors `, body: JSON.stringify({ id: storeId, - urls: [url], - amount: 1, }), }; @@ -348,11 +386,10 @@ const _addMirror = async (storeId, url) => { const data = JSON.parse(response); if (data.success) { - logger.info(`Adding mirror ${storeId} at ${url}`); - return true; + return data.mirrors; } - logger.error(`FAILED ADDING MIRROR FOR ${storeId}`); + logger.error(`FAILED GETTING MIRRORS FOR ${storeId}`); return false; } catch (error) { return false;