Refactor mobile/desktop redirects as a router configuration

This commit is contained in:
Rogerio Chaves 2020-04-25 08:57:31 +02:00
parent 207287fbc8
commit 98d6e27d2c
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
4 changed files with 140 additions and 136 deletions

View File

@ -14,7 +14,6 @@ const {
uploadPicture,
identityFilename,
ssbFolder,
isPhone,
} = require("./utils");
const queries = require("./queries");
const serveBlobs = require("./serve-blobs");
@ -160,26 +159,27 @@ app.use((_req, res, next) => {
const router = asyncRouter(app);
mobileRoutes.setupRoutes(router);
router.get("/", { public: true }, async (req, res) => {
if (!req.context.profile) {
return res.render("index");
}
if (isPhone(req)) {
return res.redirect("/mobile");
}
router.get(
"/",
{ public: true, mobileVersion: "/mobile" },
async (req, res) => {
if (!req.context.profile) {
return res.render("index");
}
const [posts, friends, secretMessages] = await Promise.all([
queries.getPosts(ssbServer, req.context.profile),
queries.getFriends(ssbServer, req.context.profile),
queries.getSecretMessages(ssbServer, req.context.profile),
]);
res.render("home", {
posts,
friends,
secretMessages,
profile: req.context.profile,
});
});
const [posts, friends, secretMessages] = await Promise.all([
queries.getPosts(ssbServer, req.context.profile),
queries.getFriends(ssbServer, req.context.profile),
queries.getSecretMessages(ssbServer, req.context.profile),
]);
res.render("home", {
posts,
friends,
secretMessages,
profile: req.context.profile,
});
}
);
router.get("/login", { public: true }, (_req, res) => {
res.render("login", { mode });
@ -301,26 +301,26 @@ router.get("/keys/download", async (req, res) => {
res.sendFile(secretPath);
});
router.get("/profile/:id(*)", async (req, res) => {
const id = req.params.id;
router.get(
"/profile/:id(*)",
{ mobileVersion: "/mobile/profile/:id" },
async (req, res) => {
const id = req.params.id;
if (id == req.context.profile.id) {
return res.redirect("/");
if (id == req.context.profile.id) {
return res.redirect("/");
}
const [profile, posts, friends, friendshipStatus] = await Promise.all([
queries.getProfile(ssbServer, id),
queries.getPosts(ssbServer, { id }),
queries.getFriends(ssbServer, { id }),
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
]);
res.render("profile", { profile, posts, friends, friendshipStatus });
}
if (isPhone(req)) {
return res.redirect(`/mobile/profile/${id}`);
}
const [profile, posts, friends, friendshipStatus] = await Promise.all([
queries.getProfile(ssbServer, id),
queries.getPosts(ssbServer, { id }),
queries.getFriends(ssbServer, { id }),
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
]);
res.render("profile", { profile, posts, friends, friendshipStatus });
});
);
router.post("/profile/:id(*)/add_friend", async (req, res) => {
const id = req.params.id;
@ -492,28 +492,15 @@ router.post("/about", async (req, res) => {
res.redirect("/");
});
router.get("/communities", async (req, res) => {
if (isPhone(req)) {
return res.redirect(`/mobile/communities`);
router.get(
"/communities",
{ mobileVersion: "/mobile/communities" },
async (_req, res) => {
const communities = await queries.getCommunities(ssbServer);
res.render("communities/list", { communities });
}
const communities = await queries.getCommunities(ssbServer);
res.render("communities/list", { communities });
});
router.get("/mobile/communities", async (req, res) => {
const communities = await queries.getCommunities(ssbServer);
if (!isPhone(req)) {
return res.redirect(`/communities`);
}
res.render("mobile/communities/list", {
communities,
layout: "mobile/_layout",
});
});
);
const communityData = (req) => {
const name = req.params.name;
@ -523,24 +510,24 @@ const communityData = (req) => {
}));
};
router.get("/communities/:name", async (req, res) => {
const name = req.params.name;
router.get(
"/communities/:name",
{ mobileVersion: "/mobile/communities/:name" },
async (req, res) => {
const name = req.params.name;
if (isPhone(req)) {
return res.redirect(`/mobile/communities/${name}`);
const [community, posts] = await Promise.all([
communityData(req),
queries.getCommunityPosts(ssbServer, name),
]);
res.render("communities/community", {
community,
posts,
layout: "communities/_layout",
});
}
const [community, posts] = await Promise.all([
communityData(req),
queries.getCommunityPosts(ssbServer, name),
]);
res.render("communities/community", {
community,
posts,
layout: "communities/_layout",
});
});
);
router.get("/communities/:name/new", async (req, res) => {
const community = await communityData(req);

View File

@ -1,5 +1,4 @@
const queries = require("./queries");
const { isPhone } = require("./utils");
let ssbServer;
module.exports.setSsbServer = (server) => {
@ -7,25 +6,25 @@ module.exports.setSsbServer = (server) => {
};
module.exports.setupRoutes = (router) => {
router.get("/mobile", async (req, res) => {
if (!isPhone(req)) {
return res.redirect("/");
}
const posts = await queries.getPosts(ssbServer, req.context.profile);
res.render("mobile/home", {
posts,
profile: req.context.profile,
layout: "mobile/_layout",
});
});
router.get("/mobile/secrets", async (req, res) => {
if (!isPhone(req)) {
return res.redirect("/");
router.get(
"/mobile",
{ public: true, desktopVersion: "/" },
async (req, res) => {
if (!req.context.profile) {
return res.render("index");
}
const posts = await queries.getPosts(ssbServer, req.context.profile);
res.render("mobile/home", {
posts,
profile: req.context.profile,
layout: "mobile/_layout",
});
}
);
router.get("/mobile/secrets", { desktopVersion: "/" }, async (req, res) => {
const [friends, secretMessages] = await Promise.all([
queries.getFriends(ssbServer, req.context.profile),
queries.getSecretMessages(ssbServer, req.context.profile),
@ -39,11 +38,7 @@ module.exports.setupRoutes = (router) => {
});
});
router.get("/mobile/friends", async (req, res) => {
if (!isPhone(req)) {
return res.redirect("/");
}
router.get("/mobile/friends", { desktopVersion: "/" }, async (req, res) => {
const friends = await queries.getFriends(ssbServer, req.context.profile);
res.render("mobile/friends", {
@ -53,46 +48,59 @@ module.exports.setupRoutes = (router) => {
});
});
router.get("/mobile/profile/:id(*)", async (req, res) => {
const id = req.params.id;
router.get(
"/mobile/profile/:id(*)",
{ desktopVersion: "/profile/:id" },
async (req, res) => {
const id = req.params.id;
if (id == req.context.profile.id) {
return res.redirect("/");
if (id == req.context.profile.id) {
return res.redirect("/mobile");
}
const [profile, posts, friends, friendshipStatus] = await Promise.all([
queries.getProfile(ssbServer, id),
queries.getPosts(ssbServer, { id }),
queries.getFriends(ssbServer, { id }),
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
]);
res.render("mobile/profile", {
profile,
posts,
friends,
friendshipStatus,
layout: "mobile/_layout",
});
}
);
if (!isPhone(req)) {
return res.redirect(`/profile/${id}`);
router.get(
"/mobile/communities",
{ desktopVersion: "/communities" },
async (_req, res) => {
const communities = await queries.getCommunities(ssbServer);
res.render("mobile/communities/list", {
communities,
layout: "mobile/_layout",
});
}
);
const [profile, posts, friends, friendshipStatus] = await Promise.all([
queries.getProfile(ssbServer, id),
queries.getPosts(ssbServer, { id }),
queries.getFriends(ssbServer, { id }),
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id),
]);
router.get(
"/mobile/communities/:name",
{ desktopVersion: "/communities/:name" },
async (req, res) => {
const name = req.params.name;
res.render("mobile/profile", {
profile,
posts,
friends,
friendshipStatus,
layout: "mobile/_layout",
});
});
const posts = await queries.getCommunityPosts(ssbServer, name);
router.get("/mobile/communities/:name", async (req, res) => {
const name = req.params.name;
if (!isPhone(req)) {
return res.redirect(`/communities/${name}`);
res.render("mobile/communities/community", {
community: { name },
posts,
layout: "mobile/_layout",
});
}
const posts = await queries.getCommunityPosts(ssbServer, name);
res.render("mobile/communities/community", {
community: { name },
posts,
layout: "mobile/_layout",
});
});
);
};

View File

@ -5,6 +5,8 @@ const split = require("split-buffer");
const metrics = require("./metrics");
const isMobile = require("ismobilejs").default;
const isPhone = (req) => isMobile(req.headers["user-agent"]).phone;
module.exports.asyncRouter = (app) => {
const debug = require("debug")("router");
@ -17,6 +19,16 @@ module.exports.asyncRouter = (app) => {
}
return res.redirect("/");
}
if (
(opts.mobileVersion && isPhone(req)) ||
(opts.desktopVersion && !isPhone(req))
) {
let url = opts.mobileVersion || opts.desktopVersion;
for (let key in req.params) {
url = url.replace(`:${key}`, req.params[key]);
}
return res.redirect(url);
}
req.context.path = path;
try {
@ -113,7 +125,3 @@ module.exports.promisePull = (...streams) =>
});
module.exports.mapValues = (x) => x.map((y) => y.value);
module.exports.isPhone = (req) => {
return isMobile(req.headers["user-agent"]).phone;
};

View File

@ -380,4 +380,5 @@ h2 {
.community-topic-replies {
width: 110px;
text-align: right;
}