2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-17 13:24:20 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-09-23 12:46:59 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-11-10 00:20:38 +03:00
|
|
|
import * as SearchManager from "~/node_common/managers/search";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-08-19 23:44:53 +03:00
|
|
|
import { Buckets, PrivateKey } from "@textile/hub";
|
2020-07-18 02:45:49 +03:00
|
|
|
|
2020-07-22 08:53:29 +03:00
|
|
|
import JWT from "jsonwebtoken";
|
|
|
|
|
2020-07-18 02:45:49 +03:00
|
|
|
const TEXTILE_KEY_INFO = {
|
2020-07-21 14:36:50 +03:00
|
|
|
key: Environment.TEXTILE_HUB_KEY,
|
|
|
|
secret: Environment.TEXTILE_HUB_SECRET,
|
2020-07-18 02:45:49 +03:00
|
|
|
};
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
export default async (req, res) => {
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2020-10-29 21:39:40 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_DELETE", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-18 02:45:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-10-29 21:39:40 +03:00
|
|
|
return res.status(404).send({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
|
2020-07-18 02:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-10-29 21:39:40 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
|
2020-07-18 02:45:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
let slates = await Data.getSlatesByUserId({ userId: user.id, publicOnly: true });
|
|
|
|
for (let slate of slates) {
|
|
|
|
SearchManager.updateSlate(slate, "REMOVE");
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:54:15 +03:00
|
|
|
await Data.deleteAPIKeysForUserId({ userId: user.id });
|
2020-08-03 01:06:46 +03:00
|
|
|
await Data.deleteSlatesForUserId({ userId: user.id });
|
2020-07-28 09:54:15 +03:00
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
const i = await PrivateKey.fromString(user.data.tokens.api);
|
2020-07-18 02:45:49 +03:00
|
|
|
const b = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
2020-09-23 12:46:59 +03:00
|
|
|
const defaultData = await Utilities.getBucketAPIFromUserToken({ user });
|
2020-07-20 09:06:36 +03:00
|
|
|
|
2020-09-23 14:17:56 +03:00
|
|
|
// NOTE(jim): Just assume they all delete even if exception is thrown, it seems like they do.
|
2020-07-18 02:45:49 +03:00
|
|
|
try {
|
2020-09-23 12:46:59 +03:00
|
|
|
const roots = await defaultData.buckets.list();
|
2020-09-23 14:17:56 +03:00
|
|
|
|
2020-09-23 12:46:59 +03:00
|
|
|
for (let i = 0; i < roots.length; i++) {
|
|
|
|
await defaultData.buckets.remove(roots[i].key);
|
|
|
|
}
|
2020-07-18 02:45:49 +03:00
|
|
|
} catch (e) {
|
2020-09-23 12:46:59 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/pages/api/users/delete.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `b.remove`,
|
|
|
|
});
|
2020-07-18 02:45:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
SearchManager.updateUser(user, "REMOVE");
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const deleted = await Data.deleteUserByUsername({
|
2020-07-22 13:59:39 +03:00
|
|
|
username: user.username,
|
2020-07-17 13:24:20 +03:00
|
|
|
});
|
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
if (!deleted || deleted.error) {
|
2020-10-29 21:39:40 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_DELETE", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({ decorator: "SERVER_USER_DELETE", deleted });
|
2020-07-17 13:24:20 +03:00
|
|
|
};
|