diff --git a/lib/apexcustom.js b/lib/apexcustom.js index 0c7c794..3a685b7 100644 --- a/lib/apexcustom.js +++ b/lib/apexcustom.js @@ -24,7 +24,7 @@ module.exports = (app, apex, client) => { && msg.activity.type) { const type = msg.activity.type.toLowerCase() if (type === 'follow' && msg.recipient.type.toLowerCase() === 'person') { - const follow = await apex.acceptFollow(msg.recipient, msg.activity) + // const follow = await apex.acceptFollow(msg.recipient, msg.activity) const act = await apex.buildActivity('Accept', apex.utils.usernameToIRI(constant.nick), ['https://www.w3.org/ns/activitystreams#Public'].concat([msg.actor.id]), { actor: msg.recipient, object: msg.activity @@ -36,35 +36,31 @@ module.exports = (app, apex, client) => { content = ('' + msg.object.content).replace(/<[^>]*>?/gm, '').split(' ').filter(token => !token.startsWith('@')) if (msg.recipient.id === apex.utils.usernameToIRI(constant.nick)) { if (content.length === 1) { - const instance = content.join('').trim(), - ac = new AbortController() try { - setTimeout(() => { - ac.abort() - }, 5000) - const response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, { - headers: { 'User-Agent': constant.agent }, - agent: new https.Agent({ - rejectUnauthorized: false, - keepAlive: false - }), - signal: ac.signal - }), - json = await response.json() - if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) { - const ac2 = new AbortController() - setTimeout(() => { - ac2.abort() - }, 5000) - const result = await fetch('https://' + constant.apexdomain + '/api/list/' + instance, { + const instance = content.join('').trim(), + ac = new AbortController(), + timeout = setTimeout(() => { + ac.abort() + }, 5000), + response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, { headers: { 'User-Agent': constant.agent }, - agent: new https.Agent({ - rejectUnauthorized: false, - keepAlive: false - }), - signal: ac2.signal - }), - res = await result.json() + signal: ac.signal, + keepalive: false + }) + clearTimeout(timeout) + const json = await response.json() + if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) { + const ac = new AbortController(), + timeout = setTimeout(() => { + ac.abort() + }, 5000), + result = await fetch('https://' + constant.apexdomain + '/api/list/' + instance, { + headers: { 'User-Agent': constant.agent }, + signal: ac.signal, + keepalive: false + }) + clearTimeout(timeout) + const res = await result.json() if (res && res.instances && Array.isArray(res.instances)) { res.instances.map(async r => { if (r.domain === instance) { diff --git a/lib/apiswagger.js b/lib/apiswagger.js index 84141ce..3ad00f8 100644 --- a/lib/apiswagger.js +++ b/lib/apiswagger.js @@ -4,7 +4,7 @@ module.exports = (app, client) => { const constant = require('./constant'), zlib = require('zlib'), clean = str => { - return str.replace(/[\/\\^$+?()`'¡¿¨!"·%&=;,\|\[\]{}]+/gmi, '') + return str.replace(/[/\\^$+?()`'¡¿¨!"·%&=;,\|\[\]{}]+/gmi, '') } /** * @swagger @@ -185,7 +185,7 @@ module.exports = (app, client) => { } } }) - const ranking = result.aggregations.blocks.ranking.buckets + const ranking = result.aggregations?.blocks?.ranking?.buckets res.json(ranking.map(r => ({ domain: r.key, count: r.doc_count }))) }) /** @@ -253,7 +253,7 @@ module.exports = (app, client) => { } } }) - const instances = result.hits && result.hits.hits && result.hits.hits.length > 0 ? result.hits.hits : [], + const instances = result.hits?.hits?.length > 0 ? result.hits.hits : [], suggests = result.suggest.suggests && result.suggest.suggests.length > 0 && result.suggest.suggests[0].options.length > 0 ? result.suggest.suggests[0].options : [] res.json({ instances: instances.map(instance => ({ @@ -319,7 +319,7 @@ module.exports = (app, client) => { } } }) - const instances = result.hits && result.hits.hits && result.hits.hits.length > 0 ? result.hits.hits : [] + const instances = result.hits?.hits?.length > 0 ? result.hits.hits : [] res.json(instances.length > 0 && instances[0]._source.blocks && instances[0]._source.blocks.length > 0 ? { blocks: instances[0]._source.blocks.map(instance => ({ domain: instance.domain, comment: instance.comment, severity: instance.severity })), @@ -368,7 +368,7 @@ module.exports = (app, client) => { } } }) - const instances = result.hits && result.hits.hits && result.hits.hits.length > 0 ? result.hits.hits : [] + const instances = result.hits?.hits?.length > 0 ? result.hits.hits : [] res.json(instances.length > 0 ? instances[0]._source.api : {}) } else { res.status(404).end() @@ -409,7 +409,7 @@ module.exports = (app, client) => { } } }) - const instances = result.hits && result.hits.hits && result.hits.hits.length > 0 ? result.hits.hits : [] + const instances = result.hits?.hits?.length > 0 ? result.hits.hits : [] res.json(instances.length > 0 ? instances[0]._source.nodeinfo : {}) } else { res.status(404).end() @@ -473,7 +473,7 @@ module.exports = (app, client) => { } } }) - const instances = result.hits && result.hits.hits && result.hits.hits.length > 0 ? result.hits.hits : [], + const instances = result.hits?.hits?.length > 0 ? result.hits.hits : [], instancescomment = instances.map(i => ({ instance: i._source.instance, comment: i._source.blocks.find(block => block.domain === clean(req.params.instance)).comment })) diff --git a/lib/fediblock.js b/lib/fediblock.js index 195f517..03f526d 100644 --- a/lib/fediblock.js +++ b/lib/fediblock.js @@ -1,5 +1,4 @@ let servers -process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 module.exports = async (client, apex, app) => { const util = require('./util')(apex), constant = require('./constant'), @@ -21,13 +20,13 @@ module.exports = async (client, apex, app) => { const ac = new AbortController(), timeout = setTimeout(() => { ac.abort() - }, constant.abort_timeout) - const response = await fetch(uri, { - headers: { 'User-Agent': constant.agent }, - signal: ac.signal, - keepalive: false, - timeout: constant.timeout, - }) + }, constant.abort_timeout), + response = await fetch(uri, { + headers: { 'User-Agent': constant.agent }, + signal: ac.signal, + keepalive: false, + timeout: constant.timeout, + }) clearTimeout(timeout) return await response.json() }, diff --git a/lib/util.js b/lib/util.js index 1fe321c..a9d63e0 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,45 +1,46 @@ module.exports = apex => { -const urlToId = url => { - try { - if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) { - return '@' + url.split('/')[4] + '@' + url.split('/')[2] - } else { - return url - } - } catch (e) { - return url - } -}, - sendFederatedMessage = async (id, summary, message, recipient, reply) => { - const users = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) - .map(token => `https://${token.split('@')[2]}/users/${token.split('@')[1]}`), - mentions = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) - .map(token => { return { type: 'Mention', href: `https://${token.split('@')[2]}/users/${token.split('@')[1]}`, name: `@${token.split('@')[1]}` } }), - hashtags = message.split(' ').filter(token => token.startsWith('#')) - .map(token => { return { id: `https://mastodon.social/tags/${token.split('#')[1]}`, name: token } }), - images = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(jpg|png|gif|jpeg|ppm)$/i)) - .map(token => { return { type: 'Image', url: token } }), - audio = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp3|wav|flac)$/i)) - .map(token => { return { type: 'Audio', url: token } }), - video = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp4|flv|avi)$/i)) - .map(token => { return { type: 'Video', url: token } }), - links = message.split(' ').filter(token => token.match(/^https?:\/\//i) && !images.concat(audio).concat(video) - .some(link => link.url === token)).map(token => { return { type: 'Link', href: token } }), - allusers = users.concat([recipient ? recipient : (await apex.store.getObject(apex.utils.usernameToIRI(id), true)).followers[0]]), - act = await apex.buildActivity('Create', apex.utils.usernameToIRI(id), ['https://www.w3.org/ns/activitystreams#Public'].concat(allusers), { - object: { - type: 'Note', - name: `Fediblock Instance`, - summary: summary ? summary : null, - content: `

${message.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)([-A-Z0-9+&@#%?\/=~_|!:,.;]*))/ig, - '$3').replace(/\s#(\S+)/g, '#$1').replace(new RegExp('\r?\n', 'g'), '
')}

`, - tag: hashtags.concat(mentions), - attachment: images.concat(links).concat(audio).concat(video), - inReplyTo: reply ? reply : null + const constant = require("./constant"), + urlToId = url => { + try { + if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) { + return '@' + url.split('/')[4] + '@' + url.split('/')[2] + } else { + return url } - }) - act.object[0].id = act.id - await apex.addToOutbox(await apex.store.getObject(apex.utils.usernameToIRI(id), true), act) - } + } catch (e) { + return url + } + }, + sendFederatedMessage = async (id, summary, message, recipient, reply) => { + const users = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) + .map(token => `https://${token.split('@')[2]}/users/${token.split('@')[1]}`), + mentions = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) + .map(token => { return { type: 'Mention', href: `https://${token.split('@')[2]}/users/${token.split('@')[1]}`, name: `@${token.split('@')[1]}` } }), + hashtags = message.split(' ').filter(token => token.startsWith('#')) + .map(token => { return { id: `https://${constant.apexdomain}/tags/${token.split('#')[1]}`, name: token } }), + images = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(jpg|png|gif|jpeg|ppm)$/i)) + .map(token => { return { type: 'Image', url: token } }), + audio = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp3|wav|flac)$/i)) + .map(token => { return { type: 'Audio', url: token } }), + video = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp4|flv|avi)$/i)) + .map(token => { return { type: 'Video', url: token } }), + links = message.split(' ').filter(token => token.match(/^https?:\/\//i) && !images.concat(audio).concat(video) + .some(link => link.url === token)).map(token => { return { type: 'Link', href: token } }), + allusers = users.concat([recipient || (await apex.store.getObject(apex.utils.usernameToIRI(id), true)).followers[0]]), + act = await apex.buildActivity('Create', apex.utils.usernameToIRI(id), ['https://www.w3.org/ns/activitystreams#Public'].concat(allusers), { + object: { + type: 'Note', + name: `Fediblock Instance`, + summary: summary || null, + content: `

${message.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)([-A-Z0-9+&@#%?/=~_|!:,.;]*))/ig, + '$3').replace(/\s#(\S+)/g, '#$1').replace(/\r?\n/g, '
')}

`, + tag: hashtags.concat(mentions), + attachment: images.concat(links).concat(audio).concat(video), + inReplyTo: reply || null + } + }) + act.object[0].id = act.id + await apex.addToOutbox(await apex.store.getObject(apex.utils.usernameToIRI(id), true), act) + } return { urlToId, sendFederatedMessage } } diff --git a/server.js b/server.js index a60a85e..3307838 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,4 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 const apexinstance = require('./lib/apex'), apexcustom = require('./lib/apexcustom'), apiswagger = require('./lib/apiswagger'),