From 76c9e5e10ed343537af65426f4b4cb5ccc29a560 Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Thu, 27 Aug 2020 16:20:04 -0700 Subject: [PATCH] serializer: adds a serialize for subscriptions for @martinalong to evaluate --- common/serializers.js | 47 ++++++++++++++++++++++++++++++++++ node_common/managers/viewer.js | 10 +++++++- pages/_/integration-page.js | 2 -- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/common/serializers.js b/common/serializers.js index 235ad992..8c0f3481 100644 --- a/common/serializers.js +++ b/common/serializers.js @@ -1,3 +1,5 @@ +import DB from "~/node_common/database"; + export const user = (entity) => { return { type: "USER", @@ -24,3 +26,48 @@ export const slate = (entity) => { }, }; }; + +export const doSubscriptions = async ({ users, slates, subscriptions }) => { + subscriptions.forEach((each) => { + if (each.target_user_id) { + users.push(each.target_user_id); + } + + if (each.target_slate_id) { + slates.push(each.target_slate_id); + } + }); + + const userEntities = await DB.select("id", "username", "data") + .from("users") + .whereIn("id", users); + + const slateEntities = await DB.select("id", "slatename", "data") + .from("slates") + .whereIn("id", slates); + + const sanitized = subscriptions.map((data) => { + let u = null; + let o = null; + let s = null; + + if (data.target_user_id) { + u = userEntities.find((e) => data.target_user_id === e.id); + u = user(u); + } + + if (data.owner_user_id) { + o = userEntities.find((e) => data.owner_user_id === e.id); + o = user(o); + } + + if (data.target_slate_id) { + s = slateEntities.find((e) => data.target_slate_id === e.id); + s = slate(s); + } + + return { ...data, user: u, owner: o, slate: s }; + }); + + return JSON.parse(JSON.stringify(sanitized)); +}; diff --git a/node_common/managers/viewer.js b/node_common/managers/viewer.js index 10c7b4e8..9c322418 100644 --- a/node_common/managers/viewer.js +++ b/node_common/managers/viewer.js @@ -2,6 +2,7 @@ import * as Utilities from "~/node_common/utilities"; import * as Data from "~/node_common/data"; import * as Powergate from "~/node_common/powergate"; import * as Constants from "~/node_common/constants"; +import * as Serializers from "~/common/serializers"; export const getById = async ({ id }) => { const user = await Data.getUserById({ @@ -20,6 +21,13 @@ export const getById = async ({ id }) => { const slates = await Data.getSlatesByUserId({ userId: id }); const keys = await Data.getAPIKeysByUserId({ userId: id }); const subscriptions = await Data.getSubscriptionsByUserId({ userId: id }); + + const serializedSubscriptions = await Serializers.doSubscriptions({ + users: [id], + slates: [], + subscriptions, + }); + const activity = await Data.getActivityForUserId({ userId: id }); const trusted = await Data.getTrustedRelationshipsByUserId({ userId: id }); const pendingTrusted = await Data.getPendingTrustedRelationshipsByUserId({ @@ -56,7 +64,7 @@ export const getById = async ({ id }) => { status: null, addrsList: null, info: null, - subscriptions, + subscriptions: serializedSubscriptions, activity, trusted, pendingTrusted, diff --git a/pages/_/integration-page.js b/pages/_/integration-page.js index a47915a8..44f89c8b 100644 --- a/pages/_/integration-page.js +++ b/pages/_/integration-page.js @@ -105,8 +105,6 @@ export default class IntegrationPage extends React.Component { }; render() { - console.log(this.state.viewer); - return (