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,7 +1543,10 @@ 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 = []
promises.push(
new Promise((res, rej) => {
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.NUM_OF_PAGES) .get(Key.NUM_OF_PAGES)
@ -1555,8 +1558,10 @@ const initWall = async () => {
} }
}) })
}) })
)
await new Promise((res, rej) => { promises.push(
new Promise((res, rej) => {
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
@ -1575,8 +1580,10 @@ const initWall = async () => {
} }
) )
}) })
)
await new Promise((res, rej) => { promises.push(
new Promise((res, rej) => {
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
@ -1590,6 +1597,9 @@ const initWall = async () => {
} }
}) })
}) })
)
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,6 +118,15 @@ const getWallPage = async (page, publicKey) => {
} }
return new Promise(res => { return new Promise(res => {
// forces data fetch
user
.get(Key.WALL)
.get(Key.PAGES)
.get(actualPageIdx.toString())
// @ts-ignore
.load(() => {})
process.nextTick(() => {
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
@ -117,34 +134,41 @@ const getWallPage = async (page, publicKey) => {
// @ts-ignore // @ts-ignore
.load(res) .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
} }
} }