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

View file

@ -129,7 +129,11 @@ let CreateClient = (token, user_id) => {
} }
if (command === 'media') { 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') { if (command === 'status') {