Migrate to better-sqlite3

This commit is contained in:
Darius Kazemi 2019-03-25 22:17:24 -07:00
parent e2ab4dd51e
commit e989126e86
7 changed files with 148 additions and 164 deletions

View file

@ -2,8 +2,8 @@ const config = require('./config.json');
const { USER, PASS, DOMAIN, PRIVKEY_PATH, CERT_PATH, PORT } = config;
const express = require('express');
const app = express();
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('bot-node.db');
const Database = require('better-sqlite3');
const db = new Database('bot-node.db');
const fs = require('fs');
const routes = require('./routes'),
bodyParser = require('body-parser'),
@ -27,9 +27,9 @@ try {
}
// if there is no `accounts` table in the DB, create an empty table
db.run('CREATE TABLE IF NOT EXISTS accounts (name TEXT PRIMARY KEY, privkey TEXT, pubkey TEXT, webfinger TEXT, actor TEXT, apikey TEXT, followers TEXT, messages TEXT)');
db.prepare('CREATE TABLE IF NOT EXISTS accounts (name TEXT PRIMARY KEY, privkey TEXT, pubkey TEXT, webfinger TEXT, actor TEXT, apikey TEXT, followers TEXT, messages TEXT)').run();
// if there is no `messages` table in the DB, create an empty table
db.run('CREATE TABLE IF NOT EXISTS messages (guid TEXT PRIMARY KEY, message TEXT)');
db.prepare('CREATE TABLE IF NOT EXISTS messages (guid TEXT PRIMARY KEY, message TEXT)').run();
app.set('db', db);
app.set('domain', DOMAIN);

View file

@ -4,13 +4,13 @@
"description": "",
"main": "index.js",
"dependencies": {
"better-sqlite3": "^5.4.0",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"express": "^4.16.3",
"express-basic-auth": "^1.1.5",
"generate-rsa-keypair": "^0.1.2",
"request": "^2.87.0",
"sqlite3": "^4.0.2"
"request": "^2.87.0"
},
"engines": {
"node": ">=10.10.0"

View file

@ -52,16 +52,13 @@ router.post('/create', function (req, res) {
let actorRecord = createActor(account, domain, pair.public);
let webfingerRecord = createWebfinger(account, domain);
const apikey = crypto.randomBytes(16).toString('hex');
db.run('insert or replace into accounts(name, actor, apikey, pubkey, privkey, webfinger) values($name, $actor, $apikey, $pubkey, $privkey, $webfinger)', {
$name: `${account}@${domain}`,
$apikey: apikey,
$pubkey: pair.public,
$privkey: pair.private,
$actor: JSON.stringify(actorRecord),
$webfinger: JSON.stringify(webfingerRecord)
}, (err, accounts) => {
try {
db.prepare('insert or replace into accounts(name, actor, apikey, pubkey, privkey, webfinger) values(?, ?, ?, ?, ?, ?)').run(`${account}@${domain}`, JSON.stringify(actorRecord), apikey, pair.public, pair.private, JSON.stringify(webfingerRecord));
res.status(200).json({msg: 'ok', apikey});
});
}
catch(e) {
res.status(200).json({error: e});
}
});
module.exports = router;

View file

@ -6,13 +6,12 @@ const express = require('express'),
router.post('/sendMessage', function (req, res) {
let db = req.app.get('db');
console.log('DB',db);
let domain = req.app.get('domain');
let acct = req.body.acct;
let apikey = req.body.apikey;
let message = req.body.message;
// check to see if your API key matches
db.get('select apikey from accounts where name = $name', {$name: `${acct}@${domain}`}, (err, result) => {
let result = db.prepare('select apikey from accounts where name = ?').get(`${acct}@${domain}`);
if (result.apikey === apikey) {
sendCreateMessage(message, acct, domain, req, res);
}
@ -20,13 +19,12 @@ router.post('/sendMessage', function (req, res) {
res.status(403).json({msg: 'wrong api key'});
}
});
});
function signAndSend(message, name, domain, req, res, targetDomain, inbox) {
// get the private key
let db = req.app.get('db');
let inboxFragment = inbox.replace('https://'+targetDomain,'');
db.get('select privkey from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
let result = db.prepare('select privkey from accounts where name = ?').get(`${name}@${domain}`);
if (result === undefined) {
console.log(`No record found for ${name}.`);
}
@ -60,7 +58,6 @@ function signAndSend(message, name, domain, req, res, targetDomain, inbox) {
}
});
}
});
}
function createMessage(text, name, domain, req, res) {
@ -88,16 +85,8 @@ function createMessage(text, name, domain, req, res) {
'object': noteMessage
};
db.run('insert or replace into messages(guid, message) values($guid, $message)', {
$guid: guidCreate,
$message: JSON.stringify(createMessage),
}, (err, accounts) => {
});
db.run('insert or replace into messages(guid, message) values($guid, $message)', {
$guid: guidNote,
$message: JSON.stringify(noteMessage),
}, (err, accounts) => {
});
db.prepare('insert or replace into messages(guid, message) values(?, ?)').run( guidCreate, JSON.stringify(createMessage));
db.prepare('insert or replace into messages(guid, message) values(?, ?)').run( guidNote, JSON.stringify(noteMessage));
return createMessage;
}
@ -106,7 +95,7 @@ function sendCreateMessage(text, name, domain, req, res) {
let message = createMessage(text, name, domain, req, res);
let db = req.app.get('db');
db.get('select followers from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
let followers = JSON.parse(result.followers);
console.log(followers);
console.log('type',typeof followers);
@ -123,7 +112,6 @@ function sendCreateMessage(text, name, domain, req, res) {
}
res.status(200).json({msg: 'ok'});
}
});
}
module.exports = router;

