parent
dadd959f23
commit
62f0fa88dd
4 changed files with 56 additions and 12 deletions
3
index.js
3
index.js
|
@ -28,6 +28,8 @@ try {
|
||||||
|
|
||||||
// if there is no `accounts` table in the DB, create an empty table
|
// 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.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('db', db);
|
||||||
app.set('domain', DOMAIN);
|
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('/admin', express.static('public/admin'));
|
||||||
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(){
|
||||||
|
|
|
@ -6,6 +6,7 @@ const express = require('express'),
|
||||||
|
|
||||||
router.post('/sendMessage', function (req, res) {
|
router.post('/sendMessage', function (req, res) {
|
||||||
let db = req.app.get('db');
|
let db = req.app.get('db');
|
||||||
|
console.log('DB',db);
|
||||||
let domain = req.app.get('domain');
|
let domain = req.app.get('domain');
|
||||||
let acct = req.body.acct;
|
let acct = req.body.acct;
|
||||||
let apikey = req.body.apikey;
|
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 guidCreate = crypto.randomBytes(16).toString('hex');
|
||||||
const guidNote = crypto.randomBytes(16).toString('hex');
|
const guidNote = crypto.randomBytes(16).toString('hex');
|
||||||
|
let db = req.app.get('db');
|
||||||
let d = new Date();
|
let d = new Date();
|
||||||
|
|
||||||
return {
|
let noteMessage = {
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
'id': `https://${domain}/m/${guidNote}`,
|
||||||
|
|
||||||
'id': `https://${domain}/${guidCreate}`,
|
|
||||||
'type': 'Create',
|
|
||||||
'actor': `https://${domain}/u/${name}`,
|
|
||||||
|
|
||||||
'object': {
|
|
||||||
'id': `https://${domain}/${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,
|
||||||
'to': 'https://www.w3.org/ns/activitystreams#Public'
|
'to': 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let createMessage = {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
|
||||||
|
'id': `https://${domain}/m/${guidCreate}`,
|
||||||
|
'type': 'Create',
|
||||||
|
'actor': `https://${domain}/u/${name}`,
|
||||||
|
|
||||||
|
'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) {
|
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');
|
let db = req.app.get('db');
|
||||||
|
|
||||||
db.get('select followers from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
|
db.get('select followers from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
|
||||||
|
|
|
@ -4,6 +4,7 @@ module.exports = {
|
||||||
api: require('./api'),
|
api: require('./api'),
|
||||||
admin: require('./admin'),
|
admin: require('./admin'),
|
||||||
user: require('./user'),
|
user: require('./user'),
|
||||||
|
message: require('./message'),
|
||||||
inbox: require('./inbox'),
|
inbox: require('./inbox'),
|
||||||
webfinger: require('./webfinger'),
|
webfinger: require('./webfinger'),
|
||||||
};
|
};
|
||||||
|
|
23
routes/message.js
Normal file
23
routes/message.js
Normal file
|
@ -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;
|
Loading…
Reference in a new issue