-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathadmin.js
269 lines (203 loc) · 7.94 KB
/
admin.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
import express from 'express';
// eslint-disable-next-line import/no-unresolved, node/no-missing-import
import { stringify as csvStringify } from 'csv-stringify/sync'; // https://github.com/adaltas/node-csv/issues/323
import { domain, actorInfo, parseJSON } from '../util.js';
import { isAuthenticated } from '../session-auth.js';
import { lookupActorInfo, createFollowMessage, createUnfollowMessage, signAndSend, getInboxFromActorProfile } from '../activitypub.js';
const DATA_PATH = '/app/.data';
const ADMIN_LINKS = [
{ href: '/admin', label: 'Bookmarklet' },
{ href: '/admin/bookmarks', label: 'Import bookmarks' },
{ href: '/admin/followers', label: 'Permissions & followers' },
{ href: '/admin/following', label: 'Federated follows' },
{ href: '/admin/data', label: 'Data export' },
];
const router = express.Router();
router.get('/', isAuthenticated, async (req, res) => {
const params = req.query.raw ? {} : { title: 'Bookmarklet' };
params.adminLinks = ADMIN_LINKS;
params.currentPath = req.originalUrl;
params.bookmarklet = `javascript:(function(){w=window.open('https://${domain}/bookmark/popup?url='+encodeURIComponent(window.location.href)+'&highlight='+encodeURIComponent(window.getSelection().toString()),'postmarks','scrollbars=yes,width=550,height=600');})();`;
params.bookmarkletTruncated = `${params.bookmarklet.substr(0, 30)}…`;
return res.render('admin', params);
});
router.get('/bookmarks', isAuthenticated, async (req, res) => {
const params = req.query.raw ? {} : { title: 'Import bookmarks' };
params.adminLinks = ADMIN_LINKS;
params.currentPath = req.originalUrl;
return res.render('admin/bookmarks', params);
});
router.get('/followers', isAuthenticated, async (req, res) => {
const params = req.query.raw ? {} : { title: 'Permissions & followers' };
params.adminLinks = ADMIN_LINKS;
params.currentPath = req.originalUrl;
const apDb = req.app.get('apDb');
if (actorInfo.disabled) {
return res.render('nonfederated', params);
}
const permissions = await apDb.getGlobalPermissions();
try {
const followers = await apDb.getFollowers();
params.followers = JSON.parse(followers || '[]');
} catch (e) {
console.log('Error fetching followers for admin page');
}
try {
const blocks = await apDb.getBlocks();
params.blocks = JSON.parse(blocks || '[]');
} catch (e) {
console.log('Error fetching blocks for admin page');
}
params.allowed = permissions?.allowed || '';
params.blocked = permissions?.blocked || '';
return res.render('admin/followers', params);
});
router.get('/following', isAuthenticated, async (req, res) => {
const params = req.query.raw ? {} : { title: 'Federated follows' };
params.adminLinks = ADMIN_LINKS;
params.currentPath = req.originalUrl;
const apDb = req.app.get('apDb');
if (actorInfo.disabled) {
return res.render('nonfederated', params);
}
try {
const following = await apDb.getFollowing();
params.following = JSON.parse(following || '[]');
} catch (e) {
console.log('Error fetching followers for admin page');
}
return res.render('admin/following', params);
});
router.get('/data', isAuthenticated, async (req, res) => {
const params = req.query.raw ? {} : { title: 'Data export' };
params.adminLinks = ADMIN_LINKS;
params.currentPath = req.originalUrl;
return res.render('admin/data', params);
});
router.get('/bookmarks.db', isAuthenticated, async (req, res) => {
const filePath = `${DATA_PATH}/bookmarks.db`;
res.setHeader('Content-Type', 'application/vnd.sqlite3');
res.setHeader('Content-Disposition', 'attachment; filename="bookmarks.db"');
res.download(filePath);
});
router.get('/bookmarks.csv', isAuthenticated, async (req, res) => {
const bookmarksDb = req.app.get('bookmarksDb');
const bookmarks = await bookmarksDb.getBookmarksForCSVExport();
const result = csvStringify(bookmarks, { quoted: true });
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="bookmarks.csv"');
res.send(result);
});
router.get('/activitypub.db', isAuthenticated, async (req, res) => {
const filePath = `${DATA_PATH}/activitypub.db`;
res.setHeader('Content-Type', 'application/vnd.sqlite3');
res.setHeader('Content-Disposition', 'attachment; filename="activitypub.db"');
res.download(filePath);
});
router.post('/followers/block', isAuthenticated, async (req, res) => {
const db = req.app.get('apDb');
const oldFollowersText = (await db.getFollowers()) || '[]';
// update followers
const followers = parseJSON(oldFollowersText);
if (followers) {
followers.forEach((follower, idx) => {
if (follower === req.body.actor) {
followers.splice(idx, 1);
}
});
}
const newFollowersText = JSON.stringify(followers);
try {
await db.setFollowers(newFollowersText);
} catch (e) {
console.log('error storing followers after unfollow', e);
}
const oldBlocksText = (await db.getBlocks()) || '[]';
let blocks = parseJSON(oldBlocksText);
if (blocks) {
blocks.push(req.body.actor);
// unique items
blocks = [...new Set(blocks)];
} else {
blocks = [req.body.actor];
}
const newBlocksText = JSON.stringify(blocks);
try {
// update into DB
await db.setBlocks(newBlocksText);
console.log('updated blocks!');
} catch (e) {
console.log('error storing blocks after block action', e);
}
res.redirect('/admin/followers');
});
router.post('/followers/unblock', isAuthenticated, async (req, res) => {
const db = req.app.get('apDb');
const oldBlocksText = (await db.getBlocks()) || '[]';
const blocks = parseJSON(oldBlocksText);
if (blocks) {
blocks.forEach((block, idx) => {
if (block === req.body.actor) {
blocks.splice(idx, 1);
}
});
}
const newBlocksText = JSON.stringify(blocks);
try {
await db.setBlocks(newBlocksText);
} catch (e) {
console.log('error storing blocks after unblock action', e);
}
res.redirect('/admin/followers');
});
router.post('/following/follow', isAuthenticated, async (req, res) => {
const db = req.app.get('apDb');
const account = req.app.get('account');
const canonicalUrl = await lookupActorInfo(req.body.actor);
try {
const inbox = await getInboxFromActorProfile(canonicalUrl);
if (inbox) {
const followMessage = await createFollowMessage(account, domain, canonicalUrl, db);
signAndSend(followMessage, account, domain, db, req.body.actor.split('@').slice(-1), inbox);
}
return res.redirect('/admin/following');
} catch (e) {
console.log(e.message);
return res.status(500).send("Couldn't process follow request");
}
});
router.post('/following/unfollow', isAuthenticated, async (req, res) => {
const db = req.app.get('apDb');
const account = req.app.get('account');
const oldFollowsText = (await db.getFollowing()) || '[]';
const follows = parseJSON(oldFollowsText);
if (follows) {
follows.forEach((follow, idx) => {
if (follow === req.body.actor) {
follows.splice(idx, 1);
}
});
const inbox = await getInboxFromActorProfile(req.body.actor);
const unfollowMessage = createUnfollowMessage(account, domain, req.body.actor, db);
signAndSend(unfollowMessage, account, domain, db, new URL(req.body.actor).hostname, inbox);
const newFollowsText = JSON.stringify(follows);
try {
await db.setFollowing(newFollowsText);
} catch (e) {
console.log('error storing follows after unfollow action', e);
}
return res.redirect('/admin/following');
}
return res.status(500).send('Encountered an error processing existing following list');
});
router.post('/permissions', isAuthenticated, async (req, res) => {
const apDb = req.app.get('apDb');
await apDb.setGlobalPermissions(req.body.allowed, req.body.blocked);
res.redirect('/admin');
});
router.post('/reset', isAuthenticated, async (req, res) => {
const db = req.app.get('bookmarksDb');
await db.deleteAllBookmarks();
res.redirect('/admin');
});
export default router;