2020-07-03 12:10:55 +03:00
|
|
|
const qs = require('qs');
|
|
|
|
const FormData = require('form-data');
|
|
|
|
|
2021-01-01 12:30:26 +03:00
|
|
|
const emojis = { public: '🌐', unlisted: '📝', private: '🔒️', direct: '✉️' };
|
2020-12-27 12:27:48 +03:00
|
|
|
exports.visibilityEmoji = v => emojis[v] || v;
|
|
|
|
|
2020-11-06 16:00:38 +03:00
|
|
|
const getFilename = header => {
|
|
|
|
if(typeof header !== 'string') return null;
|
|
|
|
try {
|
|
|
|
const m = header.match(/inline; filename(?:=(.+)|\*=utf-8''(.+))/);
|
|
|
|
return !m ? null : m[2] && decodeURIComponent(m[2]) || m[1];
|
|
|
|
} catch(e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-08 09:44:00 +03:00
|
|
|
const mediaDownload = async (url, { whitelist, blacklist }) => {
|
2020-07-03 12:10:55 +03:00
|
|
|
const media = await axios({ method: 'GET', url, responseType: 'arraybuffer' });
|
2020-11-08 09:44:00 +03:00
|
|
|
if (media.statusText !== 'OK' || blacklist.includes(media.headers['content-type'])) throw media;
|
|
|
|
if(whitelist.length && !whitelist.includes(media.headers['content-type'])) throw media;
|
2020-07-03 12:10:55 +03:00
|
|
|
return {
|
|
|
|
data: media.data,
|
2020-11-06 16:00:38 +03:00
|
|
|
filename: getFilename(media.headers['content-disposition']),
|
2020-07-03 12:10:55 +03:00
|
|
|
mimetype: media.headers['content-type']
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
const mediaUpload = async ({ domain }, { data, filename, mimetype }) => {
|
2020-07-03 12:10:55 +03:00
|
|
|
const form = new FormData();
|
|
|
|
form.append('file', data, {
|
2020-11-06 16:00:38 +03:00
|
|
|
filename: filename || 'upload',
|
2020-07-03 12:10:55 +03:00
|
|
|
contentType: mimetype,
|
|
|
|
});
|
|
|
|
const upload = await axios({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${domain}/api/v1/media`,
|
2021-02-01 11:58:59 +03:00
|
|
|
headers: form.getHeaders({ Authorization: `Bearer ${fediverse_auth.access_token}` }),
|
2020-07-03 12:10:55 +03:00
|
|
|
data: form,
|
|
|
|
});
|
|
|
|
if(upload.statusText !== 'OK') throw upload;
|
|
|
|
return upload.data.id;
|
|
|
|
};
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject) => {
|
2020-11-02 17:48:07 +03:00
|
|
|
let mediaId = null;
|
2021-02-01 11:58:59 +03:00
|
|
|
const fediverse = config.fediverse;
|
2020-11-02 17:48:07 +03:00
|
|
|
if(mediaURL) {
|
2021-02-01 11:58:59 +03:00
|
|
|
const media = await mediaDownload(mediaURL, fediverse.mimetypes);
|
|
|
|
mediaId = await mediaUpload(fediverse, media);
|
2020-11-02 17:48:07 +03:00
|
|
|
}
|
2020-07-03 12:10:55 +03:00
|
|
|
const response = await axios({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${fediverse.domain}/api/v1/statuses`,
|
2021-02-01 11:58:59 +03:00
|
|
|
headers: { Authorization: `Bearer ${fediverse_auth.access_token}`, 'Content-Type': 'application/x-www-form-urlencoded' },
|
2020-07-03 12:10:55 +03:00
|
|
|
data : qs.stringify({
|
|
|
|
status: content,
|
|
|
|
content_type: `text/markdown`,
|
2020-11-02 17:48:07 +03:00
|
|
|
media_ids: mediaURL && [ mediaId ] || undefined,
|
|
|
|
in_reply_to_id: replyId || undefined,
|
|
|
|
spoiler_text: subject || undefined
|
2020-07-03 12:10:55 +03:00
|
|
|
}, { arrayFormat: 'brackets' })
|
|
|
|
});
|
|
|
|
return matrixClient.sendHtmlNotice(roomId, '', `<a href="${response.data.url}">${response.data.id}</a>`);
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
exports.runQuery = async (client, room, userInput, { isReply, hasMedia, hasSubject }) => {
|
2020-07-03 12:10:55 +03:00
|
|
|
try {
|
2020-09-06 18:11:23 +03:00
|
|
|
const chunks = userInput.trim().split(' ');
|
2020-11-02 17:48:07 +03:00
|
|
|
if(!chunks.length || chunks.length < !!isReply + !!hasMedia) throw '';
|
2020-09-06 18:11:23 +03:00
|
|
|
let replyId = null;
|
2020-11-02 17:48:07 +03:00
|
|
|
let mediaURL = null;
|
2021-02-01 11:58:59 +03:00
|
|
|
const subject = hasSubject ? fediverse.subject : null;
|
2020-09-06 18:11:23 +03:00
|
|
|
if(isReply) {
|
|
|
|
replyId = chunks[0];
|
|
|
|
chunks.shift();
|
|
|
|
}
|
2020-11-02 17:48:07 +03:00
|
|
|
if(hasMedia) {
|
2021-01-14 13:52:37 +03:00
|
|
|
let url = new URL(chunks[0]);
|
2020-11-02 17:48:07 +03:00
|
|
|
chunks.shift();
|
2021-01-14 13:52:37 +03:00
|
|
|
if(url.protocol === 'mxc:' && url.hostname && url.pathname)
|
2021-02-01 11:58:59 +03:00
|
|
|
url = new URL(`${config.matrix.domain}/_matrix/media/r0/download/${url.hostname}${url.pathname}`);
|
2020-11-02 17:48:07 +03:00
|
|
|
if(url.protocol !== 'https:') throw '';
|
2021-02-01 11:58:59 +03:00
|
|
|
if(!config.matrix.domains.includes(url.hostname)) throw '';
|
2020-11-02 17:48:07 +03:00
|
|
|
if(!/^\/_matrix\/media\/r0\/download\/[^/]+\/[^/]+\/?$/.test(url.pathname)) throw '';
|
|
|
|
mediaURL = url.toString();
|
|
|
|
}
|
2021-02-01 11:58:59 +03:00
|
|
|
return await run(client, room, chunks.join(' '), replyId, mediaURL, subject);
|
2020-07-03 12:10:55 +03:00
|
|
|
} catch(e) {
|
|
|
|
return client.sendHtmlNotice(room.roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
|
|
|
|
}
|
|
|
|
};
|