initial commit
Todas las comprobaciones han sido exitosas
continuous-integration/drone Build is passing
Todas las comprobaciones han sido exitosas
continuous-integration/drone Build is passing
Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
83
back/lib/util.js
Archivo normal
83
back/lib/util.js
Archivo normal
@@ -0,0 +1,83 @@
|
||||
const constant = require("../constant"),
|
||||
{ apex } = require('./apex'),
|
||||
https = require('node:https'),
|
||||
json = 'application/json',
|
||||
urlToId = url => {
|
||||
try {
|
||||
if (typeof new URL(url) === 'object' && url.split('/').length === 5 && url.split('/')[3].length > 0) {
|
||||
return '@' + url.split('/')[4] + '@' + url.split('/')[2]
|
||||
} 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://${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)
|
||||
},
|
||||
requestPart = uri => new Promise((resolve, reject) => {
|
||||
try {
|
||||
const data = [],
|
||||
req = https.request(uri, {
|
||||
headers: {
|
||||
'User-Agent': constant.agent,
|
||||
'Content-Type': json
|
||||
},
|
||||
signal: AbortSignal.timeout(constant.abort_timeout),
|
||||
}, res => {
|
||||
res.setEncoding('utf8')
|
||||
res.headers['Content-Type'] = json
|
||||
res.on('data', chunk => {
|
||||
data.push(chunk)
|
||||
})
|
||||
res.on('error', error => {
|
||||
reject(error)
|
||||
})
|
||||
res.on('end', () => {
|
||||
try {
|
||||
resolve(JSON.parse(data.join('')))
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
req.on('error', error => {
|
||||
reject(error)
|
||||
})
|
||||
req.end()
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = { urlToId, sendFederatedMessage, requestPart }
|
||||
|
||||
Referencia en una nueva incidencia
Block a user