From 0b3f7a81413db84c64e8f8e8798a7eebb8e186bc Mon Sep 17 00:00:00 2001 From: boufni95 Date: Mon, 25 May 2020 17:47:30 +0200 Subject: [PATCH 01/38] gun err --- src/routes.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/src/routes.js b/src/routes.js index 594f226e..1f835db5 100644 --- a/src/routes.js +++ b/src/routes.js @@ -433,6 +433,70 @@ module.exports = async ( GunActions.saveChannelsBackup(JSON.stringify(channelBackups),user,SEA) }); + /* + const feedObj = { + feed: [ + { + id:'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', + paragraphs:[ + "SOme text and stuff" + ], + profilePic:"", + username:"bobni", + media:[ + { + type:'VIDEO', + ratio_x: 1024, + ratio_y: 436, + magnetUri:'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent', + }, + ] + }, + { + id:'3ac68afc-c605-48d3-a4f8-fbd91aa97f63', + paragraphs:[ + "SOme text and stuff" + ], + profilePic:"", + username:"bobni", + media:[ + { + type:'VIDEO', + ratio_x: 1920, + ratio_y: 804, + magnetUri:'magnet:?xt=urn:btih:c9e15763f722f23e98a29decdfae341b98d53056&dn=Cosmos+Laundromat&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fcosmos-laundromat.torrent', + }, + ] + }, + { + id:'58694a0f-3da1-471f-bd96-145571e29d72', + paragraphs:[ + "SOme text and stuff" + ], + profilePic:"", + username:"bobni", + media:[ + { + type:'VIDEO', + ratio_x: 1920, + ratio_y: 1080, + magnetUri:'magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent', + }, + ] + } + ] + }*/ + const feedObj = { + feed :{} + } + user.get("FEED_POC").put(feedObj, ack => { + if (ack.err) { + //rej(ack.err) + logger.log(ack.err) + } else { + logger.log(ack.err) + } + }) //register to listen for channel backups const onNewChannelBackup = () => { @@ -1614,11 +1678,10 @@ module.exports = async ( res.json(channelBackups); }); }); - + const {timeout5} = require('../services/gunDB/contact-api/utils') const GunEvent = Common.Constants.Event const Key = require('../services/gunDB/contact-api/key') - const { timeout5 } = require('../services/gunDB/contact-api/utils') - + app.get("/api/gun/lndchanbackups", async (req,res) => { try{ const user = require('../services/gunDB/Mediator').getUser() @@ -1633,9 +1696,21 @@ module.exports = async ( res.json({ok:"err"}) } }) + app.get("api/gun/feedpoc", async (req,res) =>{ + try{ + logger.warn("FEED POC") + const user = require('../services/gunDB/Mediator').getUser() + const feedObj = await timeout5(user.get("FEED_POC").then()) + logger.warn(feedObj) + + res.json({data:"feedObj"}) + } catch (err) { + //res.json({ok:"err"}) + } + }) const Events = require('../services/gunDB/contact-api/events') - + app.get(`/api/gun/${GunEvent.ON_RECEIVED_REQUESTS}`, (_, res) => { try { // spinup From c75e0902f8829b52faed4bc308de869ed4031f43 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Mon, 25 May 2020 18:38:43 +0200 Subject: [PATCH 02/38] redux ok gun ok --- src/routes.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/routes.js b/src/routes.js index 1f835db5..cba84101 100644 --- a/src/routes.js +++ b/src/routes.js @@ -433,13 +433,13 @@ module.exports = async ( GunActions.saveChannelsBackup(JSON.stringify(channelBackups),user,SEA) }); - /* + const feedObj = { feed: [ { id:'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', paragraphs:[ - "SOme text and stuff" + "SOme text and stuff 12" ], profilePic:"", username:"bobni", @@ -485,11 +485,8 @@ module.exports = async ( ] } ] - }*/ - const feedObj = { - feed :{} } - user.get("FEED_POC").put(feedObj, ack => { + user.get("FEED_POC").put(JSON.stringify(feedObj), ack => { if (ack.err) { //rej(ack.err) logger.log(ack.err) @@ -1696,14 +1693,14 @@ module.exports = async ( res.json({ok:"err"}) } }) - app.get("api/gun/feedpoc", async (req,res) =>{ + app.get("/api/gun/feedpoc", async (req,res) =>{ try{ logger.warn("FEED POC") const user = require('../services/gunDB/Mediator').getUser() const feedObj = await timeout5(user.get("FEED_POC").then()) logger.warn(feedObj) - res.json({data:"feedObj"}) + res.json({data:feedObj}) } catch (err) { //res.json({ok:"err"}) } From f1e5f3d04a0c18655ee6ceaa0f47113e39886513 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 16:26:57 -0400 Subject: [PATCH 03/38] move import to top --- src/routes.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/routes.js b/src/routes.js index cba84101..cda2e7e3 100644 --- a/src/routes.js +++ b/src/routes.js @@ -33,6 +33,8 @@ module.exports = async ( mySocketsEvents, { serverPort, CA, CA_KEY, usetls } ) => { + const {timeout5} = require('../services/gunDB/contact-api/utils') + const Http = Axios.create({ httpsAgent: new httpsAgent.Agent({ ca: await FS.readFile(CA) @@ -1675,7 +1677,6 @@ module.exports = async ( res.json(channelBackups); }); }); - const {timeout5} = require('../services/gunDB/contact-api/utils') const GunEvent = Common.Constants.Event const Key = require('../services/gunDB/contact-api/key') @@ -1707,7 +1708,7 @@ module.exports = async ( }) const Events = require('../services/gunDB/contact-api/events') - + app.get(`/api/gun/${GunEvent.ON_RECEIVED_REQUESTS}`, (_, res) => { try { // spinup From 254925b0e28e20e0eed9152e566217ba02fd0db5 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 18:52:51 -0400 Subject: [PATCH 04/38] upgrade shock-common --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 6221484f..82894a49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5877,9 +5877,9 @@ shellwords@^0.1.1: integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== shock-common@0.x.x: - version "0.2.0" - resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-0.2.0.tgz#b942d4b8730890aaf192eb873957c235333eddca" - integrity sha512-K9UysxR4LnArhljK3kRNdRM2yeI52YxPe0dZ62SNvsHO5oJgE6wDV0qGy9UZYS5FoAprF5QRzzADxgqmGWin+Q== + version "0.3.0" + resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-0.3.0.tgz#2e8a9c2cd1eda300a8461ea9648d250237f22f78" + integrity sha512-K0xOKkrOAvD4rlWVQD5WXASiKyfatEX2cj114LC8Wddi3wXpex5yLNdWBXNGFpL6Vc5Wzhza/sEHs4p+Q3xBmw== dependencies: lodash "^4.17.15" normalizr "^3.6.0" From 6e7497eb3973edaedd22514eda69c5c46d3c58b7 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 18:55:12 -0400 Subject: [PATCH 05/38] don't lint typescript --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 52b8992c..a1b337c1 100644 --- a/package.json +++ b/package.json @@ -78,9 +78,12 @@ "typescript": "^3.6.3" }, "lint-staged": { - "*.{js,ts}": [ + "*.js": [ "prettier --check", "eslint" + ], + "*.ts": [ + "prettier --check" ] }, "husky": { From 36ca9b438fc42f67382e809c8ffe0a71de6f5154 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 18:55:40 -0400 Subject: [PATCH 06/38] gun.load() typings --- services/gunDB/contact-api/SimpleGUN.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/gunDB/contact-api/SimpleGUN.ts b/services/gunDB/contact-api/SimpleGUN.ts index 25f9e8ba..0929b854 100644 --- a/services/gunDB/contact-api/SimpleGUN.ts +++ b/services/gunDB/contact-api/SimpleGUN.ts @@ -38,6 +38,9 @@ export interface Soul { export type OpenListenerData = Primitive | null | OpenListenerDataObj export type OpenListener = (data: OpenListenerData, key: string) => void +export type LoadListenerData = OpenListenerData +export type LoadListener = (data: LoadListenerData, key: string) => void + export interface GUNNodeBase { _: Soul @@ -48,6 +51,8 @@ export interface GUNNodeBase { open(this: GUNNode, cb?: OpenListener): GUNNode + load(this: GUNNode, cb?: LoadListener): GUNNode + off(): void user(): UserGUNNode user(epub: string): GUNNode From 2d0619b3c57cf4bfc7df5ff6913e2af285bdb05f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 18:56:35 -0400 Subject: [PATCH 07/38] follows key --- services/gunDB/contact-api/key.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/gunDB/contact-api/key.js b/services/gunDB/contact-api/key.js index be14b788..837f38e9 100644 --- a/services/gunDB/contact-api/key.js +++ b/services/gunDB/contact-api/key.js @@ -42,3 +42,5 @@ exports.CHANNELS_BACKUP = 'channelsBackup' exports.LAST_SEEN_APP = 'lastSeenApp' exports.LAST_SEEN_NODE = 'lastSeenNode' + +exports.FOLLOWS = 'follows' From 424f199b4efd9b4d7b0ea10e1822b6dc4d649166 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 18:57:24 -0400 Subject: [PATCH 08/38] follows endpoints --- src/routes.js | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/routes.js b/src/routes.js index cda2e7e3..d2e3621b 100644 --- a/src/routes.js +++ b/src/routes.js @@ -12,6 +12,7 @@ const httpsAgent = require("https"); const responseTime = require("response-time"); const uuid = require("uuid/v4"); const Common = require('shock-common') +const fromPairs = require('lodash/fromPairs') const getListPage = require("../utils/paginate"); @@ -1830,6 +1831,110 @@ module.exports = async ( } }) + ///////////////////////////////// + + /** + * @typedef {import('express-serve-static-core').Application} Application + * @typedef {import('express-serve-static-core').Response} Response + * @typedef {import('shock-common').APISchema.FollowRequest} FollowRequest + * @typedef {import('shock-common').APISchema.UnfollowRequest} UnfollowRequest + * @typedef {import('shock-common').APISchema.GetFollowsResponse} GetFollowsResponse + * @typedef {import('shock-common').Schema.Follow} Follow + */ + + const ap = /** @type {Application} */ (app); + + /** + * @param {*} _ + * @param {Response} res + */ + const apiGunFollowsGet = async (_, res) => { + try { + const user = require('../services/gunDB/Mediator').getUser(); + + /** + * @type {import('../services/gunDB/contact-api/SimpleGUN').OpenListenerData} + */ + const followsData = await timeout5(new Promise(res => { + user.get(Key.FOLLOWS).load(data => { + res(data) + }) + })) + + /** + * @type {GetFollowsResponse} + */ + const response = { + follows: {} + } + + if (Common.Schema.isObj(followsData)) { + response.follows = fromPairs( + Object.entries(followsData) + .filter(([_, f]) => Common.Schema.isFollow(f)) + ) + } else { + logger.warn('followsData not a map, expected for old users'); + } + + return res.status(200).json(response) + } catch (err) { + return res.json(500).json({ + errorMessage: err.message + }) + } + } + + + /** + * @param {{ body: FollowRequest }} req + * @param {Response} res + */ + const apiGunFollowsPost = (req, res) => { + try { + const user = require('../services/gunDB/Mediator').getUser() + const { body: { publicKey }} = req + + /** @type {Follow} */ + const newFollow = { + private: false, + status: 'ok', + user: publicKey + } + + user.get(Key.FOLLOWS).get(publicKey).put(newFollow) + + return res.status(200) + } catch (err) { + return res.status(500).json({ + errorMessage: err.message || 'Unknown error inside /api/gun/follow' + }) + } + } + + /** + * @param {{ body: UnfollowRequest }} req + * @param {Response} res + */ + const apiGunFollowsDelete = (req, res) => { + try { + const user = require('../services/gunDB/Mediator').getUser() + const { body: { publicKey }} = req + + user.get(Key.FOLLOWS).get(publicKey).put(null) + + return res.status(200) + } catch (err) { + return res.status(500).json({ + errorMessage: err.message || 'Unknown error inside /api/gun/follow' + }) + } + } + + ap.get('api/gun/follows', apiGunFollowsGet) + ap.post(`/api/gun/follows`, apiGunFollowsPost) + ap.delete(`api/gun/follows`, apiGunFollowsDelete) + /** * Return app so that it can be used by express. */ From 11c5337e0fe175b4ae7e1dc48ea340cef48068e9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 19:29:54 -0400 Subject: [PATCH 09/38] use route params for follows delete --- src/routes.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/routes.js b/src/routes.js index d2e3621b..aa6f34dd 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1916,10 +1916,13 @@ module.exports = async ( * @param {{ body: UnfollowRequest }} req * @param {Response} res */ - const apiGunFollowsDelete = (req, res) => { + const apiGunFollowsDelete = (req, res) => { try { const user = require('../services/gunDB/Mediator').getUser() - const { body: { publicKey }} = req + /** + * @type {string} + */ + const publicKey = req.params.publickey user.get(Key.FOLLOWS).get(publicKey).put(null) @@ -1933,7 +1936,7 @@ module.exports = async ( ap.get('api/gun/follows', apiGunFollowsGet) ap.post(`/api/gun/follows`, apiGunFollowsPost) - ap.delete(`api/gun/follows`, apiGunFollowsDelete) + ap.delete(`api/gun/follows/:publickey`, apiGunFollowsDelete) /** * Return app so that it can be used by express. From e402ea3a946049819c107c10895719562c6f6982 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 22:10:11 -0400 Subject: [PATCH 10/38] remove dup identifier --- src/routes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes.js b/src/routes.js index aa6f34dd..f0708ac5 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1678,6 +1678,7 @@ module.exports = async ( res.json(channelBackups); }); }); + const GunEvent = Common.Constants.Event const Key = require('../services/gunDB/contact-api/key') From 6ba6d70a5f811a953e2db2f6ee8b7e40162bad97 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 22:40:48 -0400 Subject: [PATCH 11/38] test code --- src/routes.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/routes.js b/src/routes.js index f0708ac5..48327ed3 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1891,7 +1891,7 @@ module.exports = async ( * @param {{ body: FollowRequest }} req * @param {Response} res */ - const apiGunFollowsPost = (req, res) => { + const apiGunFollowsPost = async (req, res) => { try { const user = require('../services/gunDB/Mediator').getUser() const { body: { publicKey }} = req @@ -1903,9 +1903,24 @@ module.exports = async ( user: publicKey } - user.get(Key.FOLLOWS).get(publicKey).put(newFollow) + console.log('NEWFOLLOW WILL POST ' + publicKey) - return res.status(200) + await new Promise((res, rej) => { + user.get(Key.FOLLOWS).get(publicKey).put(newFollow, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + + console.log('NEWFOLLOW POSTED') + // eslint-disable-next-line no-process-exit + process.exit(1) + + return res.status(200).send() } catch (err) { return res.status(500).json({ errorMessage: err.message || 'Unknown error inside /api/gun/follow' From ea51bef3adfef06edc55605d700db9533e966c05 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 22:46:23 -0400 Subject: [PATCH 12/38] more tst code --- src/routes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes.js b/src/routes.js index 48327ed3..4fc4c494 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1892,6 +1892,7 @@ module.exports = async ( * @param {Response} res */ const apiGunFollowsPost = async (req, res) => { + process.exit(1) try { const user = require('../services/gunDB/Mediator').getUser() const { body: { publicKey }} = req From 0bfd082658f5fc78524cf4c8188af739af9f5759 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 30 May 2020 22:57:38 -0400 Subject: [PATCH 13/38] follows -> follow --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 4fc4c494..95edb09f 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1952,7 +1952,7 @@ module.exports = async ( } ap.get('api/gun/follows', apiGunFollowsGet) - ap.post(`/api/gun/follows`, apiGunFollowsPost) + ap.post(`/api/gun/follow`, apiGunFollowsPost) ap.delete(`api/gun/follows/:publickey`, apiGunFollowsDelete) /** From 720732ba98ea7e9bb0564670e0dab9f3f779b932 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 10:16:38 -0400 Subject: [PATCH 14/38] add leading dash --- src/routes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes.js b/src/routes.js index 95edb09f..54f428b0 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1951,9 +1951,9 @@ module.exports = async ( } } - ap.get('api/gun/follows', apiGunFollowsGet) + ap.get('/api/gun/follows', apiGunFollowsGet) ap.post(`/api/gun/follow`, apiGunFollowsPost) - ap.delete(`api/gun/follows/:publickey`, apiGunFollowsDelete) + ap.delete(`/api/gun/follows/:publickey`, apiGunFollowsDelete) /** * Return app so that it can be used by express. From 80bb635b7278a93ac268269d8ea12ace6788e3e4 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 11:03:04 -0400 Subject: [PATCH 15/38] typo --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 54f428b0..e1dfb9b2 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1880,7 +1880,7 @@ module.exports = async ( return res.status(200).json(response) } catch (err) { - return res.json(500).json({ + return res.status(500).json({ errorMessage: err.message }) } From 56f04605092f79bc1251323440b0640cd6750379 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 11:03:30 -0400 Subject: [PATCH 16/38] remove debug code --- src/routes.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/routes.js b/src/routes.js index e1dfb9b2..6df95e30 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1892,7 +1892,6 @@ module.exports = async ( * @param {Response} res */ const apiGunFollowsPost = async (req, res) => { - process.exit(1) try { const user = require('../services/gunDB/Mediator').getUser() const { body: { publicKey }} = req @@ -1918,8 +1917,6 @@ module.exports = async ( console.log('NEWFOLLOW POSTED') - // eslint-disable-next-line no-process-exit - process.exit(1) return res.status(200).send() } catch (err) { From f731474dcef9e4d33ab565c28da8d2cd8f816095 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 12:02:00 -0400 Subject: [PATCH 17/38] catch the error --- src/routes.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 6df95e30..c6a23875 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1918,8 +1918,10 @@ module.exports = async ( console.log('NEWFOLLOW POSTED') - return res.status(200).send() + return res.status(200) } catch (err) { + console.log(err) + process.exit(1) return res.status(500).json({ errorMessage: err.message || 'Unknown error inside /api/gun/follow' }) From 08216586eda400ae47c6b19a1a9d3a0b22fd298e Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 12:16:45 -0400 Subject: [PATCH 18/38] remove debug code --- src/routes.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/routes.js b/src/routes.js index c6a23875..0205f35e 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1920,8 +1920,6 @@ module.exports = async ( return res.status(200) } catch (err) { - console.log(err) - process.exit(1) return res.status(500).json({ errorMessage: err.message || 'Unknown error inside /api/gun/follow' }) From 6f87d214ecc9ea5a8374105163e0fb00b6b67717 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 31 May 2020 12:17:22 -0400 Subject: [PATCH 19/38] correct wording --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 0205f35e..f2616535 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1949,7 +1949,7 @@ module.exports = async ( } ap.get('/api/gun/follows', apiGunFollowsGet) - ap.post(`/api/gun/follow`, apiGunFollowsPost) + ap.post(`/api/gun/follows`, apiGunFollowsPost) ap.delete(`/api/gun/follows/:publickey`, apiGunFollowsDelete) /** From e46e79405d933a4bd3f23e0ca07ca657c6e195a6 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 2 Jun 2020 09:42:44 -0400 Subject: [PATCH 20/38] format --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index f2616535..c20dbbec 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1862,7 +1862,7 @@ module.exports = async ( }) })) - /** + /** * @type {GetFollowsResponse} */ const response = { From dba537e9b02eda87b7ae829c4f8766bf4f060468 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 2 Jun 2020 13:06:18 -0400 Subject: [PATCH 21/38] format --- services/gunDB/contact-api/getters.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/services/gunDB/contact-api/getters.js b/services/gunDB/contact-api/getters.js index cc1104f7..ed6d4541 100644 --- a/services/gunDB/contact-api/getters.js +++ b/services/gunDB/contact-api/getters.js @@ -1,3 +1,6 @@ +/** + * @format + */ const Key = require('./key') const Utils = require('./utils') @@ -5,8 +8,13 @@ const Utils = require('./utils') * @param {string} pub * @returns {Promise} */ -exports.currentOrderAddress = async (pub) => { - const currAddr = await Utils.tryAndWait((gun) => gun.user(pub).get(Key.CURRENT_ORDER_ADDRESS).then()) +exports.currentOrderAddress = async pub => { + const currAddr = await Utils.tryAndWait(gun => + gun + .user(pub) + .get(Key.CURRENT_ORDER_ADDRESS) + .then() + ) if (typeof currAddr !== 'string') { throw new TypeError('Expected user.currentOrderAddress to be an string') @@ -19,11 +27,14 @@ exports.currentOrderAddress = async (pub) => { * @param {string} pub * @returns {Promise} */ -exports.userToIncomingID = async (pub) => { - const incomingID = await require('../Mediator').getUser().get(Key.USER_TO_INCOMING).get(pub).then() +exports.userToIncomingID = async pub => { + const incomingID = await require('../Mediator') + .getUser() + .get(Key.USER_TO_INCOMING) + .get(pub) + .then() if (typeof incomingID === 'string') return incomingID return null - -} \ No newline at end of file +} From 154f9e3a10ef7b47b5f9736158da4c2730ea2a0f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Jun 2020 10:19:05 -0400 Subject: [PATCH 22/38] follows getter --- services/gunDB/contact-api/getters.js | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/services/gunDB/contact-api/getters.js b/services/gunDB/contact-api/getters.js index ed6d4541..d71e53f4 100644 --- a/services/gunDB/contact-api/getters.js +++ b/services/gunDB/contact-api/getters.js @@ -1,6 +1,12 @@ /** * @format */ +const Common = require('shock-common') +/** + * @typedef {import('shock-common').Schema.Follow} Follow + */ +const Logger = require('winston') + const Key = require('./key') const Utils = require('./utils') @@ -38,3 +44,45 @@ exports.userToIncomingID = async pub => { return null } + +/** + * @returns {Promise>} + */ +exports.currentFollows = () => { + const user = require('../Mediator').getUser() + + return new Promise(res => { + user.get(Key.FOLLOWS).load(data => { + if (typeof data !== 'object' || data === null) { + Logger.warn( + `GunDb -> getters -> currentFollows() -> Current follows data as fetched from gun is not an object but ${typeof data}. This can happen if Gun lost its cache and it's still retrieving data from the network.` + ) + res({}) + return + } + + const rejected = Object.entries(data).filter( + ([_, follow]) => !Common.Schema.isFollow(follow) + ) + + rejected.forEach(([key, item]) => { + // expected when unfollowed + if (item !== null) { + Logger.warn( + `GunDb -> getters -> currentFollows() -> Item not conforming to schema found inside follows data. Key: ${key}, item: ${JSON.stringify( + item + )}.` + ) + } + }) + + const passed = Object.entries(data).filter(([_, follow]) => + Common.Schema.isFollow(follow) + ) + + const p = /** @type {unknown} */ (passed) + + res(/** @type {Record} */ (p)) + }) + }) +} From 924d2ce25e4e89386b1fe52cff696f371572e43f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Jun 2020 10:30:38 -0400 Subject: [PATCH 23/38] follows-related actions --- services/gunDB/contact-api/actions.js | 52 ++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index b4fe5bed..6522e6fa 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1218,6 +1218,54 @@ const setLastSeenApp = () => }) }) +/** + * @param {string} publicKey + * @param {boolean} isPrivate + * @returns {Promise} + */ +const follow = (publicKey, isPrivate = false) => { + /** @type {import('shock-common').Schema.Follow} */ + const newFollow = { + private: isPrivate, + status: 'ok', + user: publicKey + } + + return new Promise((res, rej) => { + require('../Mediator') + .getUser() + .get(Key.FOLLOWS) + .get(publicKey) + // @ts-ignore + .put(newFollow, ack => { + if (ack.err) { + rej(ack.err) + } else { + res() + } + }) + }) +} + +/** + * @param {string} publicKey + * @returns {Promise} + */ +const unfollow = publicKey => + new Promise((res, rej) => { + require('../Mediator') + .getUser() + .get(Key.FOLLOWS) + .get(publicKey) + .put(null, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + module.exports = { __createOutgoingFeed, acceptRequest, @@ -1236,5 +1284,7 @@ module.exports = { saveSeedBackup, saveChannelsBackup, disconnect, - setLastSeenApp + setLastSeenApp, + follow, + unfollow } From c7acbbdfb1f441f5494d5b2d92d6fb53bcc5f2c7 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Jun 2020 10:40:09 -0400 Subject: [PATCH 24/38] follows-related-routes --- src/routes.js | 113 +++++++++++++++++--------------------------------- 1 file changed, 38 insertions(+), 75 deletions(-) diff --git a/src/routes.js b/src/routes.js index c20dbbec..c9e00672 100644 --- a/src/routes.js +++ b/src/routes.js @@ -12,8 +12,6 @@ const httpsAgent = require("https"); const responseTime = require("response-time"); const uuid = require("uuid/v4"); const Common = require('shock-common') -const fromPairs = require('lodash/fromPairs') - const getListPage = require("../utils/paginate"); const auth = require("../services/auth/auth"); @@ -23,6 +21,7 @@ const LightningServices = require("../utils/lightningServices"); const GunDB = require("../services/gunDB/Mediator"); const { unprotectedRoutes, nonEncryptedRoutes } = require("../utils/protectedRoutes"); const GunActions = require("../services/gunDB/contact-api/actions") +const GunGetters = require('../services/gunDB/contact-api/getters') const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10; const SESSION_ID = uuid(); @@ -1833,10 +1832,13 @@ module.exports = async ( }) ///////////////////////////////// + /** + * @template P + * @typedef {import('express-serve-static-core').RequestHandler

} RequestHandler + */ /** - * @typedef {import('express-serve-static-core').Application} Application - * @typedef {import('express-serve-static-core').Response} Response + * * @typedef {import('shock-common').APISchema.FollowRequest} FollowRequest * @typedef {import('shock-common').APISchema.UnfollowRequest} UnfollowRequest * @typedef {import('shock-common').APISchema.GetFollowsResponse} GetFollowsResponse @@ -1846,111 +1848,72 @@ module.exports = async ( const ap = /** @type {Application} */ (app); /** - * @param {*} _ - * @param {Response} res + * @typedef {object} FollowsRouteParams + * @prop {(string|undefined)=} publicKey */ - const apiGunFollowsGet = async (_, res) => { + + + /** + * @type {RequestHandler} + */ + const apiGunFollowsGet = async (req, res) => { try { - const user = require('../services/gunDB/Mediator').getUser(); + const { publicKey } = req.params; + const currFollows = await GunGetters.currentFollows() - /** - * @type {import('../services/gunDB/contact-api/SimpleGUN').OpenListenerData} - */ - const followsData = await timeout5(new Promise(res => { - user.get(Key.FOLLOWS).load(data => { - res(data) - }) - })) - - /** - * @type {GetFollowsResponse} - */ - const response = { - follows: {} - } - - if (Common.Schema.isObj(followsData)) { - response.follows = fromPairs( - Object.entries(followsData) - .filter(([_, f]) => Common.Schema.isFollow(f)) - ) - } else { - logger.warn('followsData not a map, expected for old users'); - } - - return res.status(200).json(response) + return res.status(200).json(publicKey ? currFollows[publicKey] : currFollows) } catch (err) { return res.status(500).json({ - errorMessage: err.message + errorMessage: err.message || 'Unknown ERR at GET /api/follows' }) } } /** - * @param {{ body: FollowRequest }} req - * @param {Response} res + * @type {RequestHandler} */ - const apiGunFollowsPost = async (req, res) => { + const apiGunFollowsPostOrPut = async (req, res) => { try { - const user = require('../services/gunDB/Mediator').getUser() - const { body: { publicKey }} = req - - /** @type {Follow} */ - const newFollow = { - private: false, - status: 'ok', - user: publicKey + const { publicKey } = req.params; + if (!publicKey) { + throw new Error(`Missing publicKey route param.`) } - console.log('NEWFOLLOW WILL POST ' + publicKey) - - await new Promise((res, rej) => { - user.get(Key.FOLLOWS).get(publicKey).put(newFollow, ack => { - if (ack.err) { - rej(new Error(ack.err)) - } else { - res() - } - }) - }) - - - console.log('NEWFOLLOW POSTED') + await GunActions.follow(req.params.publicKey, req.body.private) + // 201 would be extraneous here return res.status(200) } catch (err) { return res.status(500).json({ - errorMessage: err.message || 'Unknown error inside /api/gun/follow' + errorMessage: err.message || 'Unknown error inside /api/gun/follows/' }) } } /** - * @param {{ body: UnfollowRequest }} req - * @param {Response} res + * @type {RequestHandler} */ - const apiGunFollowsDelete = (req, res) => { + const apiGunFollowsDelete = async (req, res) => { try { - const user = require('../services/gunDB/Mediator').getUser() - /** - * @type {string} - */ - const publicKey = req.params.publickey - - user.get(Key.FOLLOWS).get(publicKey).put(null) + const { publicKey } = req.params; + if (!publicKey) { + throw new Error(`Missing publicKey route param.`) + } + await GunActions.unfollow(req.params.publicKey) return res.status(200) } catch (err) { return res.status(500).json({ - errorMessage: err.message || 'Unknown error inside /api/gun/follow' + errorMessage: err.message || 'Unknown error inside /api/gun/follows/' }) } } - ap.get('/api/gun/follows', apiGunFollowsGet) - ap.post(`/api/gun/follows`, apiGunFollowsPost) - ap.delete(`/api/gun/follows/:publickey`, apiGunFollowsDelete) + ap.get('/api/gun/follows/:publicKey', apiGunFollowsGet) + ap.post(`/api/gun/follows/:publicKey`, apiGunFollowsPostOrPut) + ap.put(`/api/gun/follows/:publicKey`,apiGunFollowsPostOrPut) + ap.delete(`/api/gun/follows/:publicKey`, apiGunFollowsDelete) /** * Return app so that it can be used by express. From f70fc2a0abd51fb80e05b53534e676b6d860ab76 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Jun 2020 14:49:11 -0400 Subject: [PATCH 25/38] ensure boolean arg --- src/routes.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/routes.js b/src/routes.js index c9e00672..a9251e51 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1837,13 +1837,6 @@ module.exports = async ( * @typedef {import('express-serve-static-core').RequestHandler

} RequestHandler */ - /** - * - * @typedef {import('shock-common').APISchema.FollowRequest} FollowRequest - * @typedef {import('shock-common').APISchema.UnfollowRequest} UnfollowRequest - * @typedef {import('shock-common').APISchema.GetFollowsResponse} GetFollowsResponse - * @typedef {import('shock-common').Schema.Follow} Follow - */ const ap = /** @type {Application} */ (app); @@ -1880,7 +1873,7 @@ module.exports = async ( throw new Error(`Missing publicKey route param.`) } - await GunActions.follow(req.params.publicKey, req.body.private) + await GunActions.follow(req.params.publicKey, req.body.private || false) // 201 would be extraneous here return res.status(200) From 6e6cb9b479462b8ee9a64f219c808446f43420af Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Jun 2020 15:54:51 -0400 Subject: [PATCH 26/38] explicitness --- services/gunDB/contact-api/actions.js | 2 +- src/routes.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 6522e6fa..e13d8989 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1223,7 +1223,7 @@ const setLastSeenApp = () => * @param {boolean} isPrivate * @returns {Promise} */ -const follow = (publicKey, isPrivate = false) => { +const follow = (publicKey, isPrivate) => { /** @type {import('shock-common').Schema.Follow} */ const newFollow = { private: isPrivate, diff --git a/src/routes.js b/src/routes.js index a9251e51..3a041d8e 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1873,7 +1873,7 @@ module.exports = async ( throw new Error(`Missing publicKey route param.`) } - await GunActions.follow(req.params.publicKey, req.body.private || false) + await GunActions.follow(req.params.publicKey, false) // 201 would be extraneous here return res.status(200) From f375ada0c526887a162610aae29bd03f016020b2 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 4 Jun 2020 09:33:56 -0400 Subject: [PATCH 27/38] getters as a module --- .../gunDB/contact-api/{getters.js => getters/index.js} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename services/gunDB/contact-api/{getters.js => getters/index.js} (92%) diff --git a/services/gunDB/contact-api/getters.js b/services/gunDB/contact-api/getters/index.js similarity index 92% rename from services/gunDB/contact-api/getters.js rename to services/gunDB/contact-api/getters/index.js index d71e53f4..fa724cbc 100644 --- a/services/gunDB/contact-api/getters.js +++ b/services/gunDB/contact-api/getters/index.js @@ -2,13 +2,13 @@ * @format */ const Common = require('shock-common') +const Logger = require('winston') /** * @typedef {import('shock-common').Schema.Follow} Follow */ -const Logger = require('winston') -const Key = require('./key') -const Utils = require('./utils') +const Key = require('../key') +const Utils = require('../utils') /** * @param {string} pub @@ -34,7 +34,7 @@ exports.currentOrderAddress = async pub => { * @returns {Promise} */ exports.userToIncomingID = async pub => { - const incomingID = await require('../Mediator') + const incomingID = await require('../../Mediator') .getUser() .get(Key.USER_TO_INCOMING) .get(pub) @@ -49,7 +49,7 @@ exports.userToIncomingID = async pub => { * @returns {Promise>} */ exports.currentFollows = () => { - const user = require('../Mediator').getUser() + const user = require('../../Mediator').getUser() return new Promise(res => { user.get(Key.FOLLOWS).load(data => { From 47b2a4f01049a1bc23d2e14b4241725c436541ec Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 4 Jun 2020 10:27:13 -0400 Subject: [PATCH 28/38] follows as submodule --- services/gunDB/contact-api/getters/follows.js | 50 +++++++++++++++++++ services/gunDB/contact-api/getters/index.js | 48 +----------------- 2 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 services/gunDB/contact-api/getters/follows.js diff --git a/services/gunDB/contact-api/getters/follows.js b/services/gunDB/contact-api/getters/follows.js new file mode 100644 index 00000000..b74080fa --- /dev/null +++ b/services/gunDB/contact-api/getters/follows.js @@ -0,0 +1,50 @@ +const Common = require('shock-common') +const Logger = require('winston') + +const Key = require('../key') + +/** + * @typedef {Common.Schema.Follow} Follow + */ + +/** + * @returns {Promise>} + */ +exports.currentFollows = () => { + const user = require('../../Mediator').getUser() + + return new Promise(res => { + user.get(Key.FOLLOWS).load(data => { + if (typeof data !== 'object' || data === null) { + Logger.warn( + `GunDb -> getters -> currentFollows() -> Current follows data as fetched from gun is not an object but ${typeof data}. This can happen if Gun lost its cache and it's still retrieving data from the network.` + ) + res({}) + return + } + + const rejected = Object.entries(data).filter( + ([_, follow]) => !Common.Schema.isFollow(follow) + ) + + rejected.forEach(([key, item]) => { + // expected when unfollowed + if (item !== null) { + Logger.warn( + `GunDb -> getters -> currentFollows() -> Item not conforming to schema found inside follows data. Key: ${key}, item: ${JSON.stringify( + item + )}.` + ) + } + }) + + const passed = Object.entries(data).filter(([_, follow]) => + Common.Schema.isFollow(follow) + ) + + const p = /** @type {unknown} */ (passed) + + res(/** @type {Record} */ (p)) + }) + }) +} \ No newline at end of file diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index fa724cbc..9b33cb92 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -1,12 +1,6 @@ /** * @format */ -const Common = require('shock-common') -const Logger = require('winston') -/** - * @typedef {import('shock-common').Schema.Follow} Follow - */ - const Key = require('../key') const Utils = require('../utils') @@ -45,44 +39,4 @@ exports.userToIncomingID = async pub => { return null } -/** - * @returns {Promise>} - */ -exports.currentFollows = () => { - const user = require('../../Mediator').getUser() - - return new Promise(res => { - user.get(Key.FOLLOWS).load(data => { - if (typeof data !== 'object' || data === null) { - Logger.warn( - `GunDb -> getters -> currentFollows() -> Current follows data as fetched from gun is not an object but ${typeof data}. This can happen if Gun lost its cache and it's still retrieving data from the network.` - ) - res({}) - return - } - - const rejected = Object.entries(data).filter( - ([_, follow]) => !Common.Schema.isFollow(follow) - ) - - rejected.forEach(([key, item]) => { - // expected when unfollowed - if (item !== null) { - Logger.warn( - `GunDb -> getters -> currentFollows() -> Item not conforming to schema found inside follows data. Key: ${key}, item: ${JSON.stringify( - item - )}.` - ) - } - }) - - const passed = Object.entries(data).filter(([_, follow]) => - Common.Schema.isFollow(follow) - ) - - const p = /** @type {unknown} */ (passed) - - res(/** @type {Record} */ (p)) - }) - }) -} +module.exports.Follows = require('./follows') From e648ad0cfafd13466bed699a7fe081fec4fbe168 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 10:58:01 -0400 Subject: [PATCH 29/38] comment --- src/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.js b/src/routes.js index 3a041d8e..d50c828c 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1875,7 +1875,7 @@ module.exports = async ( await GunActions.follow(req.params.publicKey, false) - // 201 would be extraneous here + // 201 would be extraneous here. Implement it inside app.put return res.status(200) } catch (err) { return res.status(500).json({ From e152c2ff0db70fbcfaf97fb0705d2366376e6590 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 11:04:23 -0400 Subject: [PATCH 30/38] actually send res --- src/routes.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/routes.js b/src/routes.js index d50c828c..56dfb573 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1876,7 +1876,9 @@ module.exports = async ( await GunActions.follow(req.params.publicKey, false) // 201 would be extraneous here. Implement it inside app.put - return res.status(200) + return res.status(200).json({ + ok: true + }) } catch (err) { return res.status(500).json({ errorMessage: err.message || 'Unknown error inside /api/gun/follows/' @@ -1895,7 +1897,9 @@ module.exports = async ( } await GunActions.unfollow(req.params.publicKey) - return res.status(200) + return res.status(200).json({ + ok: true + }) } catch (err) { return res.status(500).json({ errorMessage: err.message || 'Unknown error inside /api/gun/follows/' From e6740d4c0049eb190e0aacd9265c36ca35bde7a7 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 11:34:46 -0400 Subject: [PATCH 31/38] reject with error --- services/gunDB/contact-api/actions.js | 6 +++--- src/routes.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index e13d8989..36b39716 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1156,7 +1156,7 @@ const saveSeedBackup = async (mnemonicPhrase, user, SEA) => { return new Promise((res, rej) => { user.get(Key.SEED_BACKUP).put(encryptedSeed, ack => { if (ack.err) { - rej(ack.err) + rej(new Error(ack.err)) } else { res() } @@ -1179,7 +1179,7 @@ const saveChannelsBackup = async (backups, user, SEA) => { return new Promise((res, rej) => { user.get(Key.CHANNELS_BACKUP).put(encryptBackups, ack => { if (ack.err) { - rej(ack.err) + rej(new Error(ack.err)) } else { res() } @@ -1239,7 +1239,7 @@ const follow = (publicKey, isPrivate) => { // @ts-ignore .put(newFollow, ack => { if (ack.err) { - rej(ack.err) + rej(new Error(ack.err)) } else { res() } diff --git a/src/routes.js b/src/routes.js index 56dfb573..960c1641 100644 --- a/src/routes.js +++ b/src/routes.js @@ -490,7 +490,7 @@ module.exports = async ( } user.get("FEED_POC").put(JSON.stringify(feedObj), ack => { if (ack.err) { - //rej(ack.err) + //rej(new Error(ack.err)) logger.log(ack.err) } else { logger.log(ack.err) From 668a9da9a9e3e420d217c2e15afc81e16da4d6ad Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 11:47:43 -0400 Subject: [PATCH 32/38] sanitize public key --- services/gunDB/contact-api/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 36b39716..455792cf 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1235,7 +1235,7 @@ const follow = (publicKey, isPrivate) => { require('../Mediator') .getUser() .get(Key.FOLLOWS) - .get(publicKey) + .get(`$$__PK__${publicKey}`) // @ts-ignore .put(newFollow, ack => { if (ack.err) { @@ -1256,7 +1256,7 @@ const unfollow = publicKey => require('../Mediator') .getUser() .get(Key.FOLLOWS) - .get(publicKey) + .get(`$$__PK__${publicKey}`) .put(null, ack => { if (ack.err) { rej(new Error(ack.err)) From e6fd66a8c34b13cb7adcbe87c551bcd4c8542f3d Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 13:13:08 -0400 Subject: [PATCH 33/38] comment --- services/gunDB/contact-api/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 455792cf..d8e8ef67 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1220,7 +1220,7 @@ const setLastSeenApp = () => /** * @param {string} publicKey - * @param {boolean} isPrivate + * @param {boolean} isPrivate Will overwrite previous private status. * @returns {Promise} */ const follow = (publicKey, isPrivate) => { From e1eade245252338f9d7bc05bc63941608fc9c897 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 13:13:21 -0400 Subject: [PATCH 34/38] don't sanitize public key --- services/gunDB/contact-api/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index d8e8ef67..54bd41fe 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1235,7 +1235,7 @@ const follow = (publicKey, isPrivate) => { require('../Mediator') .getUser() .get(Key.FOLLOWS) - .get(`$$__PK__${publicKey}`) + .get(publicKey) // @ts-ignore .put(newFollow, ack => { if (ack.err) { @@ -1256,7 +1256,7 @@ const unfollow = publicKey => require('../Mediator') .getUser() .get(Key.FOLLOWS) - .get(`$$__PK__${publicKey}`) + .get(publicKey) .put(null, ack => { if (ack.err) { rej(new Error(ack.err)) From 7d7d1f98de0d6a291dff188cfad68044faf25fe2 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 13:15:52 -0400 Subject: [PATCH 35/38] only use put --- src/routes.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/routes.js b/src/routes.js index 960c1641..e3f40945 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1866,7 +1866,7 @@ module.exports = async ( /** * @type {RequestHandler} */ - const apiGunFollowsPostOrPut = async (req, res) => { + const apiGunFollowsPut = async (req, res) => { try { const { publicKey } = req.params; if (!publicKey) { @@ -1908,8 +1908,7 @@ module.exports = async ( } ap.get('/api/gun/follows/:publicKey', apiGunFollowsGet) - ap.post(`/api/gun/follows/:publicKey`, apiGunFollowsPostOrPut) - ap.put(`/api/gun/follows/:publicKey`,apiGunFollowsPostOrPut) + ap.put(`/api/gun/follows/:publicKey`,apiGunFollowsPut) ap.delete(`/api/gun/follows/:publicKey`, apiGunFollowsDelete) /** From 2cc5dfd55d5fd922aa3de5672a6cfd070db268b0 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 13:17:28 -0400 Subject: [PATCH 36/38] mock response for now --- src/routes.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/routes.js b/src/routes.js index e3f40945..bf31e0ad 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1866,14 +1866,14 @@ module.exports = async ( /** * @type {RequestHandler} */ - const apiGunFollowsPut = async (req, res) => { + const apiGunFollowsPut = (req, res) => { try { const { publicKey } = req.params; if (!publicKey) { throw new Error(`Missing publicKey route param.`) } - await GunActions.follow(req.params.publicKey, false) + // await GunActions.follow(req.params.publicKey, false) // 201 would be extraneous here. Implement it inside app.put return res.status(200).json({ @@ -1889,14 +1889,15 @@ module.exports = async ( /** * @type {RequestHandler} */ - const apiGunFollowsDelete = async (req, res) => { + const apiGunFollowsDelete = (req, res) => { try { const { publicKey } = req.params; if (!publicKey) { throw new Error(`Missing publicKey route param.`) } - await GunActions.unfollow(req.params.publicKey) + // await GunActions.unfollow(req.params.publicKey) + return res.status(200).json({ ok: true }) From bde636264072f00ad30a1ffe60610d357e5b9bd5 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 14:25:03 -0400 Subject: [PATCH 37/38] collection get --- src/routes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes.js b/src/routes.js index bf31e0ad..8df67dd8 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1908,6 +1908,7 @@ module.exports = async ( } } + ap.get('/api/gun/follows/', apiGunFollowsGet) ap.get('/api/gun/follows/:publicKey', apiGunFollowsGet) ap.put(`/api/gun/follows/:publicKey`,apiGunFollowsPut) ap.delete(`/api/gun/follows/:publicKey`, apiGunFollowsDelete) From 779783aeec5c2da2c1f506131bd759332875d964 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 10 Jun 2020 14:25:21 -0400 Subject: [PATCH 38/38] mock endpoint --- src/routes.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/routes.js b/src/routes.js index 8df67dd8..9dd57c6e 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1849,12 +1849,12 @@ module.exports = async ( /** * @type {RequestHandler} */ - const apiGunFollowsGet = async (req, res) => { + const apiGunFollowsGet = (_, res) => { try { - const { publicKey } = req.params; - const currFollows = await GunGetters.currentFollows() + // const { publicKey } = req.params; + // const currFollows = await GunGetters.currentFollows() - return res.status(200).json(publicKey ? currFollows[publicKey] : currFollows) + return res.status(200).json({}) } catch (err) { return res.status(500).json({ errorMessage: err.message || 'Unknown ERR at GET /api/follows'