101 lines
6.8 KiB
JavaScript
101 lines
6.8 KiB
JavaScript
// custom side-effects for your app
|
|
module.exports = (app, apex, client) => {
|
|
const util = require('./util')(apex),
|
|
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') {
|
|
// 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) {
|
|
try {
|
|
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 },
|
|
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) {
|
|
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))
|
|
}
|
|
})
|
|
}
|