2020-08-25 23:09:08 +03:00
|
|
|
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";
|
2020-11-16 11:07:11 +03:00
|
|
|
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) {
|
2020-10-24 09:36:52 +03:00
|
|
|
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) {
|
2020-09-03 03:21:29 +03:00
|
|
|
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) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-07-27 11:33:39 +03:00
|
|
|
decorator: "SERVER_ADD_TO_SLATE_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-28 04:30:40 +03:00
|
|
|
const slate = await Data.getSlateById({ id: req.body.data.slate.id });
|
2020-07-27 11:33:39 +03:00
|
|
|
|
|
|
|
if (!slate) {
|
2020-09-03 03:21:29 +03:00
|
|
|
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) {
|
2020-09-03 03:21:29 +03:00
|
|
|
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 = [];
|
2020-11-28 04:30:40 +03:00
|
|
|
if (Array.isArray(req.body.data.data)) {
|
|
|
|
newObjects = [...req.body.data.data];
|
2020-09-23 23:52:00 +03:00
|
|
|
} else {
|
2020-11-28 04:30:40 +03:00
|
|
|
newObjects = [req.body.data.data];
|
2020-09-23 23:52:00 +03:00
|
|
|
}
|
2020-09-27 00:15:37 +03:00
|
|
|
let slateURLs = slate.data.objects.map((file) => file.url);
|
2020-10-05 00:30:28 +03:00
|
|
|
let addlObjects;
|
2020-11-28 04:30:40 +03:00
|
|
|
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 {
|
2020-11-28 04:19:30 +03:00
|
|
|
let newCIDs = [];
|
2020-10-05 00:30:28 +03:00
|
|
|
addlObjects = newObjects.filter((each) => {
|
2020-11-28 04:19:30 +03:00
|
|
|
if (slateURLs.includes(Strings.getCIDGatewayURL(each.cid)) || newCIDs.includes(each.cid)) {
|
2020-09-24 22:42:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-28 04:19:30 +03:00
|
|
|
newCIDs.push(each.cid);
|
2020-09-24 22:42:32 +03:00
|
|
|
return true;
|
|
|
|
});
|
2020-10-05 00:30:28 +03:00
|
|
|
}
|
2020-11-16 11:07:11 +03:00
|
|
|
|
2020-10-05 00:30:28 +03:00
|
|
|
addlObjects = addlObjects.map((each) => {
|
2020-11-28 04:19:30 +03:00
|
|
|
let url = each.url || Strings.getCIDGatewayURL(each.cid);
|
|
|
|
let cid = each.cid || Strings.urlToCid(each.url);
|
2020-11-16 11:07:11 +03:00
|
|
|
|
|
|
|
// 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,
|
2020-11-16 11:07:11 +03:00
|
|
|
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-11-16 11:07:11 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2020-11-28 04:30:40 +03:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
});
|
2020-09-20 23:38:27 +03:00
|
|
|
|
|
|
|
const objects = [...slate.data.objects, ...addlObjects];
|
2020-08-23 23:09:38 +03:00
|
|
|
|
2020-07-27 11:33:39 +03:00
|
|
|
const update = await Data.updateSlateById({
|
|
|
|
id: slate.id,
|
|
|
|
updated_at: new Date(),
|
|
|
|
data: {
|
|
|
|
...slate.data,
|
2020-08-23 23:09:38 +03:00
|
|
|
objects,
|
2020-07-27 11:33:39 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!update) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-07-27 11:33:39 +03:00
|
|
|
decorator: "SERVER_ADD_TO_SLATE_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2020-09-24 22:42:32 +03:00
|
|
|
return res.status(200).send({
|
|
|
|
decorator: "SERVER_SLATE_ADD_TO_SLATE",
|
|
|
|
added: addlObjects.length,
|
|
|
|
skipped: newObjects.length - addlObjects.length,
|
2020-09-27 00:15:37 +03:00
|
|
|
slate,
|
2020-09-24 22:42:32 +03:00
|
|
|
});
|
2020-07-27 11:33:39 +03:00
|
|
|
};
|