From 45e1362aa2528fe0b84d0d81d83b855dd6a08065 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Fri, 29 Mar 2019 06:47:51 -0700 Subject: [PATCH] Provide route to dereferenced messages and fix duplicate messages This commit mostly fixes the duplicate messages issue. What was happening was I was creating multiple Note objects if I was sending a Note to multiple people. Instead I create one Note object and reference that in multiple Create events. --- index.js | 3 +++ routes/index.js | 3 ++- updateFeeds.js | 16 +++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 69e2f99..ed23097 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,8 @@ try { 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 `feeds` table in the DB, create an empty table db.prepare('CREATE TABLE IF NOT EXISTS feeds (feed TEXT PRIMARY KEY, username TEXT, content TEXT)').run(); +// if there is no `messages` table in the DB, create an empty table +db.prepare('CREATE TABLE IF NOT EXISTS messages (guid TEXT PRIMARY KEY, message TEXT)').run(); app.set('db', db); app.set('domain', DOMAIN); @@ -48,6 +50,7 @@ app.use('/admin', express.static('public/admin')); app.use('/convert', express.static('public/convert')); 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/index.js b/routes/index.js index 9209545..c90af8b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,7 +2,8 @@ module.exports = { api: require('./api'), - user: require('./user'), inbox: require('./inbox'), + message: require('./message'), + user: require('./user'), webfinger: require('./webfinger'), }; diff --git a/updateFeeds.js b/updateFeeds.js index 9dc2075..1300c84 100644 --- a/updateFeeds.js +++ b/updateFeeds.js @@ -173,25 +173,26 @@ function signAndSend(message, name, domain, req, res, targetDomain, inbox) { } } -function createMessage(text, name, domain, item, follower) { - const guid = crypto.randomBytes(16).toString('hex'); +function createMessage(text, name, domain, item, follower, guidNote) { + const guidCreate = crypto.randomBytes(16).toString('hex'); let d = new Date(); let out = { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `https://${domain}/${guid}`, + 'id': `https://${domain}/m/${guidCreate}`, 'type': 'Create', 'actor': `https://${domain}/u/${name}`, 'to': [ follower ], 'object': { - 'id': `https://${domain}/${guid}`, + 'id': `https://${domain}/m/${guidNote}`, 'type': 'Note', 'published': d.toISOString(), 'attributedTo': `https://${domain}/u/${name}`, 'content': text, + 'link': item.link, 'cc': 'https://www.w3.org/ns/activitystreams#Public' } }; @@ -222,6 +223,10 @@ function createMessage(text, name, domain, item, follower) { out.object.attachment = attachment; } + console.log(guidCreate, guidNote); + db.prepare('insert or replace into messages(guid, message) values(?, ?)').run( guidCreate, JSON.stringify(out)); + db.prepare('insert or replace into messages(guid, message) values(?, ?)').run( guidNote, JSON.stringify(out.object)); + return out; } @@ -237,7 +242,8 @@ function sendCreateMessage(text, name, domain, req, res, item) { let inbox = follower+'/inbox'; let myURL = new URL(follower); let targetDomain = myURL.hostname; - let message = createMessage(text, name, domain, item, follower); + const guidNote = crypto.randomBytes(16).toString('hex'); + let message = createMessage(text, name, domain, item, follower, guidNote); signAndSend(message, name, domain, req, res, targetDomain, inbox); } }