From 0f6b4050df67bcb14a56f25aa114340b4258c511 Mon Sep 17 00:00:00 2001 From: mrfry Date: Sun, 2 Apr 2023 13:20:15 +0200 Subject: [PATCH] added post redirects --- src/modules/api/submodules/p2p.ts | 39 ++++++++++++++++++++++++++----- src/modules/qmining/qmining.ts | 16 +++++++++++++ submodules/qmining-data-editor | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/modules/api/submodules/p2p.ts b/src/modules/api/submodules/p2p.ts index 25ff9fe..8e24dd6 100644 --- a/src/modules/api/submodules/p2p.ts +++ b/src/modules/api/submodules/p2p.ts @@ -103,6 +103,7 @@ interface SyncDataRes { } } +// FIXME: to utils/http.ts function get(options: http.RequestOptions): Promise> { return new Promise((resolve) => { const req = http.get(options, function (res) { @@ -126,7 +127,27 @@ function get(options: http.RequestOptions): Promise> { }) } -function peerToString(peer: PeerInfo) { +function updateThirdPartyPeers( + newVal: Omit[] +) { + const prevVal = utils.ReadJSON(paths.thirdPartyPeersFile) + const dataToWrite = newVal.reduce((acc, peer) => { + const isIncluded = acc.find((x) => { + return peerToString(x) === peerToString(peer) + }) + if (!isIncluded) { + return [...acc, peer] + } + return acc + }, prevVal) + + utils.WriteFile( + JSON.stringify(dataToWrite, null, 2), + paths.thirdPartyPeersFile + ) +} + +function peerToString(peer: { host: string; port: string | number }) { return `${peer.host}:${peer.port}` } @@ -655,10 +676,7 @@ function setup(data: SubmoduleData): Submodule { ) }) if (thirdPartyPeers.length > 0) { - utils.WriteFile( - JSON.stringify(thirdPartyPeers, null, 2), - paths.thirdPartyPeersFile - ) + updateThirdPartyPeers(thirdPartyPeers) logger.Log( `\tPeers reported ${logger.C('green')}${ thirdPartyPeers.length @@ -899,7 +917,6 @@ function setup(data: SubmoduleData): Submodule { // TODO: get all user files app.get('/getnewdatasince', (req: Request, res: Response) => { - // TODO: add to third party peers, and recieve self info // FIXME: hash question db to see if different? // it could help in determining if it should be checked for new data, but it would only save // a getNewDataSince() call per question db @@ -968,6 +985,16 @@ function setup(data: SubmoduleData): Submodule { 'use it as a peer.', 'yellowbg' ) + if (remoteHost.includes(':')) { + const [host, port] = remoteHost.split(':') + updateThirdPartyPeers([ + { + host: host, + port: +port, + }, + ]) + logger.Log('Host info written to host info file') + } } } diff --git a/src/modules/qmining/qmining.ts b/src/modules/qmining/qmining.ts index afc13b7..6200d39 100644 --- a/src/modules/qmining/qmining.ts +++ b/src/modules/qmining/qmining.ts @@ -186,6 +186,11 @@ function GetApp(): ModuleType { from: '/patreon', to: links.patreon, }, + // ----------------------------------- + { + from: '/hasNewMsgs', + to: 'api/hasNewMsgs', + }, ] simpleRedirects.forEach((redirect) => { @@ -208,6 +213,17 @@ function GetApp(): ModuleType { }) }) + const postRedirects = [ + { from: '/ask', to: '/api/ask' }, + { from: '/isAdding', to: '/api/isAdding' }, + ] + + postRedirects.forEach((redirect) => { + app.post(redirect.from, function (_req: Request, res) { + res.redirect(307, redirect.to) + }) + }) + // -------------------------------------------------------------- function AddHtmlRoutes(files: string[]) { diff --git a/submodules/qmining-data-editor b/submodules/qmining-data-editor index b9abbbf..39dfd7a 160000 --- a/submodules/qmining-data-editor +++ b/submodules/qmining-data-editor @@ -1 +1 @@ -Subproject commit b9abbbf03c111ea5b2534681d7b11de8f8a79baa +Subproject commit 39dfd7a0f48d850b543d38d607320236038f54fb