Skip to content

Commit

Permalink
missing subtitles, switch back to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Jan 17, 2024
1 parent 366edfc commit 2b09d12
Show file tree
Hide file tree
Showing 75 changed files with 9,692 additions and 9,679 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"trailingComma": "all",
"singleQuote": false,
"printWidth": 100,
"useTabs": true,
"useTabs": false,
"tabWidth": 2,
"bracketSpacing": true,
"arrowParens": "always",
"parser": "typescript"
Expand Down
130 changes: 65 additions & 65 deletions api/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,81 @@ const REDIRECT_URI = `${process.env.REDIRECT_DOMAIN}/api/discord/callback`;
console.log("Redirect URI is: " + REDIRECT_URI);

router.get("/login", (req, res) => {
res.redirect(
`https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&response_type=code&scope=identify&redirect_uri=${encodeURIComponent(
REDIRECT_URI,
)}`,
);
res.redirect(
`https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&response_type=code&scope=identify&redirect_uri=${encodeURIComponent(
REDIRECT_URI,
)}`,
);
});

router.post("/refresh", (req, res) => {
// can be caused by a corrupted localStorage with no refresh token
// thus sending empty json
if (!req.body.refresh_token) {
res.status(400).json({
message: "No refresh token provided",
});
return;
}
// can be caused by a corrupted localStorage with no refresh token
// thus sending empty json
if (!req.body.refresh_token) {
res.status(400).json({
message: "No refresh token provided",
});
return;
}

const params = new URLSearchParams();
params.append("client_id", CLIENT_ID);
params.append("client_secret", CLIENT_TOKEN);
params.append("grant_type", "refresh_token");
params.append("refresh_token", req.body.refresh_token);
const params = new URLSearchParams();
params.append("client_id", CLIENT_ID);
params.append("client_secret", CLIENT_TOKEN);
params.append("grant_type", "refresh_token");
params.append("refresh_token", req.body.refresh_token);

fetch("https://discord.com/api/oauth2/token", {
method: "POST",
body: params,
})
.then((response) => {
return response.json();
})
.then((json) => {
res.send(json);
})
.catch((err) => {
res.status(400);
res.send(err);
})
.finally(() => {
res.end();
});
fetch("https://discord.com/api/oauth2/token", {
method: "POST",
body: params,
})
.then((response) => {
return response.json();
})
.then((json) => {
res.send(json);
})
.catch((err) => {
res.status(400);
res.send(err);
})
.finally(() => {
res.end();
});
});

router.get("/callback", (req, res) => {
if (!req.query.code) {
res.status(400).send("No code given");
return;
}
if (!req.query.code) {
res.status(400).send("No code given");
return;
}

const params = new URLSearchParams();
params.append("client_id", CLIENT_ID);
params.append("client_secret", CLIENT_TOKEN);
params.append("grant_type", "authorization_code");
params.append("code", req.query.code);
params.append("redirect_uri", REDIRECT_URI);
params.append("scope", "identify");
const params = new URLSearchParams();
params.append("client_id", CLIENT_ID);
params.append("client_secret", CLIENT_TOKEN);
params.append("grant_type", "authorization_code");
params.append("code", req.query.code);
params.append("redirect_uri", REDIRECT_URI);
params.append("scope", "identify");

fetch("https://discord.com/api/oauth2/token", {
method: "POST",
body: params,
})
.then((response) => response.json())
.then((json) => {
res.redirect(
`/?access_token=${encodeURIComponent(json.access_token)}&refresh_token=${encodeURIComponent(
json.refresh_token,
)}&expires_in=${encodeURIComponent(json.expires_in)}`,
);
})
.catch((err) => {
res.status(400);
res.send(err);
})
.finally(() => {
res.end();
});
fetch("https://discord.com/api/oauth2/token", {
method: "POST",
body: params,
})
.then((response) => response.json())
.then((json) => {
res.redirect(
`/?access_token=${encodeURIComponent(json.access_token)}&refresh_token=${encodeURIComponent(
json.refresh_token,
)}&expires_in=${encodeURIComponent(json.expires_in)}`,
);
})
.catch((err) => {
res.status(400);
res.send(err);
})
.finally(() => {
res.end();
});
});

module.exports = router;
158 changes: 79 additions & 79 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,91 @@ app.disable("x-powered-by");
const webappURL = "/";

process.on("unhandledRejection", (reason, promise) => {
console.error(reason);
console.trace(promise);
console.error(reason);
console.trace(promise);
});

app.use(
express.urlencoded({
extended: true,
limit: "50mb",
}),
express.urlencoded({
extended: true,
limit: "50mb",
}),
);
app.use(express.json({ limit: "50mb" }));

