wall add and delete

This commit is contained in:
boufni95 2020-06-12 19:03:43 +02:00 committed by Daniel Lugo
parent b3b5331b50
commit 8d1aae0ac3
3 changed files with 112 additions and 2 deletions

View file

@ -1217,6 +1217,87 @@ const setLastSeenApp = () =>
} }
}) })
}) })
const maxPostPerPage = 10
/**
* @param {string} tags
* @param {string} title
* @param {Record<string,import('shock-common').Schema.ContentItem>} content
* @returns {Promise<void>}
*/
const createPost = async (tags, title, content) => {
const user = require('../Mediator').getUser()
const wall = user.get(Key.WALL)
if (!wall) {
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
if (numPosts === maxPostPerPage) {
lastPageNum++
await new Promise((res, rej) => {
if (typeof lastPageNum !== 'number') {
throw new Error('page not found')
}
wall.get(Key.NUM_OF_PAGES).put(lastPageNum, ack => {
if (ack.err) {
rej(ack.err)
} else {
res()
}
})
})
}
/**
* @type {import('shock-common').Schema.Post}
*/
const post = {
contentItems: content,
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
.set(post, ack => {
if (ack.err) {
rej(new Error(ack.err))
} else {
res(_wallNode._.get)
}
})
})
}
/**
* @param {string} postId
* @returns {Promise<void>}
*/
const deletePost = async postId => {
await new Promise(res => {
res(postId)
})
}
module.exports = { module.exports = {
__createOutgoingFeed, __createOutgoingFeed,
@ -1236,5 +1317,7 @@ module.exports = {
saveSeedBackup, saveSeedBackup,
saveChannelsBackup, saveChannelsBackup,
disconnect, disconnect,
setLastSeenApp setLastSeenApp,
createPost,
deletePost
} }

View file

@ -42,3 +42,9 @@ exports.CHANNELS_BACKUP = 'channelsBackup'
exports.LAST_SEEN_APP = 'lastSeenApp' exports.LAST_SEEN_APP = 'lastSeenApp'
exports.LAST_SEEN_NODE = 'lastSeenNode' exports.LAST_SEEN_NODE = 'lastSeenNode'
exports.WALL = 'wall'
exports.NUM_OF_PAGES = 'numOfPages'
exports.PAGES = 'pages'

View file

@ -1616,8 +1616,8 @@ module.exports = async (
}); });
const GunEvent = Common.Constants.Event const GunEvent = Common.Constants.Event
const {timeout5} = require('../services/gunDB/contact-api/utils')
const Key = require('../services/gunDB/contact-api/key') const Key = require('../services/gunDB/contact-api/key')
const { timeout5 } = require('../services/gunDB/contact-api/utils')
app.get("/api/gun/lndchanbackups", async (req,res) => { app.get("/api/gun/lndchanbackups", async (req,res) => {
try{ try{
@ -1757,6 +1757,27 @@ module.exports = async (
} }
}) })
app.post(`/api/gun/wall`, async (req,res) => {
try{
const {tags,title,contentItems} = req.body
res.status(201).json(await GunActions.createPost(title,tags,contentItems))
} catch(e) {
const {tags,title,contentItems} = req.body
if(!tags || !title || !contentItems) {
res.status(400).json({
errorMessage: 'missing params'
})
}
res.status(500).json({
errorMessage: typeof e === 'string' ? e : e.message
})
}
})
app.delete(`/api/gun/wall/:postID`,async (req,res) => {
//res.status(200).json(await GunActions.deletePost(postID))
})
/** /**
* Return app so that it can be used by express. * Return app so that it can be used by express.
*/ */