Merge pull request #220 from shocknet/posts-mirror

Posts mirror
This commit is contained in:
Daniel Lugo 2020-10-19 16:12:54 -04:00 committed by GitHub
commit 2e1d75c288
4 changed files with 60 additions and 63 deletions

View file

@ -49,7 +49,7 @@
"request-promise": "^4.2.2", "request-promise": "^4.2.2",
"response-time": "^2.3.2", "response-time": "^2.3.2",
"shelljs": "^0.8.2", "shelljs": "^0.8.2",
"shock-common": "^13.0.1", "shock-common": "^14.2.0",
"socket.io": "2.1.1", "socket.io": "2.1.1",
"text-encoding": "^0.7.0", "text-encoding": "^0.7.0",
"tingodb": "^0.6.1", "tingodb": "^0.6.1",

View file

@ -5,6 +5,7 @@ const uuidv1 = require('uuid/v1')
const logger = require('winston') const logger = require('winston')
const Common = require('shock-common') const Common = require('shock-common')
const { Constants, Schema } = Common const { Constants, Schema } = Common
const Gun = require('gun')
const { ErrorCode } = Constants 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[]} tags
* @param {string} title * @param {string} title
@ -1312,27 +1356,24 @@ const createPost = async (tags, title, content) => {
) )
}) })
/** @type {string} */ const [postID, newPost] = await createPostNew(tags, title, content)
const postID = await new Promise((res, rej) => {
const _n = require('../Mediator') await Common.makePromise((res, rej) => {
require('../Mediator')
.getUser() .getUser()
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
.get(pageIdx) .get(pageIdx)
.get(Key.POSTS) .get(Key.POSTS)
.set( .get(postID)
{ .put(
date: Date.now(), // @ts-expect-error
status: 'publish', newPost,
tags: tags.join('-'),
page: pageIdx,
title
},
ack => { ack => {
if (ack.err && typeof ack.err !== 'number') { if (ack.err && typeof ack.err !== 'number') {
rej(new Error(ack.err)) rej(new Error(ack.err))
} else { } 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 => { const loadedPost = await new Promise(res => {
require('../Mediator') require('../Mediator')
.getUser() .getUser()

View file

@ -62,3 +62,5 @@ exports.TOTAL_TIPS = 'totalTips'
exports.TIPS_PAYMENT_STATUS = 'tipsPaymentStatus' exports.TIPS_PAYMENT_STATUS = 'tipsPaymentStatus'
exports.PROFILE_BINARY = 'profileBinary' exports.PROFILE_BINARY = 'profileBinary'
exports.POSTS_NEW = 'posts'

View file

@ -6021,10 +6021,10 @@ shellwords@^0.1.1:
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==
shock-common@^13.0.1: shock-common@^14.2.0:
version "13.0.1" version "14.2.0"
resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-13.0.1.tgz#f0916e51e63311b90509e5831896abc7c9febbcf" resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-14.2.0.tgz#b9d78d7235d84c9aaffb3d42a5db2583dec43763"
integrity sha512-lXyS1PFrQzE3yPvnPxACkQKTLuuu52s4gqw4hZ9vmmlJX3fh8cV+jZ9E1/zblyxPayZhjQPvEX13NZD2xDDwiQ== integrity sha512-kLPAgpJNQekHAfiwPj+xVR7ReIPsi37mACpmk6YF7YIx7/jTQZYWxN+YQgQnrIlw8ELPj6igj+f6w310fmxOgA==
dependencies: dependencies:
immer "^6.0.6" immer "^6.0.6"
lodash "^4.17.19" lodash "^4.17.19"