Merge branch 'master' into feat/feed

This commit is contained in:
Daniel Lugo 2020-06-28 06:12:30 -04:00
commit 8d3255eb5a
3 changed files with 118 additions and 0 deletions

View file

@ -8,6 +8,7 @@ const Utils = require('../utils')
const Wall = require('./wall') const Wall = require('./wall')
const Feed = require('./feed') const Feed = require('./feed')
const User = require('./user')
/** /**
* @param {string} pub * @param {string} pub
@ -97,3 +98,4 @@ module.exports.getWallPage = Wall.getWallPage
module.exports.getWallTotalPages = Wall.getWallTotalPages module.exports.getWallTotalPages = Wall.getWallTotalPages
module.exports.getFeedPage = Feed.getFeedPage module.exports.getFeedPage = Feed.getFeedPage
module.exports.getAnUser = User.getAnUser

View file

@ -0,0 +1,111 @@
/**
* @format
*/
const Common = require('shock-common')
const Key = require('../key')
const Utils = require('../utils')
/**
* @param {string} publicKey
* @returns {Promise<Common.SchemaTypes.User>}
*/
const getAnUser = async publicKey => {
const oldProfile = await Utils.tryAndWait(
g => {
const user = g.get(`~${publicKey}`)
return new Promise(res => user.get(Key.PROFILE).load(res))
},
v => typeof v !== 'object'
)
const bio = await Utils.tryAndWait(
g =>
g
.get(`~${publicKey}`)
.get(Key.BIO)
.then(),
v => typeof v !== 'string'
)
const lastSeenApp = await Utils.tryAndWait(
g =>
g
.get(`~${publicKey}`)
.get(Key.LAST_SEEN_APP)
.then(),
v => typeof v !== 'number'
)
const lastSeenNode = await Utils.tryAndWait(
(_, user) => user.get(Key.LAST_SEEN_NODE).then(),
v => typeof v !== 'number'
)
/** @type {Common.SchemaTypes.User} */
const u = {
avatar: oldProfile.avatar,
// @ts-ignore
bio,
displayName: oldProfile.displayName,
// @ts-ignore
lastSeenApp,
// @ts-ignore
lastSeenNode,
// @ts-ignore
publicKey
}
return u
}
module.exports.getAnUser = getAnUser
/**
* @returns {Promise<Common.SchemaTypes.User>}
*/
const getMyUser = async () => {
const oldProfile = await Utils.tryAndWait(
(_, user) => new Promise(res => user.get(Key.PROFILE).load(res)),
v => typeof v !== 'object'
)
const bio = await Utils.tryAndWait(
(_, user) => user.get(Key.BIO).then(),
v => typeof v !== 'string'
)
const lastSeenApp = await Utils.tryAndWait(
(_, user) => user.get(Key.LAST_SEEN_APP).then(),
v => typeof v !== 'number'
)
const lastSeenNode = await Utils.tryAndWait(
(_, user) => user.get(Key.LAST_SEEN_NODE).then(),
v => typeof v !== 'number'
)
const publicKey = await Utils.tryAndWait(
(_, user) => Promise.resolve(user.is && user.is.pub),
v => typeof v !== 'string'
)
/** @type {Common.SchemaTypes.User} */
const u = {
avatar: oldProfile.avatar,
// @ts-ignore
bio,
displayName: oldProfile.displayName,
// @ts-ignore
lastSeenApp,
// @ts-ignore
lastSeenNode,
// @ts-ignore
publicKey
}
return u
}
module.exports.getMyUser = getMyUser

View file

@ -2,9 +2,12 @@
* @format * @format
*/ */
const Common = require('shock-common') const Common = require('shock-common')
const Utils = require('../utils') const Utils = require('../utils')
const Key = require('../key') const Key = require('../key')
const Wall = require('./user')
/** /**
* @param {string=} publicKey * @param {string=} publicKey
* @returns {Promise<number>} * @returns {Promise<number>}
@ -104,6 +107,8 @@ const getWallPage = async (page, publicKey) => {
delete clean.posts[key] delete clean.posts[key]
clean.count-- clean.count--
} else { } else {
// eslint-disable-next-line no-await-in-loop
post.author = await Wall.getMyUser()
post.id = key post.id = key
} }
} }