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;
};
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 media = await mediaDownload(mediaURL, registrar.config.matrix.mimetypes);
const mediaId = await mediaUpload(fediverse, media);
if(mediaURL) {
const media = await mediaDownload(mediaURL, registrar.config.matrix.mimetypes);
mediaId = await mediaUpload(fediverse, media);
}
const response = await axios({
method: 'POST',
url: `${fediverse.domain}/api/v1/statuses`,
@ -39,28 +42,34 @@ const run = async (matrixClient, { roomId }, mediaURL, content, replyId, registr
data : qs.stringify({
status: content,
content_type: `text/markdown`,
media_ids: [ mediaId ],
in_reply_to_id: replyId || undefined
media_ids: mediaURL && [ mediaId ] || undefined,
in_reply_to_id: replyId || undefined,
spoiler_text: subject || undefined
}, { arrayFormat: 'brackets' })
});
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 {
const chunks = userInput.trim().split(' ');
if(chunks.length < 2) throw '';
if(!chunks.length || chunks.length < !!isReply + !!hasMedia) throw '';
let replyId = null;
let mediaURL = null;
const subject = hasSubject ? registrar.config.matrix.subject : 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(' '), replyId, registrar);
if(hasMedia) {
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 '';
mediaURL = url.toString();
}
return await run(client, room, chunks.join(' '), replyId, mediaURL, subject, registrar);
} catch(e) {
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'),
quote: quote ? {
path: quote.querySelector('a.quote-link').href,
text: quote.querySelector('.quote-text').innerHTML,
text: quote.querySelector('.quote-text') ? quote.querySelector('.quote-text').innerHTML : '',
} : null,
isReply: isReply && replies.length > 0 ? replies[replies.length - 1].classList.contains('unavailable') ? 'unavailable' : {
path: replies[replies.length - 1].querySelector('a.tweet-link').href,

View file

@ -4,7 +4,8 @@ module.exports = {
user: 'your_user',
password: 'your_password',
domains: [ 'your_homeserver.com' ],
mimetypes: [ 'image/png', 'image/jpeg' ]
mimetypes: [ 'image/png', 'image/jpeg', 'video/webm', 'image/jpg', 'video/mp4', 'audio/mp3' ],
subject: ''
},
fediverse: {
domain: 'https://your_federation.com',
@ -20,8 +21,8 @@ module.exports = {
domains: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'wwww.twitter.com' ]
},
invidious: {
domain: 'invidio.us',
domain: 'invidious.fdn.fr',
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.content.body.charAt(0) === '+') {
console.log(`Logs: ${event.event.sender} - ${event.event.content.body}`);
const args = event.event.content.body.slice(1).trim().split(/ +/g);
const command = args.shift().toLowerCase();
let args = event.event.content.body.slice(1).trim().split(/ +/g);
let command = args.shift().toLowerCase();
const userInput = args.join(' ');
const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1);
const address = args.slice(0, 1).join(' ').replace(/"/g, '');
if (command === 'boo') {
registrar.boo.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'beg') {
registrar.beg.runQuery(matrixClient, room, registrar);
}
if (command === 'clap') {
registrar.clap.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'copy') {
registrar.copy.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'flood') {
registrar.flood.runQuery(matrixClient, room, registrar);
}
if (command === 'fren') {
registrar.fren.runQuery(matrixClient, room, userInput, registrar);
}
if (command === 'help') {
registrar.help.runQuery(matrixClient, room);
}
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);
args = [];
switch(command) {
case 'config':
return;
case 'help': case 'beg': case 'flood': case 'notify':
args.push(matrixClient, room, registrar);
break;
case 'tip':
args.push(matrixClient, room, address, flaggedInput, registrar);
break;
case 'archive': case 'rearchive':
args.push(matrixClient, room, userInput, !!~command.indexOf('re'), registrar);
command = 'archive';
break;
case 'plemara': case 'reply': case 'media': case 'mediareply':
case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
args.push(matrixClient, room, userInput, registrar, {
isReply: !!~command.indexOf('reply'),
hasMedia: !!~command.indexOf('media'),
hasSubject: !!~command.indexOf('random'),
});
command = 'media';
break;
default:
args.push(matrixClient, room, userInput, registrar);
}
registrar[command] && registrar[command].runQuery.call(null, args);
}
});

View file

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

1044
yarn.lock

File diff suppressed because it is too large Load diff