-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
348 lines (331 loc) · 18.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import fs from "node:fs";
import cp from "node:child_process";
import dotenv from "dotenv";
import express from "express";
import { JSDOM } from "jsdom";
import * as dateFns from "date-fns";
import marked from "marked";
import { WebhookClient } from "discord.js";
import * as githubWebhooks from "@octokit/webhooks";
import generateErrorPage from "./modules/generate_error_page";
import extractJSONAndHTML from "./modules/extract_json_and_html";
import extractYAMLAndMD from "./modules/extract_yaml_and_md";
import unicodeEscape from "./modules/unicode_escape";
import toRFC822 from "./modules/date_toRFC822";
dotenv.config();
const redirects = JSON.parse(fs.readFileSync("./redirects.json", "utf-8"));
const html = fs.readFileSync("./public/page.html", "utf-8");
interface blogInfo {
title: string;
date: string;
categories: string[];
tags: string[];
coverImage: string | undefined;
}
interface UserCollection {
[key: string]: {
userId: string;
reason: string;
};
}
const PORT = process.env.PORT ?? 3000;
const server = express();
const webhooks = new githubWebhooks.Webhooks({
secret: process.env.GITHUB_WEBHOOK_SECRET ?? ""
});
const discordWebhookClient = new WebhookClient({ "id": process.env.DISCORD_WEBHOOK_ID ?? "", "token": process.env.DISCORD_WEBHOOK_TOKEN ?? "" });
const kanaUrl = process.env.KANA_URL ?? "https://kana.renorari.net";
let nrUsers: UserCollection = {};
let nrGuilds: UserCollection = {};
let ugcMutedUsers: UserCollection = {};
let ugcMutedGuilds: UserCollection = {};
let takasumibotMuted: UserCollection = {};
async function blockedUserCollectionUpdate() {
fetch(`${kanaUrl}/api/v2/discord/nr_users`).then((response) => {
response.ok && response.json().then((json) => {
nrUsers = json;
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
fetch(`${kanaUrl}/api/v2/discord/nr_guilds`).then((response) => {
response.ok && response.json().then((json) => {
nrGuilds = json;
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
fetch(`${kanaUrl}/api/v2/discord/muted_users`).then((response) => {
response.ok && response.json().then((json) => {
ugcMutedUsers = json;
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
fetch(`${kanaUrl}/api/v2/discord/muted_guilds`).then((response) => {
response.ok && response.json().then((json) => {
ugcMutedGuilds = json;
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
fetch("https://api.takasumibot.com/v1/mute_user").then((response) => {
response.ok && response.json().then((json: { success: boolean; message: string | null; data: { id: string; reason: string; time: string; }[]; }) => {
takasumibotMuted = {};
json.data.forEach((user) => {
takasumibotMuted[user.id] = { userId: user.id, reason: user.reason };
});
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
}
blockedUserCollectionUpdate();
setInterval(blockedUserCollectionUpdate, 1000 * 60);
webhooks.on("push", async ({ payload }) => {
if (payload.ref === "refs/heads/main") {
cp.execSync("git pull origin main", { cwd: __dirname });
if (payload.commits.map(commit => commit.modified).flat().filter((path: string) => path.startsWith("blog/")).length > 0) {
const file = payload.commits.map(commit => commit.modified).flat().filter((path: string) => path.startsWith("blog/"))[0].split("/")[1];
const content = fs.readFileSync("./blog/" + file + "/index.md", "utf-8");
const extracted = extractYAMLAndMD(content);
const info = extracted.yaml as blogInfo;
const contentMd = extracted.md;
const contentHtml = marked.parse(contentMd);
const document = new JSDOM(contentHtml);
const description = document.window.document.body.textContent?.replace(/\r\n|\r|\n/g, "").replace(/ /g, "").slice(0, 200) ?? "";
const imageDir = file;
const image = info.coverImage ? `https://renorari.net/blog/${imageDir}/images/${info.coverImage}` : contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "") ? `https://renorari.net/blog/${imageDir}/${contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "").replace("./", "")}` : "https://renorari.net/images/ogp.png";
discordWebhookClient.send({
content: `<@&1089541200958197820> ブログが更新されました!\n# [${info.title}](https://renorari.net/blog/${file}/)`,
embeds: [{
title: info.title,
description: description + "...",
url: "https://renorari.net/blog/" + file + "/",
image: {
url: image
}
}]
});
}
if (payload.commits.map(commit => commit.message).join("\n").match(/!rs/)) {
cp.execSync("npm install", { cwd: __dirname });
process.exit();
}
}
});
server.use(githubWebhooks.createNodeMiddleware(webhooks));
server.use(express.static("public"));
server.get("/blog/", (req, res) => {
const files = fs.readdirSync("./blog").filter((file) => fs.statSync("./blog/" + file).isDirectory())
.sort((a, b) => {
const contentA = fs.readFileSync("./blog/" + a + "/index.md", "utf-8");
const extractedA = extractYAMLAndMD(contentA);
const infoA = extractedA.yaml as blogInfo;
const dateA = new Date(infoA.date);
const contentB = fs.readFileSync("./blog/" + b + "/index.md", "utf-8");
const extractedB = extractYAMLAndMD(contentB);
const infoB = extractedB.yaml as blogInfo;
const dateB = new Date(infoB.date);
return dateA > dateB ? -1 : 1;
}).map((file) => {
const content = fs.readFileSync("./blog/" + file + "/index.md", "utf-8");
const extracted = extractYAMLAndMD(content);
const info = extracted.yaml as blogInfo;
const contentMd = extracted.md;
const contentHtml = marked.parse(contentMd);
const document = new JSDOM(contentHtml);
const description = document.window.document.body.textContent?.replace(/\r\n|\r|\n/g, "").replace(/ /g, "").slice(0, 200) ?? "";
const imageDir = file;
const image = info.coverImage ? `/blog/${imageDir}/images/${info.coverImage}` : contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "") ? `/blog/${imageDir}/${contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "").replace("./", "")}` : "https://renorari.net/images/ogp.png";
return `<a href="/blog/${file}/" class="card"><div class="card-image"><img src="${image}" alt="ogp image"></div><div class="card-content"><div class="card-title">${info.title}</div><div class="card-description">${description}...</div></div></a>`;
});
res.send(html.replace(/{{content}}/g, `<main><div class="panel">${files.join("")}</main>`).replace(/{{title-full}}/g, "{{title}}").replace(/{{title}}/g, "ブログ").replace(/{{description}}/g, "ブログの一覧です。").replace(/{{path}}/g, "/blog/").replace(/{{ogp_image}}/g, "https://renorari.net/images/ogp.png"));
});
server.get("/blog/*", (req, res) => {
const requestPath = decodeURI(req.url.endsWith("/") ? req.url + "index.md" : req.url).replace(/\.\./g, "");
const contentPath = requestPath.replace("/blog/", "./blog/");
if (!contentPath.endsWith(".md")) {
fs.readFile(contentPath, (error, content) => {
if (error) {
switch (error.code) {
case "ENOENT":
res.send(generateErrorPage(404));
break;
default:
res.send(generateErrorPage(500));
break;
}
} else {
res.type(express.static.mime.lookup(contentPath));
res.send(content);
}
});
return;
}
fs.readFile(contentPath, "utf-8", (error, content) => {
if (error) {
switch (error.code) {
case "ENOENT":
res.send(generateErrorPage(404));
break;
default:
res.send(generateErrorPage(500));
break;
}
} else {
const extracted = extractYAMLAndMD(content);
const info = extracted.yaml as blogInfo;
const contentMd = extracted.md;
const dateText = new Date(info.date).toLocaleDateString("ja-JP", { year: "numeric", month: "long", day: "numeric" });
const contentHtml = `<h1 class="blog-title">${info.title}</h1>\n<div class="update-date">最終更新: ${dateText}</div>` + marked.parse(contentMd);
const document = new JSDOM(contentHtml);
const description = document.window.document.body.textContent?.replace(/\r\n|\r|\n/g, "").replace(/ /g, "").slice(0, 100) ?? "";
const tags = info.tags.join(", ") + ", " + info.categories.join(", ");
const imageDir = requestPath.replace("/blog/", "").split("/").slice(0, -1).join("/");
const image = info.coverImage ? `https://renorari.net/blog/${imageDir}/images/${info.coverImage}` : contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "") ? `https://renorari.net/blog/${imageDir}/${contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "").replace("./", "")}` : "https://renorari.net/images/ogp.png";
res.send(html.replace(/{{content}}/g, `<main>${contentHtml}</main>`).replace(/{{title}}/g, "ブログ").replace(/{{title-full}}/g, info.title).replace(/{{description}}/g, description + "...").replace(/{{path}}/g, requestPath.replace("index.md", "")).replace(/{{ogp_image}}/g, image).replace(/{{tags}}/g, tags));
}
});
});
server.get("/api/block-checker/:id", (req, res) => {
const id = req.params.id;
const kana = Object.keys(nrUsers).includes(id) || Object.keys(nrGuilds).includes(id);
const ugc = Object.keys(ugcMutedUsers).includes(id) || Object.keys(ugcMutedGuilds).includes(id);
const takasumibot = Object.keys(takasumibotMuted).includes(id);
res.json({
kana, ugc, takasumibot,
kana_is_removable: Object.keys(nrUsers).includes(id) ? (nrUsers[id].reason.includes("荒らし関連のユーザー") ? true : false) : null
});
});
server.get("*", (req, res, next) => {
const short = redirects.short.filter((redirect: { from: string; to: string; }) => redirect.from == req.hostname + req.url)[0];
if (short) {
res.redirect(301, short.to);
} else {
next();
}
});
server.get("/sitemap.xml", (req, res) => {
const getFiles = (dir: string): string[] => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = dirents.map((dirent) => {
const res = dir + "/" + dirent.name;
return dirent.isDirectory() ? getFiles(res) : res;
});
return Array.prototype.concat(...files);
};
const files = getFiles("./views");
const urls = files.filter((file) => file.endsWith(".html")).map((file) => {
const url = file.replace("./views", "").replace("index.html", "");
const lastmod = dateFns.format(fs.statSync(file).mtime, "yyyy-MM-dd");
const priority = Math.max(1 - (url.split("/").length - 2) * 0.1, 0.5);
return `<url><loc>https://renorari.net${encodeURI(url)}</loc><lastmod>${lastmod}</lastmod><priority>${priority}</priority></url>`;
});
const blogUrls = fs.readdirSync("./blog").filter((file) => fs.statSync("./blog/" + file).isDirectory())
.map((file) => {
const content = fs.readFileSync("./blog/" + file + "/index.md", "utf-8");
const info = extractYAMLAndMD(content).yaml as blogInfo;
const url = "/blog/" + file + "/";
const lastmod = info.date;
return `<url><loc>https://renorari.net${encodeURI(url)}</loc><lastmod>${lastmod}</lastmod><priority>0.8</priority></url>`;
});
const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join("")}${blogUrls.join()}</urlset>`;
res.header("Content-Type", "text/xml");
res.send(xml);
});
server.get("/rss.xml", (req, res) => {
const getFiles = (dir: string): string[] => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = dirents.map((dirent) => {
const res = dir + "/" + dirent.name;
return dirent.isDirectory() ? getFiles(res) : res;
});
return Array.prototype.concat(...files);
};
const files = getFiles("./views");
const items = files.filter((file) => file.endsWith(".html")).map((file) => {
const content = fs.readFileSync(file, "utf-8");
const extracted = extractJSONAndHTML(content);
const url = file.replace("./views", "").replace("index.html", "");
const lastmod = toRFC822(fs.statSync(file).mtime);
const title = unicodeEscape(extracted.json.title ?? "");
return `<item><title>${title}</title><link>https://renorari.net${encodeURI(url)}</link><guid>https://renorari.net${encodeURI(url)}</guid><pubDate>${lastmod}</pubDate></item>`;
});
const blogItems = fs.readdirSync("./blog").filter((file) => fs.statSync("./blog/" + file).isDirectory())
.map((file) => {
const content = fs.readFileSync("./blog/" + file + "/index.md", "utf-8");
const info = extractYAMLAndMD(content).yaml as blogInfo;
const url = "/blog/" + file + "/";
const lastmod = toRFC822(new Date(info.date));
const title = unicodeEscape(info.title);
return `<item><title>${title}</title><link>https://renorari.net${encodeURI(url)}</link><guid>https://renorari.net${encodeURI(url)}</guid><pubDate>${lastmod}</pubDate></item>`;
});
const xml = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>renorari.net</title><link>https://renorari.net/</link><description>renorari.net</description><lastBuildDate>${toRFC822(new Date())}</lastBuildDate><atom:link href="https://renorari.net/rss.xml" rel="self" type="application/rss+xml" />${items.join("")}${blogItems.join("")}</channel></rss>`;
res.header("Content-Type", "text/xml");
res.send(xml);
});
server.get("/*", (req, res) => {
let requestPath = decodeURI(req.url.endsWith("/") ? req.url + "index.html" : req.url).replace(/\.\./g, "").split("?")[0];
const redirect = redirects.locate.filter((redirect: { from: string; to: string; }) => redirect.from == requestPath)[0];
(redirect) && (requestPath = redirect.to);
const contentPath = requestPath.replace("/", "./views/");
fs.readFile(contentPath, "utf-8", (error, content) => {
if (error) {
switch (error.code) {
case "ENOENT":
res.send(generateErrorPage(404));
break;
default:
res.send(generateErrorPage(500));
break;
}
} else {
const extracted = extractJSONAndHTML(content);
const info = extracted.json;
let contentHtml = extracted.html;
const document = new JSDOM(contentHtml);
const description = Object.keys(info).includes("description") ? info.description : (document.window.document.body.textContent?.replace(/\r\n|\r|\n/g, "").replace(/ /g, "").slice(0, 100) ?? "") + "...";
if (content.match(/{{blog_posts}}/) !== null) {
//最近のものを4つ表示
const blogPosts = fs.readdirSync("./blog").filter((file) => fs.statSync("./blog/" + file).isDirectory())
.sort((a, b) => {
const contentA = fs.readFileSync("./blog/" + a + "/index.md", "utf-8");
const extractedA = extractYAMLAndMD(contentA);
const infoA = extractedA.yaml as blogInfo;
const dateA = new Date(infoA.date);
const contentB = fs.readFileSync("./blog/" + b + "/index.md", "utf-8");
const extractedB = extractYAMLAndMD(contentB);
const infoB = extractedB.yaml as blogInfo;
const dateB = new Date(infoB.date);
return dateA > dateB ? -1 : 1;
}).slice(0, 4).map((file) => {
const content = fs.readFileSync("./blog/" + file + "/index.md", "utf-8");
const extracted = extractYAMLAndMD(content);
const info = extracted.yaml as blogInfo;
const contentMd = extracted.md;
const contentHtml = marked.parse(contentMd);
const document = new JSDOM(contentHtml);
const description = document.window.document.body.textContent?.replace(/\r\n|\r|\n/g, "").replace(/ /g, "").slice(0, 200) ?? "";
const image = contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "") ? `/blog/${file}/${contentHtml.match(/<img.*?>/)?.[0].match(/src=".*?"/)?.[0].replace(/src="|"/g, "").replace("./", "")}` : "https://renorari.net/images/ogp.png";
return `<a href="/blog/${file}/" class="card"><div class="card-image"><img src="${image}" alt="ogp image"></div><div class="card-content"><div class="card-title">${info.title}</div><div class="card-description">${description}...</div></div></a>`;
});
contentHtml = contentHtml.replace(/{{blog_posts}}/g, blogPosts.join(""));
}
res.send(html.replace(/{{content}}/g, contentHtml).replace(/{{title-full}}/g, "{{title}}").replace(/{{title}}/g, info.title).replace(/{{description}}/g, description).replace(/{{path}}/g, requestPath.replace("index.html", "")).replace(/{{ogp_image}}/g, info.ogp_image).replace(/{{tags}}/g, ""));
}
});
});
server.listen(PORT, () => {
console.log("Server is started on http://127.0.0.1:" + PORT);
});