fix fediverse fetch/axios

This commit is contained in:
les 2020-02-05 00:48:55 +01:00
parent 5b5474e61c
commit f820c70a25
7 changed files with 35 additions and 33 deletions

View file

@ -5,7 +5,14 @@ const config = require('config')
const fs = require('fs')
const { Op } = require('sequelize')
const _ = require('lodash')
const { event: Event, resource: Resource, tag: Tag, place: Place, notification: Notification } = require('../models')
const {
event: Event,
resource: Resource,
tag: Tag,
place: Place,
notification: Notification,
ap_user: APUser
} = require('../models')
const Sequelize = require('sequelize')
const exportController = require('./export')
const sanitizeHtml = require('sanitize-html')
@ -85,12 +92,18 @@ const eventController = {
try {
event = await Event.findByPk(id, {
attributes: {
exclude: ['createdAt', 'updatedAt']
exclude: ['createdAt', 'updatedAt', 'placeId']
},
include: [
{ model: Tag, attributes: ['tag', 'weigth'], through: { attributes: [] } },
{ model: Tag, required: false, attributes: ['tag', 'weigth'], through: { attributes: [] } },
{ model: Place, attributes: ['name', 'address'] },
{ model: Resource, where: !is_admin && { hidden: false }, required: false },
{
model: Resource,
where: !is_admin && { hidden: false },
include: [{ model: APUser, required: false, attributes: ['object', 'ap_id'] }],
required: false,
attributes: ['id', 'activitypub_id', 'data', 'hidden']
},
{ model: Event, required: false, as: 'parent' }
],
order: [[Resource, 'id', 'DESC']]
@ -98,7 +111,6 @@ const eventController = {
} catch (e) {
return res.sendStatus(400)
}
if (event && (event.is_visible || is_admin)) {
event = event.toJSON()
event.tags = event.tags.map(t => t.tag)

View file

@ -1,15 +1,9 @@
const Sequelize = require('sequelize')
const { ap_user: APUser, instance: Instance, resource: Resource } = require('../models')
const instancesController = {
async getAll (req, res) {
const instances = await Instance.findAll({
attributes: {
include: [[Sequelize.fn('count', Sequelize.col('domain')), 'users']]
},
group: ['domain'],
include: [{ model: APUser, attributes: [] }]
})
const instances = await Instance.findAll()
return res.json(instances)
},

View file

@ -1,5 +1,6 @@
const config = require('config')
const moment = require('moment-timezone')
// const debug = require('debug')('event:modals')
module.exports = (sequelize, DataTypes) => {
const Event = sequelize.define('event', {
@ -24,7 +25,6 @@ module.exports = (sequelize, DataTypes) => {
image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN,
recurrent: DataTypes.JSON,
// parent: DataTypes.INTEGER,
likes: { type: DataTypes.JSON, defaultValue: [] },
boost: { type: DataTypes.JSON, defaultValue: [] }
}, {})
@ -76,8 +76,8 @@ module.exports = (sequelize, DataTypes) => {
})),
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: follower || [],
to: follower || [],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
content,
summary: null,
sensitive: false

View file

@ -1,5 +1,4 @@
module.exports = (sequelize, DataTypes) => {
const Resource = sequelize.define('resource', {
activitypub_id: {
type: DataTypes.STRING,

View file

@ -1,4 +1,4 @@
const fetch = require('axios')
const axios = require('axios')
// const request = require('request')
const crypto = require('crypto')
const config = require('config')
@ -41,7 +41,7 @@ const Helpers = {
const signature_b64 = signature.toString('base64')
const header = `keyId="${config.baseurl}/federation/u/${settingsController.settings.instance_name}",headers="(request-target) host date",signature="${signature_b64}"`
try {
const ret = await fetch(inbox, {
const ret = await axios(inbox, {
headers: {
Host: inboxUrl.hostname,
Date: d.toUTCString(),
@ -49,10 +49,10 @@ const Helpers = {
'Content-Type': 'application/activity+json; charset=utf-8',
Accept: 'application/activity+json, application/json; chartset=utf-8'
},
method: 'POST',
body: JSON.stringify(message)
method: 'post',
data: JSON.stringify(message)
})
debug('sign %s => %s', ret.status, await ret.text())
debug('sign %s => %s', ret.status, ret.data)
} catch (e) {
debug('ERROR ', e.toString())
}
@ -77,11 +77,9 @@ const Helpers = {
const body = {
id: `${config.baseurl}/federation/m/${event.id}#create`,
type,
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: [`${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`, ...recipients[sharedInbox]],
// cc: recipients[sharedInbox],
to: recipients[sharedInbox],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
// object: event.toNoteAP(instanceAdmin.username, [`${config.baseurl}/federation/u/${instanceAdmin.username}/followers`, ...recipients[sharedInbox]])
object: event.toNoteAP(settingsController.settings.instance_name, recipients[sharedInbox])
}
body['@context'] = [
@ -106,13 +104,13 @@ const Helpers = {
}
}
fedi_user = await fetch(URL, { headers: { Accept: 'application/jrd+json, application/json' } })
fedi_user = await axios.get(URL, { headers: { Accept: 'application/jrd+json, application/json' } })
.then(res => {
if (!res.ok) {
if (res.status !== 200) {
debug('[ERR] Actor %s => %s', URL, res.statusText)
return false
}
return res.json()
return res.data
})
if (fedi_user) {
@ -132,8 +130,8 @@ const Helpers = {
if (instance) { return instance }
}
instance = await fetch(`${instance_url}/api/v1/instance`, { headers: { Accept: 'application/json' } })
.then(res => res.json())
instance = await axios.$get(`${instance_url}/api/v1/instance`, { headers: { Accept: 'application/json' } })
// .then(res => { console.error(res.data); return res.data })
.then(instance => {
const data = {
stats: instance.stats,

View file

@ -21,7 +21,7 @@ const app = express()
app.use(spamFilter)
app.use((req, res, next) => {
debug(req.path)
debug(req.method, req.path)
next()
})

View file

@ -13,7 +13,6 @@ class Task {
}
process () {
debug('PROCESS ', this.name)
this.processInNTick--
if (this.processInNTick > 0) {
return