some sonar changes

This commit is contained in:
ale 2024-09-26 02:23:49 +02:00
parent 9e728583c4
commit ffce342aa1
5 changed files with 81 additions and 84 deletions

View File

@ -24,7 +24,7 @@ module.exports = (app, apex, client) => {
&& msg.activity.type) { && msg.activity.type) {
const type = msg.activity.type.toLowerCase() const type = msg.activity.type.toLowerCase()
if (type === 'follow' && msg.recipient.type.toLowerCase() === 'person') { 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]), { 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, actor: msg.recipient,
object: msg.activity object: msg.activity
@ -36,35 +36,31 @@ module.exports = (app, apex, client) => {
content = ('' + msg.object.content).replace(/<[^>]*>?/gm, '').split(' ').filter(token => !token.startsWith('@')) content = ('' + msg.object.content).replace(/<[^>]*>?/gm, '').split(' ').filter(token => !token.startsWith('@'))
if (msg.recipient.id === apex.utils.usernameToIRI(constant.nick)) { if (msg.recipient.id === apex.utils.usernameToIRI(constant.nick)) {
if (content.length === 1) { if (content.length === 1) {
const instance = content.join('').trim(),
ac = new AbortController()
try { try {
setTimeout(() => { const instance = content.join('').trim(),
ac = new AbortController(),
timeout = setTimeout(() => {
ac.abort() ac.abort()
}, 5000) }, 5000),
const response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, { response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, {
headers: { 'User-Agent': constant.agent }, headers: { 'User-Agent': constant.agent },
agent: new https.Agent({ signal: ac.signal,
rejectUnauthorized: false, keepalive: false
keepAlive: false })
}), clearTimeout(timeout)
signal: ac.signal const json = await response.json()
}),
json = await response.json()
if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) { if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) {
const ac2 = new AbortController() const ac = new AbortController(),
setTimeout(() => { timeout = setTimeout(() => {
ac2.abort() ac.abort()
}, 5000) }, 5000),
const result = await fetch('https://' + constant.apexdomain + '/api/list/' + instance, { result = await fetch('https://' + constant.apexdomain + '/api/list/' + instance, {
headers: { 'User-Agent': constant.agent }, headers: { 'User-Agent': constant.agent },
agent: new https.Agent({ signal: ac.signal,
rejectUnauthorized: false, keepalive: false
keepAlive: false })
}), clearTimeout(timeout)
signal: ac2.signal const res = await result.json()
}),
res = await result.json()
if (res && res.instances && Array.isArray(res.instances)) { if (res && res.instances && Array.isArray(res.instances)) {
res.instances.map(async r => { res.instances.map(async r => {
if (r.domain === instance) { if (r.domain === instance) {

View File

@ -4,7 +4,7 @@ module.exports = (app, client) => {
const constant = require('./constant'), const constant = require('./constant'),
zlib = require('zlib'), zlib = require('zlib'),
clean = str => { clean = str => {
return str.replace(/[\/\\^$+?()`'¡¿¨!"·%&=;,\|\[\]{}]+/gmi, '') return str.replace(/[/\\^$+?()`'¡¿¨!"·%&=;,\|\[\]{}]+/gmi, '')
} }
/** /**
* @swagger * @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 }))) 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 : [] suggests = result.suggest.suggests && result.suggest.suggests.length > 0 && result.suggest.suggests[0].options.length > 0 ? result.suggest.suggests[0].options : []
res.json({ res.json({
instances: instances.map(instance => ({ 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 ? 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 })), 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 : {}) res.json(instances.length > 0 ? instances[0]._source.api : {})
} else { } else {
res.status(404).end() 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 : {}) res.json(instances.length > 0 ? instances[0]._source.nodeinfo : {})
} else { } else {
res.status(404).end() 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 => ({ instancescomment = instances.map(i => ({
instance: i._source.instance, comment: i._source.blocks.find(block => block.domain === clean(req.params.instance)).comment instance: i._source.instance, comment: i._source.blocks.find(block => block.domain === clean(req.params.instance)).comment
})) }))

View File

@ -1,5 +1,4 @@
let servers let servers
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
module.exports = async (client, apex, app) => { module.exports = async (client, apex, app) => {
const util = require('./util')(apex), const util = require('./util')(apex),
constant = require('./constant'), constant = require('./constant'),
@ -21,8 +20,8 @@ module.exports = async (client, apex, app) => {
const ac = new AbortController(), const ac = new AbortController(),
timeout = setTimeout(() => { timeout = setTimeout(() => {
ac.abort() ac.abort()
}, constant.abort_timeout) }, constant.abort_timeout),
const response = await fetch(uri, { response = await fetch(uri, {
headers: { 'User-Agent': constant.agent }, headers: { 'User-Agent': constant.agent },
signal: ac.signal, signal: ac.signal,
keepalive: false, keepalive: false,

View File

@ -1,5 +1,6 @@
module.exports = apex => { module.exports = apex => {
const urlToId = url => { const constant = require("./constant"),
urlToId = url => {
try { try {
if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) { if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) {
return '@' + url.split('/')[4] + '@' + url.split('/')[2] return '@' + url.split('/')[4] + '@' + url.split('/')[2]
@ -9,14 +10,14 @@ const urlToId = url => {
} catch (e) { } catch (e) {
return url return url
} }
}, },
sendFederatedMessage = async (id, summary, message, recipient, reply) => { sendFederatedMessage = async (id, summary, message, recipient, reply) => {
const users = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) const users = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3)
.map(token => `https://${token.split('@')[2]}/users/${token.split('@')[1]}`), .map(token => `https://${token.split('@')[2]}/users/${token.split('@')[1]}`),
mentions = message.split(' ').filter(token => token.startsWith('@') && token.split('@').length === 3) 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]}` } }), .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('#')) hashtags = message.split(' ').filter(token => token.startsWith('#'))
.map(token => { return { id: `https://mastodon.social/tags/${token.split('#')[1]}`, name: token } }), .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)) images = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(jpg|png|gif|jpeg|ppm)$/i))
.map(token => { return { type: 'Image', url: token } }), .map(token => { return { type: 'Image', url: token } }),
audio = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp3|wav|flac)$/i)) audio = message.split(' ').filter(token => token.match(/^https?:\/\//i) && token.match(/\.(mp3|wav|flac)$/i))
@ -25,17 +26,17 @@ const urlToId = url => {
.map(token => { return { type: 'Video', url: token } }), .map(token => { return { type: 'Video', url: token } }),
links = message.split(' ').filter(token => token.match(/^https?:\/\//i) && !images.concat(audio).concat(video) 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 } }), .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]]), 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), { act = await apex.buildActivity('Create', apex.utils.usernameToIRI(id), ['https://www.w3.org/ns/activitystreams#Public'].concat(allusers), {
object: { object: {
type: 'Note', type: 'Note',
name: `Fediblock Instance`, name: `Fediblock Instance`,
summary: summary ? summary : null, summary: summary || null,
content: `<p>${message.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)([-A-Z0-9+&@#%?\/=~_|!:,.;]*))/ig, content: `<p>${message.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)([-A-Z0-9+&@#%?/=~_|!:,.;]*))/ig,
'<a href="$1" target="_blank">$3</a>').replace(/\s#(\S+)/g, '<a href="https://mastodon.social/tags/$1">#$1</a>').replace(new RegExp('\r?\n', 'g'), '<br />')}</p>`, '<a href="$1" target="_blank">$3</a>').replace(/\s#(\S+)/g, '<a href="https://mastodon.social/tags/$1">#$1</a>').replace(/\r?\n/g, '<br />')}</p>`,
tag: hashtags.concat(mentions), tag: hashtags.concat(mentions),
attachment: images.concat(links).concat(audio).concat(video), attachment: images.concat(links).concat(audio).concat(video),
inReplyTo: reply ? reply : null inReplyTo: reply || null
} }
}) })
act.object[0].id = act.id act.object[0].id = act.id

View File

@ -1,3 +1,4 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
const apexinstance = require('./lib/apex'), const apexinstance = require('./lib/apex'),
apexcustom = require('./lib/apexcustom'), apexcustom = require('./lib/apexcustom'),
apiswagger = require('./lib/apiswagger'), apiswagger = require('./lib/apiswagger'),