slate/pages/api/slates/create.js

104 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-07-27 04:51:51 +03:00
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Monitor from "~/node_common/monitor";
2020-10-31 02:12:20 +03:00
import * as ViewerManager from "~/node_common/managers/viewer";
2020-11-10 00:20:38 +03:00
import * as SearchManager from "~/node_common/managers/search";
2020-07-27 04:51:51 +03:00
2020-09-12 02:50:29 +03:00
const SLATE_LIMIT = 50;
2020-09-09 13:01:49 +03:00
2020-07-27 04:51:51 +03:00
export default async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_FIND_USER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
const user = await Data.getUserById({
id,
});
if (!user) {
2020-07-27 12:10:12 +03:00
return res.status(404).json({
decorator: "SERVER_FIND_USER_CREATE_SLATE_USER_NOT_FOUND",
error: true,
});
2020-07-27 04:51:51 +03:00
}
if (user.error) {
2020-07-27 12:10:12 +03:00
return res.status(500).json({
decorator: "SERVER_FIND_USER_CREATE_SLATE_USER_NOT_FOUND",
error: true,
});
2020-07-27 04:51:51 +03:00
}
const slatename = Strings.createSlug(req.body.data.name);
2020-09-28 23:12:43 +03:00
const found = await Data.getSlateByName({
slatename,
ownerId: user.id,
});
2020-07-27 04:51:51 +03:00
if (found) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_EXISTING_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
2020-10-31 02:12:20 +03:00
let slates = await Data.getSlatesByUserId({ userId: id });
2020-09-09 13:01:49 +03:00
if (slates.length >= SLATE_LIMIT) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_SLATE_LIMIT", error: true });
2020-09-09 13:01:49 +03:00
}
2020-07-27 04:51:51 +03:00
const slate = await Data.createSlate({
slatename: Strings.createSlug(req.body.data.name),
2020-07-27 04:51:51 +03:00
data: {
public: req.body.data.public,
2020-07-27 04:51:51 +03:00
ownerId: id,
name: req.body.data.name,
body: req.body.data.body,
2020-07-27 04:51:51 +03:00
objects: [],
},
});
if (!slate) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
if (slate.error) {
2020-10-29 21:39:40 +03:00
return res.status(500).send({ decorator: "SERVER_CREATE_SLATE", error: true });
2020-07-27 04:51:51 +03:00
}
2020-10-31 02:12:20 +03:00
slates = await Data.getSlatesByUserId({ userId: id });
if (slates) {
ViewerManager.hydratePartialSlates(slates, id);
}
2020-11-10 00:20:38 +03:00
if (slate.data.public) {
SearchManager.updateSlate(slate, "ADD");
}
Monitor.createSlate({
userId: user.id,
data: {
actorUserId: user.id,
2020-12-04 02:34:19 +03:00
context: {
user: {
id: user.id,
username: user.username,
2020-12-06 08:13:34 +03:00
data: {
photo: user.data.photo,
name: user.data.name,
},
2020-12-04 02:34:19 +03:00
},
slate: {
slatename: slate.slatename,
id: slate.id,
2020-12-06 08:13:34 +03:00
data: {
name: slate.data.name,
},
2020-12-04 02:34:19 +03:00
},
},
},
});
return res.status(200).send({ decorator: "SERVER_CREATE_SLATE", slate });
2020-07-27 04:51:51 +03:00
};