View file

@ -10,7 +10,7 @@ function signAndSend(message, name, domain, req, res, targetDomain) {
let inboxFragment = inbox.replace('https://'+targetDomain,'');
// get the private key
let db = req.app.get('db');
db.get('select privkey from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
let result = db.prepare('select privkey from accounts where name = ?').get(name);
if (result === undefined) {
return res.status(404).send(`No record found for ${name}.`);
}
@ -44,7 +44,6 @@ function signAndSend(message, name, domain, req, res, targetDomain) {
});
return res.status(200);
}
});
}
function sendAcceptMessage(thebody, name, domain, req, res, targetDomain) {
@ -79,7 +78,7 @@ router.post('/', function (req, res) {
// Add the user to the DB of accounts that follow the account
let db = req.app.get('db');
// get the followers JSON for the user
db.get('select followers from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
if (result === undefined) {
console.log(`No record found for ${name}.`);
}
@ -95,12 +94,15 @@ router.post('/', function (req, res) {
followers = [req.body.actor];
}
let followersText = JSON.stringify(followers);
try {
// update into DB
db.run('update accounts set followers=$followers where name = $name', {$name: `${name}@${domain}`, $followers: followersText}, (err, result) => {
console.log('updated followers!', err, result);
});
let newFollowers = db.prepare('update accounts set followers=? where name = ?').run(followersText, `${name}@${domain}`);
console.log('updated followers!', newFollowers);
}
catch(e) {
console.log('error', e);
}
}
});
}
});

View file

@ -12,7 +12,7 @@ router.get('/:name', function (req, res) {
let domain = req.app.get('domain');
let username = name;
name = `${name}@${domain}`;
db.get('select actor from accounts where name = $name', {$name: name}, (err, result) => {
let result = db.prepare('select actor from accounts where name = ?').get(name);
if (result === undefined) {
return res.status(404).send(`No record found for ${name}.`);
}
@ -25,7 +25,6 @@ router.get('/:name', function (req, res) {
}
res.json(tempActor);
}
});
}
});
@ -38,10 +37,9 @@ router.get('/:name/followers', function (req, res) {
let db = req.app.get('db');
let domain = req.app.get('domain');
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
console.log(result);
result.followers = result.followers || '[]';
let followers = JSON.parse(result.followers);
if (!followers) {
followers = [];
}
let followersCollection = {
"type":"OrderedCollection",
"totalItems":followers.length,

View file

@ -10,14 +10,13 @@ router.get('/', function (req, res) {
else {
let name = resource.replace('acct:','');
let db = req.app.get('db');
db.get('select webfinger from accounts where name = $name', {$name: name}, (err, result) => {
let result = db.prepare('select webfinger from accounts where name = ?').get(name);
if (result === undefined) {
return res.status(404).send(`No record found for ${name}.`);
}
else {
res.json(JSON.parse(result.webfinger));
}
});
}
});