add AP endpoint for location

This commit is contained in:
lesion 2025-01-08 17:51:36 +01:00
parent 565cf1eb97
commit fbffa422c2
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 57 additions and 12 deletions

View file

@ -1,12 +1,33 @@
module.exports = (sequelize, DataTypes) =>
sequelize.define('place', {
name: {
type: DataTypes.STRING,
unique: true,
index: true,
allowNull: false
},
address: DataTypes.STRING,
latitude: DataTypes.FLOAT,
longitude: DataTypes.FLOAT,
})
const config = require('../../config')
module.exports = (sequelize, DataTypes) => {
const Place = sequelize.define('place', {
name: {
type: DataTypes.STRING,
unique: true,
index: true,
allowNull: false
},
address: DataTypes.STRING,
latitude: DataTypes.FLOAT,
longitude: DataTypes.FLOAT,
})
/**
* @description WIP -> https://codeberg.org/fediverse/fep/src/commit/4a75a1bc50bc6d19fc1e6112f02c52621bc178fe/fep/8a8e/fep-8a8e.md#location
* @todo support PlaceAddress type
* @returns ActivityStream location representation
*/
Place.prototype.toAP = function () {
return {
id: `${config.baseurl}/federation/p/${this.id}`,
type: 'Place',
name: this.name,
address: this.address,
latitude: this.latitude,
longitude: this.longitude
}
}
return Place
}

View file

@ -2,6 +2,7 @@ const express = require('express')
const router = express.Router()
// const cors = require('cors')
const Users = require('./users')
const Places = require('./places')
const { Event, User, Tag, Place } = require('../api/models/models')
const settingsController = require('../api/controller/settings')
@ -75,6 +76,8 @@ router.get('/u/:name/outbox', Users.outbox)
router.get('/u/:name', Users.get)
router.get('/p/:id', Places.get)
// Handle 404
router.use((req, res) => {
log.warn(`[FEDI] 404 Page not found: ${req.path}`)

View file

@ -0,0 +1,21 @@
const { Place } = require('../api/models/models')
const log = require('../log')
module.exports = {
async get (req, res) {
log.debug('[FEDI] Get location')
const id = req.params.id
if (!id) { return res.status(400).send('Bad request.') }
const place = await Place.findByPk(id)
if (!place) {
log.warn(`[FEDI] Place ${id} not found`)
return res.status(404).send('Not found.')
}
const ret = place.toAP()
ret['@context'] = ['https://www.w3.org/ns/activitystreams']
res.type('application/activity+json; charset=utf-8')
res.json(ret)
},
}