2020-07-17 13:24:20 +03:00
|
|
|
import * as MW from "~/node_common/middleware";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
|
|
|
import * as Strings from "~/common/strings";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
import DB from "~/node_common/database";
|
2020-07-22 07:11:13 +03:00
|
|
|
import PG from "~/node_common/powergate";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
const initCORS = MW.init(MW.CORS);
|
2020-07-22 07:11:13 +03:00
|
|
|
const initAuth = MW.init(MW.RequireCookieAuthentication);
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
2020-07-22 07:11:13 +03:00
|
|
|
initAuth(req, res);
|
|
|
|
|
2020-07-22 08:53:29 +03:00
|
|
|
const username = Utilities.getUserFromCookie(req);
|
|
|
|
if (!username) {
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.json({ decorator: "SERVER_USER_UPDATE", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await Data.getUserByUsername({
|
|
|
|
username,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.json({ decorator: "SERVER_USER_UPDATE", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.json({ decorator: "SERVER_USER_UPDATE", error: true });
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:04:54 +03:00
|
|
|
// TODO(jim): POWERGATE_ISSUE 0.2.0
|
2020-07-22 07:11:13 +03:00
|
|
|
// Should work when our hosted Powergate works.
|
|
|
|
if (req.body.type === "SET_DEFAULT_STORAGE_CONFIG") {
|
2020-07-22 08:53:29 +03:00
|
|
|
PG.setToken(user.data.tokens.pg);
|
2020-07-22 07:11:13 +03:00
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await PG.ffs.setDefaultStorageConfig(req.body.config);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_USER_UPDATE_SETTINGS_CONFIG", error: true });
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-22 09:04:54 +03:00
|
|
|
// TODO(jim): POWERGATE_ISSUE 0.2.0
|
|
|
|
// Should work when our hosted Powergate works.
|
|
|
|
if (req.body.type === "CREATE_FILECOIN_ADDRESS") {
|
|
|
|
PG.setToken(user.data.tokens.pg);
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await PG.ffs.newAddr(
|
|
|
|
req.body.address.name,
|
|
|
|
req.body.address.type,
|
|
|
|
req.body.address.makeDefault
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_CREATE_FILECOIN_ADDRESS", error: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
return res.status(200).json({ decorator: "SERVER_USER_UPDATE" });
|
|
|
|
};
|