2013-01-31 15:49:45 +00:00
// Main job is to check pads periodically for activity and notify owners when someone begins editing and when someone finishes.
2013-01-30 18:09:04 +00:00
var db = require ( '../../src/node/db/DB' ) . db ,
2013-01-29 23:15:53 +00:00
API = require ( '../../src/node/db/API.js' ) ,
async = require ( '../../src/node_modules/async' ) ,
2013-01-31 01:16:34 +00:00
check = require ( 'validator' ) . check ,
2013-01-31 01:57:12 +00:00
email = require ( 'emailjs' ) ,
2013-01-29 23:15:53 +00:00
settings = require ( '../../src/node/utils/Settings' ) ;
2013-01-31 15:49:45 +00:00
// Settings -- EDIT THESE IN settings.json not here..
2013-01-29 23:15:53 +00:00
var pluginSettings = settings . ep _email _notifications ;
2013-02-05 01:41:36 +00:00
var checkFrequency = pluginSettings . checkFrequency || 60000 ; // 10 seconds
var staleTime = pluginSettings . staleTime || 300000 ; // 5 minutes
2013-01-31 15:49:45 +00:00
var fromName = pluginSettings . fromName || "Etherpad" ;
var fromEmail = pluginSettings . fromEmail || "pad@etherpad.org" ;
var urlToPads = pluginSettings . urlToPads || "http://beta.etherpad.org/p/" ;
2013-01-31 21:30:48 +00:00
var emailServer = pluginSettings . emailServer || { host : "127.0.0.1" } ;
2013-01-31 15:49:45 +00:00
// A timer object we maintain to control how we send emails
2013-01-29 23:15:53 +00:00
var timers = { } ;
2013-01-31 22:14:24 +00:00
// Connect to the email server -- This might not be the ideal place to connect but it stops us having lots of connections
2013-01-31 21:30:48 +00:00
var server = email . server . connect ( emailServer ) ;
2013-01-30 18:09:04 +00:00
2013-01-29 17:35:40 +00:00
exports . padUpdate = function ( hook _name , _pad ) {
2013-01-29 23:15:53 +00:00
var pad = _pad . pad ;
var padId = pad . id ;
exports . sendUpdates ( padId ) ;
// does an interval not exist for this pad?
if ( ! timers [ padId ] ) {
2013-01-31 22:14:24 +00:00
console . debug ( "Someone started editing " + padId ) ;
2013-01-31 01:57:12 +00:00
exports . notifyBegin ( padId ) ;
2013-01-29 23:15:53 +00:00
console . debug ( "Created an interval time check for " + padId ) ;
// if not then create one and write it to the timers object
2013-01-31 15:49:45 +00:00
timers [ padId ] = exports . createInterval ( padId , checkFrequency ) ;
2013-01-29 23:15:53 +00:00
} else { // an interval already exists so don't create
}
} ;
2013-01-31 01:57:12 +00:00
exports . notifyBegin = function ( padId ) {
2013-02-07 15:04:12 +00:00
console . warn ( "Getting pad email stuff for " + padId ) ;
2013-01-31 01:57:12 +00:00
db . get ( "emailSubscription:" + padId , function ( err , recipients ) { // get everyone we need to email
2013-01-31 15:49:45 +00:00
if ( recipients ) {
async . forEach ( Object . keys ( recipients ) , function ( recipient , cb ) {
2013-01-31 22:14:24 +00:00
// Is this recipient already on the pad?
exports . isUserEditingPad ( padId , recipients [ recipient ] . authorId , function ( err , userIsOnPad ) { // is the user already on the pad?
2013-03-27 01:19:27 +00:00
var onStart = typeof ( recipients [ recipient ] . onStart ) == "undefined" || recipients [ recipient ] . onStart ? true : false ; // In case onStart wasn't defined we set it to true
if ( ! userIsOnPad && onStart ) {
2013-01-31 22:14:24 +00:00
console . debug ( "Emailing " + recipient + " about a new begin update" ) ;
server . send ( {
2013-03-26 19:44:34 +00:00
text : "Your pad at " + urlToPads + padId + " is being edited, we're just emailing you let you know :)" ,
2013-01-31 22:14:24 +00:00
from : fromName + "<" + fromEmail + ">" ,
to : recipient ,
2013-02-01 13:16:02 +00:00
subject : "Someone started editing " + padId
2013-01-31 22:14:24 +00:00
} , function ( err , message ) { console . log ( err || message ) ; } ) ;
}
else {
console . debug ( "Didn't send an email because user is already on the pad" ) ;
}
} ) ;
2013-01-31 15:49:45 +00:00
cb ( ) ; // finish each user
} ,
function ( err ) {
2013-01-31 22:14:24 +00:00
// do some error handling..
2013-01-31 15:49:45 +00:00
} ) ;
}
2013-01-31 01:57:12 +00:00
} ) ;
}
exports . notifyEnd = function ( padId ) {
// get the modified contents...
2013-01-31 02:16:20 +00:00
var changesToPad = "Functionality does not exist" ;
2013-01-31 01:57:12 +00:00
db . get ( "emailSubscription:" + padId , function ( err , recipients ) { // get everyone we need to email
2013-01-31 21:26:22 +00:00
if ( recipients ) {
async . forEach ( Object . keys ( recipients ) , function ( recipient , cb ) {
2013-01-31 22:14:24 +00:00
// Is this recipient already on the pad?
exports . isUserEditingPad ( padId , recipients [ recipient ] . authorId , function ( err , userIsOnPad ) { // is the user already on the$
2013-03-27 01:19:27 +00:00
var onEnd = typeof ( recipients [ recipient ] . onEnd ) == "undefined" || recipients [ recipient ] . onEnd ? true : false ; // In case onEnd wasn't defined we set it to false
2013-03-26 19:44:34 +00:00
if ( ! userIsOnPad && onEnd ) {
2013-01-31 22:14:24 +00:00
console . debug ( "Emailing " + recipient + " about a pad finished being updated" ) ;
server . send ( {
2013-02-01 13:16:02 +00:00
text : "Your pad at " + urlToPads + padId + " has finished being edited, we're just emailing you let you know :) \n\n The changes look like this: \n" + changesToPad ,
2013-01-31 22:14:24 +00:00
from : fromName + "<" + fromEmail + ">" ,
to : recipient ,
subject : "Someone finished editing " + padId
} , function ( err , message ) { console . log ( err || message ) ; } ) ;
}
else {
console . debug ( "Didn't send an email because user is already on the pad" ) ;
}
} ) ;
2013-01-31 21:26:22 +00:00
cb ( ) ; // finish each user
} ,
function ( err ) {
2013-01-31 22:14:24 +00:00
// do some error handling..
2013-01-31 21:26:22 +00:00
} ) ;
}
2013-01-31 01:57:12 +00:00
} ) ;
}
2013-01-29 23:15:53 +00:00
exports . sendUpdates = function ( padId ) {
// check to see if we can delete this interval
API . getLastEdited ( padId , function ( callback , message ) {
// we delete an interval if a pad hasn't been edited in X seconds.
var currTS = new Date ( ) . getTime ( ) ;
if ( currTS - message . lastEdited > staleTime ) {
2013-01-31 01:57:12 +00:00
exports . notifyEnd ( padId ) ;
2013-01-29 23:15:53 +00:00
console . warn ( "Interval went stale so deleting it from object and timer" ) ;
var interval = timers [ padId ] ;
clearInterval ( timers [ padId ] ) ; // remove the interval timer
delete timers [ padId ] ; // remove the entry from the padId
} else {
2013-01-31 01:57:12 +00:00
console . debug ( "email timeout not stale so not deleting" ) ;
2013-01-29 23:15:53 +00:00
}
} ) ;
2013-01-29 17:35:40 +00:00
// The status of the users relationship with the pad -- IE if it's subscribed to this pad / if it's already on the pad
2013-01-29 23:15:53 +00:00
// This comes frmo the database
2013-01-31 22:14:24 +00:00
var userStatus = { } ; // I'm not even sure we use this value.. I put it here when drunk or something
2013-01-29 23:15:53 +00:00
}
2013-01-29 17:35:40 +00:00
2013-01-29 23:15:53 +00:00
// Is the user editing the pad?
exports . isUserEditingPad = function ( padId , user , cb ) {
2013-02-07 15:04:12 +00:00
console . warn ( "padId is" , padId ) ;
/ *
2013-01-29 23:15:53 +00:00
API . padUsers ( padId , function ( callback , padUsers ) { // get the current users editing the pad
2013-01-29 17:35:40 +00:00
var userIsEditing = false ;
2013-01-31 22:14:24 +00:00
console . debug ( "Current Pad Users:" + padUsers ) ;
2013-01-29 23:15:53 +00:00
// for each user on the pad right now
async . forEach ( padUsers . padUsers ,
2013-01-29 17:35:40 +00:00
function ( userOnPad , callback ) {
2013-01-31 22:14:24 +00:00
if ( userOnPad . id == user ) {
console . debug ( "User is on the pad so don't send any notification" ) ;
2013-01-29 17:35:40 +00:00
userIsEditing = true ; // If the user is editing the pad then return true
2013-01-29 23:15:53 +00:00
} else {
userIsEditing = false ; // If the user isnt on this pad then that'd be okay to contact em
2013-01-29 17:35:40 +00:00
}
2013-01-29 23:15:53 +00:00
callback ( userIsEditing ) ;
2013-01-29 17:35:40 +00:00
} ,
function ( err ) {
2013-01-29 23:15:53 +00:00
cb ( null , userIsEditing ) ;
} ) ;
} ) ;
2013-02-07 15:04:12 +00:00
* /
cb ( null , false ) ;
2013-01-29 17:35:40 +00:00
} ;
2013-01-31 15:49:45 +00:00
// Creates an interval process to check to send Updates based on checkFrequency and it returns an ID
2013-01-29 23:15:53 +00:00
exports . createInterval = function ( padId ) {
return setInterval ( function ( ) {
2013-01-31 15:49:45 +00:00
exports . sendUpdates ( padId ) , checkFrequency
2013-01-29 23:15:53 +00:00
} ) ;
}