2020-01-14 14:38:33 +00:00
|
|
|
const constants = require("../../lib/constants")
|
2020-01-26 14:56:59 +00:00
|
|
|
const {fetchUser, getOrFetchShortcode} = require("../../lib/collectors")
|
2020-01-28 10:37:19 +00:00
|
|
|
const {render, redirect} = require("pinski/plugins")
|
2020-01-12 12:50:21 +00:00
|
|
|
|
|
|
|
module.exports = [
|
2020-01-28 10:37:19 +00:00
|
|
|
{
|
|
|
|
route: `/u`, methods: ["GET"], code: async ({url}) => {
|
|
|
|
if (url.searchParams.has("u")) {
|
|
|
|
let username = url.searchParams.get("u")
|
|
|
|
username = username.replace(/^(https?:\/\/)?([a-z]+\.)?instagram\.com\//, "")
|
|
|
|
username = username.replace(/^\@+/, "")
|
|
|
|
username = username.replace(/\/+$/, "")
|
|
|
|
return redirect(`/u/${username}`, 301)
|
|
|
|
} else {
|
|
|
|
return render(400, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 400,
|
|
|
|
title: "Bad request",
|
2020-01-28 12:17:32 +00:00
|
|
|
message: "Expected a username",
|
|
|
|
explanation: "Write /u/{username} or /u?u={username}."
|
2020-01-28 10:37:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-01-18 15:38:14 +00:00
|
|
|
{
|
2020-01-27 06:03:28 +00:00
|
|
|
route: `/u/(${constants.external.username_regex})`, methods: ["GET"], code: ({url, fill}) => {
|
2020-01-18 15:38:14 +00:00
|
|
|
const params = url.searchParams
|
2020-01-27 06:03:28 +00:00
|
|
|
return fetchUser(fill[0]).then(async user => {
|
|
|
|
const page = +params.get("page")
|
|
|
|
if (typeof page === "number" && !isNaN(page) && page >= 1) {
|
|
|
|
await user.timeline.fetchUpToPage(page - 1)
|
|
|
|
}
|
|
|
|
return render(200, "pug/user.pug", {url, user})
|
|
|
|
}).catch(error => {
|
|
|
|
if (error === constants.symbols.NOT_FOUND) {
|
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
|
|
|
message: "This user doesn't exist."
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2020-01-12 12:50:21 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
route: `/fragment/user/(${constants.external.username_regex})/(\\d+)`, methods: ["GET"], code: async ({url, fill}) => {
|
2020-01-27 06:03:28 +00:00
|
|
|
return fetchUser(fill[0]).then(async user => {
|
|
|
|
const pageNumber = +fill[1]
|
|
|
|
const pageIndex = pageNumber - 1
|
|
|
|
await user.timeline.fetchUpToPage(pageIndex)
|
|
|
|
if (user.timeline.pages[pageIndex]) {
|
|
|
|
return render(200, "pug/fragments/timeline_page.pug", {page: user.timeline.pages[pageIndex], pageIndex, user, url})
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
statusCode: 400,
|
|
|
|
contentType: "text/html",
|
2020-01-28 12:17:32 +00:00
|
|
|
content: "That page does not exist."
|
2020-01-27 06:03:28 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
}
|
2020-01-27 06:03:28 +00:00
|
|
|
}).catch(error => {
|
|
|
|
if (error === constants.symbols.NOT_FOUND) {
|
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
|
|
|
message: "This user doesn't exist."
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2020-01-12 15:39:50 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
},
|
2020-01-28 10:37:19 +00:00
|
|
|
{
|
|
|
|
route: "/p", methods: ["GET"], code: async ({url}) => {
|
|
|
|
if (url.searchParams.has("p")) {
|
|
|
|
let post = url.searchParams.get("p")
|
|
|
|
post = post.replace(/^(https?:\/\/)?([a-z]+\.)?instagram\.com\/p\//, "")
|
|
|
|
return redirect(`/p/${post}`, 301)
|
|
|
|
} else {
|
|
|
|
return render(400, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 400,
|
|
|
|
title: "Bad request",
|
2020-01-28 12:17:32 +00:00
|
|
|
message: "Expected a shortcode",
|
2020-01-28 12:19:15 +00:00
|
|
|
explanation: "Write /p/{shortcode} or /p?p={shortcode}."
|
2020-01-28 10:37:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-01-18 15:38:14 +00:00
|
|
|
{
|
2020-01-27 06:03:28 +00:00
|
|
|
route: `/p/(${constants.external.shortcode_regex})`, methods: ["GET"], code: ({fill}) => {
|
|
|
|
return getOrFetchShortcode(fill[0]).then(async post => {
|
|
|
|
await post.fetchChildren()
|
|
|
|
await post.fetchExtendedOwnerP() // parallel await is okay since intermediate fetch result is cached
|
2020-01-29 15:20:20 +00:00
|
|
|
if (post.isVideo()) await post.fetchVideoURL()
|
2020-01-27 06:03:28 +00:00
|
|
|
return render(200, "pug/post.pug", {post})
|
|
|
|
}).catch(error => {
|
|
|
|
if (error === constants.symbols.NOT_FOUND) {
|
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
|
|
|
message: "Somehow, you reached a post that doesn't exist."
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2020-01-18 15:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 12:50:21 +00:00
|
|
|
]
|