From 97c4e7b97789ab51b6055e905ae725cbc6b4e9c4 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 19 Oct 2020 16:09:06 -0400 Subject: [PATCH] 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'