From 16dd0040002ba4a36649b8076ea2ddea902fc77c Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 12:57:16 -0400 Subject: [PATCH 1/8] initialize wall subgraph at sign up --- services/gunDB/Mediator/index.js | 1 + services/gunDB/contact-api/actions.js | 59 ++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 98bafe55..f8fc73ec 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -1303,6 +1303,7 @@ const register = async (alias, pass) => { await API.Actions.setDisplayName('anon' + pub.slice(0, 8), user) await API.Actions.generateHandshakeAddress() await API.Actions.generateOrderAddress(user) + await API.Actions.initWall() await API.Actions.setBio('A little bit about myself.', user) return pub }) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 1998d24a..2f490d5a 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1502,6 +1502,62 @@ const unfollow = publicKey => }) }) +/** + * @throws {Error} + * @returns {Promise} + */ +const initWall = async () => { + const user = require('../Mediator').getUser() + + await new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.NUM_OF_PAGES) + .put(1, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + await new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.PAGES) + .get('0') + .get(Key.POSTS) + .put( + { + unused: null + }, + ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + } + ) + }) + + await new Promise((res, rej) => { + user + .get(Key.WALL) + .get(Key.PAGES) + .get('0') + .get(Key.COUNT) + .put(0, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) +} + module.exports = { __createOutgoingFeed, acceptRequest, @@ -1524,5 +1580,6 @@ module.exports = { createPost, deletePost, follow, - unfollow + unfollow, + initWall } From 64c16d88e7e83e88aeff0b1d3bdea095a19fcb87 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 15:12:24 -0400 Subject: [PATCH 2/8] initialize to 0 and optimize for it --- services/gunDB/contact-api/actions.js | 2 +- services/gunDB/contact-api/getters/wall.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 2f490d5a..25b98004 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1513,7 +1513,7 @@ const initWall = async () => { user .get(Key.WALL) .get(Key.NUM_OF_PAGES) - .put(1, ack => { + .put(0, ack => { if (ack.err) { rej(new Error(ack.err)) } else { diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 73eb210f..83cdb5a7 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -44,6 +44,13 @@ const getWallPage = async (page, publicKey) => { ) } + if (totalPages === 0) { + return { + count: 0, + posts: {} + } + } + const actualPageIdx = page < 0 ? totalPages + page : page - 1 if (actualPageIdx > totalPages - 1) { From 54f223ac99ac2e9e81217bd99cc65c7675642491 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 16:57:22 -0400 Subject: [PATCH 3/8] no retry if page count is zero --- services/gunDB/contact-api/getters/wall.js | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 83cdb5a7..6a4b2aa5 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -44,11 +44,13 @@ const getWallPage = async (page, publicKey) => { ) } + const empty = { + count: 0, + posts: {} + } + if (totalPages === 0) { - return { - count: 0, - posts: {} - } + return empty } const actualPageIdx = page < 0 ? totalPages + page : page - 1 @@ -57,6 +59,28 @@ const getWallPage = async (page, publicKey) => { throw new RangeError(`Requested a page out of bounds`) } + /** + * @type {number} + */ + // @ts-ignore + const count = await Utils.tryAndWait( + (g, u) => { + const user = publicKey ? g.get(`~${publicKey}`) : u + + return user + .get(Key.WALL) + .get(Key.PAGES) + .get(actualPageIdx.toString()) + .get(Key.COUNT) + .then() + }, + v => typeof v !== 'number' + ) + + if (count === 0) { + return empty + } + /** * @type {Common.SchemaTypes.WallPage} */ From b30971598e19140144160930187ef4971206ad4c Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 19:28:40 -0400 Subject: [PATCH 4/8] correct user access --- services/gunDB/contact-api/getters/user.js | 30 ++++++++++++---------- services/gunDB/contact-api/getters/wall.js | 27 ++++++++++++++++--- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/services/gunDB/contact-api/getters/user.js b/services/gunDB/contact-api/getters/user.js index fdcd7dfb..8f1d9cf2 100644 --- a/services/gunDB/contact-api/getters/user.js +++ b/services/gunDB/contact-api/getters/user.js @@ -12,8 +12,8 @@ const Utils = require('../utils') */ const getAnUser = async publicKey => { const oldProfile = await Utils.tryAndWait( - g => { - const user = g.get(`~${publicKey}`) + (g, u) => { + const user = u._.sea.pub === publicKey ? u : g.user(publicKey) return new Promise(res => user.get(Key.PROFILE).load(res)) }, @@ -21,25 +21,29 @@ const getAnUser = async publicKey => { ) const bio = await Utils.tryAndWait( - g => - g - .get(`~${publicKey}`) - .get(Key.BIO) - .then(), + (g, u) => { + const user = u._.sea.pub === publicKey ? u : g.user(publicKey) + + return user.get(Key.BIO).then() + }, v => typeof v !== 'string' ) const lastSeenApp = await Utils.tryAndWait( - g => - g - .get(`~${publicKey}`) - .get(Key.LAST_SEEN_APP) - .then(), + (g, u) => { + const user = u._.sea.pub === publicKey ? u : g.user(publicKey) + + return user.get(Key.LAST_SEEN_APP).then() + }, v => typeof v !== 'number' ) const lastSeenNode = await Utils.tryAndWait( - (_, user) => user.get(Key.LAST_SEEN_NODE).then(), + (g, u) => { + const user = u._.sea.pub === publicKey ? u : g.user(publicKey) + + return user.get(Key.LAST_SEEN_NODE).then() + }, v => typeof v !== 'number' ) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 6a4b2aa5..aeb00746 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -15,7 +15,14 @@ const Wall = require('./user') const getWallTotalPages = async publicKey => { const totalPages = await Utils.tryAndWait( (gun, u) => { - const user = publicKey ? gun.get(`~${publicKey}`) : u + /** + * @type {import('../SimpleGUN').GUNNode} + */ + let user = u + + if (publicKey && u._.sea.pub !== publicKey) { + user = gun.user(publicKey) + } return user .get(Key.WALL) @@ -65,7 +72,14 @@ const getWallPage = async (page, publicKey) => { // @ts-ignore const count = await Utils.tryAndWait( (g, u) => { - const user = publicKey ? g.get(`~${publicKey}`) : u + /** + * @type {import('../SimpleGUN').GUNNode} + */ + let user = u + + if (publicKey && u._.sea.pub === publicKey) { + user = g.user(publicKey) + } return user .get(Key.WALL) @@ -86,7 +100,14 @@ const getWallPage = async (page, publicKey) => { */ const thePage = await Utils.tryAndWait( (g, u) => { - const user = publicKey ? g.get(`~${publicKey}`) : u + /** + * @type {import('../SimpleGUN').GUNNode} + */ + let user = u + + if (publicKey && u._.sea.pub) { + user = g.user(publicKey) + } return new Promise(res => { user From 3f2466b09c113928611d0f0a64aed9db75f84f9b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 20:21:40 -0400 Subject: [PATCH 5/8] stricter log --- services/gunDB/contact-api/utils/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 28dc0ebb..4c1be37c 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -105,6 +105,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { } } catch (e) { logger.error(e) + logger.info(JSON.stringify(e)) if (e.message === 'NOT_AUTH') { throw e } From ccabc1fd19360bf0082c35c3ae545222e51c89dd Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 11 Jul 2020 20:41:00 -0400 Subject: [PATCH 6/8] better logging --- services/gunDB/contact-api/utils/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 4c1be37c..f35964ce 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -98,7 +98,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { if (shouldRetry(resolvedValue)) { logger.info( 'force retrying' + - ` args: ${promGen.toString()} -- ${shouldRetry.toString()}` + ` args: ${promGen.toString()} -- ${shouldRetry.toString()} \n resolvedValue: ${resolvedValue}, type: ${typeof resolvedValue}` ) } else { return resolvedValue @@ -129,7 +129,7 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { if (shouldRetry(resolvedValue)) { logger.info( 'force retrying' + - ` args: ${promGen.toString()} -- ${shouldRetry.toString()}` + ` args: ${promGen.toString()} -- ${shouldRetry.toString()} \n resolvedValue: ${resolvedValue}, type: ${typeof resolvedValue}` ) } else { return resolvedValue From c4e0e85c51c0aecb11c95abe5e6c960eacfdfbc2 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 13 Jul 2020 17:28:25 -0400 Subject: [PATCH 7/8] correct logic --- services/gunDB/contact-api/getters/wall.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index aeb00746..9ac3c6fb 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -77,7 +77,7 @@ const getWallPage = async (page, publicKey) => { */ let user = u - if (publicKey && u._.sea.pub === publicKey) { + if (publicKey && u._.sea.pub !== publicKey) { user = g.user(publicKey) } @@ -105,7 +105,7 @@ const getWallPage = async (page, publicKey) => { */ let user = u - if (publicKey && u._.sea.pub) { + if (publicKey && u._.sea.pub !== publicKey) { user = g.user(publicKey) } From 978652bc0a143fa77b2133ec13b1557d1c3f651c Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 13 Jul 2020 17:43:19 -0400 Subject: [PATCH 8/8] stricter type checking --- services/gunDB/contact-api/actions.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 25b98004..a0ae6171 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1269,7 +1269,15 @@ const createPost = async (tags, title, content) => { v => typeof v !== 'number' ) - return typeof maybeNumOfPages === 'number' ? maybeNumOfPages : 0 + if (typeof maybeNumOfPages !== 'number') { + throw new TypeError( + `Could not fetch number of pages from wall, instead got: ${JSON.stringify( + maybeNumOfPages + )}` + ) + } + + return maybeNumOfPages })() let pageIdx = Math.max(0, numOfPages - 1).toString()