From 263bee4415bd1a73a9af66cd6708b8fae29909ce Mon Sep 17 00:00:00 2001 From: "Glitch (fediverse-bot)" Date: Wed, 9 Sep 2020 16:14:01 +0000 Subject: [PATCH] Cleanup. --- app.js | 4 + bot/bot-copy.js | 309 +++++++++++++++++++++---------------------- bot/bot.js | 334 ++++++++++++++++++++++++----------------------- package.json | 5 +- server.js | 4 + sessions-journal | Bin 16928 -> 0 bytes shrinkwrap.yaml | 8 ++ 7 files changed, 344 insertions(+), 320 deletions(-) delete mode 100644 sessions-journal diff --git a/app.js b/app.js index 7bf4c53..34514a9 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,7 @@ +if ( !process.env.PROJECT_NAME || !process.env.PROJECT_ID ){ + require( 'dotenv' ).config(); +} + const path = require( 'path' ), express = require( 'express' ), session = require( 'express-session' ), diff --git a/bot/bot-copy.js b/bot/bot-copy.js index 950dd5c..72b3318 100644 --- a/bot/bot-copy.js +++ b/bot/bot-copy.js @@ -1,54 +1,58 @@ -const fs = require( 'fs' ), - crypto = require( 'crypto' ), - url = require( 'url' ), - util = require( 'util' ), - moment = require( 'moment' ), - dbHelper = require( __dirname + '/../helpers/db.js' ), - keys = require( __dirname + '/../helpers/keys.js' ), - request = require( 'request' ), - publicKeyPath = '.data/rsa/pubKey', - privateKeyPath = '.data/rsa/privKey', - botURL = `https://${ process.env.PROJECT_DOMAIN }.glitch.me`, - botComposeReply = require( __dirname + '/responses.js' ); +if ( !process.env.PROJECT_NAME || !process.env.PROJECT_ID ){ + require( 'dotenv' ).config(); +} + +const fs = require('fs'), + crypto = require('crypto'), + url = require('url'), + util = require('util'), + moment = require('moment'), + dbHelper = require(__dirname + '/../helpers/db.js'), + keys = require(__dirname + '/../helpers/keys.js'), + request = require('request'), + public_key_path = '.data/rsa/pubKey', + private_key_path = '.data/rsa/privKey', + bot_url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`, + botComposeReply = require(__dirname + '/responses.js'); -if ( !fs.existsSync( publicKeyPath ) || !fs.existsSync( privateKeyPath ) ) { - keys.generateKeys( function( ){ - process.kill( process.pid ); - } ); +if (!fs.existsSync(public_key_path) || !fs.existsSync(private_key_path)) { + keys.generateKeys(function(){ + process.kill(process.pid); + }); } else{ - let publicKey = fs.readFileSync( publicKeyPath, 'utf8' ), - privateKey = fs.readFileSync( privateKeyPath, 'utf8' ); + var public_key = fs.readFileSync(public_key_path, 'utf8'), + private_key = fs.readFileSync(private_key_path, 'utf8'); module.exports = { - bot_url: botURL, + bot_url: bot_url, links: [ // { // rel: 'http://webfinger.net/rel/profile-page', // type: 'text/html', - // href: `${ botURL }` + // href: `${bot_url}` // }, // { // rel: 'http://schemas.google.com/g/2010#updates-from', // type: 'application/atom+xml', - // href: `${ botURL }/feed` + // href: `${bot_url}/feed` // }, { rel: 'self', type: 'application/activity+json', - href: `${ botURL }/bot` + href: `${bot_url}/bot` }, // { // rel: 'hub', - // href: `${ botURL }/pubsub` + // href: `${bot_url}/pubsub` // }, // { // rel: 'salmon', - // href: `${ botURL }/salmon` + // href: `${bot_url}/salmon` // }, // { // rel: 'magic-public-key', - // href: `data:application/magic-public-key,RSA.${ publicKey.replace( '-----BEGIN PUBLIC KEY-----\n', '' ).replace( '\n-----END PUBLIC KEY-----', '' ).replace( '\\n', '' ) }` + // href: `data:application/magic-public-key,RSA.${public_key.replace('-----BEGIN PUBLIC KEY-----\n', '').replace('\n-----END PUBLIC KEY-----', '').replace('\\n', '')}` // } ], info: { @@ -56,7 +60,7 @@ else{ 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1' ], - 'id': `${ botURL }/bot`, + 'id': `${bot_url}/bot`, 'icon': [{ 'url': process.env.BOT_AVATAR_URL, 'type': 'Image' @@ -68,225 +72,224 @@ else{ 'type': 'Person', 'name': process.env.BOT_USERNAME, 'preferredUsername': process.env.BOT_USERNAME, - 'inbox': `${ botURL }/inbox`, + 'inbox': `${bot_url}/inbox`, 'publicKey': { - 'id': `${ botURL }/bot#main-key`, - 'owner': `${ botURL }/bot`, - 'publicKeyPem': publicKey + 'id': `${bot_url}/bot#main-key`, + 'owner': `${bot_url}/bot`, + 'publicKeyPem': public_key } }, - // script: botScript, script: require( __dirname + '/script.js' ), composeReply: botComposeReply, - sendReply: function( options, cb ){ - let bot = this, - replyToUsername = ''; + sendReply: function(options, cb){ + var bot = this, + reply_to_username = ''; try{ - let actor_url_parts = options.payload.actor.split( '/' ); - let username = actor_url_parts[actor_url_parts.length-1]; - replyToUsername = `@${ username }@${ url.parse( options.payload.actor ).hostname } `; + var actor_url_parts = options.payload.actor.split('/'); + var username = actor_url_parts[actor_url_parts.length-1]; + reply_to_username = `@${username}@${url.parse(options.payload.actor).hostname} `; - console.log( {replyToUsername} ); - } catch( err ){ /*noop*/ } + console.log({reply_to_username}); + } catch(err){ /*noop*/ } - bot.createPost( { + bot.createPost({ type: 'Note', - content: `
${ options.payload.object.content }${ options.payload.object.url }

