2020-09-18 00:14:34 +03:00
|
|
|
// TODO(jim): The claim is that we can remove this
|
|
|
|
// and the package.json depdencies at some later time.
|
2020-09-09 20:56:35 +03:00
|
|
|
import { grpc } from "@improbable-eng/grpc-web";
|
|
|
|
import { WebsocketTransport } from "@textile/grpc-transport";
|
|
|
|
grpc.setDefaultTransport(WebsocketTransport());
|
|
|
|
|
2020-07-21 14:36:50 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
|
|
|
import * as Data from "~/node_common/data";
|
2020-08-04 05:45:59 +03:00
|
|
|
import * as Constants from "~/node_common/constants";
|
2020-08-28 07:34:50 +03:00
|
|
|
import * as Serializers from "~/node_common/serializers";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-07-21 14:36:50 +03:00
|
|
|
|
2020-08-30 22:56:45 +03:00
|
|
|
// TODO(jim): Work on better serialization when adoption starts occuring.
|
2020-07-24 03:22:22 +03:00
|
|
|
export const getById = async ({ id }) => {
|
2020-07-22 13:51:40 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-21 14:36:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-30 22:56:45 +03:00
|
|
|
// TODO(jim): You can serialize this last because you will have all the information
|
|
|
|
// from subscriptionsed, trusted, and pendingTrusted most likely.
|
2020-08-28 07:34:50 +03:00
|
|
|
const activity = await Data.getActivityForUserId({ userId: id });
|
2020-08-25 09:46:02 +03:00
|
|
|
const slates = await Data.getSlatesByUserId({ userId: id });
|
|
|
|
const keys = await Data.getAPIKeysByUserId({ userId: id });
|
2020-08-26 01:24:01 +03:00
|
|
|
const subscriptions = await Data.getSubscriptionsByUserId({ userId: id });
|
2020-09-07 01:30:33 +03:00
|
|
|
const subscribers = await Data.getSubscribersByUserId({ userId: id });
|
2020-08-30 22:10:01 +03:00
|
|
|
|
|
|
|
let serializedUsersMap = { [user.id]: Serializers.user(user) };
|
|
|
|
let serializedSlatesMap = {};
|
|
|
|
|
2020-08-30 22:56:45 +03:00
|
|
|
// NOTE(jim): The most expensive call first.
|
2020-08-30 22:10:01 +03:00
|
|
|
const r1 = await Serializers.doSubscriptions({
|
|
|
|
users: [],
|
2020-08-28 02:20:04 +03:00
|
|
|
slates: [],
|
|
|
|
subscriptions,
|
2020-08-30 22:10:01 +03:00
|
|
|
serializedUsersMap,
|
|
|
|
serializedSlatesMap,
|
2020-08-28 02:20:04 +03:00
|
|
|
});
|
2020-08-30 22:10:01 +03:00
|
|
|
|
2020-09-07 01:30:33 +03:00
|
|
|
const r2 = await Serializers.doSubscribers({
|
|
|
|
users: [],
|
|
|
|
slates: [],
|
|
|
|
subscribers,
|
|
|
|
serializedUsersMap: r1.serializedUsersMap,
|
|
|
|
serializedSlatesMap: r1.serializedSlatesMap,
|
|
|
|
});
|
|
|
|
|
2020-08-30 22:56:45 +03:00
|
|
|
// NOTE(jim): If any trusted users are subscription users, this ends up being cheaper.
|
2020-08-26 01:24:01 +03:00
|
|
|
const trusted = await Data.getTrustedRelationshipsByUserId({ userId: id });
|
2020-09-07 01:30:33 +03:00
|
|
|
const r3 = await Serializers.doTrusted({
|
2020-08-30 22:10:01 +03:00
|
|
|
users: [],
|
2020-08-28 07:34:50 +03:00
|
|
|
trusted,
|
2020-09-07 01:30:33 +03:00
|
|
|
serializedUsersMap: r2.serializedUsersMap,
|
|
|
|
serializedSlatesMap: r2.serializedSlatesMap,
|
2020-08-28 07:34:50 +03:00
|
|
|
});
|
2020-08-30 22:10:01 +03:00
|
|
|
|
2020-08-30 22:56:45 +03:00
|
|
|
// NOTE(jim): This should be the cheapest call.
|
2020-08-26 13:03:39 +03:00
|
|
|
const pendingTrusted = await Data.getPendingTrustedRelationshipsByUserId({
|
2020-08-31 21:19:46 +03:00
|
|
|
userId: id,
|
2020-08-26 13:03:39 +03:00
|
|
|
});
|
2020-09-07 01:30:33 +03:00
|
|
|
const r4 = await Serializers.doPendingTrusted({
|
2020-08-28 07:34:50 +03:00
|
|
|
users: [id],
|
|
|
|
pendingTrusted,
|
2020-09-07 01:30:33 +03:00
|
|
|
serializedUsersMap: r3.serializedUsersMap,
|
|
|
|
serializedSlatesMap: r3.serializedSlatesMap,
|
2020-08-28 07:34:50 +03:00
|
|
|
});
|
2020-07-28 09:54:15 +03:00
|
|
|
|
2020-08-04 05:45:59 +03:00
|
|
|
let bytes = 0;
|
|
|
|
user.data.library[0].children.forEach((each) => {
|
|
|
|
bytes = each.size + bytes;
|
|
|
|
});
|
|
|
|
|
2020-08-28 07:34:50 +03:00
|
|
|
return {
|
|
|
|
...Serializers.user(user),
|
|
|
|
type: "VIEWER",
|
|
|
|
library: user.data.library,
|
2020-07-21 14:36:50 +03:00
|
|
|
|
2020-08-28 07:34:50 +03:00
|
|
|
// NOTE(jim): Remaining data.
|
|
|
|
stats: {
|
|
|
|
bytes,
|
|
|
|
maximumBytes: Constants.TEXTILE_ACCOUNT_BYTE_LIMIT,
|
|
|
|
},
|
|
|
|
keys,
|
|
|
|
activity,
|
|
|
|
slates,
|
2020-08-30 22:10:01 +03:00
|
|
|
subscriptions: r1.serializedSubscriptions,
|
2020-09-07 01:30:33 +03:00
|
|
|
subscribers: r2.serializedSubscribers,
|
|
|
|
trusted: r3.serializedTrusted,
|
|
|
|
pendingTrusted: r4.serializedPendingTrusted,
|
2020-08-28 07:34:50 +03:00
|
|
|
};
|
2020-07-21 14:36:50 +03:00
|
|
|
};
|
2020-09-09 20:56:35 +03:00
|
|
|
|
|
|
|
export const getTextileById = async ({ id }) => {
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let info = {};
|
|
|
|
let status = {};
|
|
|
|
let errors = [];
|
|
|
|
let jobs = [];
|
2020-09-23 22:46:56 +03:00
|
|
|
let dealJobs = [];
|
2020-09-09 20:56:35 +03:00
|
|
|
|
2020-09-23 12:46:59 +03:00
|
|
|
const defaultData = await Utilities.getBucketAPIFromUserToken({ user });
|
|
|
|
if (!defaultData || !defaultData.buckets || !defaultData.bucketRoot) {
|
2020-09-22 10:01:48 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:56:35 +03:00
|
|
|
const {
|
|
|
|
power,
|
|
|
|
powerInfo,
|
|
|
|
powerHealth,
|
2020-09-23 12:46:59 +03:00
|
|
|
} = await Utilities.getPowergateAPIFromUserToken({ user });
|
2020-09-09 20:56:35 +03:00
|
|
|
|
|
|
|
try {
|
2020-09-23 12:46:59 +03:00
|
|
|
defaultData.buckets.archiveWatch(defaultData.bucketRoot.key, (job) => {
|
2020-09-09 20:56:35 +03:00
|
|
|
if (!job) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
job.id = job.id ? job.id : "UNDEFINED";
|
|
|
|
jobs.push(job);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-09-22 03:36:45 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/managers/viewer.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `buckets.archiveWatch`,
|
|
|
|
});
|
|
|
|
|
2020-09-09 20:56:35 +03:00
|
|
|
errors.push({ decorator: "JOB", message: e.message, code: e.code });
|
|
|
|
}
|
|
|
|
|
2020-09-23 22:46:56 +03:00
|
|
|
const stagingData = await Utilities.getBucketAPIFromUserToken({
|
|
|
|
user,
|
2020-09-25 20:40:54 +03:00
|
|
|
bucketName: "encrypted-deal",
|
|
|
|
encrypted: true,
|
2020-09-23 22:46:56 +03:00
|
|
|
});
|
2020-09-09 20:56:35 +03:00
|
|
|
|
|
|
|
try {
|
2020-09-23 22:46:56 +03:00
|
|
|
stagingData.buckets.archiveWatch(stagingData.bucketRoot.key, (job) => {
|
|
|
|
if (!job) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
job.id = job.id ? job.id : "UNDEFINED";
|
|
|
|
dealJobs.push(job);
|
|
|
|
});
|
2020-09-09 20:56:35 +03:00
|
|
|
} catch (e) {
|
2020-09-22 03:36:45 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/managers/viewer.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
2020-09-23 22:46:56 +03:00
|
|
|
functionName: `buckets.archiveWatch`,
|
2020-09-22 03:36:45 +03:00
|
|
|
});
|
|
|
|
|
2020-09-23 22:46:56 +03:00
|
|
|
errors.push({ decorator: "JOB", message: e.message, code: e.code });
|
2020-09-09 20:56:35 +03:00
|
|
|
}
|
2020-09-23 12:46:59 +03:00
|
|
|
|
|
|
|
let r = null;
|
|
|
|
try {
|
|
|
|
r = await stagingData.buckets.list();
|
|
|
|
} catch (e) {
|
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/managers/viewer.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `buckets.list`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let items = null;
|
2020-09-25 20:40:54 +03:00
|
|
|
// TODO(jim): There is no indicator on dealBucket that it is encrypted?
|
|
|
|
const dealBucket = r.find((bucket) => bucket.name === "encrypted-deal");
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO(sander): Would be nice if this existed in the response.
|
|
|
|
{
|
|
|
|
key: ...
|
|
|
|
name: ...,
|
|
|
|
path: ...,
|
|
|
|
createdAt: ...,
|
|
|
|
updatedAt: ...,
|
|
|
|
thread: ...,
|
|
|
|
encrypted: true/false
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-09-23 12:46:59 +03:00
|
|
|
try {
|
2020-09-25 20:40:54 +03:00
|
|
|
// NOTE(jim): Doesn't get it for encrypted buckets.
|
|
|
|
// items = await stagingData.buckets.listIpfsPath(dealBucket.path);
|
|
|
|
|
|
|
|
const path = await stagingData.buckets.listPath(dealBucket.key, "/");
|
|
|
|
items = path.item.items;
|
2020-09-23 12:46:59 +03:00
|
|
|
} catch (e) {
|
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/node_common/managers/viewer.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
2020-09-25 20:40:54 +03:00
|
|
|
functionName: `buckets.listPath`,
|
2020-09-23 12:46:59 +03:00
|
|
|
});
|
|
|
|
}
|
2020-09-09 20:56:35 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: "VIEWER_FILECOIN",
|
|
|
|
settings: {
|
|
|
|
deals_auto_approve: user.data.settings_deals_auto_approve,
|
|
|
|
},
|
|
|
|
powerInfo,
|
|
|
|
powerHealth,
|
|
|
|
archive: {
|
|
|
|
info,
|
|
|
|
status,
|
|
|
|
errors,
|
|
|
|
jobs,
|
|
|
|
},
|
2020-09-25 20:40:54 +03:00
|
|
|
deal: items ? items.filter((f) => f.name !== ".textileseed") : [],
|
2020-09-23 22:46:56 +03:00
|
|
|
dealJobs,
|
2020-09-09 20:56:35 +03:00
|
|
|
};
|
|
|
|
};
|