Files
fediblock-instance/back/lib/apexcustom.js
ale bfaab359eb
Todas las comprobaciones han sido exitosas
continuous-integration/drone Build is passing
initial commit
Signed-off-by: ale <ale@manalejandro.com>
2025-07-18 22:19:35 +02:00

98 líneas
6.8 KiB
JavaScript

// custom side-effects for your app
module.exports = (app, apex, client) => {
const { sendFederatedMessage, requestPart } = require('./util'),
constant = require('../constant')
app.on('apex-outbox', msg => {
if (typeof msg === 'object'
&& !Object.keys(msg).filter(prop => msg[prop]
&& typeof msg[prop] === 'object').some(prop => !(msg[prop].hasOwnProperty('id')
&& msg[prop].hasOwnProperty('type')))
&& msg.activity
&& msg.activity.type) {
console.log(`New ${msg.activity.type} from ${msg.actor.id} to ${msg.recipient.id}`)
} else {
console.log(JSON.stringify(msg, '', 2))
}
})
app.on('apex-inbox', async msg => {
if (typeof msg === 'object'
&& !Object.keys(msg).filter(prop => msg[prop]
&& typeof msg[prop] === 'object').some(prop => !(msg[prop].hasOwnProperty('id')
&& msg[prop].hasOwnProperty('type')))
&& msg.activity
&& msg.activity.type) {
const type = msg.activity.type.toLowerCase()
if (type === 'follow' && msg.recipient.type.toLowerCase() === 'person') {
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
})
await apex.addToOutbox(await apex.store.getObject(apex.utils.usernameToIRI(constant.nick), true), act)
}
if (type === 'create' && msg.object.type.toLowerCase() === 'note' && msg.actor.preferredUsername && msg.object.content) {
const name = Array.isArray(msg.actor.preferredUsername) ? msg.actor.preferredUsername[0] : msg.actor.preferredUsername,
content = ('' + msg.object.content).replace(/<[^>]*>?/gm, '').split(' ').filter(token => !token.startsWith('@'))
if (msg.recipient.id === apex.utils.usernameToIRI(constant.nick)) {
if (content.length === 1) {
try {
const instance = content.join('').trim()
if (instance === 'stats') {
const statsres = await requestPart('https://' + constant.apexdomain + '/api/stats')
await sendFederatedMessage(constant.nick, null, `STATS\n
Statuses AVG: ${Math.round(statsres.status_avg)}
Statuses MAX: ${statsres.status_max}
Domain AVG: ${Math.round(statsres.domain_avg)}
Domain MAX: ${statsres.domain_max}
Users AVG: ${Math.round(statsres.user_avg)}
Users MAX: ${statsres.user_max}
Stats Instances: ${statsres.stats_filtered}
Total Instances: ${statsres.instance_count}
Users by Instance: ${(Math.round(statsres.user_avg) / statsres.instance_count).toFixed(2)}
Statuses by Domain: ${(Math.round(statsres.status_avg) / Math.round(statsres.domain_avg)).toFixed(2)}
Statuses by User: ${(Math.round(statsres.status_avg) / Math.round(statsres.user_avg)).toFixed(2)}
https://${constant.apexdomain}`, msg.actor.id, msg.object.id)
} else {
const json = await requestPart('https://' + constant.apexdomain + '/api/detail/' + instance)
if (json && json.blocks && Array.isArray(json.blocks) && json.blocks.length > 0) {
const res = await requestPart('https://' + constant.apexdomain + '/api/list/' + instance)
if (res && res.instances && Array.isArray(res.instances)) {
res.instances.map(async r => {
if (r.domain === instance) {
await sendFederatedMessage(constant.nick, null, 'Instance ' + instance + ' has ' + json.blocks.length + ' blocks\n'
+ (r.api.title ? '\n'
+ r.api.title + ' - ' + r.api.uri + '\n'
+ (r.api.email ? 'Email: ' + r.api.email + '\n' : '')
+ 'Registration: ' + (r.api.registrations ? 'open' : 'closed') + ' - Version: ' + r.api.version + '\n'
+ (r.api.stats ? 'Users: ' + r.api.stats.user_count + ' - Statuses: ' + r.api.stats.status_count + ' - Domains: ' + r.api.stats.domain_count + '\n' : '')
+ (r.api.description ? 'Description: ' + r.api.description + '\n' : '') : '')
+ 'https://' + constant.apexdomain + '/#' + instance, msg.actor.id, msg.object.id)
}
})
} else {
await sendFederatedMessage(constant.nick, null, 'Instance ' + instance + ' has ' + json.blocks.length + ' blocks - https://' + constant.apexdomain + '/#' + instance, msg.actor.id, msg.object.id)
}
} else {
await sendFederatedMessage(constant.nick, null, 'Instance ' + instance + ' not found, next try.', msg.actor.id, msg.object.id)
}
}
} catch (e) {
await sendFederatedMessage(constant.nick, null, e.message, msg.actor.id, msg.object.id)
// console.error(e)
}
} else {
await sendFederatedMessage(constant.nick, 'Hi ' + name, 'I know ' + (await client.count({ index: constant.index })).count + ' Federated Instances\nScanning ' + app.locals.server + ' instance with ' + app.locals.scantotal + ' peers\nScanned ' + app.locals.peers + ' peers from ' + app.locals.instances + ' instances, ' + app.locals.created + ' created, ' + app.locals.updated + ' updated\nhttps://' + constant.apexdomain, msg.actor.id, msg.object.id)
}
}
console.log(`New note from ${name} to ${msg.recipient.id}: ${content.join(' ')}`)
} else if (type !== 'delete') {
console.log(`New ${msg.activity.type} from ${msg.actor.id} to ${msg.recipient.id}`)
} else {
console.log(`New ${msg.activity.type} from ${msg.actor.id} to ${msg.recipient.id}`)
}
} else {
console.log(JSON.stringify(msg, '', 2))
}
})
}