2020-08-26 01:09:12 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-10-31 02:12:20 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
2021-06-10 22:54:26 +03:00
|
|
|
import * as Monitor from "~/node_common/monitor";
|
2020-08-26 01:09:12 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2021-03-07 23:53:54 +03:00
|
|
|
return res.status(401).send({ decorator: "SERVER_NOT_AUTHENTICATED", error: true });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2021-03-07 23:53:54 +03:00
|
|
|
return res.status(404).send({ decorator: "SERVER_USER_NOT_FOUND", error: true });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2021-03-07 23:53:54 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_NOT_FOUND", error: true });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 06:32:40 +03:00
|
|
|
if (!req.body.data || (!req.body.data.userId && !req.body.data.slateId)) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_MUST_PROVIDE_SLATE_OR_USER",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.id === req.body.data.userId) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_CAN_NOT_SUBSCRIBE_TO_YOURSELF",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetUser;
|
|
|
|
if (req.body.data.userId) {
|
|
|
|
targetUser = await Data.getUserById({
|
|
|
|
id: req.body.data.userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!targetUser) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_TARGET_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetUser.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_TARGET_USER_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetSlate;
|
|
|
|
if (req.body.data.slateId) {
|
|
|
|
targetSlate = await Data.getSlateById({ id: req.body.data.slateId });
|
|
|
|
|
|
|
|
if (!targetSlate) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(404).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_TARGET_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetSlate.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 06:32:40 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_TARGET_SLATE_NOT_FOUND",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
const existingResponse = await Data.getSubscription({
|
|
|
|
ownerId: user.id,
|
2020-08-26 06:32:40 +03:00
|
|
|
slateId: targetSlate ? targetSlate.id : null,
|
|
|
|
userId: targetUser ? targetUser.id : null,
|
2020-08-26 01:09:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (existingResponse && existingResponse.error) {
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(500).send({
|
2020-08-26 01:09:12 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE_SUBSCRIPTION_CHECK_ERROR",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let response;
|
|
|
|
|
2020-08-26 01:09:12 +03:00
|
|
|
// NOTE(jim): If it exists, we unsubscribe instead.
|
|
|
|
if (existingResponse) {
|
2021-03-07 23:53:54 +03:00
|
|
|
response = await Data.deleteSubscriptionById({
|
2020-08-26 01:09:12 +03:00
|
|
|
id: existingResponse.id,
|
2021-05-25 01:19:48 +03:00
|
|
|
ownerId: user.id,
|
|
|
|
slateId: targetSlate ? targetSlate.id : null,
|
|
|
|
userId: targetUser ? targetUser.id : null,
|
2020-08-26 01:09:12 +03:00
|
|
|
});
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (!response) {
|
|
|
|
return res.status(404).send({ decorator: "SERVER_UNSUBSCRIBE_FAILED", error: true });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (response.error) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_UNSUBSCRIBE_FAILED", error: true });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
2021-03-07 23:53:54 +03:00
|
|
|
} else {
|
|
|
|
response = await Data.createSubscription({
|
|
|
|
ownerId: user.id,
|
|
|
|
slateId: targetSlate ? targetSlate.id : null,
|
|
|
|
userId: targetUser ? targetUser.id : null,
|
|
|
|
});
|
2020-08-26 01:09:12 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (!response) {
|
|
|
|
return res.status(404).send({ decorator: "SERVER_SUBSCRIBE_FAILED", error: true });
|
|
|
|
}
|
2020-10-31 02:12:20 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (response.error) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_SUBSCRIBE_FAILED", error: true });
|
|
|
|
}
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:12:24 +03:00
|
|
|
if (targetUser && !existingResponse) {
|
2021-03-07 23:53:54 +03:00
|
|
|
ViewerManager.hydratePartial(id, { following: true });
|
2021-06-10 22:54:26 +03:00
|
|
|
Monitor.subscribeUser({ user, targetUser });
|
2020-08-26 01:09:12 +03:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:12:24 +03:00
|
|
|
if (targetSlate && !existingResponse) {
|
2021-03-07 23:53:54 +03:00
|
|
|
ViewerManager.hydratePartial(id, { subscriptions: true });
|
2021-06-10 22:54:26 +03:00
|
|
|
Monitor.subscribeSlate({ user, targetSlate });
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
2020-10-31 02:12:20 +03:00
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({
|
2020-08-28 07:34:50 +03:00
|
|
|
decorator: "SERVER_SUBSCRIBE",
|
2021-03-07 23:53:54 +03:00
|
|
|
data: response,
|
2020-08-28 07:34:50 +03:00
|
|
|
});
|
2020-08-26 01:09:12 +03:00
|
|
|
};
|