From a0d602bcabfafef202a80d2dcdb1bf4296679aec Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 22 Jul 2021 17:00:21 -0400 Subject: [PATCH 01/11] Remove unused code/routes --- services/gunDB/contact-api/getters/feed.js | 89 --------- services/gunDB/contact-api/getters/index.js | 22 --- services/gunDB/contact-api/getters/wall.js | 208 -------------------- src/routes.js | 119 ----------- 4 files changed, 438 deletions(-) delete mode 100644 services/gunDB/contact-api/getters/feed.js delete mode 100644 services/gunDB/contact-api/getters/wall.js diff --git a/services/gunDB/contact-api/getters/feed.js b/services/gunDB/contact-api/getters/feed.js deleted file mode 100644 index ba4704ce..00000000 --- a/services/gunDB/contact-api/getters/feed.js +++ /dev/null @@ -1,89 +0,0 @@ -/** - * @format - */ -//@ts-ignore -const Common = require('shock-common') -const isFinite = require('lodash/isFinite') -const shuffle = require('lodash/shuffle') -const R = require('ramda') - -const { asyncFilter } = require('../../../../utils') - -const Follows = require('./follows') -const Wall = require('./wall') - -/** - * @param {number} numberOfPublicKeyGroups - * @param {number} pageRequested - * @returns {[ number , number ]} - */ -const calculateWallRequest = (numberOfPublicKeyGroups, pageRequested) => { - // thanks to sebassdc - - return [ - (pageRequested - 1) % numberOfPublicKeyGroups, - Math.ceil(pageRequested / numberOfPublicKeyGroups) - ] -} - -/** - * @param {number} page - * @throws {TypeError} - * @throws {RangeError} - * @returns {Promise} - */ -//@returns {Promise} -const getFeedPage = async page => { - if (!isFinite(page)) { - throw new TypeError(`Please provide an actual number for [page]`) - } - - if (page <= 0) { - throw new RangeError(`Please provide only positive numbers for [page]`) - } - - const subbedPublicKeys = Object.values(await Follows.currentFollows()).map( - f => f.user - ) - - if (subbedPublicKeys.length === 0) { - return [] - } - - // say there are 20 public keys total - // page 1: page 1 from first 10 public keys - // page 2: page 1 from second 10 public keys - // page 3: page 2 from first 10 public keys - // page 4: page 2 from first 10 public keys - // etc - // thanks to sebassdc (github) - - const pagedPublicKeys = R.splitEvery(10, shuffle(subbedPublicKeys)) - - const [publicKeyGroupIdx, pageToRequest] = calculateWallRequest( - pagedPublicKeys.length, - page - ) - - const publicKeysRaw = pagedPublicKeys[publicKeyGroupIdx] - const publicKeys = await asyncFilter( - publicKeysRaw, - // reject public keys for which the page to request would result in an out - // of bounds error - async pk => pageToRequest <= (await Wall.getWallTotalPages(pk)) - ) - - const fetchedPages = await Promise.all( - publicKeys.map(pk => Wall.getWallPage(pageToRequest, pk)) - ) - - const fetchedPostsGroups = fetchedPages.map(wp => Object.values(wp.posts)) - const fetchedPosts = R.flatten(fetchedPostsGroups) - const sortered = R.sort((a, b) => b.date - a.date, fetchedPosts) - - return sortered -} - -module.exports = { - getFeedPage -} diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index 81ea8dc2..3f0cdba6 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -6,8 +6,6 @@ const Common = require('shock-common') const Key = require('../key') const Utils = require('../utils') -const Wall = require('./wall') -const Feed = require('./feed') const User = require('./user') const { size } = require('lodash') @@ -30,22 +28,6 @@ exports.currentOrderAddress = async pub => { return currAddr } -/** - * @param {string} pub - * @returns {Promise} - */ -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 -} - /** * @returns {Promise} */ @@ -139,8 +121,4 @@ module.exports.getMyUser = getMyUser module.exports.getUserInfo = getUserInfo module.exports.Follows = require('./follows') -module.exports.getWallPage = Wall.getWallPage -module.exports.getWallTotalPages = Wall.getWallTotalPages - -module.exports.getFeedPage = Feed.getFeedPage module.exports.getAnUser = User.getAnUser diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js deleted file mode 100644 index 96c7fd70..00000000 --- a/services/gunDB/contact-api/getters/wall.js +++ /dev/null @@ -1,208 +0,0 @@ -/** - * @format - */ -const Common = require('shock-common') -const pickBy = require('lodash/pickBy') -const size = require('lodash/size') -const mapValues = require('lodash/mapValues') - -const Utils = require('../utils') -const Key = require('../key') - -const User = require('./user') - -/** - * @param {string=} publicKey - * @returns {Promise} - */ -const getWallTotalPages = async publicKey => { - const totalPages = await Utils.tryAndWait( - (gun, u) => { - /** - * @type {import('../SimpleGUN').GUNNode} - */ - let user = u - - if (publicKey && u._.sea.pub !== publicKey) { - user = gun.user(publicKey) - } - - return user - .get(Key.WALL) - .get(Key.NUM_OF_PAGES) - .then() - }, - v => typeof v !== 'number' - ) - - return typeof totalPages === 'number' ? totalPages : 0 -} - -/** - * @param {number} page - * @param {string=} publicKey - * @throws {TypeError} - * @throws {RangeError} - * @returns {Promise} - */ -////@returns {Promise} -const getWallPage = async (page, publicKey) => { - const totalPages = await getWallTotalPages(publicKey) - - if (page === 0) { - throw new RangeError( - `Page number cannot be zero, only positive and negative integers are allowed.` - ) - } - - const empty = { - count: 0, - posts: {} - } - - if (totalPages === 0) { - return empty - } - - const actualPageIdx = page < 0 ? totalPages + page : page - 1 - - if (actualPageIdx > totalPages - 1) { - throw new RangeError(`Requested a page out of bounds`) - } - - /** - * @type {number} - */ - // @ts-ignore - const count = await Utils.tryAndWait( - (g, u) => { - /** - * @type {import('../SimpleGUN').GUNNode} - */ - let user = u - - if (publicKey && u._.sea.pub !== publicKey) { - user = g.user(publicKey) - } - - return user - .get(Key.WALL) - .get(Key.PAGES) - .get(actualPageIdx.toString()) - .get(Key.COUNT) - .then() - }, - v => typeof v !== 'number' - ) - - if (count === 0) { - return empty - } - - /** - * We just use it so Common.Schema.isWallPage passes. - */ - const mockUser = await User.getMyUser() - - /* - * @type {Common.SchemaTypes.WallPage} - */ - //@ts-ignore - const thePage = await Utils.tryAndWait( - (g, u) => { - /** - * @type {import('../SimpleGUN').GUNNode} - */ - let user = u - - if (publicKey && u._.sea.pub !== publicKey) { - user = g.user(publicKey) - } - - return new Promise(res => { - // forces data fetch - user - .get(Key.WALL) - .get(Key.PAGES) - .get(actualPageIdx.toString()) - // @ts-ignore - .load(() => {}) - - process.nextTick(() => { - user - .get(Key.WALL) - .get(Key.PAGES) - .get(actualPageIdx.toString()) - // @ts-ignore - .load(res) - }) - }) - }, - maybePage => { - // sometimes load() returns an empty object on the first call - if (size(/** @type {any} */ (maybePage)) === 0) { - return true - } - - const page = /** @type {Common.Schema.WallPage} */ (maybePage) - - if (typeof page.count !== 'number') { - return true - } - - // removes 'unused' initializer and aborted writes - page.posts = pickBy(page.posts, v => v !== null) - - // .load() sometimes doesn't load all data on first call - if (size(page.posts) === 0) { - return true - } - - // Give ids based on keys - page.posts = mapValues(page.posts, (v, k) => ({ - ...v, - id: k - })) - - page.posts = mapValues(page.posts, v => ({ - ...v, - // isWallPage() would otherwise not pass - author: mockUser - })) - - return !Common.Schema.isWallPage(page) - } - ) - - const clean = { - ...thePage - } - - for (const [key, post] of Object.entries(clean.posts)) { - // delete unsuccessful writes - if (post === null) { - delete clean.posts[key] - clean.count-- - } else { - post.author = publicKey - ? // eslint-disable-next-line no-await-in-loop - await User.getAnUser(publicKey) - : // eslint-disable-next-line no-await-in-loop - await User.getMyUser() - post.id = key - } - } - - if (!Common.Schema.isWallPage(clean)) { - throw new Error( - `Fetched page not a wall page, instead got: ${JSON.stringify(clean)}` - ) - } - - return clean -} - -module.exports = { - getWallTotalPages, - getWallPage -} diff --git a/src/routes.js b/src/routes.js index 27879828..ac04a05a 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2248,41 +2248,6 @@ module.exports = async ( } }) - app.get(`/api/gun/wall/:publicKey?`, async (req, res) => { - try { - const { page } = req.query - const { publicKey } = req.params - - const pageNum = Number(page) - - if (!isARealUsableNumber(pageNum)) { - return res.status(400).json({ - field: 'page', - errorMessage: 'Not a number' - }) - } - - if (pageNum === 0) { - return res.status(400).json({ - field: 'page', - errorMessage: 'Page must be a non-zero integer' - }) - } - - const totalPages = await GunGetters.getWallTotalPages(publicKey) - const fetchedPage = await GunGetters.getWallPage(pageNum, publicKey) - - return res.status(200).json({ - ...fetchedPage, - totalPages - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message - }) - } - }) - app.post(`/api/gun/wall/`, async (req, res) => { try { const { tags, title, contentItems, enableTipsOverlay } = req.body @@ -2438,90 +2403,6 @@ module.exports = async ( ap.put(`/api/gun/follows/:publicKey`, apiGunFollowsPut) ap.delete(`/api/gun/follows/:publicKey`, apiGunFollowsDelete) - /** - * @type {RequestHandler<{}>} - */ - const apiGunFeedGet = async (req, res) => { - try { - const MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL = 4 - - const { page: pageStr } = req.query - - /** - * Similar to a "before" query param in cursor based pagination. We call - * it "try" because it is likely that this item lies beyond - * MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL in which case we gracefully just send - * 2 pages and 205 response. - */ - // eslint-disable-next-line prefer-destructuring - const before = req.query.before - - if (pageStr) { - const page = Number(pageStr) - - if (!isARealUsableNumber(page)) { - return res.status(400).json({ - field: 'page', - errorMessage: 'page must be a number' - }) - } - - if (page < 1) { - return res.status(400).json({ - field: page, - errorMessage: 'page must be a positive number' - }) - } - - return res.status(200).json({ - posts: await GunGetters.getFeedPage(page), - page - }) - } - - if (before) { - const pages = range(1, MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL) - const promises = pages.map(p => GunGetters.getFeedPage(p)) - - let results = await Promise.all(promises) - - const idxIfFound = results.findIndex(pp => - pp.some(p => p.id === before) - ) - - if (idxIfFound > -1) { - results = results.slice(0, idxIfFound + 1) - - const posts = flatten(results) - - return res.status(200).json({ - posts, - page: idxIfFound - }) - } - - // we couldn't find the posts leading up to the requested post - // (try_until) Let's just return the ones we found with together with a - // 205 code (client should refresh UI) - - return res.status(205).json({ - posts: results[0] || [], - page: 1 - }) - } - - return res.status(400).json({ - errorMessage: `Must provide at least a page or a try_until query param.` - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message || 'Unknown error inside /api/gun/follows/' - }) - } - } - - ap.get(`/api/gun/feed`, apiGunFeedGet) - /** * @type {RequestHandler<{}>} */ From fe51bcfe1a81413ee333c5723a65915fc290e282 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 22 Jul 2021 17:02:15 -0400 Subject: [PATCH 02/11] Remove unused follows-related code/routes --- services/gunDB/contact-api/getters/follows.js | 66 ------------------- services/gunDB/contact-api/getters/index.js | 1 - src/routes.js | 17 ----- 3 files changed, 84 deletions(-) delete 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 deleted file mode 100644 index e7972276..00000000 --- a/services/gunDB/contact-api/getters/follows.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * @format - */ -const Common = require('shock-common') -const Logger = require('../../../../config/log') -const size = require('lodash/size') - -const Utils = require('../utils') -const Key = require('../key') - -/** - * @typedef {Common.Schema.Follow} Follow - */ - -/** - * @throws {TypeError} - * @returns {Promise>} - */ -exports.currentFollows = async () => { - /** - * @type {Record} - */ - const raw = await Utils.tryAndWait( - (_, user) => - new Promise(res => - // @ts-expect-error - user.get(Key.FOLLOWS).load(res) - ), - v => { - if (typeof v !== 'object' || v === null) { - return true - } - - // load sometimes returns an empty set on the first try - if (size(v) === 0) { - return true - } - - // sometimes it returns empty sub objects - return Object.values(v) - .filter(Common.Schema.isObj) - .some(obj => size(obj) === 0) - } - ) - - if (typeof raw !== 'object' || raw === null) { - Logger.error( - `Expected user.follows to be an object but instead got: ${JSON.stringify( - raw - )}` - ) - throw new TypeError('Could not get follows, not an object') - } - - const clean = { - ...raw - } - - for (const [key, followOrNull] of Object.entries(clean)) { - if (!Common.Schema.isFollow(followOrNull)) { - delete clean[key] - } - } - - return clean -} diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index 3f0cdba6..fb3223fe 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -119,6 +119,5 @@ const getUserInfo = async publicKey => { module.exports.getMyUser = getMyUser module.exports.getUserInfo = getUserInfo -module.exports.Follows = require('./follows') module.exports.getAnUser = User.getAnUser diff --git a/src/routes.js b/src/routes.js index ac04a05a..0446ae40 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2327,21 +2327,6 @@ module.exports = async ( * @prop {(string|undefined)=} publicKey */ - /** - * @type {RequestHandler} - */ - const apiGunFollowsGet = async (_, res) => { - try { - const currFollows = await GunGetters.Follows.currentFollows() - - return res.status(200).json(currFollows) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message || 'Unknown ERR at GET /api/follows' - }) - } - } - /** * @type {RequestHandler} */ @@ -2398,8 +2383,6 @@ 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 e2e7c7cfc60384ae1cbbf4586b96a96b1d82fe71 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 22 Jul 2021 17:10:08 -0400 Subject: [PATCH 03/11] Remove uoutdated routes --- src/routes.js | 93 --------------------------------------------------- 1 file changed, 93 deletions(-) diff --git a/src/routes.js b/src/routes.js index 0446ae40..44f262c4 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2456,99 +2456,6 @@ module.exports = async ( ap.get(`/api/gun/me`, apiGunMeGet) ap.put(`/api/gun/me`, apiGunMePut) - ap.get(`/api/gun/dev/currentHandshakeAddress`, async (_, res) => { - try { - const { tryAndWait } = require('../services/gunDB/contact-api/utils') - - const data = await tryAndWait((_, u) => - u.get(GunKey.CURRENT_HANDSHAKE_ADDRESS).then() - ) - - return res.status(200).json({ - data - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message - }) - } - }) - - ap.get( - `/api/gun/dev/handshakeNodes/:handshakeAddress`, - async (req, res) => { - try { - const { tryAndWait } = require('../services/gunDB/contact-api/utils') - - const data = await tryAndWait( - g => - new Promise(res => { - g.get(GunKey.HANDSHAKE_NODES) - .get(req.params.handshakeAddress) - .load(data => { - res(data) - }) - }), - v => { - if (typeof v !== 'object') { - return true - } - - if (v === null) { - return true - } - - // load sometimes returns an empty set on the first try - return size(v) === 0 - } - ) - - return res.status(200).json({ - data - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message - }) - } - } - ) - - ap.get(`/api/gun/dev/user/:publicKey`, async (req, res) => { - try { - const { tryAndWait } = require('../services/gunDB/contact-api/utils') - - const data = await tryAndWait( - g => - new Promise(res => { - g.user(req.params.publicKey).load(data => { - res(data) - }) - }), - v => { - if (typeof v !== 'object') { - return true - } - - if (v === null) { - return true - } - - // load sometimes returns an empty set on the first try - return size(v) === 0 - } - ) - - return res.status(200).json({ - data - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message - }) - } - }) - ap.get(`/api/gun/auth`, (_, res) => { const { isAuthenticated } = require('../services/gunDB/Mediator') From 233452e59b925798fef81ea1b3f824f45939085b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 22 Jul 2021 17:34:30 -0400 Subject: [PATCH 04/11] Remove unused code / routes --- services/gunDB/contact-api/getters/index.js | 35 ------ services/gunDB/contact-api/getters/user.js | 129 -------------------- src/routes.js | 21 ---- 3 files changed, 185 deletions(-) delete mode 100644 services/gunDB/contact-api/getters/user.js diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index fb3223fe..8d8260fc 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -6,7 +6,6 @@ const Common = require('shock-common') const Key = require('../key') const Utils = require('../utils') -const User = require('./user') const { size } = require('lodash') /** @@ -85,39 +84,5 @@ const getMyUser = async () => { return u } -/** - * @param {string} publicKey - */ -const getUserInfo = async publicKey => { - const userInfo = await Utils.tryAndWait( - gun => - new Promise(res => - gun - .user(publicKey) - .get(Key.PROFILE) - .load(res) - ), - v => { - if (typeof v !== 'object') { - return true - } - - if (v === null) { - return true - } - - // load sometimes returns an empty set on the first try - return size(v) === 0 - } - ) - return { - publicKey, - avatar: userInfo.avatar, - displayName: userInfo.displayName - } -} module.exports.getMyUser = getMyUser -module.exports.getUserInfo = getUserInfo - -module.exports.getAnUser = User.getAnUser diff --git a/services/gunDB/contact-api/getters/user.js b/services/gunDB/contact-api/getters/user.js deleted file mode 100644 index f560d65c..00000000 --- a/services/gunDB/contact-api/getters/user.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * @format - */ -const Common = require('shock-common') -const size = require('lodash/size') - -const Key = require('../key') -const Utils = require('../utils') - -/** - * @param {string} publicKey - * @returns {Promise} - */ -//@returns {Promise} -const getAnUser = async publicKey => { - const oldProfile = await Utils.tryAndWait( - (g, u) => { - const user = u._.sea.pub === publicKey ? u : g.user(publicKey) - - return new Promise(res => user.get(Key.PROFILE).load(res)) - }, - v => typeof v !== 'object' - ) - - const bio = await Utils.tryAndWait( - (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, 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( - (g, u) => { - const user = u._.sea.pub === publicKey ? u : g.user(publicKey) - - return user.get(Key.LAST_SEEN_NODE).then() - }, - v => typeof v !== 'number' - ) - //@ts-ignore - /** @type {Common.SchemaTypes.User} */ - const u = { - avatar: oldProfile.avatar || null, - // @ts-ignore - bio: bio || null, - displayName: oldProfile.displayName || null, - // @ts-ignore - lastSeenApp: lastSeenApp || 0, - // @ts-ignore - lastSeenNode: lastSeenNode || 0, - // @ts-ignore - publicKey - } - - return u -} - -module.exports.getAnUser = getAnUser - -/** - * @returns {Promise} - */ -//@returns {Promise} -const getMyUser = async () => { - const oldProfile = await Utils.tryAndWait( - (_, user) => new Promise(res => user.get(Key.PROFILE).load(res)), - v => { - if (typeof v !== 'object') { - return true - } - - if (v === null) { - return true - } - - // load sometimes returns an empty set on the first try - return size(v) === 0 - } - ) - - const bio = await Utils.tryAndWait( - (_, user) => user.get(Key.BIO).then(), - v => typeof v !== 'string' - ) - - const lastSeenApp = await Utils.tryAndWait( - (_, user) => user.get(Key.LAST_SEEN_APP).then(), - v => typeof v !== 'number' - ) - - const lastSeenNode = await Utils.tryAndWait( - (_, user) => user.get(Key.LAST_SEEN_NODE).then(), - v => typeof v !== 'number' - ) - - const publicKey = await Utils.tryAndWait( - (_, user) => Promise.resolve(user.is && user.is.pub), - v => typeof v !== 'string' - ) - //@ts-ignore - /** @type {Common.SchemaTypes.User} */ - const u = { - avatar: oldProfile.avatar, - // @ts-ignore - bio, - displayName: oldProfile.displayName, - // @ts-ignore - lastSeenApp, - // @ts-ignore - lastSeenNode, - // @ts-ignore - publicKey - } - - return u -} - -module.exports.getMyUser = getMyUser diff --git a/src/routes.js b/src/routes.js index 44f262c4..09522e7f 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2293,27 +2293,6 @@ module.exports = async ( } }) - app.post(`/api/gun/userInfo`, async (req, res) => { - try { - const { pubs } = req.body - const reqs = pubs.map( - e => - new Promise((res, rej) => { - GunGetters.getUserInfo(e) - .then(r => res(r)) - .catch(e => rej(e)) - }) - ) - const infos = await Promise.all(reqs) - return res.status(200).json({ - pubInfos: infos - }) - } catch (err) { - return res.status(500).json({ - errorMessage: err.message - }) - } - }) ///////////////////////////////// /** * @template P From 0a3da2abc9c0a9da6039f38f9b6d0b9c323ea1e9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:09:24 -0400 Subject: [PATCH 05/11] Remove old create post fn --- services/gunDB/contact-api/actions.js | 157 -------------------------- 1 file changed, 157 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 9312f864..4433c150 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -752,162 +752,6 @@ const createPostNew = async (tags, title, content) => { return [postID, newPost] } -/** - * @param {string[]} tags - * @param {string} title - * @param {Common.Schema.ContentItem[]} content - * @returns {Promise} - */ -const createPost = async (tags, title, content) => { - if (content.length === 0) { - throw new Error(`A post must contain at least one paragraph/image/video`) - } - - const numOfPages = await (async () => { - const maybeNumOfPages = await Utils.tryAndWait( - (_, user) => - user - .get(Key.WALL) - .get(Key.NUM_OF_PAGES) - .then(), - v => typeof v !== 'number' - ) - - 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() - - const count = await (async () => { - if (numOfPages === 0) { - return 0 - } - - const maybeCount = await Utils.tryAndWait( - (_, user) => - user - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .get(Key.COUNT) - .then(), - v => typeof v !== 'number' - ) - - return typeof maybeCount === 'number' ? maybeCount : 0 - })() - - const shouldBeNewPage = - count >= Common.Constants.Misc.NUM_OF_POSTS_PER_WALL_PAGE - - if (shouldBeNewPage) { - pageIdx = Number(pageIdx + 1).toString() - } - - await /** @type {Promise} */ (new Promise((res, rej) => { - require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .put( - { - [Key.COUNT]: shouldBeNewPage ? 1 : count + 1, - posts: { - unused: null - } - }, - ack => { - if (ack.err && typeof ack.err !== 'number') { - rej(new Error(ack.err)) - } - - res() - } - ) - })) - - const [postID, newPost] = await createPostNew(tags, title, content) - - await Common.makePromise((res, rej) => { - require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .get(Key.POSTS) - .get(postID) - .put( - // @ts-expect-error - newPost, - ack => { - if (ack.err && typeof ack.err !== 'number') { - rej(new Error(ack.err)) - } else { - res() - } - } - ) - }) - - if (shouldBeNewPage || numOfPages === 0) { - await /** @type {Promise} */ (new Promise(res => { - require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.NUM_OF_PAGES) - .put(numOfPages + 1, ack => { - if (ack.err && typeof ack.err !== 'number') { - throw new Error(ack.err) - } - - res() - }) - })) - } - - const loadedPost = await new Promise(res => { - require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .get(Key.POSTS) - .get(postID) - .load(data => { - res(data) - }) - }) - - /** @type {Common.Schema.User} */ - const userForPost = await Getters.getMyUser() - - /** @type {Common.Schema.Post} */ - const completePost = { - ...loadedPost, - author: userForPost, - id: postID - } - - if (!Common.Schema.isPost(completePost)) { - throw new Error( - `completePost not a Post inside Actions.createPost(): ${JSON.stringify( - completePost - )}` - ) - } - - return completePost -} - /** * @param {string} postId * @param {string} page @@ -1058,7 +902,6 @@ module.exports = { saveSeedBackup, saveChannelsBackup, setLastSeenApp, - createPost, deletePost, follow, unfollow, From 244e88f06a04d93c0ecbfabe8d03681614656b90 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:19:00 -0400 Subject: [PATCH 06/11] Remove unused POC endpoint --- src/routes.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/routes.js b/src/routes.js index 09522e7f..f88328fb 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2117,20 +2117,6 @@ 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_DISPLAY_NAME}`, async (_, res) => { try { From 74ab9218eb3ddc2cc7430da2d6da105c187b2bc4 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:21:01 -0400 Subject: [PATCH 07/11] Remove unused endpoints --- src/routes.js | 67 --------------------------------------------------- 1 file changed, 67 deletions(-) diff --git a/src/routes.js b/src/routes.js index f88328fb..6bebfadc 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2118,73 +2118,6 @@ module.exports = async ( } }) - app.get(`/api/gun/${GunEvent.ON_DISPLAY_NAME}`, async (_, res) => { - try { - const user = require('../services/gunDB/Mediator').getUser() - const data = await timeout5( - user - .get(Key.PROFILE) - .get(Key.DISPLAY_NAME) - .then() - ) - res.json({ - data - }) - } catch (err) { - logger.info('Error in Display Name poll:') - logger.error(err) - res - .status( - err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500 - ) - .json({ - errorMessage: typeof err === 'string' ? err : err.message - }) - } - }) - - app.get(`/api/gun/${GunEvent.ON_HANDSHAKE_ADDRESS}`, async (_, res) => { - try { - const user = require('../services/gunDB/Mediator').getUser() - const data = await timeout5( - user.get(Key.CURRENT_HANDSHAKE_ADDRESS).then() - ) - res.json({ - data - }) - } catch (err) { - logger.info('Error in Handshake Address poll:') - logger.error(err) - res - .status( - err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500 - ) - .json({ - errorMessage: typeof err === 'string' ? err : err.message - }) - } - }) - - app.get(`/api/gun/${GunEvent.ON_BIO}`, async (_, res) => { - try { - const user = require('../services/gunDB/Mediator').getUser() - const data = await timeout5(user.get(Key.BIO).then()) - logger.debug(data) - res.json({ - data - }) - } catch (err) { - logger.info('Error in BIO poll:') - logger.error(err) - res - .status( - err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500 - ) - .json({ - errorMessage: typeof err === 'string' ? err : err.message - }) - } - }) //////////////////////////////////////////////////////////////////////////////// app.post(`/api/gun/sendpayment`, async (req, res) => { From 2d63f75a0e7073c3d9cb8e12fe2d2148732e8aaf Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:22:05 -0400 Subject: [PATCH 08/11] Remove unnecessary timeout() --- src/routes.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/routes.js b/src/routes.js index 6bebfadc..6f6b1e5d 100644 --- a/src/routes.js +++ b/src/routes.js @@ -50,8 +50,6 @@ module.exports = async ( { serverPort, CA, CA_KEY, usetls } ) => { try { - const { timeout5 } = require('../services/gunDB/contact-api/utils') - const Http = Axios.create({ httpsAgent: new httpsAgent.Agent({ ca: await FS.readFile(CA) @@ -2109,7 +2107,7 @@ module.exports = async ( const SEA = require('../services/gunDB/Mediator').mySEA const mySecret = require('../services/gunDB/Mediator').getMySecret() - const encBackup = await timeout5(user.get(Key.CHANNELS_BACKUP).then()) + const encBackup = await user.get(Key.CHANNELS_BACKUP).then() const backup = await SEA.decrypt(encBackup, mySecret) logger.info(backup) res.json({ data: backup }) From 3bae8e512285dc9104d3cea072b86fcb4713ab71 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:25:51 -0400 Subject: [PATCH 09/11] Better 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 d1fd34f9..fb6963bf 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -196,6 +196,7 @@ const pubToEpub = async pub => { return epub } catch (err) { + logger.error(`Error inside pubToEpub:`) logger.error(err) throw new Error(`pubToEpub() -> ${err.message}`) } From bff2d1becbc3c0a0c3296fc815635a0c425f612a Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 10:43:14 -0400 Subject: [PATCH 10/11] Remove unused endpoint --- src/routes.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/routes.js b/src/routes.js index 6f6b1e5d..ff0fd167 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2282,20 +2282,6 @@ module.exports = async ( ap.put(`/api/gun/follows/:publicKey`, apiGunFollowsPut) ap.delete(`/api/gun/follows/:publicKey`, apiGunFollowsDelete) - /** - * @type {RequestHandler<{}>} - */ - const apiGunMeGet = async (_, res) => { - try { - return res.status(200).json(await GunGetters.getMyUser()) - } catch (err) { - logger.error(err) - return res.status(500).json({ - errorMessage: err.message - }) - } - } - /** * @type {RequestHandler<{}>} */ @@ -2349,7 +2335,6 @@ module.exports = async ( } } - ap.get(`/api/gun/me`, apiGunMeGet) ap.put(`/api/gun/me`, apiGunMePut) ap.get(`/api/gun/auth`, (_, res) => { From b64c14b0672933a8973b31e8fa99f15c57c9bd21 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 10:44:54 -0400 Subject: [PATCH 11/11] Remove unused getter --- services/gunDB/contact-api/getters/index.js | 63 --------------------- 1 file changed, 63 deletions(-) diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index 8d8260fc..d5e198b6 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -1,13 +1,10 @@ /** * @format */ -const Common = require('shock-common') const Key = require('../key') const Utils = require('../utils') -const { size } = require('lodash') - /** * @param {string} pub * @returns {Promise} @@ -26,63 +23,3 @@ exports.currentOrderAddress = async pub => { return currAddr } - -/** - * @returns {Promise} - */ -//@returns {Promise} -const getMyUser = async () => { - const oldProfile = await Utils.tryAndWait( - (_, user) => new Promise(res => user.get(Key.PROFILE).load(res)), - v => { - if (typeof v !== 'object') { - return true - } - - if (v === null) { - return true - } - - // load sometimes returns an empty set on the first try - return size(v) === 0 - } - ) - - const bio = await Utils.tryAndWait( - (_, user) => user.get(Key.BIO).then(), - v => typeof v !== 'string' - ) - - const lastSeenApp = await Utils.tryAndWait( - (_, user) => user.get(Key.LAST_SEEN_APP).then(), - v => typeof v !== 'number' - ) - - const lastSeenNode = await Utils.tryAndWait( - (_, user) => user.get(Key.LAST_SEEN_NODE).then(), - v => typeof v !== 'number' - ) - - const publicKey = await Utils.tryAndWait( - (_, user) => Promise.resolve(user.is && user.is.pub), - v => typeof v !== 'string' - ) - //@ts-ignore - /** @type {Common.SchemaTypes.User} */ - const u = { - avatar: oldProfile.avatar, - // @ts-ignore - bio, - displayName: oldProfile.displayName, - // @ts-ignore - lastSeenApp, - // @ts-ignore - lastSeenNode, - // @ts-ignore - publicKey - } - - return u -} - -module.exports.getMyUser = getMyUser