Merge pull request #127 from shocknet/enh/posts

Enh/posts
This commit is contained in:
CapDog 2020-07-30 10:42:33 -04:00 committed by GitHub
commit 10dae1dd8c
2 changed files with 94 additions and 60 deletions

View file

@ -1543,53 +1543,63 @@ const unfollow = publicKey =>
const initWall = async () => { const initWall = async () => {
const user = require('../Mediator').getUser() const user = require('../Mediator').getUser()
await new Promise((res, rej) => { const promises = []
user
.get(Key.WALL)
.get(Key.NUM_OF_PAGES)
.put(0, ack => {
if (ack.err) {
rej(new Error(ack.err))
} else {
res()
}
})
})
await new Promise((res, rej) => { promises.push(
user new Promise((res, rej) => {
.get(Key.WALL) user
.get(Key.PAGES) .get(Key.WALL)
.get('0') .get(Key.NUM_OF_PAGES)
.get(Key.POSTS) .put(0, ack => {
.put(
{
unused: null
},
ack => {
if (ack.err) { if (ack.err) {
rej(new Error(ack.err)) rej(new Error(ack.err))
} else { } else {
res() res()
} }
} })
) })
}) )
await new Promise((res, rej) => { promises.push(
user new Promise((res, rej) => {
.get(Key.WALL) user
.get(Key.PAGES) .get(Key.WALL)
.get('0') .get(Key.PAGES)
.get(Key.COUNT) .get('0')
.put(0, ack => { .get(Key.POSTS)
if (ack.err) { .put(
rej(new Error(ack.err)) {
} else { unused: null
res() },
} ack => {
}) if (ack.err) {
}) rej(new Error(ack.err))
} else {
res()
}
}
)
})
)
promises.push(
new Promise((res, rej) => {
user
.get(Key.WALL)
.get(Key.PAGES)
.get('0')
.get(Key.COUNT)
.put(0, ack => {
if (ack.err) {
rej(new Error(ack.err))
} else {
res()
}
})
})
)
await Promise.all(promises)
} }
module.exports = { module.exports = {

View file

@ -2,11 +2,14 @@
* @format * @format
*/ */
const Common = require('shock-common') const Common = require('shock-common')
const pickBy = require('lodash/pickBy')
const size = require('lodash/size')
const mapValues = require('lodash/mapValues')
const Utils = require('../utils') const Utils = require('../utils')
const Key = require('../key') const Key = require('../key')
const Wall = require('./user') const User = require('./user')
/** /**
* @param {string=} publicKey * @param {string=} publicKey
@ -95,6 +98,11 @@ const getWallPage = async (page, publicKey) => {
return empty return empty
} }
/**
* We just use it so Common.Schema.isWallPage passes.
*/
const mockUser = await User.getMyUser()
/** /**
* @type {Common.SchemaTypes.WallPage} * @type {Common.SchemaTypes.WallPage}
*/ */
@ -110,41 +118,57 @@ const getWallPage = async (page, publicKey) => {
} }
return new Promise(res => { return new Promise(res => {
// forces data fetch
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
.get(actualPageIdx.toString()) .get(actualPageIdx.toString())
// @ts-ignore // @ts-ignore
.load(res) .load(() => {})
process.nextTick(() => {
user
.get(Key.WALL)
.get(Key.PAGES)
.get(actualPageIdx.toString())
// @ts-ignore
.load(res)
})
}) })
}, },
maybePage => { maybePage => {
if (typeof maybePage !== 'object' || maybePage === null) { // sometimes load() returns an empty object on the first call
if (size(/** @type {any} */ (maybePage)) === 0) {
return true return true
} }
const clean = { const page = /** @type {Common.Schema.WallPage} */ (maybePage)
...maybePage
if (typeof page.count !== 'number') {
return true
} }
// @ts-ignore // removes 'unused' initializer and aborted writes
for (const [key, post] of Object.entries(clean.posts)) { page.posts = pickBy(page.posts, v => v !== null)
// delete unsuccessful writes
if (post === null) {
// @ts-ignore
delete clean.posts[key]
} else {
post.id = key
}
}
// .load() sometimes doesn't load all data on first call // .load() sometimes doesn't load all data on first call
// @ts-ignore if (size(page.posts) === 0) {
if (Object.keys(clean.posts).length === 0) {
return true return true
} }
return !Common.Schema.isWallPage(clean) // Give ids based on keys
page.posts = mapValues(page.posts, (v, k) => ({
...v,
id: k
}))
page.posts = mapValues(page.posts, v => ({
...v,
// isWallPage() would otherwise not pass
author: mockUser
}))
return !Common.Schema.isWallPage(page)
} }
) )
@ -160,9 +184,9 @@ const getWallPage = async (page, publicKey) => {
} else { } else {
post.author = publicKey post.author = publicKey
? // eslint-disable-next-line no-await-in-loop ? // eslint-disable-next-line no-await-in-loop
await Wall.getAnUser(publicKey) await User.getAnUser(publicKey)
: // eslint-disable-next-line no-await-in-loop : // eslint-disable-next-line no-await-in-loop
await Wall.getMyUser() await User.getMyUser()
post.id = key post.id = key
} }
} }