This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
forked from TheDailyGobo/The-Daily-Gobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
714 lines (654 loc) · 24.9 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// database
const Database = require("@replit/database")
const db = new Database()
//const allowedToWrite = ['NFlex23', 'rgantzos', 'MaterArc', 'AugieDoggie2011', 'jboys846', 'relativity-', '2022dev19', 'TheAnvils','UnofficialSDS','Virtual-Insanity','kittiemasters', 'popjam12', 'WhatijStudios', 'BendyOl183', 'Blueper_Scratch', 'kawaiinessdog', 'elip100', 'coolcreator200_alt', 'ST36_Programmer', 'BlixerEvolution', 'sinktheshot', 'Taurus7d', 'Marie_Chibi', ' IloveSpeedy6', '-lxvelydaises-', '-FunnyToons-', 'ZeldaFan_78', 'diethon', 'RoseReef', 'ZeldaFan_78']
//const allowedToMod = ['rgantzos', 'MaterArc', 'TheAnvils', 'kittiemasters', 'AugieDoggie2011', 'WhatijStudios','-FunnyToons-', 'jask82006', 'RoseReef']
const headMembers = ['MaterArc', 'rgantzos','-RabbitWorld-', 'popjam12']
const developers = ['rgantzos', 'MaterArc', 'TheAnvils', 'applejuiceproduc']
const banned = ['']
const postsPerWeekForActive = 3
async function addMoreLikes() {
var posts = await db.get("posts")
posts["271"].loves = ['NFlex23', 'rgantzos', 'MaterArc', 'AugieDoggie2011', 'jboys846', 'relativity-', '2022dev19', 'TheAnvils','UnofficialSDS','Virtual-Insanity','kittiemasters', 'popjam12', 'WhatijStudios', 'BendyOl183', 'Blueper_Scratch', 'kawaiinessdog', 'elip100', 'coolcreator200_alt', 'ST36_Programmer', 'BlixerEvolution', 'sinktheshot', 'Taurus7d', 'Marie_Chibi', ' IloveSpeedy6', '-lxvelydaises-', '-FunnyToons-', 'ZeldaFan_78', 'diethon', 'RoseReef', 'ZeldaFan_78']
posts["271"].favorites = ['NFlex23', 'rgantzos', 'MaterArc', 'AugieDoggie2011', 'jboys846', 'relativity-', '2022dev19', 'TheAnvils','UnofficialSDS','Virtual-Insanity','kittiemasters', 'popjam12', 'WhatijStudios', 'BendyOl183', 'Blueper_Scratch', 'kawaiinessdog', 'elip100', 'coolcreator200_alt', 'ST36_Programmer', 'BlixerEvolution', 'sinktheshot', 'Taurus7d', 'Marie_Chibi', ' IloveSpeedy6', '-lxvelydaises-', '-FunnyToons-', 'ZeldaFan_78', 'diethon', 'RoseReef', 'ZeldaFan_78']
await db.set("posts", posts)
}
//addMoreLikes()
async function testMessages() {
var messages = await db.get("messages")
console.log(messages)
}
testMessages()
async function banTheBanned() {
var tokens = await db.get("tokens")
Object.keys(tokens).forEach(function(el) {
if (banned.includes(tokens[el])) {
tokens[el] = undefined
}
})
await db.set("tokens", tokens)
}
banTheBanned()
// writing files
const fs = require("fs");
//db.set("tokens", {})
// discord webhook
const { WebhookClient, EmbedBuilder } = require('discord.js')
const client = new WebhookClient({url:process.env.webhook})
const deletePostClient = new WebhookClient({url:process.env.deletePost})
// keeping online
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
function getCookie(name, req) {
const value = `; ${req.headers.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
function getUserData(username, posts, allowedToWrite, allowedToMod) {
var postsThisWeek = 0
var totalLoves = 0
var totalFavorites = 0
var totalPosts = 0
Object.keys(posts).forEach(function(el) {
if (posts[el].author === username) {
if (!posts[el].deleted) {
totalPosts = totalPosts+1
totalLoves = totalLoves+posts[el].loves.length
totalFavorites = totalFavorites+posts[el].favorites.length
var then = new Date(posts[el].time);
var now = new Date();
var msBetweenDates = Math.abs(then.getTime() - now.getTime());
// 👇️ convert ms to days hour min sec ms
var daysBetweenDates = msBetweenDates / (24 * 60 * 60 * 1000);
if (daysBetweenDates < 7) {
postsThisWeek = postsThisWeek+1
}
}
}
})
return {"username":username,"writer":(allowedToWrite.includes(username)),"moderator":(allowedToMod.includes(username)),"active":(postsThisWeek > (postsPerWeekForActive-1)),"developer":(developers.includes(username)),"admin":(headMembers.includes(username)),"stats":{"loves":totalLoves,"favorites":totalFavorites,"posts":totalPosts}}
}
async function getFeatured() {
var posts = await db.get("posts")
var currentLargest = {"loves":-1,"favorites":-1}
var idOfCurrentLargest = '-1'
Object.keys(posts).forEach(function(el) {
if (!posts[el].deleted) {
var then = new Date(posts[el].time);
var now = new Date();
var msBetweenDates = Math.abs(then.getTime() - now.getTime());
// 👇️ convert ms to days hour min sec ms
var daysBetweenDates = msBetweenDates / (24 * 60 * 60 * 1000);
if (daysBetweenDates < 1) {
if (!((currentLargest.loves+(currentLargest.favorites*2)) < (posts[el].loves+(posts[el].favorites*2)))) {
currentLargest = posts[el]
idOfCurrentLargest = el
}
}
}
})
currentLargest.id = idOfCurrentLargest
return currentLargest
}
async function writePost(post, author) {
//await db.set("posts", {})
var posts = await db.get("posts")
var postId = Object.keys(posts).length.toString()
posts[postId] = {"time":Date.now(), "author":author, "loves":[], "favorites":[]}
db.set("posts", posts)
var writeStream = fs.createWriteStream("posts/post"+postId+".md");
writeStream.write(post);
writeStream.end();
var str = post
if(str.length > 10) str = str.substring(0,4096);
var embed = new EmbedBuilder()
.setTitle(`New post by ${author}`)
.setColor('Random')
.setDescription(str)
.setTimestamp()
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+author)
var data = await response.json()
embed.setAuthor({name:author, iconURL:data.profile.images['90x90'], url:"https://scratch.mit.edu/users/"+author+"/"})
client.send({
username: 'The Daily Gobo',
avatarURL: 'https://cdn.discordapp.com/icons/1026288031537303563/a1e8c8448b80ea612a13dd238e4609a4.webp?size=240',
embeds: [embed],
});
return postId
}
// everything else
const express = require('express')
const request = require('request');
const linkify = require("linkifyjs");
const linkifyHtml = require("linkify-html");
const { crypto } = require('crypto')
const options = { defaultProtocol: "https", formatHref: {
mention: (href) => "https://postit.gantzos.com/users" + href,
} };
require("linkify-plugin-mention")
const path = require('path');
const showdown = require('showdown')
const DOMPurify = require('isomorphic-dompurify');
const cloudinary = require('cloudinary')
const jsonfile = require('jsonfile');
var bodyParser = require('body-parser')
const busboy = require('busboy');
var cors = require('cors')
var jsonParser = bodyParser.json();
const cookieParser = require('cookie-parser');
function parseCookies(request) {
const list = {};
const cookieHeader = request.headers?.cookie;
if (!cookieHeader) return list;
cookieHeader.split(`;`).forEach(function(cookie) {
let [name, ...rest] = cookie.split(`=`);
name = name?.trim();
if (!name) return;
const value = rest.join(`=`).trim();
if (!value) return;
list[name] = decodeURIComponent(value);
});
return list;
}
app = express();
const PORT = 3000;
app.get('/messages/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
var messages = await db.get("messages")
if (messages[user]) {
res.send(messages[user])
} else {
res.send([])
}
}
})
app.get('/writers/', async function(req, res) {
var writers = await db.get("allowedToWrite")
res.send(writers)
})
app.get('/clear/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
var messages = await db.get("messages")
if (messages[user]) {
messages[user] = []
await db.set("messages", messages)
res.sendStatus(200)
}
}
})
app.get('/js/head-member.js', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
if (headMembers.includes(tokens[getCookie('usertoken', req)])) {
res.sendFile(path.join(__dirname, '/js/head-member.js'));
} else {
res.send("")
}
} else {
res.send("")
}
})
app.use(cookieParser())
app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({extend:true}));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/featured/', async function(req, res) {
var featured = await getFeatured()
res.send(featured)
})
app.get('/writer/:user/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
if (headMembers.includes(tokens[getCookie('usertoken', req)])) {
var mods = await db.get("allowedToWrite")
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+req.params.user)
var data = await response.json()
if (data.username) {
if (mods.includes(data.username)) {
var newMods = []
mods.forEach(function(el) {
if (el !== data.username) {
newMods.push(el)
}
})
await db.set("allowedToWrite", newMods)
res.send({"status":"removed writer"})
} else {
mods.push(data.username)
await db.set("allowedToWrite", mods)
res.send({"status":"gave writer"})
}
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/mod/:user/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
if (headMembers.includes(tokens[getCookie('usertoken', req)])) {
var mods = await db.get("allowedToMod")
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+req.params.user)
var data = await response.json()
if (data.username) {
if (mods.includes(data.username)) {
var newMods = []
mods.forEach(function(el) {
if (el !== data.username) {
newMods.push(el)
}
})
await db.set("allowedToMod", newMods)
res.send({"status":"removed mod"})
} else {
mods.push(data.username)
await db.set("allowedToMod", mods)
res.send({"status":"gave mod"})
}
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/uncensor/:post/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
if (headMembers.includes(tokens[getCookie('usertoken', req)])) {
var posts = await db.get("posts")
if (posts[req.params.post]) {
posts[req.params.post].deleted = undefined
await db.set("posts", posts)
res.send({"success":true})
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/users/:username/', async function(req, res) {
const allowedToWrite = await db.get("allowedToWrite")
const allowedToMod = await db.get("allowedToMod")
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+req.params.username)
if (response.ok) {
var data = await response.json()
if (!data.error) {
var username = data.username
var postsThisWeek = 0
var totalLoves = 0
var totalFavorites = 0
var totalPosts = 0
var posts = await db.get("posts")
Object.keys(posts).forEach(function(el) {
if (posts[el].author === username) {
if (!posts[el].deleted) {
totalPosts = totalPosts+1
totalLoves = totalLoves+posts[el].loves.length
totalFavorites = totalFavorites+posts[el].favorites.length
var then = new Date(posts[el].time);
var now = new Date();
var msBetweenDates = Math.abs(then.getTime() - now.getTime());
// 👇️ convert ms to days hour min sec ms
var daysBetweenDates = msBetweenDates / (24 * 60 * 60 * 1000);
if (daysBetweenDates < 7) {
postsThisWeek = postsThisWeek+1
}
}
}
})
res.send({"username":username,"writer":(allowedToWrite.includes(username)),"moderator":(allowedToMod.includes(username)),"active":(postsThisWeek > (postsPerWeekForActive-1)),"developer":(developers.includes(username)),"admin":(headMembers.includes(username)),"stats":{"loves":totalLoves,"favorites":totalFavorites,"posts":totalPosts}})
}
}
})
app.get('/post/:post/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var posts = await db.get("posts")
if (posts[req.params.post]) {
res.render(__dirname + "/html/post.html", {username:tokens[getCookie('usertoken', req)],post:req.params.post})
} else {
res.sendStatus(400)
}
} else {
res.redirect("https://thedailygobo.scratchtools.app/verify/")
}
})
app.get('/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
res.redirect("https://thedailygobo.scratchtools.app/feed/")
} else {
res.sendFile(path.join(__dirname, '/html/index.html'));
}
})
app.get('/logout/', function(req, res) {
res.cookie('usertoken','abc', { maxAge: 0, httpOnly: true });
res.send("ok")
})
app.get('/favorite/:post/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
var posts = await db.get("posts")
if (posts[req.params.post]) {
if (posts[req.params.post].favorites.includes(user)) {
var newFavorites = []
posts[req.params.post].favorites.forEach(function(el) {
if (el !== user) {
newFavorites.push(el)
}
})
posts[req.params.post].favorites = newFavorites
await db.set("posts", posts)
res.send(posts[req.params.post].favorites)
} else {
posts[req.params.post].favorites.push(user)
await db.set("posts", posts)
res.send(posts[req.params.post].favorites)
}
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(400)
}
})
app.get('/love/:post/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
var posts = await db.get("posts")
if (posts[req.params.post]) {
if (posts[req.params.post].loves.includes(user)) {
var newLoves = []
posts[req.params.post].loves.forEach(function(el) {
if (el !== user) {
newLoves.push(el)
}
})
posts[req.params.post].loves = newLoves
await db.set("posts", posts)
res.send(posts[req.params.post].loves)
} else {
posts[req.params.post].loves.push(user)
await db.set("posts", posts)
res.send(posts[req.params.post].loves)
}
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(400)
}
})
app.get('/users/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (headMembers.includes(user)) {
var users = []
Object.keys(tokens).forEach(function(el) {
if (!users.includes(tokens[el])) {
users.push(tokens[el])
}
})
res.send({"count":users.length, "users":users})
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.post('/warn/', jsonParser, async function(req, res) {
const messages = await db.get("messages")
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (headMembers.includes(user)) {
if (req.body.user && req.body.warning) {
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+req.body.user)
var data = await response.json()
if (data.username) {
var username = data.username
if (messages[username]) {
messages[username].push(`New admin message: `+req.body.warning)
} else {
messages[username] = [`New admin message: `+req.body.warning]
}
await db.set("messages", messages)
res.sendStatus(200)
deletePostClient.send({
content: `@${user} just warned @${username}: `+"`"+req.body.warning+"`",
username: 'The Daily Gobo',
avatarURL: 'https://cdn.discordapp.com/icons/1026288031537303563/a1e8c8448b80ea612a13dd238e4609a4.webp?size=240'
});
}
}
}
}
})
app.post('/newblogpost/', jsonParser, async function(req, res) {
const allowedToWrite = await db.get("allowedToWrite")
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (allowedToWrite.includes(user)) {
if (req.body.content) {
if (req.body.content.length > 50) {
var post = await writePost(req.body.content, user)
res.send(post)
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/getmod/:user/', async function(req, res) {
const allowedToWrite = await db.get("allowedToWrite")
const allowedToMod = await db.get("allowedToMod")
if (allowedToMod.includes(req.params.user)) {
res.send({"mod":true,"writer":(allowedToWrite.includes(req.params.user))})
} else {
res.send({"mod":false,"writer":(allowedToWrite.includes(req.params.user))})
}
})
app.get('/uncensored/', async function(req, res) {
const allowedToMod = await db.get("allowedToMod")
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (allowedToMod.includes(user)) {
var posts = await db.get("posts")
res.send(posts)
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/delete/:post/', async function(req, res) {
const allowedToMod = await db.get("allowedToMod")
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (allowedToMod.includes(user)) {
var posts = await db.get("posts")
if (posts[req.params.post]) {
posts[req.params.post].deleted = user
var embed = new EmbedBuilder()
.setTitle(`Post Deleted by ${user}`)
.setColor('Random')
.setTimestamp()
var response = await fetch('https://trampoline.turbowarp.org/proxy/users/'+user)
var data = await response.json()
embed.setAuthor({name:user, iconURL:data.profile.images['90x90'], url:"https://scratch.mit.edu/users/"+user+"/"})
deletePostClient.send({
content: `@${user} just deleted post #${req.params.post} by @${posts[req.params.post].author}.`,
username: 'The Daily Gobo',
avatarURL: 'https://cdn.discordapp.com/icons/1026288031537303563/a1e8c8448b80ea612a13dd238e4609a4.webp?size=240',
embeds: [embed],
});
await db.set("posts", posts)
res.sendStatus(200)
} else {
res.sendStatus(400)
}
} else {
res.sendStatus(403)
}
}
})
app.get('/dashboard/', async function(req, res) {
const allowedToWrite = await db.get("allowedToWrite")
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
var user = tokens[getCookie('usertoken', req)]
if (allowedToWrite.includes(user)) {
res.sendFile(path.join(__dirname, '/html/dashboard.html'));
} else {
res.sendStatus(403)
}
} else {
res.sendStatus(403)
}
})
app.get('/verify', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
res.redirect("https://thedailygobo.scratchtools.app/feed/")
} else {
var redirectLocation = new Buffer('https://thedailygobo.scratchtools.app/verified').toString('base64');
res.redirect(`https://auth.itinerary.eu.org/auth/?redirect=${redirectLocation}&name=The Daily Gobo`);
}
})
app.get('/feed/', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
res.render(__dirname + "/html/feed.html", {username:tokens[getCookie('usertoken', req)]})
} else {
res.redirect("https://thedailygobo.scratchtools.app/verify/")
}
})
app.get('/postinfo/:post/', async function(req, res) {
var posts = await db.get("posts")
if (posts[req.params.post]) {
res.send(posts[req.params.post])
} else {
res.sendStatus(404)
}
})
app.get('/posts/:post/', async function(req, res) {
fs.readFile(__dirname + "/posts/"+req.params.post+".md", function(err, data) {
var converter = new showdown.Converter()
var text = data.toString('utf8')
var html = converter.makeHtml(text)
html = DOMPurify.sanitize(html, {ALLOWED_TAGS: ['b', 'q', 'a', 'i', 'ul', 'li', 'img', 'ol', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'hr', 'br', 'u'], ALLOWED_ATTR: ['src', 'href']})
res.send(html)
})
})
app.get('/posts/', async function(req, res) {
if (req.query.offset && !isNaN(req.query.offset)) {
const allowedToWrite = await db.get("allowedToWrite")
const allowedToMod = await db.get("allowedToMod")
var featured = await getFeatured()
var posts = await db.get("posts")
var newPosts = {}
var trendingPost = {"loves":[],"favorites":[]}
var trendingPostId = "-1"
Object.keys(posts).forEach(function(el, i) {
if ((Object.keys(posts).length-i) > Number(req.query.offset)-1 && (Object.keys(posts).length-i) < Number(req.query.offset)+50) {
if (!posts[el].deleted) {
var then = new Date(posts[el].time);
var now = new Date();
var msBetweenDates = Math.abs(then.getTime() - now.getTime());
// 👇️ convert ms to days hour min sec ms
var daysBetweenDates = msBetweenDates / (24 * 60 * 60 * 1000);
if (daysBetweenDates < 1) {
if ((posts[el].loves.length+(posts[el].favorites.length*2)) > (trendingPost.loves.length+(trendingPost.favorites.length*2))) {
trendingPost = posts[el]
trendingPostId = el
}
}
posts[el].author = getUserData(posts[el].author, posts, allowedToWrite, allowedToMod)
newPosts[el] = posts[el]
}
}
})
if (newPosts[trendingPostId]) {
newPosts[trendingPostId].featured = true
}
res.send(newPosts)
} else {
res.sendStatus(400)
}
})
app.get('/verified', async function(req, res) {
var tokens = await db.get("tokens")
if (getCookie('usertoken', req) && Object.keys(tokens).includes(getCookie('usertoken', req))) {
res.redirect("https://thedailygobo.scratchtools.app/feed/")
} else {
const { privateCode } = req.query;
fetch(`https://auth.itinerary.eu.org/api/auth/verifyToken?privateCode=${privateCode}`, { method: 'GET' })
.then((response) => response.json())
.then(async function(data) {
// The `data` object sent by Scratch Auth will contain notably a `valid` property and a `username` property
// If `valid` is `true`, the user has successfully completed authentication process
if (data.valid === true) {
// Generate a session document for the user and store it in the database
var tokens = await db.get("tokens")
var token = makeid(200)
if (!banned.includes(data.username)) {
tokens[token] = data.username
await db.set("tokens", tokens)
res.cookie('usertoken',token, { maxAge: 999999999900000, httpOnly: true });
res.redirect("https://thedailygobo.scratchtools.app/feed/")
} else {
res.send("oops you're banned")
}
} else {
// Respond to the client with an error
res.status(403).json({ error: 'Authentication failed' });
}
});
}
})
app.listen(PORT, function (){
console.log('Listening on Port 3000');
});