posts mirror
This commit is contained in:
parent
a5598ba467
commit
97c4e7b977
2 changed files with 55 additions and 58 deletions
|
|
@ -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()
|
||||||
|
|
|
||||||
|
|
@ -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'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue