From 01d4a3dc50982afe3612c7656e3bf28bd11f6563 Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Thu, 23 Jul 2020 17:29:44 -0700 Subject: [PATCH] cleanup: viewer --- node_common/models.js | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 node_common/models.js diff --git a/node_common/models.js b/node_common/models.js new file mode 100644 index 00000000..7c7027d9 --- /dev/null +++ b/node_common/models.js @@ -0,0 +1,104 @@ +import * as Utilities from "~/node_common/utilities"; +import * as Data from "~/node_common/data"; +import * as Powergate from "~/node_common/powergate"; + +export const updateStateData = async (state, newState) => { + return { + ...state, + ...newState, + }; +}; + +// NOTE(jim): Requires Powergate, does not require token. +// But we provide it anyway because we can. +const refresh = async (user) => { + const PG = Powergate.get(user); + const Health = await PG.health.check(); + const status = Health.status ? Health.status : null; + const messageList = Health.messageList ? Health.messageList : null; + + const Peers = await PG.net.peers(); + const peersList = Peers.peersList ? Peers.peersList : null; + + return { peersList, messageList, status }; +}; + +// NOTE(jim): Requires Powergate & authentication +const refreshWithToken = async (user) => { + const PG = Powergate.get(user); + const Addresses = await PG.ffs.addrs(); + const addrsList = Addresses.addrsList ? Addresses.addrsList : null; + + const NetworkInfo = await PG.ffs.info(); + const info = NetworkInfo.info ? NetworkInfo.info : null; + + const includeFinal = ffsOptions.withIncludeFinal(true); + const includePending = ffsOptions.withIncludePending(true); + + // TODO(jim): Should be pulling deals from all your addresses. + // This works okay for now. But people will be confused. + const fromAddresses = ffsOptions.withFromAddresses( + info.defaultStorageConfig.cold.filecoin.addr + ); + + const s = await PG.ffs.listStorageDealRecords( + includeFinal, + includePending, + fromAddresses + ); + + const r = await PG.ffs.listRetrievalDealRecords(); + + return { + addrsList, + info, + storageList: s.recordsList, + retrievalList: r.recordsList, + }; +}; + +export const getViewer = async ({ id }) => { + const user = await Data.getUserById({ + id, + }); + + if (!user) { + return null; + } + + if (user.error) { + return null; + } + + let data = { + id: user.id, + data: { photo: user.data.photo }, + settings: { + deals_auto_approve: user.data.settings_deals_auto_approve, + }, + username: user.username, + library: user.data.library, + storageList: [], + retrievalList: [], + peersList: null, + messageList: null, + status: null, + addrsList: null, + info: null, + }; + + try { + const updates = await refresh(user); + const updatesWithToken = await refreshWithToken(user); + + data = await Utilities.updateStateData(data, { + ...updates, + ...updatesWithToken, + }); + } catch (e) { + console.log(e); + return null; + } + + return data; +};