diff --git a/api/src/api/accounts.js b/api/src/api/accounts.js index 4de76fc..ab4b1fd 100644 --- a/api/src/api/accounts.js +++ b/api/src/api/accounts.js @@ -1,5 +1,4 @@ const api = require('../api-utils.js') -const db = require('../db.js') const pwd = require('../passwd.js') const utils = require('../utils.js') @@ -55,15 +54,22 @@ module.exports = { if (await db.table.users().findOne({ email: req.body.email }) !== null) return res.status(403).json({ error: 'email_already_exists' }) - const documen = await db.table.users().insertOne({ - 'username': req.body.username, - 'email': req.body.email, - 'password': pwd.cryptPassword(req.body.password), - 'activated': 1, // TODO: change to 0 and activate by email verification later - }) + // TODO: when implementing email confirmation, move this actor creation when confirmed + var actor = await apex.store.getObject(ap_id(req.body.username)) + if (actor === null) + actor = await apex.createActor(req.body.username, req.body.username, '', null) - if (documen.insertedCount > 0) - return res.json({ result: documen.insertedId }) + if (actor !== null) { + const documen = await db.table.users().insertOne({ + 'username': req.body.username, + 'email': req.body.email, + 'password': pwd.cryptPassword(req.body.password), + 'activated': 1, // TODO: change to 0 and activate by email verification later + }) + + if (documen.insertedCount > 0) + return res.json({ result: documen.insertedId }) + } return res.status(500).json({ error: 'unknown_error' }) }, } diff --git a/api/src/server.js b/api/src/server.js index 1e29b02..174efe3 100644 --- a/api/src/server.js +++ b/api/src/server.js @@ -1,5 +1,4 @@ const express = require('express') -const db = require('./db.js') const ActivitypubExpress = require('activitypub-express') const port = 8080 @@ -21,7 +20,12 @@ const routes = { shares: '/s/:id/shares', likes: '/s/:id/likes' } -const apex = ActivitypubExpress({ +global.ap_id = (username) => { + return `https://${process.env.DOMAIN}` + + routes.actor.replace(':actor', username) +} +global.db = require('./db.js') +global.apex = ActivitypubExpress({ name: 'FediLove ActivityPub API', version: '0.0.1', domain: process.env.DOMAIN, @@ -31,7 +35,6 @@ const apex = ActivitypubExpress({ routes }) const api = require('./api.js') -const dbhost = process.env.DB_HOST || 'db' const client = db.conn() // extensions for express @@ -81,6 +84,19 @@ client.connect({ useNewUrlParser: true }) apex.store.db = db.get() return apex.store.setup() }) - .then(() => { - app.listen(port, () => console.log(`ActivityPub API listening on: 0.0.0.0:${port}`)) + .then(async () => { + const sysActor = await apex.store.getObject(ap_id('fedilove')) + if (sysActor === null) { + apex.createActor('fedilove', 'FediLove', '', null, 'Service') + .then(async (actor) => { + await db.table.users().insertOne({ username: 'fedilove' }) + await apex.store.saveObject(actor) + apex.systemUser = actor + console.log('I| Created new system user "fedilove"') + }) + } else apex.systemUser = sysActor + + app.listen(port, () => { + console.log(`I| API listening on: 0.0.0.0:${port}`) + }) })