better post creation process

This commit is contained in:
Daniel Lugo 2020-06-24 14:18:03 -04:00
parent 4a80fe5caf
commit 87a71e4fc5
2 changed files with 115 additions and 48 deletions

View file

@ -1259,76 +1259,132 @@ const createPost = async (tags, title, content) => {
throw new Error(`A post must contain at least one paragraph/image/video`) throw new Error(`A post must contain at least one paragraph/image/video`)
} }
const user = require('../Mediator').getUser()
const wall = user.get(Key.WALL)
const pages = wall.get(Key.PAGES)
const numOfPages = await (async () => { const numOfPages = await (async () => {
const maybeNumOfPages = await Utils.tryAndWait(() => const maybeNumOfPages = await Utils.tryAndWait(
wall.get(Key.NUM_OF_PAGES).then() (_, user) => user.get(Key.NUM_OF_PAGES).then(),
v => typeof v !== 'number'
) )
return (typeof maybeNumOfPages === 'number' && maybeNumOfPages) || 0 return typeof maybeNumOfPages === 'number' ? maybeNumOfPages : 0
})() })()
const pageIdx = Math.max(0, numOfPages - 1).toString() let pageIdx = Math.max(0, numOfPages - 1).toString()
let page = pages.get(pageIdx)
const count = (await page.get(Key.COUNT).then()) || 0 const count =
numOfPages === 0
? 0
: /** @type {number} */ (await Utils.tryAndWait(
(_, user) =>
user
.get(Key.WALL)
.get(Key.PAGES)
.get(pageIdx)
.get(Key.COUNT)
.then(),
v => typeof v !== 'number'
))
let isNewPage = false const shouldBeNewPage =
if (count >= Common.Constants.Misc.NUM_OF_POSTS_PER_WALL_PAGE) { count >= Common.Constants.Misc.NUM_OF_POSTS_PER_WALL_PAGE
// eslint-disable-next-line require-atomic-updates
page = wall.get(Key.PAGES) if (shouldBeNewPage) {
isNewPage = true pageIdx = Number(pageIdx + 1).toString()
} }
/** @type {string} */ /** @type {string} */
const postID = await new Promise((res, rej) => { const postID = await new Promise((res, rej) => {
const _n = page.set( const _n = require('../Mediator')
{ .getUser()
date: Date.now(), .get(Key.WALL)
status: 'publish', .get(Key.PAGES)
tags: tags.join('-'), .get(pageIdx)
title .set(
}, {
ack => { date: Date.now(),
if (ack.err) { status: 'publish',
rej(ack.err) tags: tags.join('-'),
} else { title
res(_n._.get) },
ack => {
if (ack.err) {
rej(ack.err)
} else {
res(_n._.get)
}
} }
} )
)
}) })
if (isNewPage) { if (shouldBeNewPage) {
await Utils.promisifyGunNode(wall.get(Key.NUM_OF_PAGES)).put(numOfPages + 1) await new Promise(res => {
require('../Mediator')
.getUser()
.get(Key.WALL)
.get(Key.NUM_OF_PAGES)
.put(numOfPages + 1, ack => {
if (ack.err) {
throw new Error(ack.err)
}
res()
})
})
} }
const post = page.get(postID) const contentItems = require('../Mediator')
const contentItems = post.get(Key.CONTENT_ITEMS) .getUser()
.get(Key.WALL)
.get(Key.PAGES)
.get(pageIdx)
.get(postID)
.get(Key.CONTENT_ITEMS)
await Promise.all( try {
content.map( await Promise.all(
ci => content.map(
new Promise(res => { ci =>
// @ts-ignore new Promise(res => {
contentItems.set(ci, ack => { // @ts-ignore
if (ack.err) { contentItems.set(ci, ack => {
throw new Error(ack.err) if (ack.err) {
} throw new Error(ack.err)
}
res() res()
})
}) })
}) )
) )
) } catch (e) {
await new Promise(res => {
require('../Mediator')
.getUser()
.get(Key.WALL)
.get(Key.PAGES)
.get(pageIdx)
.get(postID)
.put(null, ack => {
if (ack.err) {
throw new Error(ack.err)
}
res()
})
})
throw e
}
const loadedPost = await new Promise(res => { const loadedPost = await new Promise(res => {
post.load(data => { require('../Mediator')
res(data) .getUser()
}) .get(Key.WALL)
.get(Key.PAGES)
.get(pageIdx)
.get(postID)
.load(data => {
res(data)
})
}) })
/** @type {Common.Schema.User} */ /** @type {Common.Schema.User} */

View file

@ -51,6 +51,17 @@ const getWallPage = async page => {
) )
if (Common.Schema.isWallPage(thePage)) { if (Common.Schema.isWallPage(thePage)) {
const clean = {
...thePage
}
// delete unsuccessful writes
Object.keys(clean.posts).forEach(k => {
if (clean.posts[k] === null) {
delete clean.posts[k]
}
})
return thePage return thePage
} }