2018-11-06 19:45:33 +02:00
const config = require ( './config.json' ) ;
const { DOMAIN , PRIVKEY _PATH , CERT _PATH , PORT _HTTP , PORT _HTTPS } = config ;
2018-10-15 07:18:10 +03:00
const Database = require ( 'better-sqlite3' ) ;
const db = new Database ( 'bot-node.db' ) ,
Parser = require ( 'rss-parser' ) ,
request = require ( 'request' ) ,
crypto = require ( 'crypto' ) ,
2019-04-13 23:34:49 +03:00
parser = new Parser ( { timeout : 2000 } ) ;
2018-10-15 07:18:10 +03:00
2019-04-29 23:36:04 +03:00
const Jackd = require ( 'jackd' ) ;
const beanstalkd = new Jackd ( ) ;
2018-12-18 04:11:53 +02:00
2019-04-29 23:36:04 +03:00
beanstalkd . connect ( )
2018-10-15 22:31:18 +03:00
2019-04-29 23:36:04 +03:00
async function foo ( ) {
while ( true ) {
try {
const { id , payload } = await beanstalkd . reserve ( )
console . log ( payload )
/* ... process job here ... */
await beanstalkd . delete ( id )
2019-05-07 20:20:21 +03:00
await doFeed ( payload )
2019-04-29 23:36:04 +03:00
} catch ( err ) {
// Log error somehow
console . error ( err )
}
}
}
2018-10-15 22:31:18 +03:00
2019-04-29 23:36:04 +03:00
foo ( )
2018-10-15 22:31:18 +03:00
2019-04-29 23:36:04 +03:00
function doFeed ( feedUrl ) {
return new Promise ( ( resolve , reject ) => {
2018-10-15 07:18:10 +03:00
// fetch new RSS for each feed
2019-04-29 23:36:04 +03:00
parser . parseURL ( feedUrl , function ( err , feedData ) {
2018-10-15 07:18:10 +03:00
if ( err ) {
2019-04-29 23:36:04 +03:00
reject ( 'error fetching ' + feedUrl + '; ' + err ) ;
2018-10-15 07:18:10 +03:00
}
else {
2019-04-29 23:36:04 +03:00
let feed = db . prepare ( 'select * from feeds where feed = ?' ) . get ( feedUrl ) ;
2018-10-15 07:18:10 +03:00
// get the old feed data from the database
let oldFeed = JSON . parse ( feed . content ) ;
// compare the feed item contents. if there's one or more whole new items (aka a new item with a unique guid),
// add the items to a list like
// [ { items: [], username }, {}, ... ]
let oldItems = oldFeed . items ;
let newItems = feedData . items ;
// find the difference of the sets of guids (fall back to title or
// description since guid is not required by spec) in the old and new feeds
let oldGuidSet = new Set ( oldItems . map ( el => el . guid || el . title || el . description ) ) ;
let newGuidSet = new Set ( newItems . map ( el => el . guid || el . title || el . description ) ) ;
// find things in the new set that aren't in the old set
let difference = new Set ( [ ... newGuidSet ] . filter ( x => ! oldGuidSet . has ( x ) ) ) ;
difference = [ ... difference ] ;
2018-10-15 22:31:18 +03:00
console . log ( 'diff' , feed . feed , difference . length , difference ) ;
2018-10-15 07:18:10 +03:00
if ( difference . length > 0 ) {
// get a list of new items in the diff
let brandNewItems = newItems . filter ( el => difference . includes ( el . guid ) || difference . includes ( el . title ) || difference . includes ( el . description ) ) ;
let acct = feed . username ;
2018-11-06 19:45:33 +02:00
let domain = DOMAIN ;
2018-10-15 20:47:27 +03:00
//console.log(acct, brandNewItems);
2018-10-15 07:18:10 +03:00
// send the message to everyone for each item!
for ( var item of brandNewItems ) {
item = transformContent ( item ) ;
2019-05-25 20:24:50 +03:00
let message ;
if ( item . link . match ( '/twitter.com/' ) ) {
message = ` ${ item . content } ` ;
}
else {
message = ` <p><a href=" ${ item . link } "> ${ item . title } </a></p><p> ${ item . content || '' } </p> ` ;
}
2018-10-15 07:18:10 +03:00
if ( item . enclosure && item . enclosure . url && item . enclosure . url . includes ( '.mp3' ) ) {
message += ` <p><a href=" ${ item . enclosure . url } "> ${ item . enclosure . url } </a></p> ` ;
}
sendCreateMessage ( message , acct , domain , null , null , item ) ;
}
// update the DB with new contents
let content = JSON . stringify ( feedData ) ;
2018-10-15 20:47:27 +03:00
db . prepare ( 'insert or replace into feeds(feed, username, content) values(?, ?, ?)' ) . run ( feed . feed , acct , content ) ;
2019-04-29 23:36:04 +03:00
return resolve ( 'done with ' + feedUrl )
2018-10-15 22:31:18 +03:00
}
else {
2019-04-29 23:36:04 +03:00
return resolve ( 'done with ' + feedUrl + ', no change' )
2018-10-15 07:18:10 +03:00
}
}
} ) ;
2019-04-29 23:36:04 +03:00
} ) . catch ( ( e ) => console . log ( e ) ) ;
2018-10-15 07:18:10 +03:00
}
// TODO: update the display name of a feed if the feed title has changed
2018-12-18 04:11:53 +02:00
// This is a function with a bunch of custom rules for different kinds of content I've found in the wild in things like Reddit rss feeds. Right now we just use the first image we find, if any.
2018-10-15 07:18:10 +03:00
function transformContent ( item ) {
let cheerio = require ( 'cheerio' ) ;
2018-12-18 04:11:53 +02:00
console . log ( JSON . stringify ( item ) ) ;
2018-10-15 07:18:10 +03:00
if ( item . content === undefined ) {
item . urls = [ ] ;
return item ;
}
let $ = cheerio . load ( item . content ) ;
2019-04-29 23:37:07 +03:00
// look through all the links to find images
2018-10-15 07:18:10 +03:00
let links = $ ( 'a' ) ;
let urls = [ ] ;
2018-10-15 20:47:27 +03:00
//console.log('links', links.length);
2018-10-15 07:18:10 +03:00
links . each ( ( i , e ) => {
let url = $ ( e ) . attr ( 'href' ) ;
// if there's an image, add it as a media attachment
2018-10-15 20:47:27 +03:00
if ( url && url . match ( /(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))/ ) ) {
//console.log(url);
2018-10-15 07:18:10 +03:00
urls . push ( url ) ;
}
} ) ;
2019-04-29 23:37:07 +03:00
// look through all the images
2018-12-18 04:11:53 +02:00
let images = $ ( 'img' ) ;
images . each ( ( i , e ) => {
let url = $ ( e ) . attr ( 'src' ) ;
// if there's an image, add it as a media attachment
2019-05-25 20:12:39 +03:00
if ( url ) {
2018-12-18 04:11:53 +02:00
//console.log(url);
urls . push ( url ) ;
// remove the image from the post body since it's in the attachment now
$ ( e ) . remove ( ) ;
}
} ) ;
2018-10-15 07:18:10 +03:00
item . urls = urls ;
2019-04-29 23:37:07 +03:00
// find iframe embeds and turn them into links
let iframes = $ ( 'iframe' ) ;
iframes . each ( ( i , e ) => {
console . log ( 'iframe' , i , e ) ;
let url = $ ( e ) . attr ( 'src' ) ;
$ ( e ) . replaceWith ( $ ( ` <a href=" ${ url } ">[embedded content]</a> ` ) ) ;
} ) ;
2018-10-15 07:18:10 +03:00
// remove multiple line breaks
2019-04-29 23:37:07 +03:00
//$('br').remove();
2018-10-15 07:18:10 +03:00
$ ( 'p' ) . each ( ( i , el ) => {
if ( $ ( el ) . html ( ) . replace ( /\s| /g , '' ) . length === 0 ) { $ ( el ) . remove ( ) ; }
} ) ;
2018-12-18 04:11:53 +02:00
// couple of hacky regexes to make sure we clean up everything
2019-05-12 23:32:49 +03:00
item . content = $ ( 'body' ) . html ( ) . replace ( /^(\n|\r)/g , '' ) . replace ( />\r+</g , '><' ) . replace ( / +/g , '' ) ;
item . content = item . content . replace ( /^(\n|\r)/g , '' ) . replace ( />\r+</g , '><' ) . replace ( />\s*</g , '><' ) . replace ( /​/g , '' ) . replace ( />\u200B+</g , '><' ) . replace ( / +/g , '' ) . replace ( /<p><\/p>/g , '' ) . replace ( /(<br\/?>)+/g , '<br>' ) ;
2018-10-15 07:18:10 +03:00
return item ;
}
// for each item in the list, get the account corresponding to the username
// for each item in the ITEMS list, send a message to all followers
// TODO import these form a helper
function signAndSend ( message , name , domain , req , res , targetDomain , inbox ) {
// get the private key
console . log ( 'sending to ' , name , targetDomain , inbox ) ;
let inboxFragment = inbox . replace ( 'https://' + targetDomain , '' ) ;
2018-10-15 22:31:18 +03:00
let result = db . prepare ( 'select privkey from accounts where name = ?' ) . get ( ` ${ name } @ ${ domain } ` ) ;
2018-10-15 20:47:27 +03:00
//console.log('got key', result === undefined, `${name}@${domain}`);
2018-10-15 07:18:10 +03:00
if ( result === undefined ) {
console . log ( ` No record found for ${ name } . ` ) ;
}
else {
let privkey = result . privkey ;
const signer = crypto . createSign ( 'sha256' ) ;
let d = new Date ( ) ;
let stringToSign = ` (request-target): post ${ inboxFragment } \n host: ${ targetDomain } \n date: ${ d . toUTCString ( ) } ` ;
signer . update ( stringToSign ) ;
signer . end ( ) ;
const signature = signer . sign ( privkey ) ;
const signature _b64 = signature . toString ( 'base64' ) ;
let header = ` keyId="https:// ${ domain } /u/ ${ name } ",headers="(request-target) host date",signature=" ${ signature _b64 } " ` ;
2018-10-15 20:47:27 +03:00
//console.log('signature:',header);
2018-10-15 07:18:10 +03:00
request ( {
url : inbox ,
headers : {
'Host' : targetDomain ,
'Date' : d . toUTCString ( ) ,
'Signature' : header
} ,
method : 'POST' ,
json : true ,
body : message
} , function ( error , response , body ) {
} ) ;
}
}
2019-03-29 15:47:51 +02:00
function createMessage ( text , name , domain , item , follower , guidNote ) {
const guidCreate = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
2018-10-15 07:18:10 +03:00
let d = new Date ( ) ;
let out = {
'@context' : 'https://www.w3.org/ns/activitystreams' ,
2019-03-29 15:47:51 +02:00
'id' : ` https:// ${ domain } /m/ ${ guidCreate } ` ,
2018-10-15 07:18:10 +03:00
'type' : 'Create' ,
'actor' : ` https:// ${ domain } /u/ ${ name } ` ,
2019-03-26 19:54:51 +02:00
'to' : [ follower ] ,
2018-10-15 07:18:10 +03:00
'object' : {
2019-03-29 15:47:51 +02:00
'id' : ` https:// ${ domain } /m/ ${ guidNote } ` ,
2018-10-15 07:18:10 +03:00
'type' : 'Note' ,
'published' : d . toISOString ( ) ,
'attributedTo' : ` https:// ${ domain } /u/ ${ name } ` ,
'content' : text ,
2019-03-29 15:47:51 +02:00
'link' : item . link ,
2019-02-21 17:58:14 +02:00
'cc' : 'https://www.w3.org/ns/activitystreams#Public'
2018-10-15 07:18:10 +03:00
}
} ;
// add image attachment
let attachment ;
2019-03-26 19:54:51 +02:00
console . log ( 'NUM IMAGES' , item . urls . length ) ;
2019-06-29 01:33:07 +03:00
if ( item . enclosure && item . enclosure . url && item . enclosure . url . includes ( '.mp3' ) ) {
attachment = {
'type' : 'Document' ,
'mediaType' : 'audio/mpeg' ,
'url' : item . enclosure . url ,
'name' : null
} ;
out . object . attachment = attachment ;
}
else if ( item . urls . length > 0 ) {
2018-10-15 07:18:10 +03:00
attachment = {
'type' : 'Document' ,
'mediaType' : 'image/png' , // TODO: update the mediaType to match jpeg,gif,etc
'url' : item . urls [ 0 ] ,
'name' : null
} ;
out . object . attachment = attachment ;
}
2019-03-26 19:54:51 +02:00
else if ( item . urls . length > 1 ) {
attachment = [ ] ;
let lengthFourMax = Math . min ( item . urls . length , 4 ) ;
for ( var i = 0 ; i < lengthFourMax ; i ++ ) {
attachment . push ( {
'type' : 'Document' ,
'mediaType' : 'image/png' , // TODO: update the mediaType to match jpeg,gif,etc
'url' : item . urls [ i ] ,
'name' : null
} ) ;
}
out . object . attachment = attachment ;
}
2018-10-15 07:18:10 +03:00
2019-03-29 15:47:51 +02:00
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 ) ) ;
2018-10-15 07:18:10 +03:00
return out ;
}
function sendCreateMessage ( text , name , domain , req , res , item ) {
2018-11-06 19:45:33 +02:00
// console.log(`${name}@${domain}`);
2018-10-15 07:18:10 +03:00
let result = db . prepare ( 'select followers from accounts where name = ?' ) . get ( ` ${ name } @ ${ domain } ` ) ;
let followers = JSON . parse ( result . followers ) ;
2019-03-29 18:23:05 +02:00
const guidNote = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
2018-11-06 19:45:33 +02:00
// console.log(followers);
2018-10-15 20:47:27 +03:00
if ( ! followers ) {
followers = [ ] ;
}
2018-10-15 07:18:10 +03:00
for ( let follower of followers ) {
let inbox = follower + '/inbox' ;
let myURL = new URL ( follower ) ;
let targetDomain = myURL . hostname ;
2019-03-29 15:47:51 +02:00
let message = createMessage ( text , name , domain , item , follower , guidNote ) ;
2018-10-15 07:18:10 +03:00
signAndSend ( message , name , domain , req , res , targetDomain , inbox ) ;
}
}