place the create-on-lookup on all relevant routes, mostly to work with Mastodon instances that have a Guppe actor from the old version cached

This commit is contained in:
Will Murphy 2021-11-04 22:58:55 -05:00
parent 95fddfb98e
commit f46de1ba0a

View file

@ -54,48 +54,37 @@ const apex = ActivitypubExpress({
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status Accepts ":req[accept]" ":referrer" ":user-agent"')) app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status Accepts ":req[accept]" ":referrer" ":user-agent"'))
app.use(express.json({ type: apex.consts.jsonldTypes }), apex) app.use(express.json({ type: apex.consts.jsonldTypes }), apex)
// Guppe's magic: create new groups on demand whenever someone tries to access it // Create new groups on demand whenever someone tries to access one
async function getOrCreateActor (req, res, next) { async function actorOnDemand (req, res, next) {
const apex = req.app.locals.apex const actor = req.params.actor
const actor = req.params[apex.actorParam] if (!actor) {
const actorIRI = apex.utils.usernameToIRI(actor) return next()
let actorObj
try {
actorObj = await apex.store.getObject(actorIRI)
} catch (err) { return next(err) }
if (!actorObj && actor.length <= 100) {
try {
const summary = `I'm a group about ${actor}. Follow me to get all the group posts. Tag me to share with the group. Create other groups by searching for or tagging @yourGroupName@${DOMAIN}`
actorObj = await apex.createActor(actor, `${actor} group`, summary, icon, 'Group')
await apex.store.saveObject(actorObj)
} catch (err) { return next(err) }
res.locals.apex.target = actorObj
} else if (actor.length > 100) {
res.locals.apex.status = 400
res.locals.apex.statusMessage = 'Group names are limited to 100 characters'
} else if (actorObj.type === 'Tombstone') {
res.locals.apex.status = 410
} else {
res.locals.apex.target = actorObj
} }
const actorIRI = apex.utils.usernameToIRI(actor)
try {
if (!(await apex.store.getObject(actorIRI)) && actor.length <= 255) {
console.log(`Creating group: ${actor}`)
const summary = `I'm a group about ${actor}. Follow me to get all the group posts. Tag me to share with the group. Create other groups by searching for or tagging @yourGroupName@${DOMAIN}`
const actorObj = await apex.createActor(actor, `${actor} group`, summary, icon, 'Group')
await apex.store.saveObject(actorObj)
}
} catch (err) { return next(err) }
next() next()
} }
// define routes using prepacakged middleware collections // define routes using prepacakged middleware collections
app.route(routes.inbox) app.route(routes.inbox)
.post(apex.net.inbox.post) .post(actorOnDemand, apex.net.inbox.post)
// no C2S at present .get(actorOnDemand, apex.net.inbox.get)
// .get(apex.net.inbox.get)
app.route(routes.outbox) app.route(routes.outbox)
.get(apex.net.outbox.get) .get(actorOnDemand, apex.net.outbox.get)
// no C2S at present // no C2S at present
// .post(apex.net.outbox.post) // .post(apex.net.outbox.post)
// replace apex's target actor validator with our create on demand method // replace apex's target actor validator with our create on demand method
app.get(routes.actor, apex.net.validators.jsonld, getOrCreateActor, apex.net.responders.target) app.get(routes.actor, actorOnDemand, apex.net.actor.get)
app.get(routes.followers, apex.net.followers.get) app.get(routes.followers, actorOnDemand, apex.net.followers.get)
app.get(routes.following, apex.net.following.get) app.get(routes.following, actorOnDemand, apex.net.following.get)
app.get(routes.liked, apex.net.liked.get) app.get(routes.liked, actorOnDemand, apex.net.liked.get)
app.get(routes.object, apex.net.object.get) app.get(routes.object, apex.net.object.get)
app.get(routes.activity, apex.net.activityStream.get) app.get(routes.activity, apex.net.activityStream.get)
app.get(routes.shares, apex.net.shares.get) app.get(routes.shares, apex.net.shares.get)
@ -104,8 +93,8 @@ app.get(routes.likes, apex.net.likes.get)
app.get( app.get(
'/.well-known/webfinger', '/.well-known/webfinger',
apex.net.wellKnown.parseWebfinger, apex.net.wellKnown.parseWebfinger,
// replace apex's target actor validator with our create on demand method actorOnDemand,
getOrCreateActor, apex.net.validators.targetActor,
apex.net.wellKnown.respondWebfinger apex.net.wellKnown.respondWebfinger
) )