From e8f935b0a90834b009871689c818f3487f0f1a51 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 28 Jun 2020 06:05:25 -0400 Subject: [PATCH] include author in posts --- services/gunDB/contact-api/getters/index.js | 3 + services/gunDB/contact-api/getters/user.js | 111 ++++++++++++++++++++ services/gunDB/contact-api/getters/wall.js | 5 + 3 files changed, 119 insertions(+) create mode 100644 services/gunDB/contact-api/getters/user.js diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index 2bb192fb..64b5a531 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -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 diff --git a/services/gunDB/contact-api/getters/user.js b/services/gunDB/contact-api/getters/user.js new file mode 100644 index 00000000..fdcd7dfb --- /dev/null +++ b/services/gunDB/contact-api/getters/user.js @@ -0,0 +1,111 @@ +/** + * @format + */ +const Common = require('shock-common') + +const Key = require('../key') +const Utils = require('../utils') + +/** + * @param {string} publicKey + * @returns {Promise} + */ +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} + */ +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 diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js index 25a0b004..0c0b19d7 100644 --- a/services/gunDB/contact-api/getters/wall.js +++ b/services/gunDB/contact-api/getters/wall.js @@ -2,9 +2,12 @@ * @format */ const Common = require('shock-common') + const Utils = require('../utils') const Key = require('../key') +const Wall = require('./user') + /** * @returns {Promise} */ @@ -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 } }