Initial groundwork for storing inbox in database.

This commit is contained in:
Andrew Pietila 2023-03-24 01:24:27 -05:00
parent 1e8d9dfe06
commit dd5499a21d
10 changed files with 958 additions and 8 deletions

View file

@ -49,6 +49,16 @@
"function-call-argument-newline": [
"error",
"consistent"
],
"object-property-newline": [
"error",
{
"allowAllPropertiesOnSameLine": true
}
],
"strict": [
"error",
"safe"
]
}
}

2
.gitignore vendored
View file

@ -130,3 +130,5 @@ dist
.yarn/install-state.gz
.pnp.*
dev.sqlite3
knexfile.js

View file

@ -1,7 +1,9 @@
---
allowedLicenses:
- (BSD-2-Clause OR MIT OR Apache-2.0)
- (MIT AND CC-BY-3.0)
- (MIT OR CC0-1.0)
- (MIT OR WTFPL)
- Apache-2.0
- BSD-2-Clause
- BSD-3-Clause

3
app.js
View file

@ -50,6 +50,9 @@ const { match: createPathMatch } = require('path-to-regexp');
if ( routeObj.get ) {
app.get(`/${route}`, routeObj.get);
}
if ( routeObj.post ) {
app.post(`/${route}`, routeObj.post);
}
}
app.listen(process.env.PORT || 3000);
})();

49
knexfile-sample.js Normal file
View file

@ -0,0 +1,49 @@
'use strict';
// Update with your config settings.
/**
* @type { Object.<string, import("knex").Knex.Config> }
*/
module.exports = {
development: {
client: 'better-sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};

5
lib/db.js Normal file
View file

@ -0,0 +1,5 @@
'use strict';
const Knex = require('knex');
const db = new Knex(require('../knexfile')[process.env.NODE_ENV??'development']);
module.exports = db;

View file

@ -0,0 +1,23 @@
'use strict';
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.createTable('inbox', (table) => {
table.increments('id');
table.string('origin');
table.string('to');
table.string('cc');
table.string('object');
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.dropTable('inbox');
};

828
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -15,10 +15,15 @@
"dependencies": {
"express": "^4.18.2",
"glob": "^9.3.0",
"jsonld": "^8.1.1",
"knex": "^2.4.2",
"path-to-regexp": "^6.2.1"
},
"devDependencies": {
"eslint": "^8.36.0",
"license-checker": "^25.0.1"
},
"optionalDependencies": {
"better-sqlite3": "^8.2.0"
}
}

39
routes/inbox.js Normal file
View file

@ -0,0 +1,39 @@
'use strict';
var db = require('../lib/db');
var jsonld = require('jsonld');
module.exports = {
/**
* @param {express.Request} req
* @param {express.Response} res
* @param {express.NextFunction} _next
*/
post: async (req, res, _next) => {
if ( req.body ) {
var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams');
if ( bodyParsed.type === 'Create') {
var to;
var cc;
if ( typeof bodyParsed.object.to === 'string' ) {
to = [bodyParsed.object.to];
} else {
to = bodyParsed.object.to;
}
if ( typeof bodyParsed.object.cc === 'string' ) {
to = [bodyParsed.object.cc];
} else {
cc = bodyParsed.object.cc;
}
await db('inbox').insert({
origin: bodyParsed.object.attributedTo,
to: JSON.stringify(to),
cc: JSON.stringify(cc),
object: JSON.stringify(bodyParsed.object)
});
res.status(204);
res.end();
}
}
}
};