From 5542f768881ddacb2389e3a8b946fd552e3fd791 Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Mon, 24 Aug 2020 23:46:02 -0700 Subject: [PATCH] slate: cleans up state, guarantees JSON response from endpoints, updates server code --- common/state.js | 46 +++++++++++++------ components/core/Application.js | 30 ++++++++---- .../data/methods/get-api-key-by-key.js | 7 ++- node_common/data/methods/get-api-key.js | 2 +- .../data/methods/get-api-keys-by-user-id.js | 2 +- node_common/data/methods/get-slate-by-id.js | 2 +- node_common/data/methods/get-slate-by-name.js | 2 +- .../data/methods/get-slates-by-user-id.js | 17 +++++-- node_common/data/methods/get-user-by-id.js | 2 +- .../data/methods/get-user-by-username.js | 2 +- node_common/managers/viewer.js | 18 +------- pages/_/slate.js | 19 ++++++-- server.js | 24 ++++++++-- 13 files changed, 112 insertions(+), 61 deletions(-) diff --git a/common/state.js b/common/state.js index f1cce223..d6613a18 100644 --- a/common/state.js +++ b/common/state.js @@ -53,7 +53,7 @@ export const getSelectedState = (props) => { return { address: null, - // address: info.defaultStorageConfig.cold.filecoin.addr, + address: info.defaultStorageConfig.cold.filecoin.addr, }; }; @@ -62,6 +62,14 @@ export const getInitialState = (props) => { return null; } + return { ...props }; +}; + +export const _deprecated_getInitialState = (props) => { + if (!props) { + return null; + } + const { id, status, @@ -89,20 +97,28 @@ export const getInitialState = (props) => { settings_deals_auto_approve: settings.deals_auto_approve, // NOTE(jim): Powergate Hot Settings - // settings_hot_enabled: info.defaultStorageConfig.hot.enabled, - // settings_hot_allow_unfreeze: info.defaultStorageConfig.hot.allowUnfreeze, - // settings_hot_ipfs_add_timeout: info.defaultStorageConfig.hot.ipfs.addTimeout, + settings_hot_enabled: info.defaultStorageConfig.hot.enabled, + settings_hot_allow_unfreeze: info.defaultStorageConfig.hot.allowUnfreeze, + settings_hot_ipfs_add_timeout: + info.defaultStorageConfig.hot.ipfs.addTimeout, // NOTE(jim): Powergate Cold Settings - // settings_cold_enabled: info.defaultStorageConfig.cold.enabled, - // settings_cold_default_address: info.defaultStorageConfig.cold.filecoin.addr, - // settings_cold_default_duration: info.defaultStorageConfig.cold.filecoin.dealMinDuration, - // settings_cold_default_replication_factor: info.defaultStorageConfig.cold.filecoin.repFactor, - // settings_cold_default_excluded_miners: info.defaultStorageConfig.cold.filecoin.excludedMinersList, - // settings_cold_default_trusted_miners: info.defaultStorageConfig.cold.filecoin.trustedMinersList, - // settings_cold_default_max_price: info.defaultStorageConfig.cold.filecoin.maxPrice, - // settings_cold_default_auto_renew: info.defaultStorageConfig.cold.filecoin.renew.enabled, - // settings_cold_default_auto_renew_max_price: info.defaultStorageConfig.cold.filecoin.renew.threshold, + settings_cold_enabled: info.defaultStorageConfig.cold.enabled, + settings_cold_default_address: info.defaultStorageConfig.cold.filecoin.addr, + settings_cold_default_duration: + info.defaultStorageConfig.cold.filecoin.dealMinDuration, + settings_cold_default_replication_factor: + info.defaultStorageConfig.cold.filecoin.repFactor, + settings_cold_default_excluded_miners: + info.defaultStorageConfig.cold.filecoin.excludedMinersList, + settings_cold_default_trusted_miners: + info.defaultStorageConfig.cold.filecoin.trustedMinersList, + settings_cold_default_max_price: + info.defaultStorageConfig.cold.filecoin.maxPrice, + settings_cold_default_auto_renew: + info.defaultStorageConfig.cold.filecoin.renew.enabled, + settings_cold_default_auto_renew_max_price: + info.defaultStorageConfig.cold.filecoin.renew.threshold, storageList, retrievalList, @@ -110,7 +126,7 @@ export const getInitialState = (props) => { keys, stats, library, - // peers: transformPeers(peersList), - // addresses: transformAddresses(addrsList, info), + peers: transformPeers(peersList), + addresses: transformAddresses(addrsList, info), }; }; diff --git a/components/core/Application.js b/components/core/Application.js index e9df5df4..d4f4ad05 100644 --- a/components/core/Application.js +++ b/components/core/Application.js @@ -72,8 +72,8 @@ const SCENES = { export default class ApplicationPage extends React.Component { state = { - selected: State.getSelectedState(this.props.viewer), - viewer: State.getInitialState(this.props.viewer), + selected: {}, + viewer: this.props.viewer, history: [{ id: "V1_NAVIGATION_HOME", scrollTop: 0, data: null }], currentIndex: 0, data: null, @@ -120,7 +120,11 @@ export default class ApplicationPage extends React.Component { } // NOTE(jim): Only allow the sidebar to show with file drag and drop. - if (e.dataTransfer.items && e.dataTransfer.items.length && e.dataTransfer.items[0].kind !== "file") { + if ( + e.dataTransfer.items && + e.dataTransfer.items.length && + e.dataTransfer.items[0].kind !== "file" + ) { return; } @@ -274,7 +278,8 @@ export default class ApplicationPage extends React.Component { _handleDeleteYourself = async () => { // TODO(jim): // Put this somewhere better for messages. - const message = "Do you really want to delete your account? It will be permanently removed"; + const message = + "Do you really want to delete your account? It will be permanently removed"; if (!window.confirm(message)) { return false; } @@ -445,8 +450,12 @@ export default class ApplicationPage extends React.Component { - + url="https://slate.host/_" + > + ); } @@ -519,12 +528,17 @@ export default class ApplicationPage extends React.Component { return ( - + + onDismissSidebar={this._handleDismissSidebar} + > {scene} diff --git a/node_common/data/methods/get-api-key-by-key.js b/node_common/data/methods/get-api-key-by-key.js index 48141238..fcea28af 100644 --- a/node_common/data/methods/get-api-key-by-key.js +++ b/node_common/data/methods/get-api-key-by-key.js @@ -4,14 +4,17 @@ export default async ({ key }) => { return await runQuery({ label: "GET_API_KEY_BY_KEY", queryFn: async (DB) => { - const query = await DB.select("*").from("keys").where({ key }).first(); + const query = await DB.select("*") + .from("keys") + .where({ key }) + .first(); if (!query || query.error) { return null; } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/data/methods/get-api-key.js b/node_common/data/methods/get-api-key.js index e6633dc9..1ab038b5 100644 --- a/node_common/data/methods/get-api-key.js +++ b/node_common/data/methods/get-api-key.js @@ -14,7 +14,7 @@ export default async ({ id }) => { } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/data/methods/get-api-keys-by-user-id.js b/node_common/data/methods/get-api-keys-by-user-id.js index 6a556af7..f95770d4 100644 --- a/node_common/data/methods/get-api-keys-by-user-id.js +++ b/node_common/data/methods/get-api-keys-by-user-id.js @@ -12,7 +12,7 @@ export default async ({ userId }) => { return []; } - return query; + return JSON.parse(JSON.stringify(query)); }, errorFn: async (e) => { return { diff --git a/node_common/data/methods/get-slate-by-id.js b/node_common/data/methods/get-slate-by-id.js index 8ccf6113..515f8321 100644 --- a/node_common/data/methods/get-slate-by-id.js +++ b/node_common/data/methods/get-slate-by-id.js @@ -14,7 +14,7 @@ export default async ({ id }) => { } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/data/methods/get-slate-by-name.js b/node_common/data/methods/get-slate-by-name.js index fb257cb3..c12f06f8 100644 --- a/node_common/data/methods/get-slate-by-name.js +++ b/node_common/data/methods/get-slate-by-name.js @@ -14,7 +14,7 @@ export default async ({ slatename }) => { } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/data/methods/get-slates-by-user-id.js b/node_common/data/methods/get-slates-by-user-id.js index 7d1fe758..33d718d4 100644 --- a/node_common/data/methods/get-slates-by-user-id.js +++ b/node_common/data/methods/get-slates-by-user-id.js @@ -4,21 +4,28 @@ export default async ({ userId, publicOnly = false }) => { return await runQuery({ label: "GET_SLATES_BY_USER_ID", queryFn: async (DB) => { - const hasUser = (id) => DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ ownerId: id })]); - const isPublic = () => DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ public: true })]); + const hasUser = (id) => + DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ ownerId: id })]); + const isPublic = () => + DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ public: true })]); let query; if (publicOnly) { - query = await DB.select("*").from("slates").where(hasUser(userId)).where(isPublic()); + query = await DB.select("*") + .from("slates") + .where(hasUser(userId)) + .where(isPublic()); } else { - query = await DB.select("*").from("slates").where(hasUser(userId)); + query = await DB.select("*") + .from("slates") + .where(hasUser(userId)); } if (!query || query.error) { return []; } - return query; + return JSON.parse(JSON.stringify(query)); }, errorFn: async (e) => { return { diff --git a/node_common/data/methods/get-user-by-id.js b/node_common/data/methods/get-user-by-id.js index fcc6fc2d..1b4de2d7 100644 --- a/node_common/data/methods/get-user-by-id.js +++ b/node_common/data/methods/get-user-by-id.js @@ -14,7 +14,7 @@ export default async ({ id }) => { } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/data/methods/get-user-by-username.js b/node_common/data/methods/get-user-by-username.js index 8275972d..9000c801 100644 --- a/node_common/data/methods/get-user-by-username.js +++ b/node_common/data/methods/get-user-by-username.js @@ -14,7 +14,7 @@ export default async ({ username }) => { } if (query.id) { - return query; + return JSON.parse(JSON.stringify(query)); } return null; diff --git a/node_common/managers/viewer.js b/node_common/managers/viewer.js index 9bdb1b82..f533772f 100644 --- a/node_common/managers/viewer.js +++ b/node_common/managers/viewer.js @@ -17,11 +17,8 @@ export const getById = async ({ id }) => { } let data = null; - const response = await Data.getSlatesByUserId({ userId: id }); - const slates = JSON.parse(JSON.stringify(response)); - - const keysRaw = await Data.getAPIKeysByUserId({ userId: id }); - const keys = JSON.parse(JSON.stringify(keysRaw)); + const slates = await Data.getSlatesByUserId({ userId: id }); + const keys = await Data.getAPIKeysByUserId({ userId: id }); let bytes = 0; user.data.library[0].children.forEach((each) => { @@ -54,17 +51,6 @@ export const getById = async ({ id }) => { addrsList: null, info: null, }; - - // TODO(jim): Disables Powergate Features - /* - const updates = await Utilities.refresh(user); - const updatesWithToken = await Utilities.refreshWithToken(user); - - data = await Utilities.updateStateData(data, { - ...updates, - ...updatesWithToken, - }); - */ } catch (e) { console.log(e); return null; diff --git a/pages/_/slate.js b/pages/_/slate.js index 792ea99b..1f9b6ee8 100644 --- a/pages/_/slate.js +++ b/pages/_/slate.js @@ -98,7 +98,9 @@ export default class SlatePage extends React.Component { return { id: each.id, data: each, - component: , + component: ( + + ), }; }), }, @@ -112,8 +114,10 @@ export default class SlatePage extends React.Component { }); render() { - const title = `${this.props.slate.ownername}/${this.props.slate.slatename}`; - const url = `https://slate.host/${this.props.slate.ownername}`; + const title = `${this.props.creator.username}/${ + this.props.slate.slatename + }`; + const url = `https://slate.host/${this.props.creator.username}`; const description = this.props.slate.data.body; let image; @@ -124,12 +128,17 @@ export default class SlatePage extends React.Component { }); return ( - +
- {this.props.slate.ownername} / {this.props.slate.slatename} + {this.props.creator.username} / {this.props.slate.slatename}
{this.props.slate.data.body}
diff --git a/server.js b/server.js index ee1e8fc6..626c589b 100644 --- a/server.js +++ b/server.js @@ -106,12 +106,12 @@ app.prepare().then(async () => { viewer, creator: { username: creator.username, + slates, data: { photo: creator.data.photo, name: creator.data.name ? creator.data.name : creator.username, body: creator.data.body ? creator.data.body : "A user on Slate.", }, - slates: JSON.parse(JSON.stringify(slates)), }, }); }); @@ -148,10 +148,26 @@ app.prepare().then(async () => { return res.redirect("/403"); } + const id = Utilities.getIdFromCookie(req); + + let viewer = null; + if (id) { + viewer = await ViewerManager.getById({ + id, + }); + } + return app.render(req, res, "/_/slate", { - slate: JSON.parse( - JSON.stringify({ ...slate, ownername: req.params.username }) - ), + viewer, + creator: { + username: creator.username, + data: { + photo: creator.data.photo, + name: creator.data.name ? creator.data.name : creator.username, + body: creator.data.body ? creator.data.body : "A user on Slate.", + }, + }, + slate, }); });