fediblock-instance/lib/apexcustom.js

106 lines
7.0 KiB
JavaScript
Raw Permalink Normal View History

2024-09-15 17:44:53 +00:00
// custom side-effects for your app
module.exports = (app, apex, client) => {
const util = require('./util')(apex),
constant = require('./constant'),
https = require('https')
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') {
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
})
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) {
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, {
headers: { 'User-Agent': constant.agent },
agent: new https.Agent({
rejectUnauthorized: false,
keepAlive: false
}),
signal: ac2.signal
}),
res = await result.json()
if (res && res.instances && Array.isArray(res.instances)) {
res.instances.map(async r => {
if (r.domain === instance) {
await util.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 util.sendFederatedMessage(constant.nick, null, 'Instance ' + instance + ' has ' + json.blocks.length + ' blocks - https://' + constant.apexdomain + '/#' + instance, msg.actor.id, msg.object.id)
}
} else {
await util.sendFederatedMessage(constant.nick, null, 'Instance ' + instance + ' not found, next try.', msg.actor.id, msg.object.id)
}
} catch (e) {
await util.sendFederatedMessage(constant.nick, null, e.message, msg.actor.id, msg.object.id)
// console.error(e)
}
} else {
await util.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))
}
})
}