bibliogram/src/site/api/routes.js

201 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-01-14 14:38:33 +00:00
const constants = require("../../lib/constants")
2020-02-02 15:09:40 +00:00
const switcher = require("../../lib/utils/torswitcher")
2020-02-02 14:53:37 +00:00
const {fetchUser, getOrFetchShortcode, userRequestCache, history} = 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-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-01-12 12:50:21 +00:00
module.exports = [
2020-02-01 08:17:10 +00:00
{
route: "/", methods: ["GET"], code: async () => {
return render(200, "pug/home.pug", {
2020-05-01 02:37:27 +00:00
rssEnabled: constants.feeds.enabled,
2020-02-02 15:09:40 +00:00
allUnblocked: history.testNoneBlocked(),
2020-02-05 10:11:00 +00:00
torAvailable: switcher.canUseTor(),
hasPrivacyPolicy: constants.has_privacy_policy
2020-02-01 08:17:10 +00:00
})
}
},
2020-02-05 10:11:00 +00:00
{
route: "/privacy", methods: ["GET"], code: async () => {
if (constants.has_privacy_policy && pugCache.has("pug/privacy.pug")) {
return render(200, "pug/privacy.pug")
} 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."
+"\nIf you own this instance, please read the file stored at /src/site/pug/privacy.pug.template."
})
}
}
},
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(/\/+$/, "")
username = username.toLowerCase()
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",
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-01-27 06:03:28 +00:00
route: `/u/(${constants.external.username_regex})`, methods: ["GET"], code: ({url, fill}) => {
if (fill[0] !== fill[0].toLowerCase()) { // some capital letters
return Promise.resolve(redirect(`/u/${fill[0].toLowerCase()}`, 301))
}
2020-01-18 15:38:14 +00:00
const params = url.searchParams
2020-04-12 14:52:04 +00:00
return fetchUser(fill[0]).then(async user => {
2020-01-27 06:03:28 +00:00
const page = +params.get("page")
if (typeof page === "number" && !isNaN(page) && page >= 1) {
await user.timeline.fetchUpToPage(page - 1)
}
const followerCountsAvailable = !(user.constructor.name === "ReelUser" && user.following === 0 && user.followedBy === 0)
2020-04-22 11:59:45 +00:00
return render(200, "pug/user.pug", {url, user, followerCountsAvailable, constants})
2020-01-27 06:03:28 +00:00
}).catch(error => {
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",
message: "This user doesn't exist.",
withInstancesLink: false
2020-01-27 06:03:28 +00:00
})
2020-04-07 06:30:00 +00:00
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN || error === constants.symbols.RATE_LIMITED) {
2020-01-30 03:05:43 +00:00
return {
statusCode: 503,
contentType: "text/html",
headers: {
2020-02-02 14:53:37 +00:00
"Retry-After": userRequestCache.getTtl("user/"+fill[0], 1000)
2020-01-30 03:05:43 +00:00
},
content: pugCache.get("pug/blocked.pug").web({
2020-05-04 11:50:54 +00:00
expiresMinutes: userRequestCache.getTtl("user/"+fill[0], 1000*60),
getStaticURL
2020-01-30 03:05:43 +00:00
})
}
} else if (error === constants.symbols.extractor_results.AGE_RESTRICTED) {
return render(403, "pug/age_gated.pug")
2020-01-27 06:03:28 +00:00
} 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-04-12 14:52:04 +00:00
return fetchUser(fill[0]).then(async user => {
2020-01-27 06:03:28 +00:00
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 || error === constants.symbols.ENDPOINT_OVERRIDDEN) {
2020-01-27 06:03:28 +00:00
return render(404, "pug/friendlyerror.pug", {
statusCode: 404,
title: "Not found",
message: "This user doesn't exist.",
withInstancesLink: false
2020-01-27 06:03:28 +00:00
})
} else {
throw error
}
})
2020-01-12 15:39:50 +00:00
}
2020-01-18 15:38:14 +00:00
},
2020-02-21 12:35:19 +00:00
{
route: `/fragment/post/(${constants.external.shortcode_regex})`, methods: ["GET"], code: ({fill}) => {
return getOrFetchShortcode(fill[0]).then(async post => {
await post.fetchChildren()
await post.fetchExtendedOwnerP() // serial await is okay since intermediate fetch result is cached
if (post.isVideo()) await post.fetchVideoURL()
return {
statusCode: 200,
contentType: "application/json",
content: {
2020-03-01 03:43:43 +00:00
title: getPageTitle(post),
2020-02-21 12:35:19 +00:00
html: pugCache.get("pug/fragments/post.pug").web({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.",
withInstancesLink: false
})
} else {
throw error
}
})
}
},
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",
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-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()
2020-02-21 12:35:19 +00:00
await post.fetchExtendedOwnerP() // serial await is okay since intermediate fetch result is cached
2020-01-29 15:20:20 +00:00
if (post.isVideo()) await post.fetchVideoURL()
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,
website_origin: constants.website_origin
2020-03-01 03:43:43 +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: "Somehow, you reached a post that doesn't exist.",
withInstancesLink: false
2020-01-27 06:03:28 +00:00
})
} else {
throw error
}
})
2020-01-18 15:38:14 +00:00
}
}
2020-01-12 12:50:21 +00:00
]