diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index b4fe5bed..ef145e1b 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1217,6 +1217,87 @@ const setLastSeenApp = () => } }) }) +const maxPostPerPage = 10 +/** + * @param {string} tags + * @param {string} title + * @param {Record} content + * @returns {Promise} + */ +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} + */ +const deletePost = async postId => { + await new Promise(res => { + res(postId) + }) +} module.exports = { __createOutgoingFeed, @@ -1236,5 +1317,7 @@ module.exports = { saveSeedBackup, saveChannelsBackup, disconnect, - setLastSeenApp + setLastSeenApp, + createPost, + deletePost } diff --git a/services/gunDB/contact-api/key.js b/services/gunDB/contact-api/key.js index be14b788..4e7e3115 100644 --- a/services/gunDB/contact-api/key.js +++ b/services/gunDB/contact-api/key.js @@ -42,3 +42,9 @@ exports.CHANNELS_BACKUP = 'channelsBackup' exports.LAST_SEEN_APP = 'lastSeenApp' exports.LAST_SEEN_NODE = 'lastSeenNode' + +exports.WALL = 'wall' + +exports.NUM_OF_PAGES = 'numOfPages' + +exports.PAGES = 'pages' diff --git a/src/routes.js b/src/routes.js index 594f226e..48c08b65 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1616,8 +1616,8 @@ module.exports = async ( }); const GunEvent = Common.Constants.Event + const {timeout5} = require('../services/gunDB/contact-api/utils') const Key = require('../services/gunDB/contact-api/key') - const { timeout5 } = require('../services/gunDB/contact-api/utils') app.get("/api/gun/lndchanbackups", async (req,res) => { 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. */