forked from ckolderup/postmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
186 lines (166 loc) · 5.83 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
import * as dotenv from 'dotenv';
import express from 'express';
import cors from 'cors';
import { create } from 'express-handlebars';
import escapeHTML from 'escape-html';
import { domain, account, simpleLogger, actorInfo, replaceEmptyText } from './src/util.js';
import session, { isAuthenticated } from './src/session-auth.js';
import * as bookmarksDb from './src/bookmarks-db.js';
import * as apDb from './src/activity-pub-db.js';
import routes from './src/routes/index.js';
dotenv.config();
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.json({ type: 'application/activity+json' }));
app.use(session());
app.use((req, res, next) => {
res.locals.loggedIn = req.session.loggedIn;
return next();
});
app.set('site_name', actorInfo.displayName || 'Tom\'s Bookmarks');
app.set('bookmarksDb', bookmarksDb);
app.set('apDb', apDb);
app.set('account', account);
app.set('domain', domain);
app.disable('x-powered-by');
// force HTTPS in production
if (process.env.ENVIRONMENT === 'production') {
app.set('trust proxy', ['127.0.0.1', '10.0.0.0/8']);
app.use(({ secure, hostname, url, port }, response, next) => {
if (!secure) {
return response.redirect(308, `https://${hostname}${url}${port ? `:${port}` : ''}`);
}
return next();
});
} else {
console.log("ENVIRONMENT is not 'production', HTTPS not forced");
}
const hbs = create({
helpers: {
pluralize(number, singular, plural) {
if (number === 1) return singular;
return typeof plural === 'string' ? plural : `${singular}s`;
},
htmlize(text) {
// uh-oh. ohhhh no.
const returnText = escapeHTML(text);
return returnText?.replace('\n', '<br/>');
},
siteName() {
return app.get('site_name');
},
account() {
return app.get('account');
},
feedUrl() {
return `https://${app.get('domain')}/index.xml`;
},
projectUrl() {
return `https://${app.get('domain')}`;
},
searchUrl() {
return `https://${app.get('domain')}/opensearch.xml`;
},
glitchProjectName() {
return process.env.PROJECT_DOMAIN;
},
section(name, options) {
if (!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
},
mastodonAccount() {
return process.env.MASTODON_ACCOUNT;
},
ifIn(item, array, options) {
const lowercased = array.map((tag) => tag.toLowerCase());
return lowercased.indexOf(item.toLowerCase()) >= 0 ? options.fn(this) : options.inverse(this);
},
removeTag(tag, path) {
return path
.split('/')
.filter((x) => x.toLowerCase() !== tag.toLowerCase())
.join('/');
},
ifThisTag(tag, path, options) {
return path.toLowerCase() === `/tagged/${tag}`.toLowerCase() ? options.fn(this) : options.inverse(this);
},
eq(a, b, options) {
return a === b ? options.fn(this) : options.inverse(this);
},
setTitle(item) {
return replaceEmptyText(item.title, item.url);
},
bookmarkIsYouTubeURL(url) {
const youtubeRegex = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/gi;
return youtubeRegex.test(url);
},
// Helper to extract YouTube video ID from the URL
getYouTubeVideoID(url) {
const youtubeRegex = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/gi;
const match = youtubeRegex.exec(url);
return match ? match[1] : null;
},
getGithubEmbed(url) {
const githubRepoRegex = /github\.com\/([^/]+)\/([^/]+)/i;
if (githubRepoRegex.test(url)) {
const match = url.match(githubRepoRegex);
const username = match[1];
const reponame = match[2];
return [username, reponame];
} else {
return null;
}
},
getSpotifySrcURL(url) {
const trackRegex = /spotify\.com\/track\/([^?/]+)/;
const episodeRegex = /spotify\.com\/episode\/([^?/]+)/;
if (trackRegex.test(url)) {
return `https://open.spotify.com/embed/track/${trackRegex.exec(url)[1]}?utm_source=generator`;
} else if (episodeRegex.test(url)) {
return `https://open.spotify.com/embed/episode/${episodeRegex.exec(url)[1]}?utm_source=generator`;
}
return null;
},
convertToWaybackMachineTimestamp(timestamp, url) {
let convertedTimestamp = '';
// Check if the timestamp has the correct length
if (timestamp && timestamp.length === 19) {
const year = timestamp.slice(0, 4);
const month = timestamp.slice(5, 7);
const day = timestamp.slice(8, 10);
const hour = timestamp.slice(11, 13);
const minute = timestamp.slice(14, 16);
const second = timestamp.slice(17, 19);
convertedTimestamp = `${year}${month}${day}${hour}${minute}${second}`;
}
if (convertedTimestamp && url) {
return `${convertedTimestamp}/${url}`;
}
return convertedTimestamp;
},
},
partialsDir: './src/pages/partials',
extname: '.hbs',
});
app.set('view engine', '.hbs');
app.set('views', './src/pages');
app.engine('.hbs', hbs.engine);
app.use(simpleLogger);
app.use('/admin', isAuthenticated, routes.admin);
app.use('/', routes.auth);
app.use('/bookmark', routes.bookmark);
app.use('/comment', routes.comment);
app.use('/.well-known/webfinger', cors(), routes.webfinger);
app.use('/u', cors(), routes.user);
app.use('/m', cors(), routes.message);
app.use('/', routes.core);
app.use('/api/inbox', cors(), routes.inbox);
app.use('/.well-known/nodeinfo', routes.nodeinfo);
app.use('/nodeinfo/2.0', routes.nodeinfo);
app.use('/nodeinfo/2.1', routes.nodeinfo);
app.use('/opensearch.xml', routes.opensearch);
app.listen(PORT, () => console.log(`App listening on port ${PORT}`));