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

View File

@ -1,5 +1,4 @@
const queries = require("./queries"); const queries = require("./queries");
const { isPhone } = require("./utils");
let ssbServer; let ssbServer;
module.exports.setSsbServer = (server) => { module.exports.setSsbServer = (server) => {
@ -7,25 +6,25 @@ module.exports.setSsbServer = (server) => {
}; };
module.exports.setupRoutes = (router) => { module.exports.setupRoutes = (router) => {
router.get("/mobile", async (req, res) => { router.get(
if (!isPhone(req)) { "/mobile",
return res.redirect("/"); { public: true, desktopVersion: "/" },
} async (req, res) => {
if (!req.context.profile) {
const posts = await queries.getPosts(ssbServer, req.context.profile); return res.render("index");
}
res.render("mobile/home", {
posts, const posts = await queries.getPosts(ssbServer, req.context.profile);
profile: req.context.profile,
layout: "mobile/_layout", 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/secrets", { desktopVersion: "/" }, async (req, res) => {
const [friends, secretMessages] = await Promise.all([ const [friends, secretMessages] = await Promise.all([
queries.getFriends(ssbServer, req.context.profile), queries.getFriends(ssbServer, req.context.profile),
queries.getSecretMessages(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) => { router.get("/mobile/friends", { desktopVersion: "/" }, async (req, res) => {
if (!isPhone(req)) {
return res.redirect("/");
}
const friends = await queries.getFriends(ssbServer, req.context.profile); const friends = await queries.getFriends(ssbServer, req.context.profile);
res.render("mobile/friends", { res.render("mobile/friends", {
@ -53,46 +48,59 @@ module.exports.setupRoutes = (router) => {
}); });
}); });
router.get("/mobile/profile/:id(*)", async (req, res) => { router.get(
const id = req.params.id; "/mobile/profile/:id(*)",
{ desktopVersion: "/profile/:id" },
async (req, res) => {
const id = req.params.id;
if (id == req.context.profile.id) { if (id == req.context.profile.id) {
return res.redirect("/"); 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)) { router.get(
return res.redirect(`/profile/${id}`); "/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([ router.get(
queries.getProfile(ssbServer, id), "/mobile/communities/:name",
queries.getPosts(ssbServer, { id }), { desktopVersion: "/communities/:name" },
queries.getFriends(ssbServer, { id }), async (req, res) => {
queries.getFriendshipStatus(ssbServer, req.context.profile.id, id), const name = req.params.name;
]);
res.render("mobile/profile", { const posts = await queries.getCommunityPosts(ssbServer, name);
profile,
posts,
friends,
friendshipStatus,
layout: "mobile/_layout",
});
});
router.get("/mobile/communities/:name", async (req, res) => { res.render("mobile/communities/community", {
const name = req.params.name; community: { name },
posts,
if (!isPhone(req)) { layout: "mobile/_layout",
return res.redirect(`/communities/${name}`); });
} }
);
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 metrics = require("./metrics");
const isMobile = require("ismobilejs").default; const isMobile = require("ismobilejs").default;
const isPhone = (req) => isMobile(req.headers["user-agent"]).phone;
module.exports.asyncRouter = (app) => { module.exports.asyncRouter = (app) => {
const debug = require("debug")("router"); const debug = require("debug")("router");
@ -17,6 +19,16 @@ module.exports.asyncRouter = (app) => {
} }
return res.redirect("/"); 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; req.context.path = path;
try { try {
@ -113,7 +125,3 @@ module.exports.promisePull = (...streams) =>
}); });
module.exports.mapValues = (x) => x.map((y) => y.value); 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 { .community-topic-replies {
width: 110px; width: 110px;
text-align: right;
} }