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-01-28 10:37:19 +00:00
|
|
|
const {render, redirect} = require("pinski/plugins")
|
2020-01-30 03:05:43 +00:00
|
|
|
const {pugCache} = require("../passthrough")
|
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", {
|
|
|
|
rssEnabled: constants.settings.rss_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(/\/+$/, "")
|
|
|
|
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-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-02-02 14:53:37 +00:00
|
|
|
return fetchUser(fill[0], false).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)
|
|
|
|
}
|
2020-02-18 00:39:20 +00:00
|
|
|
return render(200, "pug/user.pug", {url, user, constants, website_origin: constants.website_origin})
|
2020-01-27 06:03:28 +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
|
|
|
})
|
2020-01-30 03:05:43 +00:00
|
|
|
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN) {
|
|
|
|
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-02-02 14:53:37 +00:00
|
|
|
expiresMinutes: userRequestCache.getTtl("user/"+fill[0], 1000*60)
|
2020-01-30 03:05:43 +00:00
|
|
|
})
|
|
|
|
}
|
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-02-02 14:53:37 +00:00
|
|
|
return fetchUser(fill[0], false).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 => {
|
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
|
|
|
})
|
|
|
|
} 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-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-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",
|
2020-02-01 03:15:12 +00:00
|
|
|
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
|
|
|
]
|