[lint] linting

This commit is contained in:
les
2019-10-28 17:33:20 +01:00
parent bda72f1559
commit e467a28902
56 changed files with 373 additions and 306 deletions

View File

@@ -1,5 +1,4 @@
const { event: Event, comment: Comment } = require('../api/models')
const config = require('config')
const debug = require('debug')('fediverse:comment')
module.exports = {
@@ -7,7 +6,7 @@ module.exports = {
const body = req.body
// search for related event
const inReplyTo = body.object.inReplyTo
const match = inReplyTo.match('.*\/federation\/m\/(.*)')
const match = inReplyTo.match('.*/federation/m/(.*)')
if (!match || match.length < 2) {
debug('Comment not found %s', inReplyTo)
return res.status(404).send('Event not found!')

View File

@@ -14,12 +14,12 @@ module.exports = {
},
async unboost (req, res) {
const match = req.body.object.match*`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) return res.status(404).send('Event not found!')
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
debug('unboost %s', match[1])
const event = await Event.findByPk(Number(match[1]))
if (!event) return res.status(404).send('Event not found!')
await event.update({ boost: [...event.boost.filter(actor => actor !== body.actor )]})
if (!event) { return res.status(404).send('Event not found!') }
await event.update({ boost: event.boost.filter(actor => actor !== req.body.actor) })
},
async bookmark (req, res) {
@@ -40,7 +40,7 @@ module.exports = {
const event = await Event.findByPk(Number(match[1]))
debug('%s unbookmark %s (%d)', body.actor, event.title, event.likes.length)
if (!event) { return res.status(404).send('Event not found!') }
await event.update({ likes: [...event.likes.filter(actor => actor !== body.actor)] })
await event.update({ likes: event.likes.filter(actor => actor !== body.actor) })
res.sendStatus(201)
}
}

View File

@@ -10,14 +10,14 @@ module.exports = {
const body = req.body
if (typeof body.object !== 'string') { return }
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers' }})
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers' } })
if (!user) { return res.status(404).send('User not found') }
// check for duplicate
if (!user.followers.includes(body.actor)) {
await user.addFollowers([req.fedi_user.id])
// await user.update({ followers: [...user.followers, body.actor] })
debug('%s followed by %s (%d)', username, body.actor, user.followers.length+1)
debug('%s followed by %s (%d)', username, body.actor, user.followers.length + 1)
} else {
debug('duplicate %s followed by %s', username, body.actor)
}
@@ -37,7 +37,7 @@ module.exports = {
async unfollow (req, res) {
const body = req.body
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '')
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers'} })
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers' } })
if (!user) { return res.status(404).send('User not found') }
if (body.actor !== body.object.actor || body.actor !== req.fedi_user.id) {
@@ -45,7 +45,7 @@ module.exports = {
return res.status(400).send('Bad things')
}
if (req.fedi_user) await user.removeFollowers(req.fedi_user.id)
if (req.fedi_user) { await user.removeFollowers(req.fedi_user.id) }
debug('%s unfollowed by %s', username, body.actor)
res.sendStatus(200)
}

View File

@@ -11,16 +11,16 @@ const settingsController = require('../api/controller/settings')
const Helpers = {
// ignore unimplemented ping url from fediverse
async spamFilter (req, res, next) {
spamFilter (req, res, next) {
const urlToIgnore = [
'/api/v1/instance',
'/api/meta',
'/api/statusnet/version.json',
'/api/gnusocial/version.json',
'/api/statusnet/config.json',
'/poco',
'/poco'
]
if (urlToIgnore.includes(req.path)) return res.status(404).send('Not Found')
if (urlToIgnore.includes(req.path)) { return res.status(404).send('Not Found') }
next()
},
@@ -52,7 +52,7 @@ const Helpers = {
debug('sign %s => %s', ret.status, await ret.text())
},
async sendEvent (event, user, type='Create') {
async sendEvent (event, user, type = 'Create') {
if (!settingsController.settings.enable_federation) {
debug('event not send, federation disabled')
return
@@ -60,7 +60,7 @@ const Helpers = {
// event is sent by user that published it and by the admin instance
// collect followers from admin and user
const instanceAdmin = await User.findOne({ where: { email: config.admin_email }, include: { model: FedUsers, as: 'followers' } })
const instanceAdmin = await User.findOne({ where: { email: config.admin_email }, include: { model: FedUsers, as: 'followers' } })
if (!instanceAdmin || !instanceAdmin.username) {
debug('Instance admin not found (there is no user with email => %s)', config.admin_email)
return
@@ -69,18 +69,18 @@ const Helpers = {
let recipients = {}
instanceAdmin.followers.forEach(follower => {
const sharedInbox = follower.object.endpoints.sharedInbox
if (!recipients[sharedInbox]) recipients[sharedInbox] = []
if (!recipients[sharedInbox]) { recipients[sharedInbox] = [] }
recipients[sharedInbox].push(follower.ap_id)
})
for(const sharedInbox in recipients) {
for (const sharedInbox in recipients) {
debug('Notify %s with event %s (from admin %s) cc => %d', sharedInbox, event.title, instanceAdmin.username, recipients[sharedInbox].length)
const body = {
id: `${config.baseurl}/federation/m/${event.id}#create`,
type,
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: [`${config.baseurl}/federation/u/${instanceAdmin.username}/followers`, ...recipients[sharedInbox]],
//cc: recipients[sharedInbox],
// cc: recipients[sharedInbox],
actor: `${config.baseurl}/federation/u/${instanceAdmin.username}`,
// object: event.toAP(instanceAdmin.username, [`${config.baseurl}/federation/u/${instanceAdmin.username}/followers`, ...recipients[sharedInbox]])
object: event.toAP(instanceAdmin.username, recipients[sharedInbox])

View File

@@ -16,8 +16,10 @@ const debug = require('debug')('federation')
*/
router.use(cors())
// is federation enabled? middleware
router.use((req, res, next) => {
if(settingsController.settings.enable_federation) return next()
if (settingsController.settings.enable_federation) { return next() }
debug('Federation disabled!')
res.status(401).send('Federation disabled')
next(false)
@@ -27,7 +29,7 @@ router.use(express.json({ type: ['application/json', 'application/activity+json'
router.get('/m/:event_id', async (req, res) => {
const event_id = req.params.event_id
if (req.accepts('html')) return res.redirect(301, `/event/${event_id}`)
if (req.accepts('html')) { return res.redirect(301, `/event/${event_id}`) }
const event = await Event.findByPk(req.params.event_id, { include: [ User, Tag, Place ] })
if (!event) { return res.status(404).send('Not found') }
@@ -57,7 +59,7 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
Ego.boost(req, res)
break
case 'Note':
console.error('This is a note ! I probably should not receive this')
debug('This is a note! I probably should create a comment here')
break
case 'Like':
Ego.bookmark(req, res)
@@ -70,7 +72,7 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
if (b.object.type === 'Note' && b.object.inReplyTo) {
await Comments.create(req, res)
} else {
console.error('Create what? ', b.object.type)
debug('Create with unsupported Object or not a reply => %s ', b.object.type)
}
break
}

View File

@@ -6,9 +6,9 @@ const debug = require('debug')('fediverse:user')
module.exports = {
async get (req, res) {
const name = req.params.name
if (!name) return res.status(400).send('Bad request.')
const user = await User.findOne({where: { username: name }})
if (!user) return res.status(404).send(`No record found for ${name}`)
if (!name) { return res.status(400).send('Bad request.') }
const user = await User.findOne({ where: { username: name } })
if (!user) { return res.status(404).send(`No record found for ${name}`) }
const ret = {
'@context': [
'https://www.w3.org/ns/activitystreams',
@@ -44,9 +44,9 @@ module.exports = {
const name = req.params.name
const page = req.query.page
debug('Retrieve %s followers', name)
if (!name) return res.status(400).send('Bad request.')
const user = await User.findOne({where: { username: name }, include: { model: FedUsers, as: 'followers' }})
if (!user) return res.status(404).send(`No record found for ${name}`)
if (!name) { return res.status(400).send('Bad request.') }
const user = await User.findOne({ where: { username: name }, include: { model: FedUsers, as: 'followers' } })
if (!user) { return res.status(404).send(`No record found for ${name}`) }
res.type('application/activity+json; charset=utf-8')
@@ -58,7 +58,7 @@ module.exports = {
type: 'OrderedCollection',
totalItems: user.followers.length,
first: `${config.baseurl}/federation/u/${name}/followers?page=true`,
last: `${config.baseurl}/federation/u/${name}/followers?page=true`,
last: `${config.baseurl}/federation/u/${name}/followers?page=true`
})
}
return res.json({
@@ -66,22 +66,22 @@ module.exports = {
id: `${config.baseurl}/federation/u/${name}/followers?page=${page}`,
type: 'OrderedCollectionPage',
totalItems: user.followers.length,
partOf: `${config.baseurl}/federation/u/${name}/followers` ,
orderedItems: user.followers
partOf: `${config.baseurl}/federation/u/${name}/followers`,
orderedItems: user.followers
})
},
async outbox (req, res) {
const name = req.params.name
const page = req.query.page
if (!name) return res.status(400).send('Bad request.')
if (!name) { return res.status(400).send('Bad request.') }
const user = await User.findOne({
include: [ { model: Event, include: [ Place, Tag ] } ],
where: { username: name }
})
if (!user) return res.status(404).send(`No record found for ${name}`)
if (!user) { return res.status(404).send(`No record found for ${name}`) }
debug('Inside outbox, should return all events from this user')
@@ -98,14 +98,14 @@ module.exports = {
last: `${config.baseurl}/federation/u/${name}/outbox?page=true`
})
}
debug('With pagination %s', page)
return res.json({
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${config.baseurl}/federation/u/${name}/outbox?page=${page}`,
type: 'OrderedCollectionPage',
totalItems: user.events.length,
partOf: `${config.baseurl}/federation/u/${name}/outbox` ,
partOf: `${config.baseurl}/federation/u/${name}/outbox`,
orderedItems: user.events.map(e => ({
id: `${config.baseurl}/federation/m/${e.id}#create`,
type: 'Create',
@@ -115,6 +115,6 @@ module.exports = {
actor: `${config.baseurl}/federation/u/${user.username}`,
object: e.toAP(user.username)
}))
})
})
}
}

View File

@@ -10,12 +10,11 @@ const debug = require('debug')('webfinger')
router.use(cors())
router.use((req, res, next) => {
// is federation enabled ?
if (req.settings.enable_federation) return next()
if (req.settings.enable_federation) { return next() }
debug('Federation disabled')
res.status(404).send('Federation disabled')
})
router.get('/webfinger', async (req, res) => {
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) {
debug('Bad webfinger request => %s', resource.query)
@@ -28,7 +27,7 @@ router.get('/webfinger', async (req, res) => {
if (domain !== req_domain) {
debug('Bad webfinger request, requested domain "%s" instead of "%s"', req_domain, domain)
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
}
const user = await User.findOne({ where: { username: name } })