feat(fediverse): add media reply

This commit is contained in:
vulet 2020-09-06 23:11:23 +08:00
parent 2e483e4744
commit a1dc8200bc
2 changed files with 15 additions and 5 deletions

View file

@ -28,7 +28,7 @@ const mediaUpload = async ({ domain, token }, { data, mimetype }) => {
return upload.data.id;
};
const run = async (matrixClient, { roomId }, mediaURL, content, registrar) => {
const run = async (matrixClient, { roomId }, mediaURL, content, replyId, registrar) => {
const fediverse = registrar.config.fediverse;
const media = await mediaDownload(mediaURL, registrar.config.matrix.mimetypes);
const mediaId = await mediaUpload(fediverse, media);
@ -40,21 +40,27 @@ const run = async (matrixClient, { roomId }, mediaURL, content, registrar) => {
status: content,
content_type: `text/markdown`,
media_ids: [ mediaId ],
in_reply_to_id: replyId || undefined
}, { arrayFormat: 'brackets' })
});
return matrixClient.sendHtmlNotice(roomId, '', `<a href="${response.data.url}">${response.data.id}</a>`);
}
exports.runQuery = async (client, room, userInput, registrar) => {
exports.runQuery = async (client, room, userInput, isReply, registrar) => {
try {
let chunks = userInput.trim().split(' ');
const chunks = userInput.trim().split(' ');
if(chunks.length < 2) throw '';
let replyId = null;
if(isReply) {
replyId = chunks[0];
chunks.shift();
}
const url = new URL(chunks[0]);
chunks.shift();
if(url.protocol !== 'https:') throw '';
if(!registrar.config.matrix.domains.includes(url.hostname)) throw '';
if(!/^\/_matrix\/media\/r0\/download\/[^/]+\/[^/]+\/?$/.test(url.pathname)) throw '';
return await run(client, room, url.toString(), chunks.join(' '), registrar);
return await run(client, room, url.toString(), chunks.join(' '), replyId, registrar);
} catch(e) {
return client.sendHtmlNotice(room.roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
}

View file

@ -129,7 +129,11 @@ let CreateClient = (token, user_id) => {
}
if (command === 'media') {
registrar.media.runQuery(matrixClient, room, userInput, registrar);
registrar.media.runQuery(matrixClient, room, userInput, false, registrar);
}
if (command === 'mediareply') {
registrar.media.runQuery(matrixClient, room, userInput, true, registrar);
}
if (command === 'status') {