${ options.reply_message }

`, - reply_message: `${ replyToUsername } ${ options.reply_message}`, + content: `
${options.payload.object.content}${options.payload.object.url}

${options.reply_message}

`, + reply_message: `${reply_to_username} ${options.reply_message}`, in_reply_to: options.payload.object.url - }, function( err, message ){ - // console.log( err, message ); - } ); + }, function(err, message){ + // console.log(err, message); + }); }, - createPost: function( options, cb ){ - let bot = this; + createPost: function(options, cb){ + var bot = this; - if ( ( !options.content || options.content.trim( ).length === 0 ) && !options.attachment ){ - console.log( 'error: no post content or attachments' ); + if ((!options.content || options.content.trim().length === 0 ) && !options.attachment ){ + console.log('error: no post content or attachments'); return false; } - let post_type = options.type || 'Note', + var post_type = options.type || 'Note', post_description = options.description, - post_date = moment( ).format( ), + post_date = moment().format(), post_in_reply_to = options.in_reply_to || null, reply_message = options.reply_message || null, post_content = options.content || options.url || '', - post_attachment = JSON.stringify( options.attachment ) || '[]'; + post_attachment = JSON.stringify(options.attachment) || '[]'; - dbHelper.savePost( { + dbHelper.savePost({ type: post_type, content: post_content, attachment: post_attachment - }, function( err, data ){ - let post_id = data.lastID; + }, function(err, data){ + var post_id = data.lastID; - let post_object; + var post_object; - if ( post_type === 'Note' ){ + if ( post_type === 'Note' ){ post_object = { - 'id': `${ botURL }/post/${ post_id }`, + 'id': `${bot_url}/post/${post_id}`, 'type': post_type, 'published': post_date, - 'attributedTo': `${ botURL }/bot`, + 'attributedTo': `${bot_url}/bot`, 'content': reply_message || post_content, 'to': 'https://www.w3.org/ns/activitystreams#Public' }; - if ( options.attachment ){ - let attachments = []; + if (options.attachment){ + var attachments = []; - options.attachment.forEach( function( attachment ){ - attachments.push( { + options.attachment.forEach(function(attachment){ + attachments.push({ 'type': 'Image', 'content': attachment.content, 'url': attachment.url - } ); - } ); + }); + }); post_object.attachment = attachments; } } - if ( post_in_reply_to ){ + if (post_in_reply_to){ post_object.inReplyTo = post_in_reply_to; } - let post = { + var post = { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `${ botURL }/post/${ post_id }`, + 'id': `${bot_url}/post/${post_id}`, 'type': 'Create', - 'actor': `${ botURL }/bot`, + 'actor': `${bot_url}/bot`, 'object': post_object } - console.log( {post_in_reply_to} ); + console.log({post_in_reply_to}); - dbHelper.getFollowers( function( err, followers ){ - if ( followers ){ - console.log( `sending update to ${ followers.length } follower( s )...` ); + dbHelper.getFollowers(function(err, followers){ + if (followers){ + console.log(`sending update to ${followers.length} follower(s)...`); - followers.forEach( function( follower ){ - if ( follower.url ){ - bot.signAndSend( { + followers.forEach(function(follower){ + if (follower.url){ + bot.signAndSend({ follower: follower, message: post - }, function( err, data ){ + }, function(err, data){ - } ); + }); } - } ); + }); } - } ); + }); - if ( cb ){ - cb( null, post ); + if (cb){ + cb(null, post); } - } ); + }); }, - deletePost: function( post_id, follower_url, cb ){ - let bot = this; - // guid = crypto.randomBytes( 16 ).toString( 'hex' ); + deletePost: function(post_id, follower_url, cb){ + var bot = this; + // guid = crypto.randomBytes(16).toString('hex'); - bot.signAndSend( { + bot.signAndSend({ follower: { url: follower_url }, message: { '@context': 'https://www.w3.org/ns/activitystreams', - // 'summary': `${ bot} deleted a post`, - // 'id': `${ bot.bot_url}/${ guid}`, + // 'summary': `${bot} deleted a post`, + // 'id': `${bot.bot_url}/${guid}`, 'type': 'Delete', - 'actor': `${ bot.bot_url }/bot`, - 'object': `${ bot.bot_url }/post/${ post_id }` + 'actor': `${bot.bot_url}/bot`, + 'object': `${bot.bot_url}/post/${post_id}` } - }, function( err, data ){ - if ( cb ){ - cb( err, data ); + }, function(err, data){ + if (cb){ + cb(err, data); } - } ); + }); }, - accept: function( payload, cb ){ - let bot = this, - guid = crypto.randomBytes( 16 ).toString( 'hex' ); + accept: function(payload, cb){ + var bot = this, + guid = crypto.randomBytes(16).toString('hex'); - dbHelper.getEvent( payload.id, function( err, data ){ - // console.log( 'get_event', err, data ); + dbHelper.getEvent(payload.id, function(err, data){ + // console.log('get_event', err, data); - bot.signAndSend( { + bot.signAndSend({ follower: { url: payload.actor }, message: { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `${ bot.bot_url }/${ guid }`, + 'id': `${bot.bot_url}/${guid}`, 'type': 'Accept', - 'actor': `${ bot.bot_url }/bot`, + 'actor': `${bot.bot_url}/bot`, 'object': payload, } - }, function( err, data ){ - if ( cb ){ - cb( err, payload, data ); + }, function(err, data){ + if (cb){ + cb(err, payload, data); } - console.log( 'saving event', payload.id ) - dbHelper.saveEvent( payload.id ); - } ); + console.log('saving event', payload.id) + dbHelper.saveEvent(payload.id); + }); - } ); -// dbHelper.getEvent( payload.id, function( err, data ){ -// console.log( 'get_event', err, data ); + }); +// dbHelper.getEvent(payload.id, function(err, data){ +// console.log('get_event', err, data); -// if ( !err && !data ){ -// bot.signAndSend( { +// if (!err && !data){ +// bot.signAndSend({ // follower: { // url: payload.actor // }, // message: { // '@context': 'https://www.w3.org/ns/activitystreams', -// 'id': `${ bot.bot_url }/${ guid }`, +// 'id': `${bot.bot_url}/${guid}`, // 'type': 'Accept', -// 'actor': `${ bot.bot_url }/bot`, +// 'actor': `${bot.bot_url}/bot`, // 'object': payload, // } -// }, function( err, data ){ -// if ( cb ){ -// cb( err, payload, data ); +// }, function(err, data){ +// if (cb){ +// cb(err, payload, data); // } -// console.log( 'saving event', payload.id ) -// dbHelper.saveEvent( payload.id ); -// } ); -// } else if ( !err ){ -// console.log( 'duplicate event' ); +// console.log('saving event', payload.id) +// dbHelper.saveEvent(payload.id); +// }); +// } else if (!err){ +// console.log('duplicate event'); // } -// } ); +// }); }, - signAndSend: function( options, cb ){ - let bot = this; - console.log( 'message to sign:' ); - console.log( util.inspect( options.message, false, null, true ) ); + signAndSend: function(options, cb){ + var bot = this; + // console.log('message to sign:'); + // console.log(util.inspect(options.message, false, null, true)); - options.follower.url = options.follower.url.replace( 'http://localhost:3000', 'https://befc66af.ngrok.io' ); + options.follower.url = options.follower.url.replace('http://localhost:3000', 'https://befc66af.ngrok.io'); - if ( options.follower.url && options.follower.url !== 'undefined' ){ - options.follower.domain = url.parse( options.follower.url ).hostname; + if (options.follower.url && options.follower.url !== 'undefined'){ + options.follower.domain = url.parse(options.follower.url).hostname; - let signer = crypto.createSign( 'sha256' ), + var signer = crypto.createSign('sha256'), d = new Date(), - string_to_sign = `( request-target ): post /inbox\nhost: ${ options.follower.domain}\ndate: ${ d.toUTCString() }`; + string_to_sign = `(request-target): post /inbox\nhost: ${options.follower.domain}\ndate: ${d.toUTCString()}`; - signer.update( string_to_sign ); - signer.end( ); + signer.update(string_to_sign); + signer.end(); - let signature = signer.sign( privateKey ); - let signatureB64 = signature.toString( 'base64' ); - let header = `keyId="${ botURL }/bot",headers="( request-target ) host date",signature="${ signatureB64 }"`; + var signature = signer.sign(private_key); + var signature_b64 = signature.toString('base64'); + var header = `keyId="${bot_url}/bot",headers="(request-target) host date",signature="${signature_b64}"`; - let reqObject = { - url: `https://${ options.follower.domain }/inbox`, + var req_object = { + url: `https://${options.follower.domain}/inbox`, headers: { 'Host': options.follower.domain, - 'Date': d.toUTCString( ), + 'Date': d.toUTCString(), 'Signature': header }, method: 'POST', @@ -294,23 +297,23 @@ else{ body: options.message }; - console.log( 'request object:' ); - console.log( util.inspect( reqObject, false, null, true ) ); + // console.log('request object:'); + // console.log(util.inspect(req_object, false, null, true)); - request( reqObject, function ( error, response ){ - console.log( `sent message to ${ options.follower.url }...` ); - if ( error ) { - console.log( 'error:', error, response ); + request(req_object, function (error, response){ + console.log(`sent message to ${options.follower.url}...`); + if (error) { + console.log('error:', error, response); } else { - console.log( 'response:', response.statusCode, response.statusMessage ); - console.log( response.body ); + console.log('response:', response.statusCode, response.statusMessage); + // console.log(response); } - if ( cb ){ - cb( error, response ); + if (cb){ + cb(error, response); } - } ); + }); } } }; diff --git a/bot/bot.js b/bot/bot.js index 9a33898..1be3719 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -1,54 +1,58 @@ -var fs = require('fs'), - crypto = require('crypto'), - url = require('url'), - util = require('util'), - moment = require('moment'), - dbHelper = require(__dirname + '/../helpers/db.js'), - keys = require(__dirname + '/../helpers/keys.js'), - request = require('request'), - public_key_path = '.data/rsa/pubKey', - private_key_path = '.data/rsa/privKey', - bot_url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`, - botComposeReply = require(__dirname + '/responses.js'); +if ( !process.env.PROJECT_NAME || !process.env.PROJECT_ID ){ + require( 'dotenv' ).config(); +} + +const fs = require( 'fs' ), + crypto = require( 'crypto' ), + url = require( 'url' ), + util = require( 'util' ), + moment = require( 'moment' ), + dbHelper = require( __dirname + '/../helpers/db.js' ), + keys = require( __dirname + '/../helpers/keys.js' ), + request = require( 'request' ), + publicKeyPath = '.data/rsa/pubKey', + privateKeyPath = '.data/rsa/privKey', + botUrl = `https://${ process.env.PROJECT_DOMAIN}.glitch.me`, + botComposeReply = require( __dirname + '/responses.js' ); -if (!fs.existsSync(public_key_path) || !fs.existsSync(private_key_path)) { - keys.generateKeys(function(){ - process.kill(process.pid); - }); +if ( !fs.existsSync( publicKeyPath ) || !fs.existsSync( privateKeyPath ) ) { + keys.generateKeys( function(){ + process.kill( process.pid ); + } ); } else{ - var public_key = fs.readFileSync(public_key_path, 'utf8'), - private_key = fs.readFileSync(private_key_path, 'utf8'); + const publicKey = fs.readFileSync( publicKeyPath, 'utf8' ), + privateKey = fs.readFileSync( privateKeyPath, 'utf8' ); module.exports = { - bot_url: bot_url, + bot_url: botUrl, links: [ // { // rel: 'http://webfinger.net/rel/profile-page', // type: 'text/html', - // href: `${bot_url}` + // href: `${ botUrl }` // }, // { // rel: 'http://schemas.google.com/g/2010#updates-from', // type: 'application/atom+xml', - // href: `${bot_url}/feed` + // href: `${ botUrl }/feed` // }, { rel: 'self', type: 'application/activity+json', - href: `${bot_url}/bot` + href: `${ botUrl }/bot` }, // { // rel: 'hub', - // href: `${bot_url}/pubsub` + // href: `${ botUrl }/pubsub` // }, // { // rel: 'salmon', - // href: `${bot_url}/salmon` + // href: `${ botUrl }/salmon` // }, // { // rel: 'magic-public-key', - // href: `data:application/magic-public-key,RSA.${public_key.replace('-----BEGIN PUBLIC KEY-----\n', '').replace('\n-----END PUBLIC KEY-----', '').replace('\\n', '')}` + // href: `data:application/magic-public-key,RSA.${ publicKey.replace( '-----BEGIN PUBLIC KEY-----\n', '').replace( '\n-----END PUBLIC KEY-----', '').replace( '\\n', '') }` // } ], info: { @@ -56,7 +60,7 @@ else{ 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1' ], - 'id': `${bot_url}/bot`, + 'id': `${ botUrl }/bot`, 'icon': [{ 'url': process.env.BOT_AVATAR_URL, 'type': 'Image' @@ -68,221 +72,221 @@ else{ 'type': 'Person', 'name': process.env.BOT_USERNAME, 'preferredUsername': process.env.BOT_USERNAME, - 'inbox': `${bot_url}/inbox`, + 'inbox': `${ botUrl }/inbox`, 'publicKey': { - 'id': `${bot_url}/bot#main-key`, - 'owner': `${bot_url}/bot`, - 'publicKeyPem': public_key + 'id': `${ botUrl }/bot#main-key`, + 'owner': `${ botUrl }/bot`, + 'publicKeyPem': publicKey } }, script: require( __dirname + '/script.js' ), composeReply: botComposeReply, - sendReply: function(options, cb){ - var bot = this, - reply_to_username = ''; + sendReply: function( options, cb ){ + let bot = this, + replyToUsername = ''; try{ - var actor_url_parts = options.payload.actor.split('/'); - var username = actor_url_parts[actor_url_parts.length-1]; - reply_to_username = `@${username}@${url.parse(options.payload.actor).hostname} `; + let actorUrlParts = options.payload.actor.split( '/' ); + let username = actorUrlParts[actorUrlParts.length-1]; + replyToUsername = `@${ username}@${ url.parse(options.payload.actor).hostname} `; - console.log({reply_to_username}); - } catch(err){ /*noop*/ } + console.log( { replyToUsername } ); + } catch( err ){ /* noop */ } - bot.createPost({ + bot.createPost( { type: 'Note', - content: `
${options.payload.object.content}${options.payload.object.url}

