This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
213 lines (189 loc) Β· 6.71 KB
/
server.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
// server.js
// where your node app starts
// init project
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// store session data in user's cookies (so: don't store much of it)
const cookieSession = require('cookie-session')
app.use(cookieSession({
secret: process.env.SECRET, /* for tamper-proofing */
maxAge: 24 * 60 * 60 * 1000 /* 1 day */
}))
// this lib provides express routes to handle the IndieAuth dance
const IndieAuthAuthn = require('./lib/indieauth-authn');
const indieAuth = IndieAuthAuthn({
successRedirect: '/dashboard',
clientId: 'https://' + process.env.MAIN_URL + '/' });
app.use(indieAuth)
// render html with http://handlebarsjs.com/
var exphbs = require('express-handlebars');
var hbs = exphbs.create({
defaultLayout: 'main',
extname: '.hbs',
});
app.engine('hbs', hbs.engine);
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
// we've gotta parse people's webpages
const checkSiteLinks = require('./lib/check-links');
// and get their h-cards for profile data
const fetchRepresentativeHCard = require('./lib/representative-h-card');
const DB = require('./lib/db');
const db = DB();
const Sites = require('./lib/models/site');
const {addSite, getSite, getSiteBySlug, saveProfile, saveSiteCheckStatus} = Sites(db);
// middleware to always set up a context to pass to templates
app.use(function(req, res, next){
res.locals.context = {
'projectUrl': 'https://glitch.com/~' + process.env.PROJECT_NAME
};
if(req.session.user) {
res.locals.context['user'] = req.session.user
}
next();
});
// a middleware to kick people to the homepage if they are not logged in
function requireLogin(request, response, next) {
if(! request.session.user) { return response.redirect('/') }
next();
}
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
function profileFromHCard(site, card) {
const u = new URL(site.url);
let profile = {
cute_url: u.hostname.replace(/^www./,'') + u.pathname.replace(/\/(index.html)?$/,''),
slug: site.slug,
slug_encoded: encodeURIComponent(site.slug),
url: site.url, // don't necessarily trust the URL in the h-card.
};
if ( ! ('properties' in card) ) {
return profile;
}
["name","note","photo"].forEach((prop) => {
if (prop in card.properties) {
profile[prop] = card.properties[prop][0];
}
});
return profile;
}
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (request, response) {
response.render('index', response.locals.context);
});
app.get("/terms", function (request, response) {
response.render('terms', response.locals.context);
});
app.get('/dashboard', requireLogin, function(request, response) {
getSite(request.session.user.me, true) // find site in the db or add it
.then((site) => {
return new Promise((fulfill, reject) => {
db.all(`SELECT * FROM SiteChecks WHERE url="${site.url}" ORDER BY datetime DESC LIMIT 3`, function(err, rows) {
let parsed_rows = Array.prototype.map.call(rows, row => {
row.result = JSON.parse(row.result);
return row;
});
if(site.profile) {
site.profile = profileFromHCard(site, JSON.parse(site.profile));
}
site.slug_encoded = encodeURIComponent(site.slug);
response.locals.context['site'] = site;
response.locals.context['hostname'] = process.env.MAIN_URL;
response.locals.context['checks'] = rows;
fulfill();
});
});
})
.then(() => {
response.render('dashboard', response.locals.context);
});
});
app.get('/directory', function(request, response) {
return new Promise((fulfill, reject) => {
db.all('SELECT * FROM Sites WHERE active=1 AND profile IS NOT NULL ORDER BY timestamp DESC', function(err, rows){
let profiles = Array.prototype.map.call(rows, row => {
return profileFromHCard(row, JSON.parse(row.profile));
});
fulfill(profiles);
});
})
.then((profiles) => {
response.locals.context['profiles'] = profiles;
response.render('directory', response.locals.context);
});
});
app.post('/check-profile', requireLogin, function(request, response) {
getSite(request.session.user.me)
.then((site) => {
fetchRepresentativeHCard(site.url)
.then((card) => {
if(card) {
return saveProfile(site, card);
}
})
})
.then(() => { setTimeout(() => { response.redirect('/dashboard'); }, 500); });
});
app.post('/remove-profile', requireLogin, function(request, response) {
getSite(request.session.user.me)
.then((site) => {
return saveProfile(site, null);
})
.then(() => { response.redirect('/dashboard'); });
});
app.post('/check-links', requireLogin, function(request, response) {
getSite(request.session.user.me) // find site in db but don't add if missing
.then((site) => {
if(site) {
checkSiteLinks(site)
.then((status) => saveSiteCheckStatus(site, status))
.then(() => { response.redirect('/dashboard'); });
}
});
});
app.get('/:slug/next', function(request, response) {
getSiteBySlug(request.params.slug)
.then((site) => {
db.get(`SELECT * from Sites where active = 1 AND slug != "${site.slug}" ORDER BY RANDOM() LIMIT 1`, function(err, row) {
response.redirect(row['url']);
});
})
.catch(() => {
console.log('Tried to next unknown slug ', request.params.slug);
response.redirect('/');
});
});
app.get('/:slug/previous', function(request, response) {
getSiteBySlug(request.params.slug)
.then((site) => {
db.get(`SELECT * from Sites where active = 1 AND slug != "${site.slug}" ORDER BY RANDOM() LIMIT 1`, function(err, row) {
response.redirect(row['url']);
});
})
.catch(() => {
console.log('Tried to previous unknown slug ', request.params.slug);
response.redirect('/');
});
});
app.get('/:slug', function(request, response) {
getSiteBySlug(request.params.slug)
.then((site) => {
if(site.profile) {
let profile = profileFromHCard(site, JSON.parse(site.profile));
profile.standalone = true;
response.locals.context['profile'] = profile;
response.render('profile', response.locals.context);
} else {
response.redirect(site['url']);
}
})
.catch(() => { response.redirect('/'); });
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});getSiteBySlug