include author in posts

This commit is contained in:
Daniel Lugo 2020-06-28 06:05:25 -04:00
parent 7ccb642aff
commit e8f935b0a9
3 changed files with 119 additions and 0 deletions

View file

@ -7,6 +7,7 @@ const Key = require('../key')
const Utils = require('../utils')
const Wall = require('./wall')
const User = require('./user')
/**
* @param {string} pub
@ -94,3 +95,5 @@ module.exports.Follows = require('./follows')
module.exports.getWallPage = Wall.getWallPage
module.exports.getWallTotalPages = Wall.getWallTotalPages
module.exports.getUser = 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
*/
const Common = require('shock-common')
const Utils = require('../utils')
const Key = require('../key')
const Wall = require('./user')
/**
* @returns {Promise<number>}
*/
@ -95,6 +98,8 @@ const getWallPage = async page => {
if (post === null) {
delete clean.posts[key]
} else {
// eslint-disable-next-line no-await-in-loop
post.author = await Wall.getMyUser()
post.id = key
}
}