From 62f0fa88dd03f69e027331a10d57cdf3aa5871a8 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 24 Sep 2018 20:10:39 -0700 Subject: [PATCH] Store messages and allow retrieval via the message URI Addresses #1. --- index.js | 3 +++ routes/api.js | 41 +++++++++++++++++++++++++++++------------ routes/index.js | 1 + routes/message.js | 23 +++++++++++++++++++++++ 4 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 routes/message.js diff --git a/index.js b/index.js index 6f6b133..b5ea5ff 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,8 @@ 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)'); +// 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)'); app.set('db', db); app.set('domain', DOMAIN); @@ -65,6 +67,7 @@ app.use('/api/admin', cors({ credentials: true, origin: true }), basicUserAuth, app.use('/admin', express.static('public/admin')); app.use('/.well-known/webfinger', cors(), routes.webfinger); app.use('/u', cors(), routes.user); +app.use('/m', cors(), routes.message); app.use('/api/inbox', cors(), routes.inbox); http.createServer(app).listen(app.get('port'), function(){ diff --git a/routes/api.js b/routes/api.js index 43ddb2f..80fa486 100644 --- a/routes/api.js +++ b/routes/api.js @@ -6,6 +6,7 @@ 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; @@ -62,31 +63,47 @@ function signAndSend(message, name, domain, req, res, targetDomain, inbox) { }); } -function createMessage(text, name, domain) { +function createMessage(text, name, domain, req, res) { const guidCreate = crypto.randomBytes(16).toString('hex'); const guidNote = crypto.randomBytes(16).toString('hex'); + let db = req.app.get('db'); let d = new Date(); - return { + let noteMessage = { + 'id': `https://${domain}/m/${guidNote}`, + 'type': 'Note', + 'published': d.toISOString(), + 'attributedTo': `https://${domain}/u/${name}`, + 'content': text, + 'to': 'https://www.w3.org/ns/activitystreams#Public' + }; + + let createMessage = { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `https://${domain}/${guidCreate}`, + 'id': `https://${domain}/m/${guidCreate}`, 'type': 'Create', 'actor': `https://${domain}/u/${name}`, - 'object': { - 'id': `https://${domain}/${guidNote}`, - 'type': 'Note', - 'published': d.toISOString(), - 'attributedTo': `https://${domain}/u/${name}`, - 'content': text, - 'to': 'https://www.w3.org/ns/activitystreams#Public' - } + '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) => { + }); + + return createMessage; } function sendCreateMessage(text, name, domain, req, res) { - let message = createMessage(text, name, domain); + 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) => { diff --git a/routes/index.js b/routes/index.js index 2b97780..afd9141 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,6 +4,7 @@ module.exports = { api: require('./api'), admin: require('./admin'), user: require('./user'), + message: require('./message'), inbox: require('./inbox'), webfinger: require('./webfinger'), }; diff --git a/routes/message.js b/routes/message.js new file mode 100644 index 0000000..3b819ac --- /dev/null +++ b/routes/message.js @@ -0,0 +1,23 @@ +'use strict'; +const express = require('express'), + router = express.Router(); + +router.get('/:guid', function (req, res) { + let guid = req.params.guid; + if (!guid) { + return res.status(400).send('Bad request.'); + } + else { + let db = req.app.get('db'); + db.get('select message from messages where guid = $guid', {$guid: guid}, (err, result) => { + if (result === undefined) { + return res.status(404).send(`No record found for ${guid}.`); + } + else { + res.json(JSON.parse(result.message)); + } + }); + } +}); + +module.exports = router;