${options.reply_message}

`, - reply_message: `${reply_to_username} ${options.reply_message}`, + content: `
${ options.payload.object.content }${ options.payload.object.url }

${ options.reply_message }

`, + reply_message: `${ replyToUsername } ${ options.reply_message }`, in_reply_to: options.payload.object.url - }, function(err, message){ - // console.log(err, message); - }); + }, function( err, message ){ + // console.log( err, message ); + } ); }, - createPost: function(options, cb){ - var bot = this; + createPost: function( options, cb ){ + let bot = this; - if ((!options.content || options.content.trim().length === 0 ) && !options.attachment ){ - console.log('error: no post content or attachments'); + if ( ( !options.content || options.content.trim().length === 0 ) && !options.attachment ){ + console.log( 'error: no post content or attachments' ); return false; } - var post_type = options.type || 'Note', - post_description = options.description, - post_date = moment().format(), - post_in_reply_to = options.in_reply_to || null, - reply_message = options.reply_message || null, - post_content = options.content || options.url || '', - post_attachment = JSON.stringify(options.attachment) || '[]'; + let postType = options.type || 'Note', + postDescription = options.description, + postDate = moment().format(), + postInReplyTo = options.in_reply_to || null, + replyMessage = options.reply_message || null, + postContent = options.content || options.url || '', + postAttachment = JSON.stringify( options.attachment ) || '[]'; - dbHelper.savePost({ - type: post_type, - content: post_content, - attachment: post_attachment - }, function(err, data){ - var post_id = data.lastID; + dbHelper.savePost( { + type: postType, + content: postContent, + attachment: postAttachment + }, function( err, data ){ + let postID = data.lastID; - var post_object; + let postObject; - if ( post_type === 'Note' ){ - post_object = { - 'id': `${bot_url}/post/${post_id}`, - 'type': post_type, - 'published': post_date, - 'attributedTo': `${bot_url}/bot`, - 'content': reply_message || post_content, + if ( postType === 'Note' ){ + postObject = { + 'id': `${ botUrl }/post/${ postID }`, + 'type': postType, + 'published': postDate, + 'attributedTo': `${ botUrl }/bot`, + 'content': replyMessage || postContent, 'to': 'https://www.w3.org/ns/activitystreams#Public' }; - if (options.attachment){ - var attachments = []; + if ( options.attachment ){ + let attachments = []; - options.attachment.forEach(function(attachment){ - attachments.push({ + options.attachment.forEach( function( attachment ){ + attachments.push( { 'type': 'Image', 'content': attachment.content, 'url': attachment.url - }); - }); - post_object.attachment = attachments; + } ); + } ); + postObject.attachment = attachments; } } - if (post_in_reply_to){ - post_object.inReplyTo = post_in_reply_to; + if ( postInReplyTo ){ + postObject.inReplyTo = postInReplyTo; } - var post = { + let post = { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `${bot_url}/post/${post_id}`, + 'id': `${ botUrl }/post/${ postID }`, 'type': 'Create', - 'actor': `${bot_url}/bot`, - 'object': post_object + 'actor': `${ botUrl }/bot`, + 'object': postObject } - console.log({post_in_reply_to}); + console.log( {postInReplyTo} ); - dbHelper.getFollowers(function(err, followers){ - if (followers){ - console.log(`sending update to ${followers.length} follower(s)...`); + dbHelper.getFollowers( function( err, followers ){ + if ( followers ){ + console.log( `sending update to ${ followers.length} follower(s)...` ); - followers.forEach(function(follower){ - if (follower.url){ - bot.signAndSend({ + followers.forEach( function( follower ){ + if ( follower.url ){ + bot.signAndSend( { follower: follower, message: post - }, function(err, data){ + }, function( err, data ){ - }); + } ); } - }); + } ); } - }); + } ); - if (cb){ - cb(null, post); + if ( cb ){ + cb( null, post ); } - }); + } ); }, - deletePost: function(post_id, follower_url, cb){ - var bot = this; - // guid = crypto.randomBytes(16).toString('hex'); + deletePost: function( postID, followerUrl, cb ){ + let bot = this; + // guid = crypto.randomBytes(16).toString( 'hex' ); - bot.signAndSend({ + bot.signAndSend( { follower: { - url: follower_url + url: followerUrl }, message: { '@context': 'https://www.w3.org/ns/activitystreams', - // 'summary': `${bot} deleted a post`, - // 'id': `${bot.bot_url}/${guid}`, + // 'summary': `${ bot} deleted a post`, + // 'id': `${ bot.bot_url }/${ guid }`, 'type': 'Delete', - 'actor': `${bot.bot_url}/bot`, - 'object': `${bot.bot_url}/post/${post_id}` + 'actor': `${ bot.bot_url }/bot`, + 'object': `${ bot.bot_url }/post/${ postID }` } - }, function(err, data){ - if (cb){ - cb(err, data); + }, function( err, data ){ + if ( cb ){ + cb( err, data ); } - }); + } ); }, - accept: function(payload, cb){ - var bot = this, - guid = crypto.randomBytes(16).toString('hex'); + accept: function( payload, cb ){ + let bot = this, + guid = crypto.randomBytes(16).toString( 'hex' ); - dbHelper.getEvent(payload.id, function(err, data){ - // console.log('get_event', err, data); + dbHelper.getEvent(payload.id, function( err, data ){ + // console.log( 'getEvent', err, data ); - bot.signAndSend({ + bot.signAndSend( { follower: { url: payload.actor }, message: { '@context': 'https://www.w3.org/ns/activitystreams', - 'id': `${bot.bot_url}/${guid}`, + 'id': `${ bot.bot_url }/${ guid }`, 'type': 'Accept', - 'actor': `${bot.bot_url}/bot`, + 'actor': `${ bot.bot_url }/bot`, 'object': payload, } - }, function(err, data){ - if (cb){ - cb(err, payload, data); + }, function( err, data ){ + if ( cb ){ + cb( err, payload, data ); } - console.log('saving event', payload.id) - dbHelper.saveEvent(payload.id); - }); + console.log( 'saving event', payload.id) + dbHelper.saveEvent(payload.id ); + } ); - }); -// dbHelper.getEvent(payload.id, function(err, data){ -// console.log('get_event', err, data); + } ); +// dbHelper.getEvent(payload.id, function( err, data ){ +// console.log( 'getEvent', err, data ); -// if (!err && !data){ -// bot.signAndSend({ +// if ( !err && !data ){ +// bot.signAndSend( { // follower: { // url: payload.actor // }, // message: { // '@context': 'https://www.w3.org/ns/activitystreams', -// 'id': `${bot.bot_url}/${guid}`, +// 'id': `${ bot.bot_url }/${ guid }`, // 'type': 'Accept', -// 'actor': `${bot.bot_url}/bot`, +// 'actor': `${ bot.bot_url }/bot`, // 'object': payload, // } -// }, function(err, data){ -// if (cb){ -// cb(err, payload, data); +// }, function( err, data ){ +// if ( cb ){ +// cb( err, payload, data ); // } -// console.log('saving event', payload.id) -// dbHelper.saveEvent(payload.id); -// }); -// } else if (!err){ -// console.log('duplicate event'); +// console.log( 'saving event', payload.id) +// dbHelper.saveEvent(payload.id ); +// } ); +// } else if ( !err ){ +// console.log( 'duplicate event' ); // } -// }); +// } ); }, - signAndSend: function(options, cb){ - var bot = this; - // console.log('message to sign:'); - // console.log(util.inspect(options.message, false, null, true)); + signAndSend: function( options, cb ){ + let bot = this; + // console.log( 'message to sign:' ); + // console.log( util.inspect(options.message, false, null, true) ); - options.follower.url = options.follower.url.replace('http://localhost:3000', 'https://befc66af.ngrok.io'); + options.follower.url = options.follower.url.replace( 'http://localhost:3000', 'https://befc66af.ngrok.io' ); - if (options.follower.url && options.follower.url !== 'undefined'){ - options.follower.domain = url.parse(options.follower.url).hostname; + if ( options.follower.url && options.follower.url !== 'undefined' ){ + options.follower.domain = url.parse( options.follower.url ).hostname; - var signer = crypto.createSign('sha256'), + let signer = crypto.createSign( 'sha256' ), d = new Date(), - string_to_sign = `(request-target): post /inbox\nhost: ${options.follower.domain}\ndate: ${d.toUTCString()}`; + stringToSign = `(request-target): post /inbox\nhost: ${ options.follower.domain}\ndate: ${ d.toUTCString() }`; - signer.update(string_to_sign); + signer.update( stringToSign ); signer.end(); - var signature = signer.sign(private_key); - var signature_b64 = signature.toString('base64'); - var header = `keyId="${bot_url}/bot",headers="(request-target) host date",signature="${signature_b64}"`; + let signature = signer.sign(privateKey ); + let signatureB64 = signature.toString( 'base64' ); + let header = `keyId="${ botUrl }/bot",headers="(request-target) host date",signature="${ signatureB64}"`; - var req_object = { - url: `https://${options.follower.domain}/inbox`, + let reqObject = { + url: `https://${ options.follower.domain }/inbox`, headers: { 'Host': options.follower.domain, 'Date': d.toUTCString(), @@ -293,23 +297,23 @@ else{ body: options.message }; - // console.log('request object:'); - // console.log(util.inspect(req_object, false, null, true)); + // console.log( 'request object:' ); + // console.log( util.inspect(reqObject, false, null, true) ); - request(req_object, function (error, response){ - console.log(`sent message to ${options.follower.url}...`); - if (error) { - console.log('error:', error, response); + request( reqObject, function (error, response ){ + console.log( `sent message to ${ options.follower.url}...` ); + if ( error) { + console.log( 'error:', error, response ); } else { - console.log('response:', response.statusCode, response.statusMessage); - // console.log(response); + console.log( 'response:', response.statusCode, response.statusMessage ); + // console.log( response ); } - if (cb){ - cb(error, response); + if ( cb ){ + cb( error, response ); } - }); + } ); } } }; diff --git a/package.json b/package.json index 6599b6b..587c487 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "color-scheme": "^1.0.1", "gifencoder": "^1.1.0", "neocities": "^0.0.3", - "jsdom": "^12.1.0" + "jsdom": "^12.1.0", + "dotenv": "^8.2.0" }, "engines": { "node": "8.x" @@ -43,4 +44,4 @@ "fediverse", "bot" ] -} +} \ No newline at end of file diff --git a/server.js b/server.js index cd4e240..1179663 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,7 @@ +if ( !process.env.PROJECT_NAME || !process.env.PROJECT_ID ){ + require( 'dotenv' ).config(); +} + const app = require(__dirname + "/app.js"), db = require(__dirname + "/helpers/db.js"), bot = require(__dirname + "/bot/bot.js"), diff --git a/sessions-journal b/sessions-journal deleted file mode 100644 index 38ec07747196874fd25984bf37af9389ed2bdad7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16928 zcmeI4d#vRAS>N~Ky}s}60!@=b2wh$XDe3EtJ-*Hc+IV~$d+hPuGoF+>p0UUF_%il* zZZ=8@H&H}IQzB4Ep-@PvBGf9Sidx#V!0y>S+opvV)DWd0DxgK63KbzLngVG(UPbh; z>`Kc&MeCzI=giD!p5J$#-_OtQ@;qlRT=>)L`#y2?1MQ>#df~#Q_Q#j}=?gD^`}6F9 zx7@t;_=0<}f8pYHUHScs@4F;k`kIS>eDTwlKL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2@6}9vs38F*4zs)dq zIUm>Seb~D9vmF-ZYt@{y+-76SUJmyYN)`_Gq8L_3D)m|5>~D6MN8*M*r5$t-YLyE+ zf@suRe#369U0Zme1848=Fgul|n1okOP|t=-teH6&o)2wLw)e@}8Uhcx;tg8uE4n_R z7CeD(8)+Ny%2GZMik&y*oOd;ow6bu06&e!z&4kdCDc^*=%9>eJxAV+)%>a-7 zW!I>@o6*z3PC$26zV%rm_7)cG3vfuH!I*P_2XcqeekPPruMu4Vj3=uY%@VH1I7Xj~ zHsbLJJUr_#3T3#ZF{CD`Qy#J@C>4FFo+m11~-B(gQC&@X`Z+Zako0y!w{6 zz3pPX_2y|%1yvQV^XdyfFVQ+nne1JY8c6KjFFbqquT-&rSJefX(w(~_)_MKz(@J>% z^^W=acU`*r7W!=$FT}YY9IG;I_w=r}+pfFE?ib9s@E3l&+=8' + resolution: + integrity: sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== /duplexer2/0.1.4: dependencies: readable-stream: 2.3.6 @@ -5379,6 +5386,7 @@ specifiers: color-scheme: ^1.0.1 connect-sqlite3: ^0.9.11 cron: ^1.8.2 + dotenv: ^8.2.0 express: ^4.16.3 express-babelify-middleware: ^0.2.1 express-handlebars: ^3.0.0