2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-22 10:41:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-08-30 22:10:01 +03:00
|
|
|
import * as Serializers from "~/node_common/serializers";
|
2020-08-14 12:17:19 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
|
|
|
import * as AnalyticsManager from "~/node_common/managers/analytics";
|
2020-10-27 07:41:42 +03:00
|
|
|
import * as Websocket from "~/node_common/nodejs-websocket";
|
2020-11-25 06:39:35 +03:00
|
|
|
import * as NodeLogging from "~/node_common/node-logging";
|
2020-10-27 07:41:42 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
|
|
|
import * as Window from "~/common/window";
|
|
|
|
import * as Strings from "~/common/strings";
|
2020-06-17 21:05:13 +03:00
|
|
|
|
2020-10-29 09:03:12 +03:00
|
|
|
import ApiV1GetSlateObjects from "~/pages/api/v1/get-slate-objects";
|
|
|
|
|
2020-10-29 10:05:57 +03:00
|
|
|
import limit from "express-rate-limit";
|
2020-07-09 06:19:08 +03:00
|
|
|
import express from "express";
|
|
|
|
import next from "next";
|
|
|
|
import compression from "compression";
|
2020-08-18 01:07:31 +03:00
|
|
|
import cors from "cors";
|
2020-10-08 03:51:49 +03:00
|
|
|
import morgan from "morgan";
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2021-01-28 09:17:01 +03:00
|
|
|
import { FilecoinNumber, Converter } from "@glif/filecoin-number";
|
|
|
|
|
2020-07-21 21:01:12 +03:00
|
|
|
const app = next({
|
|
|
|
dev: !Environment.IS_PRODUCTION,
|
|
|
|
dir: __dirname,
|
|
|
|
quiet: false,
|
|
|
|
});
|
2020-07-22 22:17:08 +03:00
|
|
|
|
2020-10-29 10:05:57 +03:00
|
|
|
const createLimiter = limit({
|
|
|
|
windowMs: 10 * 60 * 1000, // 10 minutes
|
|
|
|
max: 5,
|
2020-11-17 07:17:09 +03:00
|
|
|
message: {
|
|
|
|
decorator: "SIGN_UP_RATE_LIMITED",
|
|
|
|
error: true,
|
|
|
|
message: "You have made too many requests.",
|
|
|
|
},
|
2020-10-29 10:05:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const loginLimiter = limit({
|
|
|
|
windowMs: 10 * 60 * 1000, // 10 minutes
|
|
|
|
max: 5,
|
2020-11-17 07:17:09 +03:00
|
|
|
message: {
|
|
|
|
decorator: "SIGN_IN_RATE_LIMITED",
|
|
|
|
error: true,
|
|
|
|
message: "You have made too many requests.",
|
|
|
|
},
|
2020-10-29 10:05:57 +03:00
|
|
|
});
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const handler = app.getRequestHandler();
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-10-24 19:59:11 +03:00
|
|
|
const EXTERNAL_RESOURCES = {
|
|
|
|
storageDealUpload: Strings.isEmpty(Environment.RESOURCE_URI_STORAGE_UPLOAD)
|
2020-10-24 20:04:01 +03:00
|
|
|
? null
|
|
|
|
: Environment.RESOURCE_URI_STORAGE_UPLOAD,
|
2020-10-24 19:59:11 +03:00
|
|
|
upload: Strings.isEmpty(Environment.RESOURCE_URI_UPLOAD)
|
2020-10-24 20:04:01 +03:00
|
|
|
? null
|
|
|
|
: Environment.RESOURCE_URI_STORAGE_UPLOAD,
|
2020-11-14 16:27:45 +03:00
|
|
|
uploadZip: Strings.isEmpty(Environment.RESOURCE_URI_UPLOAD)
|
|
|
|
? null
|
|
|
|
: Environment.RESOURCE_URI_STORAGE_UPLOAD,
|
2020-10-26 01:56:57 +03:00
|
|
|
pubsub: Strings.isEmpty(Environment.RESOURCE_URI_PUBSUB) ? null : Environment.RESOURCE_URI_PUBSUB,
|
2020-11-11 05:38:22 +03:00
|
|
|
search: Strings.isEmpty(Environment.RESOURCE_URI_SEARCH) ? null : Environment.RESOURCE_URI_SEARCH,
|
2020-10-24 19:59:11 +03:00
|
|
|
};
|
|
|
|
|
2021-01-24 09:20:57 +03:00
|
|
|
let exploreSlates = [];
|
2021-01-24 04:18:04 +03:00
|
|
|
|
|
|
|
const fetchExploreSlates = async () => {
|
2021-01-25 21:12:03 +03:00
|
|
|
let exploreSlates = [];
|
2021-01-24 04:18:04 +03:00
|
|
|
if (Environment.IS_PRODUCTION) {
|
|
|
|
exploreSlates = await Data.getSlatesByIds({
|
|
|
|
ids: [
|
|
|
|
//NOTE(tara): slates in prod
|
|
|
|
"d2861ac4-fc41-4c07-8f21-d0bf06be364c",
|
|
|
|
"9c2c458c-d92a-4e81-a4b6-bf6ab4607470",
|
|
|
|
"7f461144-0647-43d7-8294-788b37ae5979",
|
|
|
|
"f72c2594-b8ac-41f6-91e0-b2da6788ae23",
|
|
|
|
"a0d6e2f2-564d-47ed-bf56-13c42634703d",
|
|
|
|
"0ba92c73-92e7-4b00-900e-afae4856c9ea",
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let exploreSlate of exploreSlates) {
|
|
|
|
let user = await Data.getUserById({ id: exploreSlate.data.ownerId });
|
|
|
|
exploreSlate.username = user.username;
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 09:20:57 +03:00
|
|
|
// else {
|
|
|
|
// exploreSlates = await Data.getSlatesByIds({
|
|
|
|
// ids: [
|
|
|
|
// //NOTE(tara): slates in localhost for testing
|
|
|
|
// "857ad84d-7eff-4861-a988-65c84b62fc23",
|
|
|
|
// "81fa0b39-0e96-4c7f-8587-38468bb67cb3",
|
|
|
|
// "c4e8dad7-4ba0-4f25-a92a-c73ef5522d29",
|
|
|
|
// "df05cb1f-2ecf-4872-b111-c4b8493d08f8",
|
|
|
|
// "435035e6-dee4-4bbf-9521-64c219a527e7",
|
|
|
|
// "ac907aa3-2fb2-46fd-8eba-ec8ceb87b5eb",
|
|
|
|
// ],
|
|
|
|
// });
|
|
|
|
|
|
|
|
// for (let exploreSlate of exploreSlates) {
|
|
|
|
// let user = await Data.getUserById({ id: exploreSlate.data.ownerId });
|
|
|
|
// exploreSlate.username = user.username;
|
|
|
|
// }
|
|
|
|
// }
|
2021-01-25 21:12:03 +03:00
|
|
|
return exploreSlates;
|
2021-01-24 04:18:04 +03:00
|
|
|
};
|
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
app.prepare().then(async () => {
|
2020-02-19 09:30:47 +03:00
|
|
|
const server = express();
|
2020-07-22 21:34:24 +03:00
|
|
|
|
2020-08-18 01:11:08 +03:00
|
|
|
server.use(cors());
|
2020-10-22 08:38:37 +03:00
|
|
|
server.use(morgan(":method :url :status :res[content-length] - :response-time ms"));
|
2020-08-18 01:07:31 +03:00
|
|
|
|
2020-10-23 23:28:08 +03:00
|
|
|
if (Environment.IS_PRODUCTION) {
|
|
|
|
server.use(compression());
|
|
|
|
}
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.use("/public", express.static("public"));
|
2020-08-30 22:10:01 +03:00
|
|
|
server.get("/system", async (r, s) => s.redirect("/_/system"));
|
|
|
|
server.get("/experiences", async (r, s) => s.redirect("/_/system"));
|
|
|
|
server.get("/_/experiences", async (r, s) => s.redirect("/_/system"));
|
2020-10-22 08:38:37 +03:00
|
|
|
server.get("/system/:c", async (r, s) => s.redirect(`/_/system/${r.params.c}`));
|
|
|
|
server.get("/experiences/:m", async (r, s) => s.redirect(`/_/experiences/${r.params.m}`));
|
2020-10-09 13:43:56 +03:00
|
|
|
|
2020-10-29 09:03:12 +03:00
|
|
|
// NOTE(jim): Example of simple query to query slates by CID.
|
|
|
|
server.get("/api/v1/cid::cid", async (r, s) => {
|
|
|
|
return await ApiV1GetSlateObjects(r, s);
|
|
|
|
});
|
|
|
|
|
2020-10-29 10:05:57 +03:00
|
|
|
server.all("/api/users/create", createLimiter, async (r, s, next) => {
|
|
|
|
return handler(r, s, r.url);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.all("/api/sign-in", loginLimiter, async (r, s, next) => {
|
|
|
|
return handler(r, s, r.url);
|
|
|
|
});
|
|
|
|
|
2020-10-05 04:33:33 +03:00
|
|
|
server.all("/api/:a", async (r, s, next) => {
|
|
|
|
return handler(r, s, r.url);
|
|
|
|
});
|
2020-10-09 13:43:56 +03:00
|
|
|
|
2020-10-05 04:33:33 +03:00
|
|
|
server.all("/api/:a/:b", async (r, s, next) => {
|
|
|
|
return handler(r, s, r.url);
|
|
|
|
});
|
2020-08-20 08:57:46 +03:00
|
|
|
|
2020-09-14 10:28:09 +03:00
|
|
|
server.get("/", async (req, res) => {
|
|
|
|
return app.render(req, res, "/", {});
|
|
|
|
});
|
|
|
|
|
2020-08-20 08:57:46 +03:00
|
|
|
server.get("/_", async (req, res) => {
|
2020-10-22 08:38:37 +03:00
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
2020-10-05 00:30:28 +03:00
|
|
|
|
2020-08-20 08:57:46 +03:00
|
|
|
const isBucketsAvailable = await Utilities.checkTextile();
|
2020-08-19 21:44:18 +03:00
|
|
|
|
2020-12-06 06:15:26 +03:00
|
|
|
if (!isBucketsAvailable && Environment.IS_PRODUCTION) {
|
2020-09-17 23:00:07 +03:00
|
|
|
return res.redirect("/maintenance");
|
|
|
|
}
|
2020-08-19 21:44:18 +03:00
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-07-21 14:36:50 +03:00
|
|
|
|
2020-07-22 10:41:29 +03:00
|
|
|
let viewer = null;
|
2020-07-22 13:51:40 +03:00
|
|
|
if (id) {
|
2020-07-24 03:22:22 +03:00
|
|
|
viewer = await ViewerManager.getById({
|
2020-07-22 13:51:40 +03:00
|
|
|
id,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
2020-07-21 14:36:50 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 12:17:19 +03:00
|
|
|
let analytics = await AnalyticsManager.get();
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_", {
|
2020-07-21 14:36:50 +03:00
|
|
|
viewer,
|
2020-08-14 12:17:19 +03:00
|
|
|
analytics,
|
2020-10-05 00:30:28 +03:00
|
|
|
mobile,
|
2020-10-24 19:59:11 +03:00
|
|
|
resources: EXTERNAL_RESOURCES,
|
2020-06-17 21:05:13 +03:00
|
|
|
});
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-08-26 03:44:45 +03:00
|
|
|
server.get("/_/integration-page", async (req, res) => {
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
|
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.render(req, res, "/_/integration-page", {
|
|
|
|
viewer,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-30 22:10:01 +03:00
|
|
|
server.all("/_/:a", async (r, s) => handler(r, s, r.url));
|
|
|
|
server.all("/_/:a/:b", async (r, s) => handler(r, s, r.url));
|
|
|
|
|
2021-01-17 23:57:34 +03:00
|
|
|
server.get("/[$]/slate/:id", async (req, res) => {
|
|
|
|
const slate = await Data.getSlateById({
|
|
|
|
id: req.params.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!slate) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slate.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slate.data.public) {
|
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = await Data.getUserById({ id: slate.data.ownerId });
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.redirect(`/${creator.username}/${slate.slatename}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.get("/[$]/user/:id", async (req, res) => {
|
|
|
|
const creator = await Data.getUserById({ id: req.params.id });
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.redirect(`/${creator.username}`);
|
|
|
|
});
|
|
|
|
|
2020-11-16 11:07:11 +03:00
|
|
|
server.get("/[$]/:id", async (req, res) => {
|
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
|
|
|
|
|
|
|
const slate = await Data.getSlateById({
|
|
|
|
id: req.params.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!slate) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slate.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slate.data.public) {
|
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = await Data.getUserById({ id: slate.data.ownerId });
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
|
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.render(req, res, "/_/slate", {
|
|
|
|
viewer,
|
|
|
|
creator: Serializers.user(creator),
|
|
|
|
slate,
|
|
|
|
mobile,
|
|
|
|
resources: EXTERNAL_RESOURCES,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
server.get("/:username", async (req, res) => {
|
2020-10-22 08:38:37 +03:00
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
2020-10-05 00:30:28 +03:00
|
|
|
|
2020-08-07 02:28:54 +03:00
|
|
|
// TODO(jim): Temporary workaround
|
2020-08-07 02:36:03 +03:00
|
|
|
if (!Validations.userRoute(req.params.username)) {
|
2020-10-23 13:34:31 +03:00
|
|
|
return handler(req, res, req.url, {
|
|
|
|
mobile,
|
2020-10-24 19:59:11 +03:00
|
|
|
resources: EXTERNAL_RESOURCES,
|
2020-10-23 13:34:31 +03:00
|
|
|
});
|
2020-08-07 02:28:54 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-11-17 23:38:26 +03:00
|
|
|
const shouldViewerRedirect = await ViewerManager.shouldRedirect({ id });
|
2020-11-17 23:36:36 +03:00
|
|
|
if (shouldViewerRedirect) {
|
2020-11-17 05:10:43 +03:00
|
|
|
return res.redirect(
|
|
|
|
`/_${Strings.createQueryParams({
|
2020-12-19 08:25:50 +03:00
|
|
|
scene: "NAV_PROFILE",
|
2020-11-17 05:10:43 +03:00
|
|
|
user: req.params.username,
|
|
|
|
})}`
|
|
|
|
);
|
|
|
|
}
|
2020-11-17 23:36:36 +03:00
|
|
|
|
2020-07-22 10:41:29 +03:00
|
|
|
let viewer = null;
|
2020-07-22 13:51:40 +03:00
|
|
|
if (id) {
|
2020-07-24 03:22:22 +03:00
|
|
|
viewer = await ViewerManager.getById({
|
2020-07-22 13:51:40 +03:00
|
|
|
id,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-09 07:13:00 +03:00
|
|
|
let creator = await Data.getUserByUsername({
|
2020-07-22 10:41:29 +03:00
|
|
|
username: req.params.username,
|
|
|
|
});
|
|
|
|
|
2020-08-02 01:46:54 +03:00
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
2021-01-09 07:13:00 +03:00
|
|
|
let library = creator.data.library;
|
|
|
|
|
|
|
|
creator = Serializers.user(creator);
|
|
|
|
|
2020-08-06 23:59:06 +03:00
|
|
|
const slates = await Data.getSlatesByUserId({
|
|
|
|
userId: creator.id,
|
|
|
|
publicOnly: true,
|
|
|
|
});
|
2020-07-27 12:50:25 +03:00
|
|
|
|
2021-01-09 07:13:00 +03:00
|
|
|
let publicFileIds = [];
|
|
|
|
for (let slate of slates) {
|
|
|
|
publicFileIds.push(...slate.data.objects.map((obj) => obj.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
creator.slates = slates;
|
|
|
|
|
|
|
|
if (library && library.length) {
|
|
|
|
library[0].children = library[0].children.filter((file) => {
|
|
|
|
return file.public || publicFileIds.includes(file.id);
|
|
|
|
});
|
|
|
|
}
|
2021-01-22 20:31:16 +03:00
|
|
|
|
2021-01-09 07:13:00 +03:00
|
|
|
creator.library = library;
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_/profile", {
|
2020-07-22 10:41:29 +03:00
|
|
|
viewer,
|
2021-01-09 07:13:00 +03:00
|
|
|
creator,
|
2020-10-05 00:30:28 +03:00
|
|
|
mobile,
|
2020-10-24 19:59:11 +03:00
|
|
|
resources: EXTERNAL_RESOURCES,
|
2021-01-24 04:18:04 +03:00
|
|
|
exploreSlates,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-01 07:17:41 +03:00
|
|
|
server.get("/:username/cid::cid", async (req, res) => {
|
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
|
|
|
|
|
|
|
// TODO(jim): Temporary workaround
|
|
|
|
if (!Validations.userRoute(req.params.username)) {
|
|
|
|
return handler(req, res, req.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
const shouldViewerRedirect = await ViewerManager.shouldRedirect({ id });
|
|
|
|
if (shouldViewerRedirect) {
|
|
|
|
return res.redirect(
|
|
|
|
`/_${Strings.createQueryParams({
|
|
|
|
scene: "NAV_PROFILE",
|
|
|
|
user: req.params.username,
|
|
|
|
cid: req.params.cid,
|
|
|
|
})}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let creator = await Data.getUserByUsername({
|
|
|
|
username: req.params.username,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
let library = creator.data.library;
|
|
|
|
|
|
|
|
creator = Serializers.user(creator);
|
|
|
|
|
|
|
|
const slates = await Data.getSlatesByUserId({
|
|
|
|
userId: creator.id,
|
|
|
|
publicOnly: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
let publicFileIds = [];
|
|
|
|
for (let slate of slates) {
|
|
|
|
publicFileIds.push(...slate.data.objects.map((obj) => obj.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
creator.slates = slates;
|
|
|
|
|
|
|
|
if (library && library.length) {
|
|
|
|
library[0].children = library[0].children.filter((file) => {
|
|
|
|
return file.public || publicFileIds.includes(file.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
creator.library = library;
|
|
|
|
|
|
|
|
return app.render(req, res, "/_/profile", {
|
|
|
|
viewer,
|
|
|
|
creator: Serializers.user(creator),
|
|
|
|
mobile,
|
|
|
|
resources: EXTERNAL_RESOURCES,
|
|
|
|
cid: req.params.cid,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
server.get("/:username/:slatename", async (req, res) => {
|
2020-10-22 08:38:37 +03:00
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
2020-10-23 06:55:24 +03:00
|
|
|
|
2020-08-07 02:28:54 +03:00
|
|
|
// TODO(jim): Temporary workaround
|
2020-08-07 02:36:03 +03:00
|
|
|
if (!Validations.userRoute(req.params.username)) {
|
2020-10-23 13:34:31 +03:00
|
|
|
return handler(req, res, req.url, {
|
|
|
|
mobile,
|
2020-10-24 19:59:11 +03:00
|
|
|
resources: EXTERNAL_RESOURCES,
|
2020-10-23 13:34:31 +03:00
|
|
|
});
|
2020-08-07 02:28:54 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 05:10:43 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-11-17 23:38:26 +03:00
|
|
|
const shouldViewerRedirect = await ViewerManager.shouldRedirect({ id });
|
2020-11-17 23:36:36 +03:00
|
|
|
if (shouldViewerRedirect) {
|
2020-11-17 05:10:43 +03:00
|
|
|
return res.redirect(
|
|
|
|
`/_${Strings.createQueryParams({
|
2020-12-19 08:25:50 +03:00
|
|
|
scene: "NAV_SLATE",
|
2020-11-17 05:10:43 +03:00
|
|
|
user: req.params.username,
|
|
|
|
slate: req.params.slatename,
|
|
|
|
})}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:50:25 +03:00
|
|
|
const slate = await Data.getSlateByName({
|
|
|
|
slatename: req.params.slatename,
|
2020-09-29 07:50:11 +03:00
|
|
|
username: req.params.username,
|
2020-07-27 12:50:25 +03:00
|
|
|
});
|
|
|
|
|
2020-08-02 01:46:54 +03:00
|
|
|
if (!slate) {
|
|
|
|
return res.redirect("/404");
|
2020-07-27 12:50:25 +03:00
|
|
|
}
|
|
|
|
|
2020-09-14 05:58:33 +03:00
|
|
|
if (slate.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
2020-12-22 03:39:34 +03:00
|
|
|
if (!slate.data.public && slate.data.ownerId !== id) {
|
2020-09-14 05:58:33 +03:00
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = await Data.getUserById({ id: slate.data.ownerId });
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.params.username !== creator.username) {
|
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.render(req, res, "/_/slate", {
|
|
|
|
viewer,
|
|
|
|
creator: Serializers.user(creator),
|
|
|
|
slate,
|
2020-10-05 00:30:28 +03:00
|
|
|
mobile,
|
2020-10-24 19:59:11 +03:00
|
|
|
resources: EXTERNAL_RESOURCES,
|
2020-09-14 05:58:33 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.get("/:username/:slatename/cid::cid", async (req, res) => {
|
2021-03-01 07:17:41 +03:00
|
|
|
let mobile = Window.isMobileBrowser(req.headers["user-agent"]);
|
|
|
|
|
2020-09-14 05:58:33 +03:00
|
|
|
// TODO(jim): Temporary workaround
|
|
|
|
if (!Validations.userRoute(req.params.username)) {
|
|
|
|
return handler(req, res, req.url);
|
|
|
|
}
|
|
|
|
|
2020-11-17 05:10:43 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-11-17 23:36:36 +03:00
|
|
|
|
|
|
|
const shouldViewerRedirect = await ViewerManager.shouldRedirect({ id });
|
|
|
|
if (shouldViewerRedirect) {
|
2020-11-17 05:10:43 +03:00
|
|
|
return res.redirect(
|
|
|
|
`/_${Strings.createQueryParams({
|
2020-12-19 08:25:50 +03:00
|
|
|
scene: "NAV_SLATE",
|
2020-11-17 05:10:43 +03:00
|
|
|
user: req.params.username,
|
|
|
|
slate: req.params.slatename,
|
|
|
|
cid: req.params.cid,
|
|
|
|
})}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:58:33 +03:00
|
|
|
const slate = await Data.getSlateByName({
|
|
|
|
slatename: req.params.slatename,
|
2020-09-29 07:50:11 +03:00
|
|
|
username: req.params.username,
|
2020-09-14 05:58:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!slate) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slate.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
2020-12-22 03:39:34 +03:00
|
|
|
if (!slate.data.public && slate.data.ownerId !== id) {
|
2020-08-06 23:59:06 +03:00
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = await Data.getUserById({ id: slate.data.ownerId });
|
|
|
|
|
|
|
|
if (!creator) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (creator.error) {
|
|
|
|
return res.redirect("/404");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.params.username !== creator.username) {
|
2020-08-03 02:29:11 +03:00
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
2020-08-25 09:46:02 +03:00
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_/slate", {
|
2020-08-25 09:46:02 +03:00
|
|
|
viewer,
|
2020-08-30 22:10:01 +03:00
|
|
|
creator: Serializers.user(creator),
|
2020-08-25 09:46:02 +03:00
|
|
|
slate,
|
2021-03-01 07:17:41 +03:00
|
|
|
mobile,
|
|
|
|
resources: EXTERNAL_RESOURCES,
|
2020-09-14 05:58:33 +03:00
|
|
|
cid: req.params.cid,
|
2020-07-27 12:50:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-30 22:10:01 +03:00
|
|
|
server.all("*", async (r, s) => handler(r, s, r.url));
|
2020-02-19 09:30:47 +03:00
|
|
|
|
2021-01-25 21:12:03 +03:00
|
|
|
const listenServer = server.listen(Environment.PORT, async (e) => {
|
2020-07-17 13:24:20 +03:00
|
|
|
if (e) throw e;
|
2021-01-10 02:30:52 +03:00
|
|
|
Websocket.create();
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-11-25 06:39:35 +03:00
|
|
|
NodeLogging.log(`started on http://localhost:${Environment.PORT}`);
|
2021-01-25 21:12:03 +03:00
|
|
|
|
|
|
|
exploreSlates = await fetchExploreSlates();
|
2021-01-28 09:17:01 +03:00
|
|
|
|
|
|
|
const filecoinNumber = new FilecoinNumber("10000", "attoFil");
|
|
|
|
|
|
|
|
console.log(`Testing Values: ${filecoinNumber.toPicoFil()} PICO FIL`);
|
|
|
|
console.log(`Testing Values: ${filecoinNumber.toAttoFil()} ATTO FIL`);
|
|
|
|
console.log(`Testing Values: ${filecoinNumber.toFil()} FIL`);
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
});
|