feat(cmd): add subjects. refactor(cmd): handling. fix(nitter): quotes. chore(config): update invidious instances. update deps.

This commit is contained in:
vulet 2020-11-02 22:48:07 +08:00
parent a1dc8200bc
commit beb8a4520c
6 changed files with 884 additions and 331 deletions

View file

@ -28,10 +28,13 @@ const mediaUpload = async ({ domain, token }, { data, mimetype }) => {
return upload.data.id; return upload.data.id;
}; };
const run = async (matrixClient, { roomId }, mediaURL, content, replyId, registrar) => { const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject, registrar) => {
let mediaId = null;
const fediverse = registrar.config.fediverse; const fediverse = registrar.config.fediverse;
const media = await mediaDownload(mediaURL, registrar.config.matrix.mimetypes); if(mediaURL) {
const mediaId = await mediaUpload(fediverse, media); const media = await mediaDownload(mediaURL, registrar.config.matrix.mimetypes);
mediaId = await mediaUpload(fediverse, media);
}
const response = await axios({ const response = await axios({
method: 'POST', method: 'POST',
url: `${fediverse.domain}/api/v1/statuses`, url: `${fediverse.domain}/api/v1/statuses`,
@ -39,28 +42,34 @@ const run = async (matrixClient, { roomId }, mediaURL, content, replyId, registr
data : qs.stringify({ data : qs.stringify({
status: content, status: content,
content_type: `text/markdown`, content_type: `text/markdown`,
media_ids: [ mediaId ], media_ids: mediaURL && [ mediaId ] || undefined,
in_reply_to_id: replyId || undefined in_reply_to_id: replyId || undefined,
spoiler_text: subject || 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, isReply, registrar) => { exports.runQuery = async (client, room, userInput, registrar, { isReply, hasMedia, hasSubject }) => {
try { try {
const chunks = userInput.trim().split(' '); const chunks = userInput.trim().split(' ');
if(chunks.length < 2) throw ''; if(!chunks.length || chunks.length < !!isReply + !!hasMedia) throw '';
let replyId = null; let replyId = null;
let mediaURL = null;
const subject = hasSubject ? registrar.config.matrix.subject : null;
if(isReply) { if(isReply) {
replyId = chunks[0]; replyId = chunks[0];
chunks.shift(); chunks.shift();
} }
const url = new URL(chunks[0]); if(hasMedia) {
chunks.shift(); const url = new URL(chunks[0]);
if(url.protocol !== 'https:') throw ''; chunks.shift();
if(!registrar.config.matrix.domains.includes(url.hostname)) throw ''; if(url.protocol !== 'https:') throw '';
if(!/^\/_matrix\/media\/r0\/download\/[^/]+\/[^/]+\/?$/.test(url.pathname)) throw ''; if(!registrar.config.matrix.domains.includes(url.hostname)) throw '';
return await run(client, room, url.toString(), chunks.join(' '), replyId, registrar); if(!/^\/_matrix\/media\/r0\/download\/[^/]+\/[^/]+\/?$/.test(url.pathname)) throw '';
mediaURL = url.toString();
}
return await run(client, room, chunks.join(' '), replyId, mediaURL, subject, 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

@ -24,7 +24,7 @@ const nitter = async (instance, url) => {
hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'), hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'),
quote: quote ? { quote: quote ? {
path: quote.querySelector('a.quote-link').href, path: quote.querySelector('a.quote-link').href,
text: quote.querySelector('.quote-text').innerHTML, text: quote.querySelector('.quote-text') ? quote.querySelector('.quote-text').innerHTML : '',
} : null, } : null,
isReply: isReply && replies.length > 0 ? replies[replies.length - 1].classList.contains('unavailable') ? 'unavailable' : { isReply: isReply && replies.length > 0 ? replies[replies.length - 1].classList.contains('unavailable') ? 'unavailable' : {
path: replies[replies.length - 1].querySelector('a.tweet-link').href, path: replies[replies.length - 1].querySelector('a.tweet-link').href,

View file

@ -4,7 +4,8 @@ module.exports = {
user: 'your_user', user: 'your_user',
password: 'your_password', password: 'your_password',
domains: [ 'your_homeserver.com' ], domains: [ 'your_homeserver.com' ],
mimetypes: [ 'image/png', 'image/jpeg' ] mimetypes: [ 'image/png', 'image/jpeg', 'video/webm', 'image/jpg', 'video/mp4', 'audio/mp3' ],
subject: ''
}, },
fediverse: { fediverse: {
domain: 'https://your_federation.com', domain: 'https://your_federation.com',
@ -20,8 +21,8 @@ module.exports = {
domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'wwww.twitter.com' ] domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'wwww.twitter.com' ]
}, },
invidious: { invidious: {
domain: 'invidio.us', domain: 'invidious.fdn.fr',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)', userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'invidio.us', 'www.invidio.us', 'youtube.com', 'www.youtube.com' ] domains: [ 'invidious.snopyta.org', 'invidious.xyz', 'youtube.com', 'www.youtube.com' ]
} }
}; };

121
main.js
View file

@ -42,104 +42,41 @@ let CreateClient = (token, user_id) => {
if (event.event.unsigned.age > 10000) return; if (event.event.unsigned.age > 10000) return;
if (event.event.content.body.charAt(0) === '+') { if (event.event.content.body.charAt(0) === '+') {
console.log(`Logs: ${event.event.sender} - ${event.event.content.body}`); console.log(`Logs: ${event.event.sender} - ${event.event.content.body}`);
const args = event.event.content.body.slice(1).trim().split(/ +/g); let args = event.event.content.body.slice(1).trim().split(/ +/g);
const command = args.shift().toLowerCase(); let command = args.shift().toLowerCase();
const userInput = args.join(' '); const userInput = args.join(' ');
const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1); const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1);
const address = args.slice(0, 1).join(' ').replace(/"/g, ''); const address = args.slice(0, 1).join(' ').replace(/"/g, '');
if (command === 'boo') { args = [];
registrar.boo.runQuery(matrixClient, room, userInput, registrar);
} switch(command) {
case 'config':
if (command === 'beg') { return;
registrar.beg.runQuery(matrixClient, room, registrar); case 'help': case 'beg': case 'flood': case 'notify':
} args.push(matrixClient, room, registrar);
break;
if (command === 'clap') { case 'tip':
registrar.clap.runQuery(matrixClient, room, userInput, registrar); args.push(matrixClient, room, address, flaggedInput, registrar);
} break;
case 'archive': case 'rearchive':
if (command === 'copy') { args.push(matrixClient, room, userInput, !!~command.indexOf('re'), registrar);
registrar.copy.runQuery(matrixClient, room, userInput, registrar); command = 'archive';
} break;
case 'plemara': case 'reply': case 'media': case 'mediareply':
if (command === 'flood') { case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
registrar.flood.runQuery(matrixClient, room, registrar); args.push(matrixClient, room, userInput, registrar, {
} isReply: !!~command.indexOf('reply'),
hasMedia: !!~command.indexOf('media'),
if (command === 'fren') { hasSubject: !!~command.indexOf('random'),
registrar.fren.runQuery(matrixClient, room, userInput, registrar); });
} command = 'media';
break;
if (command === 'help') { default:
registrar.help.runQuery(matrixClient, room); args.push(matrixClient, room, userInput, registrar);
}
if (command === 'pin') {
registrar.pin.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'plemara') {
registrar.plemara.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'notify') {
registrar.notify.runQuery(matrixClient, room, registrar);
}
if (command === 'redact') {
registrar.redact.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'reply') {
registrar.reply.runQuery(matrixClient, room, address, flaggedInput, registrar);
}
if (command === 'tip') {
registrar.tip.runQuery(matrixClient, room, address, flaggedInput, registrar);
}
if (command === 'unfren') {
registrar.unfren.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'unpin') {
registrar.unpin.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'mordy') {
registrar.mordy.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'archive') {
registrar.archive.runQuery(matrixClient, room, userInput, false, registrar);
}
if (command === 'rearchive') {
registrar.archive.runQuery(matrixClient, room, userInput, true, registrar);
}
if (command === 'nitter') {
registrar.nitter.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'invidious') {
registrar.invidious.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'media') {
registrar.media.runQuery(matrixClient, room, userInput, false, registrar);
}
if (command === 'mediareply') {
registrar.media.runQuery(matrixClient, room, userInput, true, registrar);
}
if (command === 'status') {
registrar.status.runQuery(matrixClient, room, userInput, registrar);
} }
registrar[command] && registrar[command].runQuery.call(null, args);
} }
}); });

View file

@ -20,9 +20,9 @@
"dependencies": { "dependencies": {
"axios": "^0.19.2", "axios": "^0.19.2",
"file-system": "^2.2.2", "file-system": "^2.2.2",
"matrix-js-sdk": "^2.4.6", "form-data": "^3.0.0",
"jsdom": "^16.2.2", "jsdom": "^16.2.2",
"form-data": "^3.0.0" "matrix-js-sdk": "^2.4.6"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^5.16.0", "eslint": "^5.16.0",

1044
yarn.lock

File diff suppressed because it is too large Load diff