app.get(webappURL, async (req, res) => {
let file = fs.readFileSync("./index.html", "utf8");

const WINDOW_ENV = {
DISCORD_USER_URL: process.env["DISCORD_USER_URL"] || undefined,
};

file = file.replace(
"</head>",
` <script>\n` +
` window.apiURL = '${API_URL}'\n` +
` window.env = ${JSON.stringify(WINDOW_ENV)}\n` +
` window.DEV = ${DEV}\n` +
` </script>\n</head>`,
);

// change Vue to dev version for devtools
if (DEV) {
file = file.replace("/vue.min.js", "/vue.js");
file = file.replace("vuetify.min.js", "vuetify.js");
file = file.replace("/pinia.iife.min.js", "/pinia.iife.js");
}

if (DEV && process.env.BROWSER_REFRESH_URL) {
file = file.replace(
"</body>",
`<script src="${process.env.BROWSER_REFRESH_URL}"></script></body>`,
);
}

let langs = await getLanguages().catch(errorHandler(res));

file = file.replace(
"</body>",
"<script>const LANGUAGES = " + JSON.stringify(langs) + "</script></body>",
);

res.send(file);
let file = fs.readFileSync("./index.html", "utf8");

const WINDOW_ENV = {
DISCORD_USER_URL: process.env["DISCORD_USER_URL"] || undefined,
};

file = file.replace(
"</head>",
` <script>\n` +
` window.apiURL = '${API_URL}'\n` +
` window.env = ${JSON.stringify(WINDOW_ENV)}\n` +
` window.DEV = ${DEV}\n` +
` </script>\n</head>`,
);

// change Vue to dev version for devtools
if (DEV) {
file = file.replace("/vue.min.js", "/vue.js");
file = file.replace("vuetify.min.js", "vuetify.js");
file = file.replace("/pinia.iife.min.js", "/pinia.iife.js");
}

if (DEV && process.env.BROWSER_REFRESH_URL) {
file = file.replace(
"</body>",
`<script src="${process.env.BROWSER_REFRESH_URL}"></script></body>`,
);
}

let langs = await getLanguages().catch(errorHandler(res));

file = file.replace(
"</body>",
"<script>const LANGUAGES = " + JSON.stringify(langs) + "</script></body>",
);

res.send(file);
});

app.listen(port, () => {
console.log(`API at ${API_URL}`);
console.log(`Web App at http://localhost:${port}${webappURL}`);
console.log(`API at ${API_URL}`);
console.log(`Web App at http://localhost:${port}${webappURL}`);

if (DEV && process.send) {
process.send("online");
}
if (DEV && process.send) {
process.send("online");
}
});

// https://www.techonthenet.com/js/language_tags.php
const langPath = ["resources", "strings"];
const languagesPath = path.join(__dirname, ...langPath);
const getLanguages = function () {
return fs.promises.readdir(languagesPath).then((files) => {
const result = files
.filter((f) => f.endsWith("js"))
.map((e) => {
const name = e.split(".").slice(0, -1).join(".");
return {
lang: name.includes("en") ? "en" : name.slice(-2).toLowerCase(),
bcp47: name.replace("_", "-"),
file: ["", ...langPath, e].join("/"),
};
});

return result;
});
return fs.promises.readdir(languagesPath).then((files) => {
const result = files
.filter((f) => f.endsWith("js"))
.map((e) => {
const name = e.split(".").slice(0, -1).join(".");
return {
lang: name.includes("en") ? "en" : name.slice(-2).toLowerCase(),
bcp47: name.replace("_", "-"),
file: ["", ...langPath, e].join("/"),
};
});

return result;
});
};

app.use(
express.static(".", {
extensions: ["html", "xml", "json"],
}),
express.static(".", {
extensions: ["html", "xml", "json"],
}),
);
app.use("/api/discord", require("./api/discord"));

Expand All @@ -108,18 +108,18 @@ app.use("/api/discord", require("./api/discord"));
* @return {Function}
*/
const errorHandler = function (res) {
return (err) => {
// advance parsing for axios errors and custom codes errors
const code = (err.response ? err.response.status : err.code) || 400;
const message =
(err.response && err.response.data ? err.response.data.error : err.message) || err;

if (VERBOSE) {
console.error(code, message);
console.error(err.stack);
}
res.status(code);
res.send({ error: `${message}` });
res.end();
};
return (err) => {
// advance parsing for axios errors and custom codes errors
const code = (err.response ? err.response.status : err.code) || 400;
const message =
(err.response && err.response.data ? err.response.data.error : err.message) || err;

if (VERBOSE) {
console.error(code, message);
console.error(err.stack);
}
res.status(code);
res.send({ error: `${message}` });
res.end();
};
};
Loading

0 comments on commit 2b09d12

Please sign in to comment.