feat(nitter): show content of quoted

This commit is contained in:
vulet 2020-06-24 06:00:53 +08:00
parent d7396b8786
commit 2aeea1bc37

View file

@ -10,17 +10,27 @@ const nitter = async (instance, url) => {
const req = await instance({ method: 'GET', url }); const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req; if (req.statusText !== 'OK') throw req;
const dom = new JSDOM(req.data); const dom = new JSDOM(req.data);
const tweet = dom.window.document.querySelector('#m'); const document = dom.window.document;
const tweet = document.querySelector('#m');
const stats = tweet.querySelectorAll('.tweet-body > .tweet-stats .icon-container'); const stats = tweet.querySelectorAll('.tweet-body > .tweet-stats .icon-container');
const quote = tweet.querySelector('.tweet-body > .quote a.quote-link'); const quote = tweet.querySelector('.tweet-body > .quote');
const isReply = tweet.querySelector('.tweet-body > .replying-to');
const replies = document.querySelectorAll('.main-thread > .before-tweet > .timeline-item');
return { return {
text: tweet.querySelector('.tweet-body > .tweet-content').innerHTML, text: tweet.querySelector('.tweet-body > .tweet-content').innerHTML,
date: tweet.querySelector('.tweet-body > .tweet-published').textContent, date: tweet.querySelector('.tweet-body > .tweet-published').textContent,
name: tweet.querySelector('.tweet-body > div .fullname').textContent, name: tweet.querySelector('.tweet-body > div .fullname').textContent,
handle: tweet.querySelector('.tweet-body > div .username').textContent, handle: tweet.querySelector('.tweet-body > div .username').textContent,
hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'), hasAttachments: !!tweet.querySelector('.tweet-body > .attachments'),
quote: quote ? quote.href : null, quote: quote ? {
stats: { path: quote.querySelector('a.quote-link').href,
text: quote.querySelector('.quote-text').innerHTML,
} : null,
isReply: isReply && replies.length > 0 ? {
path: replies[replies.length - 1].querySelector('a.tweet-link').href,
text: replies[replies.length - 1].querySelector('.tweet-content').innerHTML,
} : null,
stats: {
replies: stats[0].textContent.trim(), replies: stats[0].textContent.trim(),
retweets: stats[1].textContent.trim(), retweets: stats[1].textContent.trim(),
favorites: stats[2].textContent.trim() favorites: stats[2].textContent.trim()
@ -36,8 +46,8 @@ const card = (tweet, base, path) =>
`<span>❤️ ${tweet.stats.favorites}</span> ` + `<span>❤️ ${tweet.stats.favorites}</span> ` +
`<br /><blockquote><b><i>${tweet.text.replace('\n', '<br />')}</i></b></blockquote>` + `<br /><blockquote><b><i>${tweet.text.replace('\n', '<br />')}</i></b></blockquote>` +
(tweet.hasAttachments ? '<blockquote><b>This tweet has attached media.</b></blockquote>' : '') + (tweet.hasAttachments ? '<blockquote><b>This tweet has attached media.</b></blockquote>' : '') +
(tweet.quote ? `<blockquote><b><a href="${base}${tweet.quote}">Quoted Tweet</a></b></blockquote>` : ''); (tweet.isReply ? `<blockquote><b><a href="${base}${tweet.isReply.path}">Replied Tweet</a></b><br /><b><i>${tweet.isReply.text.replace('\n', '<br />')}</i></b></blockquote>` : '') +
(tweet.quote ? `<blockquote><b><a href="${base}${tweet.quote.path}">Quoted Tweet</a></b><br /><b><i>${tweet.quote.text.replace('\n', '<br />')}</i></b></blockquote>` : '');
const run = async (matrixClient, { roomId }, userInput, registrar) => { const run = async (matrixClient, { roomId }, userInput, registrar) => {
const config = registrar.config.nitter; const config = registrar.config.nitter;
const instance = axios.create({ const instance = axios.create({
@ -49,7 +59,7 @@ const run = async (matrixClient, { roomId }, userInput, registrar) => {
const tweet = await nitter(instance, userInput); const tweet = await nitter(instance, userInput);
return await matrixClient.sendHtmlNotice(roomId, '', card(tweet, `https://${config.domain}`, userInput)); return await matrixClient.sendHtmlNotice(roomId, '', card(tweet, `https://${config.domain}`, userInput));
} }
exports.runQuery = async (client, room, userInput, registrar) => { exports.runQuery = async (client, room, userInput, registrar) => {
try { try {
const url = new URL(userInput); const url = new URL(userInput);