-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
micropub.js
191 lines (148 loc) · 5.08 KB
/
micropub.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
import { STATUS_CODES } from 'http';
import { JSDOM } from 'jsdom';
import { mf2tojf2 } from '@paulrobertlloyd/mf2tojf2';
import { checkAuth } from './function-helpers/check-auth.js';
import { parseMultipart } from './function-helpers/parse-multipart.js';
import { upload } from './function-helpers/upload.js';
import { uploadImage } from './function-helpers/upload-image.js';
import { responseHeaders } from './function-helpers/response-headers.js';
import { HttpError, handleError } from './function-helpers/http-error.js';
import { getEnvVar } from './function-helpers/get-env-var.js';
async function getTitle(url) {
try {
const dom = await JSDOM.fromURL(url);
return dom.window.document.title;
} catch {
return url;
}
}
function syndications() {
return [
{
uid: 'mastodon',
name: 'qubyte on mastodon.social',
service: {
name: 'Mastodon',
url: 'https://mastodon.social/'
}
}
];
}
/** @param {Request} req */
async function convertFormRequestToObject(req) {
/** @type {Record<String, String>} */
const properties = {};
const query = await req.formData();
const keys = new Set(query.keys());
let type = 'h-entry';
for (const key of keys) {
const normalizedKey = key.endsWith('[]') ? key.slice(0, -2) : key;
if (normalizedKey === 'h') {
type = `h-${query.get(key)}`;
} else {
properties[normalizedKey] = query.getAll(key).filter(Boolean);
}
}
return { type: [type], properties };
}
async function createFile(message, type, data, optionalFilename) {
const time = data.published ? Date.parse(data.published) : Date.now();
const buffer = Buffer.from(`${JSON.stringify(data, null, 2)}\n`);
const filename = optionalFilename || `${time}.json`;
console.log('Creating file:', filename, 'with content:', data);
await upload(message, type, filename, buffer);
return `https://qubyte.codes/${type}/${time}`;
}
/** @param {Request} req */
async function parseBody(req) {
const type = req.headers.get('content-type');
let parsed = null;
if (type.match(/multipart/)) {
parsed = await parseMultipart(req);
} else if (type.match(/json/)) {
parsed = await req.json();
} else if (type.match(/x-www-form-urlencoded/)) {
parsed = await convertFormRequestToObject(req);
} else {
throw new HttpError(`Unhandled MIME type: ${type}`, { status: 400 });
}
const spoiler = (parsed.properties.spoiler || [])
.map(s => s.trim())
.filter(Boolean);
if (spoiler.length) {
parsed.properties.spoiler = spoiler;
} else {
delete parsed.properties.spoiler;
}
return parsed;
}
// eslint-disable-next-line max-statements
async function determineTypeAndCreate(data) {
const publishedDate = new Date();
const filename = `${publishedDate.getTime()}.jf2.json`;
const published = publishedDate.toISOString();
const jf2 = mf2tojf2({ items: [data] });
if (jf2['repost-of']) {
const name = await getTitle(jf2['repost-of']);
return createFile('New link.', 'links', { ...jf2, name, published }, filename);
}
if (jf2['bookmark-of']) {
return createFile('New link.', 'links', { ...jf2, published }, filename);
}
if (jf2['like-of']) {
return createFile('New like.', 'likes', { ...jf2, published }, filename);
}
if (jf2['in-reply-to']) {
const name = await getTitle(jf2['in-reply-to']);
return createFile('New Reply.', 'replies', { ...jf2, name, published }, filename);
}
if (jf2.category?.includes('study-session')) {
return createFile('New study session.', 'study-sessions', { ...jf2, published }, filename);
}
// The default is a note, which I allow to have images.
if (data?.files?.photo.length) { // quill uses a photo field
const uploadedImage = await uploadImage(data.files.photo[0]);
delete jf2.files;
jf2.photos = [uploadedImage];
}
return createFile('New note.', 'notes', { ...jf2, published });
}
/** @param {Request} req */
function isSyndicationsQuery(req) {
const q = new URL(req.url).searchParams.get('q');
return q === 'syndicate-to' || q === 'config';
}
/** @param {Request} req */
export default async function handler(req) {
console.log('GOT REQUEST');
try {
await checkAuth(req);
} catch (e) {
return handleError(e, 'Error checking auth.');
}
if (isSyndicationsQuery(req)) {
console.log('Responding to syndications query.');
const baseUrl = getEnvVar('URL');
return Response.json({
'syndicate-to': syndications(),
'media-endpoint': `${baseUrl}/functions/micropub-media`
});
}
let data;
try {
data = await parseBody(req);
} catch (e) {
return handleError(e, 'Error parsing body.', responseHeaders());
}
delete data.access_token; // quill
console.log(JSON.stringify({ data }, null, 2));
if (!Object.keys(data).length) {
console.log('Responding to empty body.');
return new Response(null, { status: 204, headers: responseHeaders() });
}
const location = await determineTypeAndCreate(data);
return new Response(STATUS_CODES[202], { status: 202, headers: responseHeaders({ location }) });
}
export const config = {
path: '/functions/micropub'
};