From a5598ba46734c5e93817928867823602b153d70f Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 19 Oct 2020 15:56:33 -0400 Subject: [PATCH 1/2] upgrade shock-common --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f86f5033..12e60b52 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "request-promise": "^4.2.2", "response-time": "^2.3.2", "shelljs": "^0.8.2", - "shock-common": "^13.0.1", + "shock-common": "^14.2.0", "socket.io": "2.1.1", "text-encoding": "^0.7.0", "tingodb": "^0.6.1", diff --git a/yarn.lock b/yarn.lock index 8bc2f20b..139ce777 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6021,10 +6021,10 @@ shellwords@^0.1.1: resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== -shock-common@^13.0.1: - version "13.0.1" - resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-13.0.1.tgz#f0916e51e63311b90509e5831896abc7c9febbcf" - integrity sha512-lXyS1PFrQzE3yPvnPxACkQKTLuuu52s4gqw4hZ9vmmlJX3fh8cV+jZ9E1/zblyxPayZhjQPvEX13NZD2xDDwiQ== +shock-common@^14.2.0: + version "14.2.0" + resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-14.2.0.tgz#b9d78d7235d84c9aaffb3d42a5db2583dec43763" + integrity sha512-kLPAgpJNQekHAfiwPj+xVR7ReIPsi37mACpmk6YF7YIx7/jTQZYWxN+YQgQnrIlw8ELPj6igj+f6w310fmxOgA== dependencies: immer "^6.0.6" lodash "^4.17.19" From 97c4e7b97789ab51b6055e905ae725cbc6b4e9c4 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 19 Oct 2020 16:09:06 -0400 Subject: [PATCH 2/2] posts mirror --- services/gunDB/contact-api/actions.js | 111 ++++++++++++-------------- services/gunDB/contact-api/key.js | 2 + 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 9308303e..293b0337 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -5,6 +5,7 @@ const uuidv1 = require('uuid/v1') const logger = require('winston') const Common = require('shock-common') const { Constants, Schema } = Common +const Gun = require('gun') const { ErrorCode } = Constants @@ -1229,6 +1230,49 @@ const setLastSeenApp = () => }) ) +/** + * @param {string[]} tags + * @param {string} title + * @param {Common.Schema.ContentItem[]} content + * @returns {Promise<[string, Common.Schema.RawPost]>} + */ +const createPostNew = async (tags, title, content) => { + /** @type {Common.Schema.RawPost} */ + const newPost = { + date: Date.now(), + status: 'publish', + tags: tags.join('-'), + title, + contentItems: {} + } + + content.forEach(c => { + // @ts-expect-error + const uuid = Gun.text.random() + newPost.contentItems[uuid] = c + }) + + /** @type {string} */ + const postID = await Common.makePromise((res, rej) => { + const _n = require('../Mediator') + .getUser() + .get(Key.POSTS_NEW) + .set( + // @ts-expect-error + newPost, + ack => { + if (ack.err && typeof ack.err !== 'number') { + rej(new Error(ack.err)) + } else { + res(_n._.get) + } + } + ) + }) + + return [postID, newPost] +} + /** * @param {string[]} tags * @param {string} title @@ -1312,27 +1356,24 @@ const createPost = async (tags, title, content) => { ) }) - /** @type {string} */ - const postID = await new Promise((res, rej) => { - const _n = require('../Mediator') + 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) - .set( - { - date: Date.now(), - status: 'publish', - tags: tags.join('-'), - page: pageIdx, - title - }, + .get(postID) + .put( + // @ts-expect-error + newPost, ack => { if (ack.err && typeof ack.err !== 'number') { rej(new Error(ack.err)) } else { - res(_n._.get) + res() } } ) @@ -1354,52 +1395,6 @@ const createPost = async (tags, title, content) => { }) } - const contentItems = require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .get(Key.POSTS) - .get(postID) - .get(Key.CONTENT_ITEMS) - - try { - await Promise.all( - content.map( - ci => - new Promise(res => { - // @ts-ignore - contentItems.set(ci, ack => { - if (ack.err && typeof ack.err !== 'number') { - throw new Error(ack.err) - } - - res() - }) - }) - ) - ) - } catch (e) { - await new Promise(res => { - require('../Mediator') - .getUser() - .get(Key.WALL) - .get(Key.PAGES) - .get(pageIdx) - .get(Key.POSTS) - .get(postID) - .put(null, ack => { - if (ack.err && typeof ack.err !== 'number') { - throw new Error(ack.err) - } - - res() - }) - }) - - throw e - } - const loadedPost = await new Promise(res => { require('../Mediator') .getUser() diff --git a/services/gunDB/contact-api/key.js b/services/gunDB/contact-api/key.js index 58d43fba..ece0c1ed 100644 --- a/services/gunDB/contact-api/key.js +++ b/services/gunDB/contact-api/key.js @@ -62,3 +62,5 @@ exports.TOTAL_TIPS = 'totalTips' exports.TIPS_PAYMENT_STATUS = 'tipsPaymentStatus' exports.PROFILE_BINARY = 'profileBinary' + +exports.POSTS_NEW = 'posts'