slate/pages/api/slates/add-url.js

175 lines
4.3 KiB
JavaScript
Raw Normal View History

import * as Constants from "~/node_common/constants";
2020-07-27 11:33:39 +03:00
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
2020-10-05 00:30:28 +03:00
import * as Strings from "~/common/strings";
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";
import * as Monitor from "~/node_common/monitor";
2020-09-03 09:00:02 +03:00
2020-07-27 11:33:39 +03:00
export default async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res.status(403).send({ decorator: "SERVER_ADD_TO_SLATE_USER_NOT_FOUND", error: true });
2020-07-27 11:33:39 +03:00
}
const user = await Data.getUserById({
id,
});
if (!user) {
return res.status(404).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_USER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_USER_NOT_FOUND",
error: true,
});
}
const slate = await Data.getSlateById({ id: req.body.data.slate.id });
2020-07-27 11:33:39 +03:00
if (!slate) {
return res.status(404).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_SLATE_NOT_FOUND",
error: true,
});
}
if (slate.error) {
return res.status(500).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_SLATE_NOT_FOUND",
error: true,
});
}
2020-09-22 05:00:12 +03:00
let newObjects = [];
if (Array.isArray(req.body.data.data)) {
newObjects = [...req.body.data.data];
2020-09-23 23:52:00 +03:00
} else {
newObjects = [req.body.data.data];
2020-09-23 23:52:00 +03:00
}
let slateURLs = slate.data.objects.map((file) => file.url);
2020-10-05 00:30:28 +03:00
let addlObjects;
if (req.body.data.fromSlate) {
2020-10-05 00:30:28 +03:00
let newURLs = [];
addlObjects = newObjects.filter((each) => {
if (slateURLs.includes(each.url) || newURLs.includes(each.url)) {
return false;
}
newURLs.push(each.url);
return true;
});
} else {
let newCIDs = [];
2020-10-05 00:30:28 +03:00
addlObjects = newObjects.filter((each) => {
if (slateURLs.includes(Strings.getCIDGatewayURL(each.cid)) || newCIDs.includes(each.cid)) {
return false;
}
newCIDs.push(each.cid);
return true;
});
2020-10-05 00:30:28 +03:00
}
2020-10-05 00:30:28 +03:00
addlObjects = addlObjects.map((each) => {
let url = each.url || Strings.getCIDGatewayURL(each.cid);
let cid = each.cid || Strings.urlToCid(each.url);
// NOTE(jim): Share events if public.
if (slate.data.public) {
Monitor.createSlateObject({
slateId: slate.id,
2020-12-06 08:13:34 +03:00
userId: user.id,
data: {
actorUserId: user.id,
context: {
2020-12-04 02:34:19 +03:00
slate: {
id: slate.id,
slatename: slate.slatename,
2020-12-06 08:13:34 +03:00
data: {
name: slate.data.name,
},
2020-12-04 02:34:19 +03:00
},
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
},
file: {
blurhash: each.blurhash,
id: each.id,
url,
cid,
type: each.type,
name: each.name,
title: each.title,
},
},
},
});
}
2020-10-05 00:30:28 +03:00
return {
blurhash: each.blurhash,
2020-11-30 02:47:08 +03:00
coverImage: each.coverImage,
2020-10-05 00:30:28 +03:00
cid: cid,
size: each.size,
id: each.id,
ownerId: req.body.data.fromSlate ? each.ownerId : user.id,
2020-10-05 00:30:28 +03:00
name: each.name,
title: each.title,
type: each.type,
2020-12-15 00:01:35 +03:00
unityGameConfig: each.unityGameConfig,
unityGameLoader: each.unityGameLoader,
2020-10-05 00:30:28 +03:00
url,
};
});
const objects = [...slate.data.objects, ...addlObjects];
2020-07-27 11:33:39 +03:00
const update = await Data.updateSlateById({
id: slate.id,
updated_at: new Date(),
data: {
...slate.data,
objects,
2020-07-27 11:33:39 +03:00
},
});
if (!update) {
return res.status(500).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_ERROR",
error: true,
});
}
if (update.error) {
return res.status(500).send({
2020-07-27 11:33:39 +03:00
decorator: "SERVER_ADD_TO_SLATE_ERROR",
error: true,
});
}
2020-10-31 02:12:20 +03:00
const slates = await Data.getSlatesByUserId({ userId: id });
if (slates) {
ViewerManager.hydratePartialSlates(slates, id);
}
2020-11-10 00:20:38 +03:00
if (update.data.public) {
SearchManager.updateSlate(update, "EDIT");
}
return res.status(200).send({
decorator: "SERVER_SLATE_ADD_TO_SLATE",
added: addlObjects.length,
skipped: newObjects.length - addlObjects.length,
slate,
});
2020-07-27 11:33:39 +03:00
};