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.abort() ac = new AbortController(),
}, 5000) timeout = setTimeout(() => {
const response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, { ac.abort()
headers: { 'User-Agent': constant.agent }, }, 5000),
agent: new https.Agent({ response = await fetch('https://' + constant.apexdomain + '/api/detail/' + instance, {
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, {
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 json = await response.json()
}), if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) {
res = await result.json() 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)) { 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,13 +20,13 @@ 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,
timeout: constant.timeout, timeout: constant.timeout,
}) })
clearTimeout(timeout) clearTimeout(timeout)
return await response.json() return await response.json()
}, },

View File

@ -1,45 +1,46 @@
module.exports = apex => { module.exports = apex => {
const urlToId = url => { const constant = require("./constant"),
try { urlToId = url => {
if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) { try {
return '@' + url.split('/')[4] + '@' + url.split('/')[2] if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) {
} else { return '@' + url.split('/')[4] + '@' + url.split('/')[2]
return url } 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: `<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>`,
tag: hashtags.concat(mentions),
attachment: images.concat(links).concat(audio).concat(video),
inReplyTo: reply ? reply : null
} }
}) } catch (e) {
act.object[0].id = act.id return url
await apex.addToOutbox(await apex.store.getObject(apex.utils.usernameToIRI(id), true), act) }
} },
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: `<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(/\r?\n/g, '<br />')}</p>`,
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 } return { urlToId, sendFederatedMessage }
} }

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'),