better post creation process
This commit is contained in:
parent
4a80fe5caf
commit
87a71e4fc5
2 changed files with 115 additions and 48 deletions
|
|
@ -1259,33 +1259,46 @@ 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()
|
||||||
|
.get(Key.WALL)
|
||||||
|
.get(Key.PAGES)
|
||||||
|
.get(pageIdx)
|
||||||
|
.set(
|
||||||
{
|
{
|
||||||
date: Date.now(),
|
date: Date.now(),
|
||||||
status: 'publish',
|
status: 'publish',
|
||||||
|
|
@ -1302,13 +1315,31 @@ const createPost = async (tags, title, content) => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
const post = page.get(postID)
|
res()
|
||||||
const contentItems = post.get(Key.CONTENT_ITEMS)
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentItems = require('../Mediator')
|
||||||
|
.getUser()
|
||||||
|
.get(Key.WALL)
|
||||||
|
.get(Key.PAGES)
|
||||||
|
.get(pageIdx)
|
||||||
|
.get(postID)
|
||||||
|
.get(Key.CONTENT_ITEMS)
|
||||||
|
|
||||||
|
try {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
content.map(
|
content.map(
|
||||||
ci =>
|
ci =>
|
||||||
|
|
@ -1324,9 +1355,34 @@ const createPost = async (tags, title, content) => {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
} 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')
|
||||||
|
.getUser()
|
||||||
|
.get(Key.WALL)
|
||||||
|
.get(Key.PAGES)
|
||||||
|
.get(pageIdx)
|
||||||
|
.get(postID)
|
||||||
|
.load(data => {
|
||||||
res(data)
|
res(data)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue