createPost()
This commit is contained in:
parent
8abfe5ef13
commit
ea77a4dd7e
1 changed files with 78 additions and 59 deletions
|
|
@ -3,7 +3,8 @@
|
||||||
*/
|
*/
|
||||||
const uuidv1 = require('uuid/v1')
|
const uuidv1 = require('uuid/v1')
|
||||||
const logger = require('winston')
|
const logger = require('winston')
|
||||||
const { Constants, Schema } = require('shock-common')
|
const Common = require('shock-common')
|
||||||
|
const { Constants, Schema } = Common
|
||||||
|
|
||||||
const { ErrorCode } = Constants
|
const { ErrorCode } = Constants
|
||||||
|
|
||||||
|
|
@ -1248,76 +1249,94 @@ const setLastSeenApp = () =>
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} tags
|
* @param {string[]} tags
|
||||||
* @param {string} title
|
* @param {string} title
|
||||||
* @param {Record<string,import('shock-common').Schema.ContentItem>} content
|
* @param {Common.Schema.ContentItem[]} content
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<Common.Schema.Post>}
|
||||||
*/
|
*/
|
||||||
const createPost = async (tags, title, content) => {
|
const createPost = async (tags, title, content) => {
|
||||||
const user = require('../Mediator').getUser()
|
const user = require('../Mediator').getUser()
|
||||||
const wall = user.get(Key.WALL)
|
const wall = user.get(Key.WALL)
|
||||||
if (!wall) {
|
const pages = wall.get(Key.PAGES)
|
||||||
throw new Error('wall not found')
|
|
||||||
}
|
|
||||||
let lastPageNum = await wall.get(Key.NUM_OF_PAGES).then()
|
|
||||||
if (typeof lastPageNum !== 'number') {
|
|
||||||
throw new Error('page not found')
|
|
||||||
}
|
|
||||||
const lastPage = await new Promise(res => {
|
|
||||||
if (typeof lastPageNum !== 'number') {
|
|
||||||
throw new Error('page not found')
|
|
||||||
}
|
|
||||||
wall
|
|
||||||
.get(Key.PAGES)
|
|
||||||
.get(lastPageNum.toString())
|
|
||||||
//@ts-ignore
|
|
||||||
.load(res)
|
|
||||||
})
|
|
||||||
|
|
||||||
const numPosts = Object.keys(lastPage).length
|
const numOfPages = await (async () => {
|
||||||
if (numPosts === maxPostPerPage) {
|
const maybeNumOfPages = await Utils.tryAndWait(() =>
|
||||||
lastPageNum++
|
wall.get(Key.NUM_OF_PAGES).then()
|
||||||
await new Promise((res, rej) => {
|
)
|
||||||
if (typeof lastPageNum !== 'number') {
|
|
||||||
throw new Error('page not found')
|
return (typeof maybeNumOfPages === 'number' && maybeNumOfPages) || 0
|
||||||
|
})()
|
||||||
|
|
||||||
|
const pageIdx = Math.max(0, numOfPages - 1).toString()
|
||||||
|
let page = pages.get(pageIdx)
|
||||||
|
|
||||||
|
const count = (await page.get(Key.COUNT).then()) || 0
|
||||||
|
|
||||||
|
let isNewPage = false
|
||||||
|
if (count >= Common.Constants.Misc.NUM_OF_POSTS_PER_WALL_PAGE) {
|
||||||
|
// eslint-disable-next-line require-atomic-updates
|
||||||
|
page = wall.get(Key.PAGES)
|
||||||
|
isNewPage = true
|
||||||
}
|
}
|
||||||
wall.get(Key.NUM_OF_PAGES).put(lastPageNum, ack => {
|
|
||||||
|
/** @type {string} */
|
||||||
|
const postID = await new Promise((res, rej) => {
|
||||||
|
const _n = page.set(
|
||||||
|
{
|
||||||
|
date: Date.now(),
|
||||||
|
status: 'publish',
|
||||||
|
tags: tags.join('-'),
|
||||||
|
title
|
||||||
|
},
|
||||||
|
ack => {
|
||||||
if (ack.err) {
|
if (ack.err) {
|
||||||
rej(ack.err)
|
rej(ack.err)
|
||||||
} else {
|
} else {
|
||||||
res()
|
res(_n._.get)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
if (isNewPage) {
|
||||||
|
await Utils.promisifyGunNode(wall.get(Key.NUM_OF_PAGES)).put(numOfPages + 1)
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @type {import('shock-common').Schema.Post}
|
const post = page.get(postID)
|
||||||
*/
|
|
||||||
const post = {
|
await Promise.all(
|
||||||
contentItems: content,
|
content.map(ci => {
|
||||||
date: +Date(),
|
|
||||||
status: 'publish',
|
|
||||||
tags,
|
|
||||||
title
|
|
||||||
}
|
|
||||||
//** @type {string} */
|
|
||||||
/*const newPostID = */ await new Promise((res, rej) => {
|
|
||||||
if (typeof lastPageNum !== 'number') {
|
|
||||||
throw new Error('page not found')
|
|
||||||
}
|
|
||||||
const _wallNode = wall
|
|
||||||
.get(Key.PAGES)
|
|
||||||
.get(lastPageNum.toString())
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
.set(post, ack => {
|
return Utils.promisifyGunNode(post).set(ci)
|
||||||
if (ack.err) {
|
})
|
||||||
rej(new Error(ack.err))
|
)
|
||||||
} else {
|
|
||||||
res(_wallNode._.get)
|
const loadedPost = await new Promise(res => {
|
||||||
}
|
post.load(data => {
|
||||||
|
res(data)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/** @type {Common.Schema.User} */
|
||||||
|
const userForPost = await Getters.getMyUser()
|
||||||
|
|
||||||
|
/** @type {Common.Schema.Post} */
|
||||||
|
const completePost = {
|
||||||
|
...loadedPost,
|
||||||
|
author: userForPost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Common.Schema.isPost(completePost)) {
|
||||||
|
throw new Error(
|
||||||
|
`completePost not a Post inside Actions.createPost(): ${JSON.stringify(
|
||||||
|
createPost
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return completePost
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} postId
|
* @param {string} postId
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue