aboutsummaryrefslogtreecommitdiff
path: root/vnext/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-10-21 05:53:23 +0300
committerGravatar Vitaly Takmazov2024-10-21 05:53:23 +0300
commit6f7c755dca704ccf880538b7884473c9c829c2b7 (patch)
treeb3691287d72d6d0d7117b771e46944d778c93a21 /vnext/server
parent1eac455507b75c0795c169d92e1ce9bb36733209 (diff)
vnext: `webfinger` middleware
Diffstat (limited to 'vnext/server')
-rw-r--r--vnext/server/db/Users.js14
-rw-r--r--vnext/server/index.js5
-rw-r--r--vnext/server/middleware/webfinger.js52
3 files changed, 70 insertions, 1 deletions
diff --git a/vnext/server/db/Users.js b/vnext/server/db/Users.js
index 3db3eb5b..a6148497 100644
--- a/vnext/server/db/Users.js
+++ b/vnext/server/db/Users.js
@@ -1,7 +1,7 @@
import { DataTypes, Op } from 'sequelize'
import db from './index'
-export const User = db.define('user', {
+const User = db.define('user', {
id: {
type: DataTypes.INTEGER,
primaryKey: true
@@ -9,6 +9,8 @@ export const User = db.define('user', {
nick: DataTypes.STRING,
banned: DataTypes.INTEGER,
last_seen: DataTypes.DATE
+}, {
+ timestamps: false
})
export const getMonthlyActiveUsers = async () => {
@@ -25,3 +27,13 @@ export const getMonthlyActiveUsers = async () => {
}
})
}
+
+export const getUserByName = async (name = '') => {
+ return User.findOne({
+ where: {
+ nick: {
+ [Op.eq]: name
+ }
+ }
+ })
+}
diff --git a/vnext/server/index.js b/vnext/server/index.js
index f27f0908..75b92270 100644
--- a/vnext/server/index.js
+++ b/vnext/server/index.js
@@ -15,6 +15,7 @@ import { instance } from './middleware/mastodon'
const PORT = process.env.LISTEN_PORT || 8081
import path from 'path'
import { webhook, webhookPath } from './durov'
+import { webfinger } from './middleware/webfinger'
// initialize the application and create the routes
const app = express()
@@ -27,6 +28,10 @@ router.get('/api/v2/oembed', oembed)
router.get('/api/v2/urlexpand', urlExpand)
router.get('/api/apps/android/releases', releases)
+// WebFinger
+
+router.get('/.well-known/webfinger', webfinger)
+
// Mastodon API
router.get('/api/v2/instance', instance)
diff --git a/vnext/server/middleware/webfinger.js b/vnext/server/middleware/webfinger.js
new file mode 100644
index 00000000..9800fc01
--- /dev/null
+++ b/vnext/server/middleware/webfinger.js
@@ -0,0 +1,52 @@
+import config from 'config'
+import addrparser from 'address-rfc2822'
+
+import { getUserByName } from '../db/Users'
+
+const baseUrl = config.get('service.baseURL')
+
+/**
+ * WebFinger endpoint
+ * @type {import('express').RequestParamHandler}
+ */
+export const webfinger = async (req, res) => {
+ const resource = req.query.resource
+ if (resource) {
+ const acct = resource.substring(5) // drop "acct:"
+ const addresses = parseAddress(acct)
+ if (addresses && addresses.length == 1) {
+ const address = addresses[0]
+ const ourAddress = new URL(baseUrl)
+ if (address.host() === ourAddress.hostname) {
+ const user = await getUserByName(addresses[0].user())
+ if (user) {
+ return res.json({
+ subject: resource,
+ links: [
+ {
+ rel: 'self',
+ type: 'application/activity+json',
+ href: `${baseUrl}/u/${user.nick}`
+ }
+ ]
+ })
+ } else {
+ return res.status(404).send('User not found')
+ }
+ } else {
+ return res.status(404).send('Address not found')
+ }
+ } else {
+ return res.status(400).send('Invalid resource')
+ }
+ }
+ res.status(400).send('Missing `resource` param')
+}
+
+const parseAddress = (address = '') => {
+ try {
+ return addrparser.parse(address, { startAt: 'address' })
+ } catch {
+ return undefined
+ }
+}