-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (131 loc) · 4.88 KB
/
index.js
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
const express = require("express");
const path = require("path");
const sqlite = require("sqlite3");
const bodyParser = require("body-parser");
const app = express();
const port = parseInt(process.env.PORT) || process.argv[3] || 3000;
const db = new sqlite.Database('./db.db', (err) => {
if (err) {
console.error(err.message);
}
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
db.run(`CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL
)`, (err) => {
if (err) {
console.error(err.message);
}
});
db.run(`CREATE TABLE IF NOT EXISTS topics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
author TEXT NOT NULL,
category INTEGER NOT NULL,
pinned BOOLEAN
)`, (err) => {
if (err) {
console.error(err.message);
}
});
db.run(`CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author TEXT NOT NULL,
content TEXT,
topic INTEGER NOT NULL
)`, (err) => {
if (err) {
console.error(err.message);
}
});
// db.run("INSERT INTO categories (name, description) VALUES ('Announcements', 'Announcements from the Hatch Team.')", (err) => { if (err) { console.error(err.message); } });
// db.run("INSERT INTO categories (name, description) VALUES ('Suggestions', 'Suggestions for feature additions to Hatch.')", (err) => { if (err) { console.error(err.message); } });
// db.run("INSERT INTO categories (name, description) VALUES ('Questions about Hatch', 'General questions about Hatch.')", (err) => { if (err) { console.error(err.message); } });
// db.run("INSERT INTO categories (name, description) VALUES ('Project Help', 'Need help with a project? Ask for help here.')", (err) => { if (err) { console.error(err.message); } });
// db.run("INSERT INTO categories (name, description) VALUES ('Bug Reports', 'Report bugs found on Hatch here.')", (err) => { if (err) { console.error(err.message); } });
// db.run("INSERT INTO categories (name, description) VALUES ('Show and Tell', 'Show off your creations here!')", (err) => { if (err) { console.error(err.message); } });
app.use(express.static(path.join(__dirname, "public")))
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/category/:category", (req, res) => {
db.get("SELECT * FROM categories WHERE id = ?", [req.params.category], (err, row) => {
db.all("SELECT * FROM topics WHERE category = ?", [req.params.category], (err, topics) => {
res.render("category", {
category: row,
topics: topics
});
});
});
});
app.get("/category/:category/new", (req, res) => {
db.get("SELECT * FROM categories WHERE id = ?", [req.params.category], (err, row) => {
res.render("new_topic", {
category: row
});
});
});
app.get("/topic/:topic", (req, res) => {
db.get("SELECT * FROM topics WHERE id = ?", [req.params.topic], (err, row) => {
db.all("SELECT * FROM posts WHERE topic = ?", [req.params.topic], (err, posts) => {
res.render("topic", {
topic: row,
posts: posts
});
});
});
});
app.post("/api/new/topic", (req, res) => {
fetch("https://api.hatch.lol/auth/me", {
headers: {
"Token": req.header("Token")
}
}).then(fres => {
if (req.body.content.length > 8000 || req.body.content.trim().length < 1 || req.body.title.length > 100 || req.body.title.trim().length < 1) {
res.sendStatus(400);
return;
}
res.sendStatus(fres.status);
if (fres.status === 200) {
fres.json().then(data => {
if (req.body.category === 1 && !data.hatchTeam) {
return;
}
db.get("SELECT COUNT(*) FROM topics", (err, count) => {
if (err) { console.error(err.message); }
db.run("INSERT INTO topics (name, author, category) VALUES (?, ?, ?)", [req.body.title, data.name, req.body.category], (err) => { if (err) { console.error(err.message); } });
db.run("INSERT INTO posts (author, content, topic) VALUES (?, ?, ?)", [data.name, req.body.content, count["COUNT(*)"]+1], (err) => { if (err) { console.error(err.message); } });
});
});
}
})
});
app.post("/api/new/post", (req, res) => {
fetch("https://api.hatch.lol/auth/me", {
headers: {
"Token": req.header("Token")
}
}).then(fres => {
if (req.body.content.length > 8000 || req.body.content.length < 1) {
res.sendStatus(400);
return;
}
res.sendStatus(fres.status);
if (fres.status === 200) {
fres.json().then(data => {
db.run("INSERT INTO posts (author, content, topic) VALUES (?, ?, ?)", [data.name, req.body.content, req.body.topic], (err) => { if (err) { console.error(err.message); } });
});
}
})
});
app.listen(port, () => {
console.log(`listening on http://localhost:${port}`);
})