2020-01-14 14:38:33 +00:00
|
|
|
const constants = require("../../lib/constants")
|
2020-07-19 13:40:27 +00:00
|
|
|
const lang = require("../../lang")
|
2020-02-02 15:09:40 +00:00
|
|
|
const switcher = require("../../lib/utils/torswitcher")
|
2020-05-19 15:54:22 +00:00
|
|
|
const {fetchUser, getOrFetchShortcode, userRequestCache, history, assistantSwitcher} = require("../../lib/collectors")
|
2020-05-04 11:50:54 +00:00
|
|
|
const {render, redirect, getStaticURL} = require("pinski/plugins")
|
2020-01-30 03:05:43 +00:00
|
|
|
const {pugCache} = require("../passthrough")
|
2020-05-05 14:14:11 +00:00
|
|
|
const {getSettings} = require("./utils/getsettings")
|
2020-05-29 08:46:45 +00:00
|
|
|
const {getSettingsReferrer} = require("./utils/settingsreferrer")
|
2020-07-22 12:58:21 +00:00
|
|
|
const quota = require("../../lib/quota")
|
2020-01-12 12:50:21 +00:00
|
|
|
|
2020-03-01 03:43:43 +00:00
|
|
|
/** @param {import("../../lib/structures/TimelineEntry")} post */
|
|
|
|
function getPageTitle(post) {
|
|
|
|
return (post.getCaptionIntroduction() || `Post from @${post.getBasicOwner().username}`) + " | Bibliogram"
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:51:41 +00:00
|
|
|
function getPostAndQuota(req, shortcode) {
|
|
|
|
if (quota.remaining(req) === 0) {
|
|
|
|
throw constants.symbols.QUOTA_REACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
return getOrFetchShortcode(shortcode).then(async ({post, fromCache: fromCache1}) => {
|
|
|
|
const {fromCache: fromCache2} = await post.fetchChildren()
|
|
|
|
const {fromCache: fromCache3} = await post.fetchExtendedOwnerP() // serial await is okay since intermediate fetch result is cached
|
|
|
|
const {fromCache: fromCache4} = await post.fetchVideoURL() // if post is not a video, function will just return, so this is fine
|
|
|
|
|
|
|
|
// I'd _love_ to be able to put these in an array, but I can't destructure directly into one, so this is easier.
|
|
|
|
const quotaUsed = (fromCache1 && fromCache2 && fromCache3 && fromCache4) ? 0 : 1 // if any of them is false then one request was needed to get the post.
|
|
|
|
const remaining = quota.add(req, quotaUsed)
|
|
|
|
|
|
|
|
return {post, remaining}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:50:21 +00:00
|
|
|
module.exports = [
|
2020-02-01 08:17:10 +00:00
|
|
|
{
|
2020-05-09 09:34:00 +00:00
|
|
|
route: "/", methods: ["GET"], code: async ({req}) => {
|
|
|
|
const settings = getSettings(req)
|
2020-02-01 08:17:10 +00:00
|
|
|
return render(200, "pug/home.pug", {
|
2020-05-09 09:34:00 +00:00
|
|
|
settings,
|
2020-05-29 08:46:45 +00:00
|
|
|
settingsReferrer: getSettingsReferrer("/"),
|
2020-05-01 02:37:27 +00:00
|
|
|
rssEnabled: constants.feeds.enabled,
|
2020-05-19 15:54:22 +00:00
|
|
|
allUnblocked: history.testNoneBlocked() || assistantSwitcher.displaySomeUnblocked(),
|
2020-02-05 10:11:00 +00:00
|
|
|
torAvailable: switcher.canUseTor(),
|
2020-06-19 05:45:51 +00:00
|
|
|
hasPrivacyPolicy: constants.has_privacy_policy,
|
|
|
|
onionLocation: constants.onion_location
|
2020-02-01 08:17:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2020-02-05 10:11:00 +00:00
|
|
|
{
|
2020-05-09 09:34:00 +00:00
|
|
|
route: "/privacy", methods: ["GET"], code: async ({req}) => {
|
2020-06-16 11:01:40 +00:00
|
|
|
const settings = getSettings(req)
|
2020-02-05 10:11:00 +00:00
|
|
|
if (constants.has_privacy_policy && pugCache.has("pug/privacy.pug")) {
|
2020-05-09 09:34:00 +00:00
|
|
|
return render(200, "pug/privacy.pug", {settings})
|
2020-02-05 10:11:00 +00:00
|
|
|
} else {
|
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "No privacy policy",
|
|
|
|
message: "No privacy policy",
|
|
|
|
explanation:
|
|
|
|
"The owner of this instance has not actually written a privacy policy."
|
2020-06-16 11:01:40 +00:00
|
|
|
+"\nIf you own this instance, please read the file stored at /src/site/pug/privacy.pug.template.",
|
|
|
|
settings
|
2020-02-05 10:11:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
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(/\/+$/, "")
|
2020-11-08 10:54:50 +00:00
|
|
|
username = username.toLowerCase().trim()
|
2020-01-28 10:37:19 +00:00
|
|
|
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",
|
2020-02-01 03:15:12 +00:00
|
|
|
explanation: "Write /u/{username} or /u?u={username}.",
|
|
|
|
withInstancesLink: false
|
2020-01-28 10:37:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-01-18 15:38:14 +00:00
|
|
|
{
|
2020-07-22 12:58:21 +00:00
|
|
|
route: `/u/(${constants.external.username_regex})(/channel)?`, methods: ["GET"], code: async ({req, url, fill}) => {
|
2020-06-24 14:58:01 +00:00
|
|
|
const username = fill[0]
|
|
|
|
const type = fill[1] ? "igtv" : "timeline"
|
|
|
|
|
|
|
|
if (username !== username.toLowerCase()) { // some capital letters
|
2020-07-22 12:58:21 +00:00
|
|
|
return redirect(`/u/${username.toLowerCase()}`, 301)
|
2020-04-06 01:53:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:01:40 +00:00
|
|
|
const settings = getSettings(req)
|
2020-01-18 15:38:14 +00:00
|
|
|
const params = url.searchParams
|
2020-07-22 12:58:21 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (quota.remaining(req) === 0) {
|
|
|
|
throw constants.symbols.QUOTA_REACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
const {user, quotaUsed} = await fetchUser(username)
|
|
|
|
let remaining = quota.add(req, quotaUsed)
|
|
|
|
|
2020-06-24 14:58:01 +00:00
|
|
|
const selectedTimeline = user[type]
|
|
|
|
let pageNumber = +params.get("page")
|
|
|
|
if (isNaN(pageNumber) || pageNumber < 1) pageNumber = 1
|
2020-07-22 12:58:21 +00:00
|
|
|
const pageIndex = pageNumber - 1
|
|
|
|
|
|
|
|
const pagesNeeded = pageNumber - selectedTimeline.pages.length
|
|
|
|
if (pagesNeeded > remaining) {
|
|
|
|
throw constants.symbols.QUOTA_REACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
const quotaUsed2 = await selectedTimeline.fetchUpToPage(pageIndex)
|
|
|
|
remaining = quota.add(req, quotaUsed2)
|
|
|
|
|
2020-04-20 07:51:53 +00:00
|
|
|
const followerCountsAvailable = !(user.constructor.name === "ReelUser" && user.following === 0 && user.followedBy === 0)
|
2020-05-29 08:46:45 +00:00
|
|
|
return render(200, "pug/user.pug", {
|
|
|
|
url,
|
|
|
|
user,
|
2020-06-24 14:58:01 +00:00
|
|
|
selectedTimeline,
|
|
|
|
type,
|
2020-05-29 08:46:45 +00:00
|
|
|
followerCountsAvailable,
|
|
|
|
constants,
|
|
|
|
settings,
|
2020-07-22 12:58:21 +00:00
|
|
|
settingsReferrer: getSettingsReferrer(req.url),
|
|
|
|
remaining
|
2020-05-29 08:46:45 +00:00
|
|
|
})
|
2020-07-22 12:58:21 +00:00
|
|
|
} catch (error) {
|
2020-02-05 12:32:51 +00:00
|
|
|
if (error === constants.symbols.NOT_FOUND || error === constants.symbols.ENDPOINT_OVERRIDDEN) {
|
2020-01-27 06:03:28 +00:00
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
2020-02-01 03:15:12 +00:00
|
|
|
message: "This user doesn't exist.",
|
2020-06-16 11:01:40 +00:00
|
|
|
withInstancesLink: false,
|
|
|
|
settings
|
2020-01-27 06:03:28 +00:00
|
|
|
})
|
2020-07-28 12:53:29 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN) {
|
2020-01-30 03:05:43 +00:00
|
|
|
return {
|
|
|
|
statusCode: 503,
|
|
|
|
contentType: "text/html",
|
|
|
|
headers: {
|
2020-06-24 14:58:01 +00:00
|
|
|
"Retry-After": userRequestCache.getTtl("user/"+username, 1000)
|
2020-01-30 03:05:43 +00:00
|
|
|
},
|
|
|
|
content: pugCache.get("pug/blocked.pug").web({
|
2020-06-11 16:09:28 +00:00
|
|
|
website_origin: constants.website_origin,
|
2020-06-24 14:58:01 +00:00
|
|
|
username,
|
|
|
|
expiresMinutes: userRequestCache.getTtl("user/"+username, 1000*60),
|
2020-06-16 11:01:40 +00:00
|
|
|
getStaticURL,
|
2020-07-19 13:40:27 +00:00
|
|
|
settings,
|
|
|
|
lang
|
2020-01-30 03:05:43 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-18 12:45:22 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) {
|
|
|
|
return render(503, "pug/blocked_december.pug")
|
2020-07-28 12:53:29 +00:00
|
|
|
} else if (error === constants.symbols.RATE_LIMITED) {
|
|
|
|
return render(503, "pug/blocked_graphql.pug")
|
2020-04-13 15:46:23 +00:00
|
|
|
} else if (error === constants.symbols.extractor_results.AGE_RESTRICTED) {
|
2020-06-16 11:01:40 +00:00
|
|
|
return render(403, "pug/age_gated.pug", {settings})
|
2020-07-22 12:58:21 +00:00
|
|
|
} else if (error === constants.symbols.QUOTA_REACHED) {
|
2020-07-30 15:19:55 +00:00
|
|
|
const isProxyNetwork = quota.isProxyNetwork(req)
|
|
|
|
return render(429, "pug/quota_reached.pug", {isProxyNetwork})
|
2020-01-27 06:03:28 +00:00
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
2020-07-22 12:58:21 +00:00
|
|
|
}
|
2020-01-12 12:50:21 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-19 15:03:21 +00:00
|
|
|
route: `/fragment/user/(${constants.external.username_regex})/(\\d+)`, methods: ["GET"], code: async ({req, url, fill}) => {
|
2020-06-24 14:58:01 +00:00
|
|
|
const username = fill[0]
|
|
|
|
let pageNumber = +fill[1]
|
|
|
|
if (isNaN(pageNumber) || pageNumber < 1) {
|
|
|
|
return {
|
|
|
|
statusCode: 400,
|
|
|
|
contentType: "text/html",
|
|
|
|
content: "Bad page number"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let type = url.searchParams.get("type")
|
|
|
|
if (!["timeline", "igtv"].includes(type)) type = "timeline"
|
|
|
|
|
2020-07-22 12:58:21 +00:00
|
|
|
try {
|
|
|
|
if (quota.remaining(req) === 0) {
|
|
|
|
throw constants.symbols.QUOTA_REACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings = getSettings(req)
|
|
|
|
|
|
|
|
const {user, quotaUsed} = await fetchUser(username)
|
|
|
|
const remaining = quota.add(req, quotaUsed)
|
|
|
|
|
2020-01-27 06:03:28 +00:00
|
|
|
const pageIndex = pageNumber - 1
|
2020-06-24 14:58:01 +00:00
|
|
|
const selectedTimeline = user[type]
|
2020-07-22 12:58:21 +00:00
|
|
|
|
|
|
|
const pagesNeeded = pageNumber - selectedTimeline.pages.length
|
|
|
|
if (pagesNeeded > remaining) {
|
|
|
|
throw constants.symbols.QUOTA_REACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
const quotaUsed2 = await selectedTimeline.fetchUpToPage(pageIndex)
|
|
|
|
quota.add(req, quotaUsed2)
|
|
|
|
|
2020-06-24 14:58:01 +00:00
|
|
|
if (selectedTimeline.pages[pageIndex]) {
|
|
|
|
return render(200, "pug/fragments/timeline_page.pug", {page: selectedTimeline.pages[pageIndex], selectedTimeline, type, pageIndex, user, url, settings})
|
2020-01-27 06:03:28 +00:00
|
|
|
} 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-07-22 12:58:21 +00:00
|
|
|
} catch (error) {
|
2020-02-05 12:32:51 +00:00
|
|
|
if (error === constants.symbols.NOT_FOUND || error === constants.symbols.ENDPOINT_OVERRIDDEN) {
|
2020-01-27 06:03:28 +00:00
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
2020-02-01 03:15:12 +00:00
|
|
|
message: "This user doesn't exist.",
|
|
|
|
withInstancesLink: false
|
2020-01-27 06:03:28 +00:00
|
|
|
})
|
2021-01-18 12:45:22 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) {
|
|
|
|
return render(502, "pug/fragments/timeline_loading_blocked_december.pug")
|
2020-06-24 08:20:43 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN || error === constants.symbols.RATE_LIMITED) {
|
2020-07-16 11:41:18 +00:00
|
|
|
return render(503, "pug/fragments/timeline_loading_blocked.pug")
|
2020-07-22 12:58:21 +00:00
|
|
|
} else if (error === constants.symbols.QUOTA_REACHED) {
|
2020-07-30 15:43:01 +00:00
|
|
|
return render(429, "pug/fragments/timeline_quota_reached.pug")
|
2020-01-27 06:03:28 +00:00
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
2020-07-22 12:58:21 +00:00
|
|
|
}
|
2020-01-12 15:39:50 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
},
|
2020-02-21 12:35:19 +00:00
|
|
|
{
|
2020-07-29 09:51:41 +00:00
|
|
|
route: `/fragment/post/(${constants.external.shortcode_regex})`, methods: ["GET"], code: async ({req, fill}) => {
|
2020-07-28 12:53:29 +00:00
|
|
|
const shortcode = fill[0]
|
2020-07-29 09:51:41 +00:00
|
|
|
const settings = getSettings(req)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const {post, remaining} = await getPostAndQuota(req, shortcode)
|
2020-02-21 12:35:19 +00:00
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: "application/json",
|
|
|
|
content: {
|
2020-03-01 03:43:43 +00:00
|
|
|
title: getPageTitle(post),
|
2020-07-29 09:51:41 +00:00
|
|
|
html: pugCache.get("pug/fragments/post.pug").web({lang, post, settings, getStaticURL}),
|
|
|
|
quota: remaining
|
2020-02-21 12:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 09:51:41 +00:00
|
|
|
} catch (error) {
|
2021-01-18 12:57:19 +00:00
|
|
|
if (error === constants.symbols.NOT_FOUND || constants.symbols.RATE_LIMITED || error === constants.symbols.QUOTA_REACHED || error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) {
|
2020-07-29 09:51:41 +00:00
|
|
|
const statusCode = error === constants.symbols.QUOTA_REACHED ? 429 : 503
|
2020-07-28 12:53:29 +00:00
|
|
|
return {
|
2020-07-29 09:51:41 +00:00
|
|
|
statusCode,
|
2020-07-28 12:53:29 +00:00
|
|
|
contentType: "application/json",
|
|
|
|
content: {
|
|
|
|
redirectTo: `/p/${shortcode}`
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 12:35:19 +00:00
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
2020-07-29 09:51:41 +00:00
|
|
|
}
|
2020-02-21 12:35:19 +00:00
|
|
|
}
|
|
|
|
},
|
2020-01-28 10:37:19 +00:00
|
|
|
{
|
|
|
|
route: "/p", methods: ["GET"], code: async ({url}) => {
|
|
|
|
if (url.searchParams.has("p")) {
|
2020-11-08 10:54:50 +00:00
|
|
|
let post = url.searchParams.get("p").trim()
|
2020-01-28 10:37:19 +00:00
|
|
|
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-02-01 03:15:12 +00:00
|
|
|
explanation: "Write /p/{shortcode} or /p?p={shortcode}.",
|
|
|
|
withInstancesLink: false
|
2020-01-28 10:37:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-01-18 15:38:14 +00:00
|
|
|
{
|
2020-08-31 08:30:31 +00:00
|
|
|
route: `/(?:p|tv|igtv|reel)/(${constants.external.shortcode_regex})`, methods: ["GET"], code: async ({req, fill}) => {
|
2020-07-29 09:51:41 +00:00
|
|
|
const shortcode = fill[0]
|
2020-06-16 11:01:40 +00:00
|
|
|
const settings = getSettings(req)
|
2020-07-29 09:51:41 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const {post} = await getPostAndQuota(req, shortcode)
|
2020-03-01 03:43:43 +00:00
|
|
|
return render(200, "pug/post.pug", {
|
|
|
|
title: getPageTitle(post),
|
2020-03-04 12:12:24 +00:00
|
|
|
post,
|
2020-05-05 14:14:11 +00:00
|
|
|
website_origin: constants.website_origin,
|
|
|
|
settings
|
2020-03-01 03:43:43 +00:00
|
|
|
})
|
2020-07-29 09:51:41 +00:00
|
|
|
} catch (error) {
|
2020-01-27 06:03:28 +00:00
|
|
|
if (error === constants.symbols.NOT_FOUND) {
|
|
|
|
return render(404, "pug/friendlyerror.pug", {
|
|
|
|
statusCode: 404,
|
|
|
|
title: "Not found",
|
2020-02-01 03:15:12 +00:00
|
|
|
message: "Somehow, you reached a post that doesn't exist.",
|
2020-06-16 11:01:40 +00:00
|
|
|
withInstancesLink: false,
|
|
|
|
settings
|
2020-01-27 06:03:28 +00:00
|
|
|
})
|
2021-01-18 12:57:19 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) {
|
|
|
|
return render(502, "pug/blocked_december.pug")
|
2020-07-28 12:53:29 +00:00
|
|
|
} else if (error === constants.symbols.RATE_LIMITED) {
|
|
|
|
return render(503, "pug/blocked_graphql.pug")
|
2020-07-29 09:51:41 +00:00
|
|
|
} else if (error === constants.symbols.QUOTA_REACHED) {
|
2020-07-30 15:19:55 +00:00
|
|
|
const isProxyNetwork = quota.isProxyNetwork(req)
|
|
|
|
return render(429, "pug/quota_reached.pug", {isProxyNetwork})
|
2020-01-27 06:03:28 +00:00
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
2020-07-29 09:51:41 +00:00
|
|
|
}
|
2020-01-18 15:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 12:50:21 +00:00
|
|
|
]
|