-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
343 lines (306 loc) · 11.6 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
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
// Political Engagement and Nucleation by Interpersonal Socialization (PENIS)
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
const serviceAccount = require('./politicalsentimentsocialmedia-912a5a03cd6b.json');
// db_functions.js
async function getUser_Other(user_id, other_id, otherName, dbName){
let user_post = await db.collection(dbName).where('user_id', '==', user_id).where(`${otherName}_id`, '==', other_id).get();
return user_post;
}
async function userCommentsRead(user_id, post_id){
let data = await db.collection("User_Post").where("user_id", "==", user_id).where("post_id", "==", post_id).get();
if(data.size == 0)
return 0;
else
return data.docs[0].get("comments_read");
}
async function userCommentsMade(user_id, topic_id){
let data = await db.collection("User_Topic").where("user_id", "==", user_id).where("topic_id", "==", topic_id).get();
if(data.size == 0)
return 0;
else
return data.docs[0].get("comments_made");
}
async function canUserComment(user_id, post_id){
return await userCommentsRead(user_id, post_id) >= 4;
}
async function canUserPost(user_id, topic_id){
return await userCommentsMade(user_id, topic_id) >= 4;
}
async function userRead(user_id, post_id){
const user_post_query = await db.collection("User_Post").where("user_id", "==", user_id).where("post_id", "==", post_id).get();
if(user_post_query.size == 0){
db.collection("User_Post").add({
user_id,
post_id,
comments_read: 1
});
return;
}
const user_post_doc = user_post_query.docs[0];
const user_doc = await db.collection("users").doc(user_id);
await user_doc.update({karma: user_doc.get("karma") + 1});
await db.collection("User_Post").doc(user_post_doc.id).update({comments_read: user_post_doc.get("comments_read") + 1});
}
async function userCommented(user_id, topic_id){
const user_topic_query = await db.collection("User_Topic").where("user_id", "==", user_id).where("topic_id", "==", topic_id).get();
if(user_topic_query.size == 0){
db.collection("User_Topic").add({
user_id,
topic_id,
comments_made: 0
});
return;
}
const user_topic_doc = user_topic_query.docs[0];
const user_doc = await db.collection("users").doc(user_id);
await user_doc.update({karma: user_doc.get("karma") + 2});
await db.collection("User_Topic").doc(user_topic_doc.id).update({comments_made: user_topic_doc.get("comments_made") + 1});
}
async function userPosted(user){
const cur_karma = user.get('karma')
return await user.update({karma: cur_karma + 4})
}
initializeApp({
credential: cert(serviceAccount)
});
const db = getFirestore();
const shuffle = require("./viewShuffler.js");
const bigml = require("./bigML.js");
const express = require("express");
const app = express();
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const verifyToken = require("./verifyToken.js");
require("dotenv/config");
const EMAIL_REGEX = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(require('cookie-parser')());
app.use("/static", express.static(`${__dirname}/public`));
app.set('view engine', 'pug');
app.get("/", (req,res) => {
res.render("index");
});
app.get("/login", verifyToken, (req,res) => {
res.render("login");
});
app.post("/login", async (req,res) => {
const { identifier, password } = req.body;
let users = await db.collection('users').where('email', '==', identifier).get();
if(!users.size)
users = await db.collection('users').where('username', '==', identifier).get();
if(!users.size)
return res.status(404).send("User Not Found :(");
const user = users.docs[0];
bcrypt.compare(password, user.get("password"), (err, same)=>{
if(err)
// redirect to error page
return res.status(500, err);
if(!same)
return res.status(400).send("Bad Password");
// Validate URL
jwt.sign({ username: user.get("username") }, process.env.KEY, function(err, token) {
if(err)
return res.status(500).send("token not created");
if(req.query.next)
return res.cookie("token", token).redirect(req.query.next);
return res.cookie("token", token).redirect(".");
});
});
});
app.get("/register", verifyToken, (req,res) => {
res.render("register");
});
app.post("/register", async (req,res)=>{
const { username, email, password } = req.body;
const users = await db.collection('users').where('username', '==', username).get()
if(users.size > 1){
res.send(500, "You're a dumbass. You broke everything. Now watch it burn!");
throw new Error("Two usernames same name");
}else if(users.size == 1){
return res.send(400, "User already exists");
}
await db.collection('users').add({
username: username,
email: email,
password: bcrypt.hashSync(password, 10),
verified: false,
total_read: 0,
karma: 0,
read_today: 0,
});
jwt.sign({username: username}, process.env.KEY, function(err, token) {
if(err){
console.error(err);
return res.status(500).send("jwt failed");
}
res.cookie("token", token);
if(req.query.next)
return res.redirect(req.query.next);
return res.redirect("/");
});
});
async function getTopics(){
return (await db.collection("Topics").get()).docs;
}
async function getTopic(topic_id){
if(!topic_id || topic_id == "")
return null;
return await db.collection("Topics").doc(topic_id).get();
}
async function getPost(post_id, topic_id){
return await db.collection("Topics").doc(topic_id).collection("Posts").doc(post_id).get();
}
async function getTopicPosts(topic_id){
return await db.collection("Topics").doc(topic_id).collection("Posts").get();
}
async function getPostComments(post_id, topic_id){
return await db.collection("Topics").doc(topic_id).collection("Posts").doc(post_id).collection("Comments").get();
}
app.get("/chat", verifyToken, async (req,res)=>{
if(req.anonymous)
return res.redirect(`/login?next=${req.originalUrl}`);
const username = req.JWTBody.username;
const temp = await db.collection("users").where("username", "==", username).get();
const user = temp.docs[0];
res.render("chat", {
topics: await getTopics(),
user_id: user.id,
user_karma: user.get("karma"),
});
});
app.get("/chat/:topic/", verifyToken, async (req,res)=>{
if(req.anonymous)
return res.redirect(`/login?next=${req.originalUrl}`);
if(!req.params.topic){
console.error("No topic");
return res.redirect("/chat");
}
const topic = await getTopic(req.params.topic);
if(!topic){ // 404
console.error("Topic Not Found");
return res.redirect("/");
}
const posts = await getTopicPosts(req.params.topic);
const username = req.JWTBody.username;
const users = await db.collection("users").where("username", "==", username).get();
const user = users.docs[0];
res.render("topic", {
topic,
posts: shuffle(posts.docs),
user_id: user.id,
comments_made: await userCommentsMade(user.id, req.params.topic) ?? 0
});
});
app.get("/chat/:topic/:post", verifyToken, async (req,res)=>{
if(req.anonymous)
return res.redirect(`/login?next=${req.originalUrl}`);
if(!req.params.topic){
console.error("No topic");
return res.redirect("/chat");
}
const topic = await getTopic(req.params.topic);
if(!topic){ // 404
console.error("Topic Not Found");
return res.redirect("/");
}
if(!req.params.post){
console.error("No post");
return res.redirect("/chat");
}
// Unsafe:
const user = (await db.collection("users").where("username", "==", req.JWTBody.username).get()).docs[0];
const post = await getPost(req.params.post, req.params.topic);
if(!post){ // 404
console.error("Post Not Found");
return res.redirect("/");
}
let comments = await getPostComments(req.params.post, req.params.topic);
let comments_read = await userCommentsRead(user.id, req.params.post);
res.render("post", {
topic,
post,
comments_read,
user_id: user.id,
comments: shuffle(comments?.docs)
});
});
app.post("/chat", verifyToken, async (req,res)=>{
if(req.anonymous)
return res.redirect(`/login?next=${req.originalUrl}`);
const {title, context} = req.body;
const username = req.JWTBody.username;
const users = await db.collection("users").where("username", "==", username).get();
const user = users.docs[0];
if(user.get("verified") || user.get("karma") >= 50){
db.collection("users").doc(user.id).update({karma: user.get("karma") - 50});
db.collection("Topics").add({
author: username,
date: new Date().toISOString(),
title,
context,
});
res.redirect(req.originalUrl);
}
});
app.post("/chat/:topic", verifyToken, async (req,res)=>{
const {title, content} = req.body;
if(req.anonymous)
return res.redirect(`/login/?next=${req.originalUrl}`);
const username = req.JWTBody.username;
const users = await db.collection("users").where("username", "==", username).get();
const user = users.docs[0];
if(canUserPost(user.id, req.params.topic)){
db.collection("Topics").doc(req.params.topic).collection("Posts").add({
author: username,
date: new Date().toISOString(),
title,
content,
politicization_index: await bigml(content)
});
res.redirect(req.originalUrl);
}
});
app.post("/chat/:topic/:post", verifyToken, async (req,res)=>{
const {content} = req.body;
if(req.anonymous)
return res.redirect(`/login/?next=${req.originalUrl}`);
const username = req.JWTBody.username;
const users = await db.collection("users").where("username", "==", username).get();
const user = users.docs[0];
if(canUserComment(user.id, req.params.post)){
await db.collection("Topics").doc(req.params.topic).collection("Posts").doc(req.params.post).collection("Comments").add({
author: username,
date: new Date().toISOString(),
content,
politicization_index: await bigml(content)
});
userCommented(user.id, req.params.topic);
res.redirect(req.originalUrl);
}
});
app.get("/karma_please", verifyToken, async (req,res)=>{
if(req.anonymous){
return res.redirect("/login");
}
const username = req.JWTBody.username;
const users = await db.collection("users").where("username", "==", username).get();
const user = users.docs[0];
await db.collection("users").doc(user.id).update({karma: user.get("karma") + 500});
res.send("The karma has been added to your account!");
});
app.get("/rules", verifyToken, (req, res)=>{
res.render("rules");
});
app.get("/about_us", verifyToken, (req, res)=>{
res.render("about_us");
});
// API stuff
app.post("/increaseUserCommentsRead", async (req,res)=>{
const {user_id, post_id} = req.body;
await userRead(user_id, post_id);
res.send("Comment marked as read if no error");
})
const PORT = process.env.PORT || 80;
app.listen(PORT);