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.
This commit is contained in:
Darius Kazemi 2019-03-29 06:47:51 -07:00
parent 753872d31d
commit 45e1362aa2
3 changed files with 16 additions and 6 deletions

View File

@ -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(); 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 // 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(); 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('db', db);
app.set('domain', DOMAIN); app.set('domain', DOMAIN);
@ -48,6 +50,7 @@ app.use('/admin', express.static('public/admin'));
app.use('/convert', express.static('public/convert')); app.use('/convert', express.static('public/convert'));
app.use('/.well-known/webfinger', cors(), routes.webfinger); app.use('/.well-known/webfinger', cors(), routes.webfinger);
app.use('/u', cors(), routes.user); app.use('/u', cors(), routes.user);
app.use('/m', cors(), routes.message);
app.use('/api/inbox', cors(), routes.inbox); app.use('/api/inbox', cors(), routes.inbox);
http.createServer(app).listen(app.get('port'), function(){ http.createServer(app).listen(app.get('port'), function(){

View File

@ -2,7 +2,8 @@
module.exports = { module.exports = {
api: require('./api'), api: require('./api'),
user: require('./user'),
inbox: require('./inbox'), inbox: require('./inbox'),
message: require('./message'),
user: require('./user'),
webfinger: require('./webfinger'), webfinger: require('./webfinger'),
}; };

View File

@ -173,25 +173,26 @@ function signAndSend(message, name, domain, req, res, targetDomain, inbox) {
} }
} }
function createMessage(text, name, domain, item, follower) { function createMessage(text, name, domain, item, follower, guidNote) {
const guid = crypto.randomBytes(16).toString('hex'); const guidCreate = crypto.randomBytes(16).toString('hex');
let d = new Date(); let d = new Date();
let out = { let out = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${domain}/${guid}`, 'id': `https://${domain}/m/${guidCreate}`,
'type': 'Create', 'type': 'Create',
'actor': `https://${domain}/u/${name}`, 'actor': `https://${domain}/u/${name}`,
'to': [ follower ], 'to': [ follower ],
'object': { 'object': {
'id': `https://${domain}/${guid}`, 'id': `https://${domain}/m/${guidNote}`,
'type': 'Note', 'type': 'Note',
'published': d.toISOString(), 'published': d.toISOString(),
'attributedTo': `https://${domain}/u/${name}`, 'attributedTo': `https://${domain}/u/${name}`,
'content': text, 'content': text,
'link': item.link,
'cc': 'https://www.w3.org/ns/activitystreams#Public' 'cc': 'https://www.w3.org/ns/activitystreams#Public'
} }
}; };
@ -222,6 +223,10 @@ function createMessage(text, name, domain, item, follower) {
out.object.attachment = attachment; 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; return out;
} }
@ -237,7 +242,8 @@ function sendCreateMessage(text, name, domain, req, res, item) {
let inbox = follower+'/inbox'; let inbox = follower+'/inbox';
let myURL = new URL(follower); let myURL = new URL(follower);
let targetDomain = myURL.hostname; 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); signAndSend(message, name, domain, req, res, targetDomain, inbox);
} }
} }