Use JavaScript Standard Style

Introduce JavaScript Standard Style as project style rule,
and fixed all fail on backend code.
This commit is contained in:
BoHong Li 2017-03-08 18:45:51 +08:00
parent 8f1c97f4a4
commit 4889e9732d
21 changed files with 3723 additions and 3784 deletions

1122
app.js

File diff suppressed because it is too large Load diff

View file

@ -1,190 +1,192 @@
//auth // auth
//external modules // external modules
var passport = require('passport'); var passport = require('passport')
var FacebookStrategy = require('passport-facebook').Strategy; var FacebookStrategy = require('passport-facebook').Strategy
var TwitterStrategy = require('passport-twitter').Strategy; var TwitterStrategy = require('passport-twitter').Strategy
var GithubStrategy = require('passport-github').Strategy; var GithubStrategy = require('passport-github').Strategy
var GitlabStrategy = require('passport-gitlab2').Strategy; var GitlabStrategy = require('passport-gitlab2').Strategy
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy; var DropboxStrategy = require('passport-dropbox-oauth2').Strategy
var GoogleStrategy = require('passport-google-oauth20').Strategy; var GoogleStrategy = require('passport-google-oauth20').Strategy
var LdapStrategy = require('passport-ldapauth'); var LdapStrategy = require('passport-ldapauth')
var LocalStrategy = require('passport-local').Strategy; var LocalStrategy = require('passport-local').Strategy
var validator = require('validator'); var validator = require('validator')
//core // core
var config = require('./config.js'); var config = require('./config.js')
var logger = require("./logger.js"); var logger = require('./logger.js')
var models = require("./models"); var models = require('./models')
function callback(accessToken, refreshToken, profile, done) { function callback (accessToken, refreshToken, profile, done) {
//logger.info(profile.displayName || profile.username); // logger.info(profile.displayName || profile.username);
var stringifiedProfile = JSON.stringify(profile); var stringifiedProfile = JSON.stringify(profile)
models.User.findOrCreate({ models.User.findOrCreate({
where: {
profileid: profile.id.toString()
},
defaults: {
profile: stringifiedProfile,
accessToken: accessToken,
refreshToken: refreshToken
}
}).spread(function (user, created) {
if (user) {
var needSave = false
if (user.profile !== stringifiedProfile) {
user.profile = stringifiedProfile
needSave = true
}
if (user.accessToken !== accessToken) {
user.accessToken = accessToken
needSave = true
}
if (user.refreshToken !== refreshToken) {
user.refreshToken = refreshToken
needSave = true
}
if (needSave) {
user.save().then(function () {
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
})
} else {
if (config.debug) { logger.info('user login: ' + user.id) }
return done(null, user)
}
}
}).catch(function (err) {
logger.error('auth callback failed: ' + err)
return done(err, null)
})
}
function registerAuthMethod () {
// facebook
if (config.facebook) {
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.serverurl + '/auth/facebook/callback'
}, callback))
}
// twitter
if (config.twitter) {
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
callbackURL: config.serverurl + '/auth/twitter/callback'
}, callback))
}
// github
if (config.github) {
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.serverurl + '/auth/github/callback'
}, callback))
}
// gitlab
if (config.gitlab) {
passport.use(new GitlabStrategy({
baseURL: config.gitlab.baseURL,
clientID: config.gitlab.clientID,
clientSecret: config.gitlab.clientSecret,
callbackURL: config.serverurl + '/auth/gitlab/callback'
}, callback))
}
// dropbox
if (config.dropbox) {
passport.use(new DropboxStrategy({
apiVersion: '2',
clientID: config.dropbox.clientID,
clientSecret: config.dropbox.clientSecret,
callbackURL: config.serverurl + '/auth/dropbox/callback'
}, callback))
}
// google
if (config.google) {
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.serverurl + '/auth/google/callback'
}, callback))
}
// ldap
if (config.ldap) {
passport.use(new LdapStrategy({
server: {
url: config.ldap.url || null,
bindDn: config.ldap.bindDn || null,
bindCredentials: config.ldap.bindCredentials || null,
searchBase: config.ldap.searchBase || null,
searchFilter: config.ldap.searchFilter || null,
searchAttributes: config.ldap.searchAttributes || null,
tlsOptions: config.ldap.tlsOptions || null
}
},
function (user, done) {
var profile = {
id: 'LDAP-' + user.uidNumber,
username: user.uid,
displayName: user.displayName,
emails: user.mail ? [user.mail] : [],
avatarUrl: null,
profileUrl: null,
provider: 'ldap'
}
var stringifiedProfile = JSON.stringify(profile)
models.User.findOrCreate({
where: { where: {
profileid: profile.id.toString() profileid: profile.id.toString()
}, },
defaults: { defaults: {
profile: stringifiedProfile, profile: stringifiedProfile
accessToken: accessToken,
refreshToken: refreshToken
} }
}).spread(function (user, created) { }).spread(function (user, created) {
if (user) { if (user) {
var needSave = false; var needSave = false
if (user.profile != stringifiedProfile) { if (user.profile !== stringifiedProfile) {
user.profile = stringifiedProfile; user.profile = stringifiedProfile
needSave = true; needSave = true
} }
if (user.accessToken != accessToken) { if (needSave) {
user.accessToken = accessToken; user.save().then(function () {
needSave = true; if (config.debug) { logger.info('user login: ' + user.id) }
} return done(null, user)
if (user.refreshToken != refreshToken) { })
user.refreshToken = refreshToken; } else {
needSave = true; if (config.debug) { logger.info('user login: ' + user.id) }
} return done(null, user)
if (needSave) { }
user.save().then(function () {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
});
} else {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
}
} }
}).catch(function (err) { }).catch(function (err) {
logger.error('auth callback failed: ' + err); logger.error('ldap auth failed: ' + err)
return done(err, null); return done(err, null)
}); })
}))
}
// email
if (config.email) {
passport.use(new LocalStrategy({
usernameField: 'email'
},
function (email, password, done) {
if (!validator.isEmail(email)) return done(null, false)
models.User.findOne({
where: {
email: email
}
}).then(function (user) {
if (!user) return done(null, false)
if (!user.verifyPassword(password)) return done(null, false)
return done(null, user)
}).catch(function (err) {
logger.error(err)
return done(err)
})
}))
}
} }
//facebook module.exports = {
if (config.facebook) { registerAuthMethod: registerAuthMethod
module.exports = passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.serverurl + '/auth/facebook/callback'
}, callback));
}
//twitter
if (config.twitter) {
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
callbackURL: config.serverurl + '/auth/twitter/callback'
}, callback));
}
//github
if (config.github) {
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.serverurl + '/auth/github/callback'
}, callback));
}
//gitlab
if (config.gitlab) {
passport.use(new GitlabStrategy({
baseURL: config.gitlab.baseURL,
clientID: config.gitlab.clientID,
clientSecret: config.gitlab.clientSecret,
callbackURL: config.serverurl + '/auth/gitlab/callback'
}, callback));
}
//dropbox
if (config.dropbox) {
passport.use(new DropboxStrategy({
apiVersion: '2',
clientID: config.dropbox.clientID,
clientSecret: config.dropbox.clientSecret,
callbackURL: config.serverurl + '/auth/dropbox/callback'
}, callback));
}
//google
if (config.google) {
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.serverurl + '/auth/google/callback'
}, callback));
}
// ldap
if (config.ldap) {
passport.use(new LdapStrategy({
server: {
url: config.ldap.url || null,
bindDn: config.ldap.bindDn || null,
bindCredentials: config.ldap.bindCredentials || null,
searchBase: config.ldap.searchBase || null,
searchFilter: config.ldap.searchFilter || null,
searchAttributes: config.ldap.searchAttributes || null,
tlsOptions: config.ldap.tlsOptions || null
},
},
function(user, done) {
var profile = {
id: 'LDAP-' + user.uidNumber,
username: user.uid,
displayName: user.displayName,
emails: user.mail ? [user.mail] : [],
avatarUrl: null,
profileUrl: null,
provider: 'ldap',
}
var stringifiedProfile = JSON.stringify(profile);
models.User.findOrCreate({
where: {
profileid: profile.id.toString()
},
defaults: {
profile: stringifiedProfile,
}
}).spread(function (user, created) {
if (user) {
var needSave = false;
if (user.profile != stringifiedProfile) {
user.profile = stringifiedProfile;
needSave = true;
}
if (needSave) {
user.save().then(function () {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
});
} else {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
}
}
}).catch(function (err) {
logger.error('ldap auth failed: ' + err);
return done(err, null);
});
}));
}
// email
if (config.email) {
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
if (!validator.isEmail(email)) return done(null, false);
models.User.findOne({
where: {
email: email
}
}).then(function (user) {
if (!user) return done(null, false);
if (!user.verifyPassword(password)) return done(null, false);
return done(null, user);
}).catch(function (err) {
logger.error(err);
return done(err);
});
}));
} }

View file

@ -1,118 +1,117 @@
// external modules // external modules
var fs = require('fs'); var fs = require('fs')
var path = require('path'); var path = require('path')
var fs = require('fs');
// configs // configs
var env = process.env.NODE_ENV || 'development'; var env = process.env.NODE_ENV || 'development'
var config = require(path.join(__dirname, '..', 'config.json'))[env]; var config = require(path.join(__dirname, '..', 'config.json'))[env]
var debug = process.env.DEBUG ? (process.env.DEBUG === 'true') : ((typeof config.debug === 'boolean') ? config.debug : (env === 'development')); var debug = process.env.DEBUG ? (process.env.DEBUG === 'true') : ((typeof config.debug === 'boolean') ? config.debug : (env === 'development'))
// Create function that reads docker secrets but fails fast in case of a non docker environment // Create function that reads docker secrets but fails fast in case of a non docker environment
var handleDockerSecret = fs.existsSync('/run/secrets/') ? function(secret) { var handleDockerSecret = fs.existsSync('/run/secrets/') ? function (secret) {
return fs.existsSync('/run/secrets/' + secret) ? fs.readFileSync('/run/secrets/' + secret) : null; return fs.existsSync('/run/secrets/' + secret) ? fs.readFileSync('/run/secrets/' + secret) : null
} : function() { } : function () {
return null return null
}; }
// url // url
var domain = process.env.DOMAIN || process.env.HMD_DOMAIN || config.domain || ''; var domain = process.env.DOMAIN || process.env.HMD_DOMAIN || config.domain || ''
var urlpath = process.env.URL_PATH || process.env.HMD_URL_PATH || config.urlpath || ''; var urlpath = process.env.URL_PATH || process.env.HMD_URL_PATH || config.urlpath || ''
var port = process.env.PORT || process.env.HMD_PORT || config.port || 3000; var port = process.env.PORT || process.env.HMD_PORT || config.port || 3000
var alloworigin = process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : (config.alloworigin || ['localhost']); var alloworigin = process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : (config.alloworigin || ['localhost'])
var usessl = !!config.usessl; var usessl = !!config.usessl
var protocolusessl = (usessl === true && typeof process.env.HMD_PROTOCOL_USESSL === 'undefined' && typeof config.protocolusessl === 'undefined') var protocolusessl = (usessl === true && typeof process.env.HMD_PROTOCOL_USESSL === 'undefined' && typeof config.protocolusessl === 'undefined')
? true : (process.env.HMD_PROTOCOL_USESSL ? (process.env.HMD_PROTOCOL_USESSL === 'true') : !!config.protocolusessl); ? true : (process.env.HMD_PROTOCOL_USESSL ? (process.env.HMD_PROTOCOL_USESSL === 'true') : !!config.protocolusessl)
var urladdport = process.env.HMD_URL_ADDPORT ? (process.env.HMD_URL_ADDPORT === 'true') : !!config.urladdport; var urladdport = process.env.HMD_URL_ADDPORT ? (process.env.HMD_URL_ADDPORT === 'true') : !!config.urladdport
var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((typeof config.usecdn === 'boolean') ? config.usecdn : true); var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((typeof config.usecdn === 'boolean') ? config.usecdn : true)
var allowanonymous = process.env.HMD_ALLOW_ANONYMOUS ? (process.env.HMD_ALLOW_ANONYMOUS === 'true') : ((typeof config.allowanonymous === 'boolean') ? config.allowanonymous : true); var allowanonymous = process.env.HMD_ALLOW_ANONYMOUS ? (process.env.HMD_ALLOW_ANONYMOUS === 'true') : ((typeof config.allowanonymous === 'boolean') ? config.allowanonymous : true)
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl; var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl
var permissions = ['editable', 'limited', 'locked', 'protected', 'private']; var permissions = ['editable', 'limited', 'locked', 'protected', 'private']
if (allowanonymous) { if (allowanonymous) {
permissions.unshift('freely'); permissions.unshift('freely')
} }
var defaultpermission = process.env.HMD_DEFAULT_PERMISSION || config.defaultpermission; var defaultpermission = process.env.HMD_DEFAULT_PERMISSION || config.defaultpermission
defaultpermission = permissions.indexOf(defaultpermission) != -1 ? defaultpermission : 'editable'; defaultpermission = permissions.indexOf(defaultpermission) !== -1 ? defaultpermission : 'editable'
// db // db
var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl; var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl
var db = config.db || {}; var db = config.db || {}
// ssl path // ssl path
var sslkeypath = (fs.existsSync('/run/secrets/key.pem') ? '/run/secrets/key.pem' : null) || config.sslkeypath || ''; var sslkeypath = (fs.existsSync('/run/secrets/key.pem') ? '/run/secrets/key.pem' : null) || config.sslkeypath || ''
var sslcertpath = (fs.existsSync('/run/secrets/cert.pem') ? '/run/secrets/cert.pem' : null) || config.sslcertpath || ''; var sslcertpath = (fs.existsSync('/run/secrets/cert.pem') ? '/run/secrets/cert.pem' : null) || config.sslcertpath || ''
var sslcapath = (fs.existsSync('/run/secrets/ca.pem') ? '/run/secrets/ca.pem' : null) || config.sslcapath || ''; var sslcapath = (fs.existsSync('/run/secrets/ca.pem') ? '/run/secrets/ca.pem' : null) || config.sslcapath || ''
var dhparampath = (fs.existsSync('/run/secrets/dhparam.pem') ? '/run/secrets/dhparam.pem' : null) || config.dhparampath || ''; var dhparampath = (fs.existsSync('/run/secrets/dhparam.pem') ? '/run/secrets/dhparam.pem' : null) || config.dhparampath || ''
// other path // other path
var tmppath = config.tmppath || './tmp'; var tmppath = config.tmppath || './tmp'
var defaultnotepath = config.defaultnotepath || './public/default.md'; var defaultnotepath = config.defaultnotepath || './public/default.md'
var docspath = config.docspath || './public/docs'; var docspath = config.docspath || './public/docs'
var indexpath = config.indexpath || './public/views/index.ejs'; var indexpath = config.indexpath || './public/views/index.ejs'
var hackmdpath = config.hackmdpath || './public/views/hackmd.ejs'; var hackmdpath = config.hackmdpath || './public/views/hackmd.ejs'
var errorpath = config.errorpath || './public/views/error.ejs'; var errorpath = config.errorpath || './public/views/error.ejs'
var prettypath = config.prettypath || './public/views/pretty.ejs'; var prettypath = config.prettypath || './public/views/pretty.ejs'
var slidepath = config.slidepath || './public/views/slide.ejs'; var slidepath = config.slidepath || './public/views/slide.ejs'
// session // session
var sessionname = config.sessionname || 'connect.sid'; var sessionname = config.sessionname || 'connect.sid'
var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret'; var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret'
var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000; //14 days var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000 // 14 days
// static files // static files
var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000; // 1 day var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000 // 1 day
// socket.io // socket.io
var heartbeatinterval = config.heartbeatinterval || 5000; var heartbeatinterval = config.heartbeatinterval || 5000
var heartbeattimeout = config.heartbeattimeout || 10000; var heartbeattimeout = config.heartbeattimeout || 10000
// document // document
var documentmaxlength = config.documentmaxlength || 100000; var documentmaxlength = config.documentmaxlength || 100000
// image upload setting, available options are imgur/s3/filesystem // image upload setting, available options are imgur/s3/filesystem
var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur'; var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur'
config.s3 = config.s3 || {}; config.s3 = config.s3 || {}
var s3 = { var s3 = {
accessKeyId: handleDockerSecret('s3_acccessKeyId') || process.env.HMD_S3_ACCESS_KEY_ID || config.s3.accessKeyId, accessKeyId: handleDockerSecret('s3_acccessKeyId') || process.env.HMD_S3_ACCESS_KEY_ID || config.s3.accessKeyId,
secretAccessKey: handleDockerSecret('s3_secretAccessKey') || process.env.HMD_S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey, secretAccessKey: handleDockerSecret('s3_secretAccessKey') || process.env.HMD_S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey,
region: process.env.HMD_S3_REGION || config.s3.region region: process.env.HMD_S3_REGION || config.s3.region
} }
var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket; var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket
// auth // auth
var facebook = (process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET || fs.existsSync('/run/secrets/facebook_clientID') && fs.existsSync('/run/secrets/facebook_clientSecret')) ? { var facebook = ((process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) || (fs.existsSync('/run/secrets/facebook_clientID') && fs.existsSync('/run/secrets/facebook_clientSecret'))) ? {
clientID: handleDockerSecret('facebook_clientID') || process.env.HMD_FACEBOOK_CLIENTID, clientID: handleDockerSecret('facebook_clientID') || process.env.HMD_FACEBOOK_CLIENTID,
clientSecret: handleDockerSecret('facebook_clientSecret') || process.env.HMD_FACEBOOK_CLIENTSECRET clientSecret: handleDockerSecret('facebook_clientSecret') || process.env.HMD_FACEBOOK_CLIENTSECRET
} : config.facebook || false; } : config.facebook || false
var twitter = (process.env.HMD_TWITTER_CONSUMERKEY && process.env.HMD_TWITTER_CONSUMERSECRET || fs.existsSync('/run/secrets/twitter_consumerKey') && fs.existsSync('/run/secrets/twitter_consumerSecret')) ? { var twitter = ((process.env.HMD_TWITTER_CONSUMERKEY && process.env.HMD_TWITTER_CONSUMERSECRET) || (fs.existsSync('/run/secrets/twitter_consumerKey') && fs.existsSync('/run/secrets/twitter_consumerSecret'))) ? {
consumerKey: handleDockerSecret('twitter_consumerKey') || process.env.HMD_TWITTER_CONSUMERKEY, consumerKey: handleDockerSecret('twitter_consumerKey') || process.env.HMD_TWITTER_CONSUMERKEY,
consumerSecret: handleDockerSecret('twitter_consumerSecret') || process.env.HMD_TWITTER_CONSUMERSECRET consumerSecret: handleDockerSecret('twitter_consumerSecret') || process.env.HMD_TWITTER_CONSUMERSECRET
} : config.twitter || false; } : config.twitter || false
var github = (process.env.HMD_GITHUB_CLIENTID && process.env.HMD_GITHUB_CLIENTSECRET || fs.existsSync('/run/secrets/github_clientID') && fs.existsSync('/run/secrets/github_clientSecret')) ? { var github = ((process.env.HMD_GITHUB_CLIENTID && process.env.HMD_GITHUB_CLIENTSECRET) || (fs.existsSync('/run/secrets/github_clientID') && fs.existsSync('/run/secrets/github_clientSecret'))) ? {
clientID: handleDockerSecret('github_clientID') || process.env.HMD_GITHUB_CLIENTID, clientID: handleDockerSecret('github_clientID') || process.env.HMD_GITHUB_CLIENTID,
clientSecret: handleDockerSecret('github_clientSecret') || process.env.HMD_GITHUB_CLIENTSECRET clientSecret: handleDockerSecret('github_clientSecret') || process.env.HMD_GITHUB_CLIENTSECRET
} : config.github || false; } : config.github || false
var gitlab = (process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET || fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret')) ? { var gitlab = ((process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET) || (fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret'))) ? {
baseURL: process.env.HMD_GITLAB_BASEURL, baseURL: process.env.HMD_GITLAB_BASEURL,
clientID: handleDockerSecret('gitlab_clientID') || process.env.HMD_GITLAB_CLIENTID, clientID: handleDockerSecret('gitlab_clientID') || process.env.HMD_GITLAB_CLIENTID,
clientSecret: handleDockerSecret('gitlab_clientSecret') || process.env.HMD_GITLAB_CLIENTSECRET clientSecret: handleDockerSecret('gitlab_clientSecret') || process.env.HMD_GITLAB_CLIENTSECRET
} : config.gitlab || false; } : config.gitlab || false
var dropbox = ((process.env.HMD_DROPBOX_CLIENTID && process.env.HMD_DROPBOX_CLIENTSECRET) || (fs.existsSync('/run/secrets/dropbox_clientID') && fs.existsSync('/run/secrets/dropbox_clientSecret'))) ? { var dropbox = ((process.env.HMD_DROPBOX_CLIENTID && process.env.HMD_DROPBOX_CLIENTSECRET) || (fs.existsSync('/run/secrets/dropbox_clientID') && fs.existsSync('/run/secrets/dropbox_clientSecret'))) ? {
clientID: handleDockerSecret('dropbox_clientID') || process.env.HMD_DROPBOX_CLIENTID, clientID: handleDockerSecret('dropbox_clientID') || process.env.HMD_DROPBOX_CLIENTID,
clientSecret: handleDockerSecret('dropbox_clientSecret') || process.env.HMD_DROPBOX_CLIENTSECRET clientSecret: handleDockerSecret('dropbox_clientSecret') || process.env.HMD_DROPBOX_CLIENTSECRET
} : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false; } : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false
var google = ((process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSECRET) var google = ((process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSECRET) ||
|| (fs.existsSync('/run/secrets/google_clientID') && fs.existsSync('/run/secrets/google_clientSecret'))) ? { (fs.existsSync('/run/secrets/google_clientID') && fs.existsSync('/run/secrets/google_clientSecret'))) ? {
clientID: handleDockerSecret('google_clientID') || process.env.HMD_GOOGLE_CLIENTID, clientID: handleDockerSecret('google_clientID') || process.env.HMD_GOOGLE_CLIENTID,
clientSecret: handleDockerSecret('google_clientSecret') || process.env.HMD_GOOGLE_CLIENTSECRET clientSecret: handleDockerSecret('google_clientSecret') || process.env.HMD_GOOGLE_CLIENTSECRET
} : (config.google && config.google.clientID && config.google.clientSecret && config.google) || false; } : (config.google && config.google.clientID && config.google.clientSecret && config.google) || false
var ldap = config.ldap || (( var ldap = config.ldap || ((
process.env.HMD_LDAP_URL || process.env.HMD_LDAP_URL ||
process.env.HMD_LDAP_BINDDN || process.env.HMD_LDAP_BINDDN ||
@ -123,106 +122,97 @@ var ldap = config.ldap || ((
process.env.HMD_LDAP_SEARCHATTRIBUTES || process.env.HMD_LDAP_SEARCHATTRIBUTES ||
process.env.HMD_LDAP_TLS_CA || process.env.HMD_LDAP_TLS_CA ||
process.env.HMD_LDAP_PROVIDERNAME process.env.HMD_LDAP_PROVIDERNAME
) ? {} : false); ) ? {} : false)
if (process.env.HMD_LDAP_URL) if (process.env.HMD_LDAP_URL) { ldap.url = process.env.HMD_LDAP_URL }
ldap.url = process.env.HMD_LDAP_URL; if (process.env.HMD_LDAP_BINDDN) { ldap.bindDn = process.env.HMD_LDAP_BINDDN }
if (process.env.HMD_LDAP_BINDDN) if (process.env.HMD_LDAP_BINDCREDENTIALS) { ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS }
ldap.bindDn = process.env.HMD_LDAP_BINDDN; if (process.env.HMD_LDAP_TOKENSECRET) { ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET }
if (process.env.HMD_LDAP_BINDCREDENTIALS) if (process.env.HMD_LDAP_SEARCHBASE) { ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE }
ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS; if (process.env.HMD_LDAP_SEARCHFILTER) { ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER }
if (process.env.HMD_LDAP_TOKENSECRET) if (process.env.HMD_LDAP_SEARCHATTRIBUTES) { ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES }
ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET;
if (process.env.HMD_LDAP_SEARCHBASE)
ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE;
if (process.env.HMD_LDAP_SEARCHFILTER)
ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER;
if (process.env.HMD_LDAP_SEARCHATTRIBUTES)
ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES;
if (process.env.HMD_LDAP_TLS_CA) { if (process.env.HMD_LDAP_TLS_CA) {
var ca = { var ca = {
ca: process.env.HMD_LDAP_TLS_CA.split(',') ca: process.env.HMD_LDAP_TLS_CA.split(',')
} }
ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca; ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca
if (Array.isArray(ldap.tlsOptions.ca) && ldap.tlsOptions.ca.length > 0) { if (Array.isArray(ldap.tlsOptions.ca) && ldap.tlsOptions.ca.length > 0) {
var i, len, results; var i, len, results
results = []; results = []
for (i = 0, len = ldap.tlsOptions.ca.length; i < len; i++) { for (i = 0, len = ldap.tlsOptions.ca.length; i < len; i++) {
results.push(fs.readFileSync(ldap.tlsOptions.ca[i], 'utf8')); results.push(fs.readFileSync(ldap.tlsOptions.ca[i], 'utf8'))
}
ldap.tlsOptions.ca = results;
} }
ldap.tlsOptions.ca = results
}
} }
if (process.env.HMD_LDAP_PROVIDERNAME) { if (process.env.HMD_LDAP_PROVIDERNAME) {
ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME; ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME
} }
var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false; var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email; var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email
var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true); var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true)
function getserverurl() { function getserverurl () {
var url = ''; var url = ''
if (domain) { if (domain) {
var protocol = protocolusessl ? 'https://' : 'http://'; var protocol = protocolusessl ? 'https://' : 'http://'
url = protocol + domain; url = protocol + domain
if (urladdport && ((usessl && port != 443) || (!usessl && port != 80))) if (urladdport && ((usessl && port !== 443) || (!usessl && port !== 80))) { url += ':' + port }
url += ':' + port; }
} if (urlpath) { url += '/' + urlpath }
if (urlpath) return url
url += '/' + urlpath;
return url;
} }
var version = '0.5.0'; var version = '0.5.0'
var minimumCompatibleVersion = '0.5.0'; var minimumCompatibleVersion = '0.5.0'
var maintenance = true; var maintenance = true
var cwd = path.join(__dirname, '..'); var cwd = path.join(__dirname, '..')
module.exports = { module.exports = {
version: version, version: version,
minimumCompatibleVersion: minimumCompatibleVersion, minimumCompatibleVersion: minimumCompatibleVersion,
maintenance: maintenance, maintenance: maintenance,
debug: debug, debug: debug,
urlpath: urlpath, urlpath: urlpath,
port: port, port: port,
alloworigin: alloworigin, alloworigin: alloworigin,
usessl: usessl, usessl: usessl,
serverurl: getserverurl(), serverurl: getserverurl(),
usecdn: usecdn, usecdn: usecdn,
allowanonymous: allowanonymous, allowanonymous: allowanonymous,
allowfreeurl: allowfreeurl, allowfreeurl: allowfreeurl,
defaultpermission: defaultpermission, defaultpermission: defaultpermission,
dburl: dburl, dburl: dburl,
db: db, db: db,
sslkeypath: path.join(cwd, sslkeypath), sslkeypath: path.join(cwd, sslkeypath),
sslcertpath: path.join(cwd, sslcertpath), sslcertpath: path.join(cwd, sslcertpath),
sslcapath: path.join(cwd, sslcapath), sslcapath: path.join(cwd, sslcapath),
dhparampath: path.join(cwd, dhparampath), dhparampath: path.join(cwd, dhparampath),
tmppath: path.join(cwd, tmppath), tmppath: path.join(cwd, tmppath),
defaultnotepath: path.join(cwd, defaultnotepath), defaultnotepath: path.join(cwd, defaultnotepath),
docspath: path.join(cwd, docspath), docspath: path.join(cwd, docspath),
indexpath: path.join(cwd, indexpath), indexpath: path.join(cwd, indexpath),
hackmdpath: path.join(cwd, hackmdpath), hackmdpath: path.join(cwd, hackmdpath),
errorpath: path.join(cwd, errorpath), errorpath: path.join(cwd, errorpath),
prettypath: path.join(cwd, prettypath), prettypath: path.join(cwd, prettypath),
slidepath: path.join(cwd, slidepath), slidepath: path.join(cwd, slidepath),
sessionname: sessionname, sessionname: sessionname,
sessionsecret: sessionsecret, sessionsecret: sessionsecret,
sessionlife: sessionlife, sessionlife: sessionlife,
staticcachetime: staticcachetime, staticcachetime: staticcachetime,
heartbeatinterval: heartbeatinterval, heartbeatinterval: heartbeatinterval,
heartbeattimeout: heartbeattimeout, heartbeattimeout: heartbeattimeout,
documentmaxlength: documentmaxlength, documentmaxlength: documentmaxlength,
facebook: facebook, facebook: facebook,
twitter: twitter, twitter: twitter,
github: github, github: github,
gitlab: gitlab, gitlab: gitlab,
dropbox: dropbox, dropbox: dropbox,
google: google, google: google,
ldap: ldap, ldap: ldap,
imgur: imgur, imgur: imgur,
email: email, email: email,
allowemailregister: allowemailregister, allowemailregister: allowemailregister,
imageUploadType: imageUploadType, imageUploadType: imageUploadType,
s3: s3, s3: s3,
s3bucket: s3bucket s3bucket: s3bucket
}; }

View file

@ -1,172 +1,175 @@
//history // history
//external modules // external modules
var async = require('async');
//core // core
var config = require("./config.js"); var config = require('./config.js')
var logger = require("./logger.js"); var logger = require('./logger.js')
var response = require("./response.js"); var response = require('./response.js')
var models = require("./models"); var models = require('./models')
//public // public
var History = { var History = {
historyGet: historyGet, historyGet: historyGet,
historyPost: historyPost, historyPost: historyPost,
historyDelete: historyDelete, historyDelete: historyDelete,
updateHistory: updateHistory updateHistory: updateHistory
};
function getHistory(userid, callback) {
models.User.findOne({
where: {
id: userid
}
}).then(function (user) {
if (!user)
return callback(null, null);
var history = {};
if (user.history)
history = parseHistoryToObject(JSON.parse(user.history));
if (config.debug)
logger.info('read history success: ' + user.id);
return callback(null, history);
}).catch(function (err) {
logger.error('read history failed: ' + err);
return callback(err, null);
});
} }
function setHistory(userid, history, callback) { function getHistory (userid, callback) {
models.User.update({ models.User.findOne({
history: JSON.stringify(parseHistoryToArray(history)) where: {
}, { id: userid
where: {
id: userid
}
}).then(function (count) {
return callback(null, count);
}).catch(function (err) {
logger.error('set history failed: ' + err);
return callback(err, null);
});
}
function updateHistory(userid, noteId, document, time) {
if (userid && noteId && typeof document !== 'undefined') {
getHistory(userid, function (err, history) {
if (err || !history) return;
if (!history[noteId]) {
history[noteId] = {};
}
var noteHistory = history[noteId];
var noteInfo = models.Note.parseNoteInfo(document);
noteHistory.id = noteId;
noteHistory.text = noteInfo.title;
noteHistory.time = time || Date.now();
noteHistory.tags = noteInfo.tags;
setHistory(userid, history, function (err, count) {
return;
});
});
} }
} }).then(function (user) {
if (!user) {
function parseHistoryToArray(history) { return callback(null, null)
var _history = [];
Object.keys(history).forEach(function (key) {
var item = history[key];
_history.push(item);
});
return _history;
}
function parseHistoryToObject(history) {
var _history = {};
for (var i = 0, l = history.length; i < l; i++) {
var item = history[i];
_history[item.id] = item;
} }
return _history; var history = {}
if (user.history) {
history = parseHistoryToObject(JSON.parse(user.history))
}
if (config.debug) {
logger.info('read history success: ' + user.id)
}
return callback(null, history)
}).catch(function (err) {
logger.error('read history failed: ' + err)
return callback(err, null)
})
} }
function historyGet(req, res) { function setHistory (userid, history, callback) {
if (req.isAuthenticated()) { models.User.update({
getHistory(req.user.id, function (err, history) { history: JSON.stringify(parseHistoryToArray(history))
if (err) return response.errorInternalError(res); }, {
if (!history) return response.errorNotFound(res); where: {
res.send({ id: userid
history: parseHistoryToArray(history) }
}); }).then(function (count) {
}); return callback(null, count)
}).catch(function (err) {
logger.error('set history failed: ' + err)
return callback(err, null)
})
}
function updateHistory (userid, noteId, document, time) {
if (userid && noteId && typeof document !== 'undefined') {
getHistory(userid, function (err, history) {
if (err || !history) return
if (!history[noteId]) {
history[noteId] = {}
}
var noteHistory = history[noteId]
var noteInfo = models.Note.parseNoteInfo(document)
noteHistory.id = noteId
noteHistory.text = noteInfo.title
noteHistory.time = time || Date.now()
noteHistory.tags = noteInfo.tags
setHistory(userid, history, function (err, count) {
if (err) {
logger.log(err)
}
})
})
}
}
function parseHistoryToArray (history) {
var _history = []
Object.keys(history).forEach(function (key) {
var item = history[key]
_history.push(item)
})
return _history
}
function parseHistoryToObject (history) {
var _history = {}
for (var i = 0, l = history.length; i < l; i++) {
var item = history[i]
_history[item.id] = item
}
return _history
}
function historyGet (req, res) {
if (req.isAuthenticated()) {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
res.send({
history: parseHistoryToArray(history)
})
})
} else {
return response.errorForbidden(res)
}
}
function historyPost (req, res) {
if (req.isAuthenticated()) {
var noteId = req.params.noteId
if (!noteId) {
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res)
if (config.debug) { logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history) }
try {
var history = JSON.parse(req.body.history)
} catch (err) {
return response.errorBadRequest(res)
}
if (Array.isArray(history)) {
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res)
res.end()
})
} else {
return response.errorBadRequest(res)
}
} else { } else {
return response.errorForbidden(res); if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res)
} getHistory(req.user.id, function (err, history) {
} if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
function historyPost(req, res) { if (!history[noteId]) return response.errorNotFound(res)
if (req.isAuthenticated()) { if (req.body.pinned === 'true' || req.body.pinned === 'false') {
var noteId = req.params.noteId; history[noteId].pinned = (req.body.pinned === 'true')
if (!noteId) { setHistory(req.user.id, history, function (err, count) {
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res); if (err) return response.errorInternalError(res)
if (config.debug) res.end()
logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history); })
try {
var history = JSON.parse(req.body.history);
} catch (err) {
return response.errorBadRequest(res);
}
if (Array.isArray(history)) {
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
} else {
return response.errorBadRequest(res);
}
} else { } else {
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res); return response.errorBadRequest(res)
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
if (!history[noteId]) return response.errorNotFound(res);
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
history[noteId].pinned = (req.body.pinned === 'true');
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
} else {
return response.errorBadRequest(res);
}
});
} }
} else { })
return response.errorForbidden(res);
} }
} else {
return response.errorForbidden(res)
}
} }
function historyDelete(req, res) { function historyDelete (req, res) {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
var noteId = req.params.noteId; var noteId = req.params.noteId
if (!noteId) { if (!noteId) {
setHistory(req.user.id, [], function (err, count) { setHistory(req.user.id, [], function (err, count) {
if (err) return response.errorInternalError(res); if (err) return response.errorInternalError(res)
res.end(); res.end()
}); })
} else {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res);
if (!history) return response.errorNotFound(res);
delete history[noteId];
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res);
res.end();
});
});
}
} else { } else {
return response.errorForbidden(res); getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
delete history[noteId]
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res)
res.end()
})
})
} }
} else {
return response.errorForbidden(res)
}
} }
module.exports = History; module.exports = History

View file

@ -1,25 +1,23 @@
"use strict";
// external modules // external modules
var randomcolor = require('randomcolor'); var randomcolor = require('randomcolor')
// core // core
module.exports = function(name) { module.exports = function (name) {
var color = randomcolor({ var color = randomcolor({
seed: name, seed: name,
luminosity: 'dark' luminosity: 'dark'
}); })
var letter = name.substring(0, 1).toUpperCase(); var letter = name.substring(0, 1).toUpperCase()
var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'; var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">'; svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">'
svg += '<g>'; svg += '<g>'
svg += '<rect width="96" height="96" fill="' + color + '" />'; svg += '<rect width="96" height="96" fill="' + color + '" />'
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">'; svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">'
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>'; svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>'
svg += '</text>'; svg += '</text>'
svg += '</g>'; svg += '</g>'
svg += '</svg>'; svg += '</svg>'
return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64'); return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64')
}; }

View file

@ -1,22 +1,22 @@
var winston = require('winston'); var winston = require('winston')
winston.emitErrs = true; winston.emitErrs = true
var logger = new winston.Logger({ var logger = new winston.Logger({
transports: [ transports: [
new winston.transports.Console({ new winston.transports.Console({
level: 'debug', level: 'debug',
handleExceptions: true, handleExceptions: true,
json: false, json: false,
colorize: true, colorize: true,
timestamp: true timestamp: true
}) })
], ],
exitOnError: false exitOnError: false
}); })
module.exports = logger; module.exports = logger
module.exports.stream = { module.exports.stream = {
write: function(message, encoding){ write: function (message, encoding) {
logger.info(message); logger.info(message)
} }
}; }

View file

@ -1,15 +1,11 @@
"use strict";
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Users', 'accessToken', Sequelize.STRING); queryInterface.addColumn('Users', 'accessToken', Sequelize.STRING)
queryInterface.addColumn('Users', 'refreshToken', Sequelize.STRING); queryInterface.addColumn('Users', 'refreshToken', Sequelize.STRING)
return; },
},
down: function (queryInterface, Sequelize) { down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Users', 'accessToken'); queryInterface.removeColumn('Users', 'accessToken')
queryInterface.removeColumn('Users', 'refreshToken'); queryInterface.removeColumn('Users', 'refreshToken')
return; }
} }
};

View file

@ -1,8 +1,6 @@
'use strict';
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'savedAt', Sequelize.DATE); queryInterface.addColumn('Notes', 'savedAt', Sequelize.DATE)
queryInterface.createTable('Revisions', { queryInterface.createTable('Revisions', {
id: { id: {
type: Sequelize.UUID, type: Sequelize.UUID,
@ -15,13 +13,11 @@ module.exports = {
length: Sequelize.INTEGER, length: Sequelize.INTEGER,
createdAt: Sequelize.DATE, createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE updatedAt: Sequelize.DATE
}); })
return;
}, },
down: function (queryInterface, Sequelize) { down: function (queryInterface, Sequelize) {
queryInterface.dropTable('Revisions'); queryInterface.dropTable('Revisions')
queryInterface.removeColumn('Notes', 'savedAt'); queryInterface.removeColumn('Notes', 'savedAt')
return;
} }
}; }

View file

@ -1,9 +1,7 @@
'use strict';
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'authorship', Sequelize.TEXT); queryInterface.addColumn('Notes', 'authorship', Sequelize.TEXT)
queryInterface.addColumn('Revisions', 'authorship', Sequelize.TEXT); queryInterface.addColumn('Revisions', 'authorship', Sequelize.TEXT)
queryInterface.createTable('Authors', { queryInterface.createTable('Authors', {
id: { id: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
@ -15,14 +13,12 @@ module.exports = {
userId: Sequelize.UUID, userId: Sequelize.UUID,
createdAt: Sequelize.DATE, createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE updatedAt: Sequelize.DATE
}); })
return;
}, },
down: function (queryInterface, Sequelize) { down: function (queryInterface, Sequelize) {
queryInterface.dropTable('Authors'); queryInterface.dropTable('Authors')
queryInterface.removeColumn('Revisions', 'authorship'); queryInterface.removeColumn('Revisions', 'authorship')
queryInterface.removeColumn('Notes', 'authorship'); queryInterface.removeColumn('Notes', 'authorship')
return;
} }
}; }

View file

@ -1,11 +1,9 @@
'use strict';
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE); queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE)
}, },
down: function (queryInterface, Sequelize) { down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Notes', 'deletedAt'); queryInterface.removeColumn('Notes', 'deletedAt')
} }
}; }

View file

@ -1,13 +1,11 @@
'use strict';
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.addColumn('Users', 'email', Sequelize.TEXT); queryInterface.addColumn('Users', 'email', Sequelize.TEXT)
queryInterface.addColumn('Users', 'password', Sequelize.TEXT); queryInterface.addColumn('Users', 'password', Sequelize.TEXT)
}, },
down: function (queryInterface, Sequelize) { down: function (queryInterface, Sequelize) {
queryInterface.removeColumn('Users', 'email'); queryInterface.removeColumn('Users', 'email')
queryInterface.removeColumn('Users', 'password'); queryInterface.removeColumn('Users', 'password')
} }
}; }

View file

@ -1,43 +1,37 @@
"use strict";
// external modules // external modules
var Sequelize = require("sequelize"); var Sequelize = require('sequelize')
// core
var logger = require("../logger.js");
module.exports = function (sequelize, DataTypes) { module.exports = function (sequelize, DataTypes) {
var Author = sequelize.define("Author", { var Author = sequelize.define('Author', {
id: { id: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
}, },
color: { color: {
type: DataTypes.STRING type: DataTypes.STRING
} }
}, { }, {
indexes: [ indexes: [
{ {
unique: true, unique: true,
fields: ['noteId', 'userId'] fields: ['noteId', 'userId']
} }
], ],
classMethods: { classMethods: {
associate: function (models) { associate: function (models) {
Author.belongsTo(models.Note, { Author.belongsTo(models.Note, {
foreignKey: "noteId", foreignKey: 'noteId',
as: "note", as: 'note',
constraints: false constraints: false
}); })
Author.belongsTo(models.User, { Author.belongsTo(models.User, {
foreignKey: "userId", foreignKey: 'userId',
as: "user", as: 'user',
constraints: false constraints: false
}); })
} }
} }
}); })
return Author
return Author; }
};

View file

@ -1,57 +1,55 @@
"use strict";
// external modules // external modules
var fs = require("fs"); var fs = require('fs')
var path = require("path"); var path = require('path')
var Sequelize = require("sequelize"); var Sequelize = require('sequelize')
// core // core
var config = require('../config.js'); var config = require('../config.js')
var logger = require("../logger.js"); var logger = require('../logger.js')
var dbconfig = config.db; var dbconfig = config.db
dbconfig.logging = config.debug ? logger.info : false; dbconfig.logging = config.debug ? logger.info : false
var sequelize = null; var sequelize = null
// Heroku specific // Heroku specific
if (config.dburl) if (config.dburl) {
sequelize = new Sequelize(config.dburl, dbconfig); sequelize = new Sequelize(config.dburl, dbconfig)
else } else {
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
}
// [Postgres] Handling NULL bytes // [Postgres] Handling NULL bytes
// https://github.com/sequelize/sequelize/issues/6485 // https://github.com/sequelize/sequelize/issues/6485
function stripNullByte(value) { function stripNullByte (value) {
return value ? value.replace(/\u0000/g, "") : value; return value ? value.replace(/\u0000/g, '') : value
} }
sequelize.stripNullByte = stripNullByte; sequelize.stripNullByte = stripNullByte
function processData(data, _default, process) { function processData (data, _default, process) {
if (data === undefined) return data; if (data === undefined) return data
else return data === null ? _default : (process ? process(data) : data); else return data === null ? _default : (process ? process(data) : data)
} }
sequelize.processData = processData; sequelize.processData = processData
var db = {}; var db = {}
fs fs.readdirSync(__dirname)
.readdirSync(__dirname)
.filter(function (file) { .filter(function (file) {
return (file.indexOf(".") !== 0) && (file !== "index.js"); return (file.indexOf('.') !== 0) && (file !== 'index.js')
}) })
.forEach(function (file) { .forEach(function (file) {
var model = sequelize.import(path.join(__dirname, file)); var model = sequelize.import(path.join(__dirname, file))
db[model.name] = model; db[model.name] = model
}); })
Object.keys(db).forEach(function (modelName) { Object.keys(db).forEach(function (modelName) {
if ("associate" in db[modelName]) { if ('associate' in db[modelName]) {
db[modelName].associate(db); db[modelName].associate(db)
} }
}); })
db.sequelize = sequelize; db.sequelize = sequelize
db.Sequelize = Sequelize; db.Sequelize = Sequelize
module.exports = db; module.exports = db

File diff suppressed because it is too large Load diff

View file

@ -1,306 +1,306 @@
"use strict";
// external modules // external modules
var Sequelize = require("sequelize"); var Sequelize = require('sequelize')
var async = require('async'); var async = require('async')
var moment = require('moment'); var moment = require('moment')
var childProcess = require('child_process'); var childProcess = require('child_process')
var shortId = require('shortid'); var shortId = require('shortid')
// core // core
var config = require("../config.js"); var config = require('../config.js')
var logger = require("../logger.js"); var logger = require('../logger.js')
var dmpWorker = createDmpWorker(); var dmpWorker = createDmpWorker()
var dmpCallbackCache = {}; var dmpCallbackCache = {}
function createDmpWorker() { function createDmpWorker () {
var worker = childProcess.fork("./lib/workers/dmpWorker.js", { var worker = childProcess.fork('./lib/workers/dmpWorker.js', {
stdio: 'ignore' stdio: 'ignore'
}); })
if (config.debug) logger.info('dmp worker process started'); if (config.debug) logger.info('dmp worker process started')
worker.on('message', function (data) { worker.on('message', function (data) {
if (!data || !data.msg || !data.cacheKey) { if (!data || !data.msg || !data.cacheKey) {
return logger.error('dmp worker error: not enough data on message'); return logger.error('dmp worker error: not enough data on message')
} }
var cacheKey = data.cacheKey; var cacheKey = data.cacheKey
switch(data.msg) { switch (data.msg) {
case 'error': case 'error':
dmpCallbackCache[cacheKey](data.error, null); dmpCallbackCache[cacheKey](data.error, null)
break; break
case 'check': case 'check':
dmpCallbackCache[cacheKey](null, data.result); dmpCallbackCache[cacheKey](null, data.result)
break; break
} }
delete dmpCallbackCache[cacheKey]; delete dmpCallbackCache[cacheKey]
}); })
worker.on('close', function (code) { worker.on('close', function (code) {
dmpWorker = null; dmpWorker = null
if (config.debug) logger.info('dmp worker process exited with code ' + code); if (config.debug) logger.info('dmp worker process exited with code ' + code)
}); })
return worker; return worker
} }
function sendDmpWorker(data, callback) { function sendDmpWorker (data, callback) {
if (!dmpWorker) dmpWorker = createDmpWorker(); if (!dmpWorker) dmpWorker = createDmpWorker()
var cacheKey = Date.now() + '_' + shortId.generate(); var cacheKey = Date.now() + '_' + shortId.generate()
dmpCallbackCache[cacheKey] = callback; dmpCallbackCache[cacheKey] = callback
data = Object.assign(data, { data = Object.assign(data, {
cacheKey: cacheKey cacheKey: cacheKey
}); })
dmpWorker.send(data); dmpWorker.send(data)
} }
module.exports = function (sequelize, DataTypes) { module.exports = function (sequelize, DataTypes) {
var Revision = sequelize.define("Revision", { var Revision = sequelize.define('Revision', {
id: { id: {
type: DataTypes.UUID, type: DataTypes.UUID,
primaryKey: true, primaryKey: true,
defaultValue: Sequelize.UUIDV4 defaultValue: Sequelize.UUIDV4
}, },
patch: { patch: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
get: function () { get: function () {
return sequelize.processData(this.getDataValue('patch'), ""); return sequelize.processData(this.getDataValue('patch'), '')
},
set: function (value) {
this.setDataValue('patch', sequelize.stripNullByte(value))
}
},
lastContent: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('lastContent'), '')
},
set: function (value) {
this.setDataValue('lastContent', sequelize.stripNullByte(value))
}
},
content: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('content'), '')
},
set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value))
}
},
length: {
type: DataTypes.INTEGER
},
authorship: {
type: DataTypes.TEXT,
get: function () {
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
},
set: function (value) {
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
}
}
}, {
classMethods: {
associate: function (models) {
Revision.belongsTo(models.Note, {
foreignKey: 'noteId',
as: 'note',
constraints: false
})
},
getNoteRevisions: function (note, callback) {
Revision.findAll({
where: {
noteId: note.id
},
order: '"createdAt" DESC'
}).then(function (revisions) {
var data = []
for (var i = 0, l = revisions.length; i < l; i++) {
var revision = revisions[i]
data.push({
time: moment(revision.createdAt).valueOf(),
length: revision.length
})
}
callback(null, data)
}).catch(function (err) {
callback(err, null)
})
},
getPatchedNoteRevisionByTime: function (note, time, callback) {
// find all revisions to prepare for all possible calculation
Revision.findAll({
where: {
noteId: note.id
},
order: '"createdAt" DESC'
}).then(function (revisions) {
if (revisions.length <= 0) return callback(null, null)
// measure target revision position
Revision.count({
where: {
noteId: note.id,
createdAt: {
$gte: time
}
}, },
set: function (value) { order: '"createdAt" DESC'
this.setDataValue('patch', sequelize.stripNullByte(value)); }).then(function (count) {
} if (count <= 0) return callback(null, null)
}, sendDmpWorker({
lastContent: { msg: 'get revision',
type: DataTypes.TEXT, revisions: revisions,
get: function () { count: count
return sequelize.processData(this.getDataValue('lastContent'), ""); }, callback)
}, }).catch(function (err) {
set: function (value) { return callback(err, null)
this.setDataValue('lastContent', sequelize.stripNullByte(value)); })
} }).catch(function (err) {
}, return callback(err, null)
content: { })
type: DataTypes.TEXT, },
get: function () { checkAllNotesRevision: function (callback) {
return sequelize.processData(this.getDataValue('content'), ""); Revision.saveAllNotesRevision(function (err, notes) {
}, if (err) return callback(err, null)
set: function (value) { if (!notes || notes.length <= 0) {
this.setDataValue('content', sequelize.stripNullByte(value)); return callback(null, notes)
} } else {
}, Revision.checkAllNotesRevision(callback)
length: { }
type: DataTypes.INTEGER })
}, },
authorship: { saveAllNotesRevision: function (callback) {
type: DataTypes.TEXT, sequelize.models.Note.findAll({
get: function () { // query all notes that need to save for revision
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse); where: {
}, $and: [
set: function (value) { {
this.setDataValue('authorship', value ? JSON.stringify(value) : value); lastchangeAt: {
} $or: {
} $eq: null,
}, { $and: {
classMethods: { $ne: null,
associate: function (models) { $gt: sequelize.col('createdAt')
Revision.belongsTo(models.Note, {
foreignKey: "noteId",
as: "note",
constraints: false
});
},
getNoteRevisions: function (note, callback) {
Revision.findAll({
where: {
noteId: note.id
},
order: '"createdAt" DESC'
}).then(function (revisions) {
var data = [];
for (var i = 0, l = revisions.length; i < l; i++) {
var revision = revisions[i];
data.push({
time: moment(revision.createdAt).valueOf(),
length: revision.length
});
} }
callback(null, data); }
}).catch(function (err) { }
callback(err, null); },
}); {
}, savedAt: {
getPatchedNoteRevisionByTime: function (note, time, callback) { $or: {
// find all revisions to prepare for all possible calculation $eq: null,
Revision.findAll({ $lt: sequelize.col('lastchangeAt')
where: { }
noteId: note.id }
}, }
order: '"createdAt" DESC' ]
}).then(function (revisions) { }
if (revisions.length <= 0) return callback(null, null); }).then(function (notes) {
// measure target revision position if (notes.length <= 0) return callback(null, notes)
Revision.count({ var savedNotes = []
where: { async.each(notes, function (note, _callback) {
noteId: note.id, // revision saving policy: note not been modified for 5 mins or not save for 10 mins
createdAt: { if (note.lastchangeAt && note.savedAt) {
$gte: time var lastchangeAt = moment(note.lastchangeAt)
} var savedAt = moment(note.savedAt)
}, if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
order: '"createdAt" DESC' savedNotes.push(note)
}).then(function (count) { Revision.saveNoteRevision(note, _callback)
if (count <= 0) return callback(null, null); } else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
sendDmpWorker({ savedNotes.push(note)
msg: 'get revision', Revision.saveNoteRevision(note, _callback)
revisions: revisions, } else {
count: count return _callback(null, null)
}, callback); }
}).catch(function (err) { } else {
return callback(err, null); savedNotes.push(note)
}); Revision.saveNoteRevision(note, _callback)
}).catch(function (err) {
return callback(err, null);
});
},
checkAllNotesRevision: function (callback) {
Revision.saveAllNotesRevision(function (err, notes) {
if (err) return callback(err, null);
if (!notes || notes.length <= 0) {
return callback(null, notes);
} else {
Revision.checkAllNotesRevision(callback);
}
});
},
saveAllNotesRevision: function (callback) {
sequelize.models.Note.findAll({
// query all notes that need to save for revision
where: {
$and: [
{
lastchangeAt: {
$or: {
$eq: null,
$and: {
$ne: null,
$gt: sequelize.col('createdAt')
}
}
}
},
{
savedAt: {
$or: {
$eq: null,
$lt: sequelize.col('lastchangeAt')
}
}
}
]
}
}).then(function (notes) {
if (notes.length <= 0) return callback(null, notes);
var savedNotes = [];
async.each(notes, function (note, _callback) {
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
if (note.lastchangeAt && note.savedAt) {
var lastchangeAt = moment(note.lastchangeAt);
var savedAt = moment(note.savedAt);
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
} else {
return _callback(null, null);
}
} else {
savedNotes.push(note);
Revision.saveNoteRevision(note, _callback);
}
}, function (err) {
if (err) return callback(err, null);
// return null when no notes need saving at this moment but have delayed tasks to be done
var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes;
return callback(null, result);
});
}).catch(function (err) {
return callback(err, null);
});
},
saveNoteRevision: function (note, callback) {
Revision.findAll({
where: {
noteId: note.id
},
order: '"createdAt" DESC'
}).then(function (revisions) {
if (revisions.length <= 0) {
// if no revision available
Revision.create({
noteId: note.id,
lastContent: note.content,
length: note.content.length,
authorship: note.authorship
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback);
}).catch(function (err) {
return callback(err, null);
});
} else {
var latestRevision = revisions[0];
var lastContent = latestRevision.content || latestRevision.lastContent;
var content = note.content;
sendDmpWorker({
msg: 'create patch',
lastDoc: lastContent,
currDoc: content,
}, function (err, patch) {
if (err) logger.error('save note revision error', err);
if (!patch) {
// if patch is empty (means no difference) then just update the latest revision updated time
latestRevision.changed('updatedAt', true);
latestRevision.update({
updatedAt: Date.now()
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback);
}).catch(function (err) {
return callback(err, null);
});
} else {
Revision.create({
noteId: note.id,
patch: patch,
content: note.content,
length: note.content.length,
authorship: note.authorship
}).then(function (revision) {
// clear last revision content to reduce db size
latestRevision.update({
content: null
}).then(function () {
Revision.finishSaveNoteRevision(note, revision, callback);
}).catch(function (err) {
return callback(err, null);
});
}).catch(function (err) {
return callback(err, null);
});
}
});
}
}).catch(function (err) {
return callback(err, null);
});
},
finishSaveNoteRevision: function (note, revision, callback) {
note.update({
savedAt: revision.updatedAt
}).then(function () {
return callback(null, revision);
}).catch(function (err) {
return callback(err, null);
});
} }
} }, function (err) {
}); if (err) {
return callback(err, null)
}
// return null when no notes need saving at this moment but have delayed tasks to be done
var result = ((savedNotes.length === 0) && (notes.length > savedNotes.length)) ? null : savedNotes
return callback(null, result)
})
}).catch(function (err) {
return callback(err, null)
})
},
saveNoteRevision: function (note, callback) {
Revision.findAll({
where: {
noteId: note.id
},
order: '"createdAt" DESC'
}).then(function (revisions) {
if (revisions.length <= 0) {
// if no revision available
Revision.create({
noteId: note.id,
lastContent: note.content,
length: note.content.length,
authorship: note.authorship
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null)
})
} else {
var latestRevision = revisions[0]
var lastContent = latestRevision.content || latestRevision.lastContent
var content = note.content
sendDmpWorker({
msg: 'create patch',
lastDoc: lastContent,
currDoc: content
}, function (err, patch) {
if (err) logger.error('save note revision error', err)
if (!patch) {
// if patch is empty (means no difference) then just update the latest revision updated time
latestRevision.changed('updatedAt', true)
latestRevision.update({
updatedAt: Date.now()
}).then(function (revision) {
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null)
})
} else {
Revision.create({
noteId: note.id,
patch: patch,
content: note.content,
length: note.content.length,
authorship: note.authorship
}).then(function (revision) {
// clear last revision content to reduce db size
latestRevision.update({
content: null
}).then(function () {
Revision.finishSaveNoteRevision(note, revision, callback)
}).catch(function (err) {
return callback(err, null)
})
}).catch(function (err) {
return callback(err, null)
})
}
})
}
}).catch(function (err) {
return callback(err, null)
})
},
finishSaveNoteRevision: function (note, revision, callback) {
note.update({
savedAt: revision.updatedAt
}).then(function () {
return callback(null, revision)
}).catch(function (err) {
return callback(err, null)
})
}
}
})
return Revision; return Revision
}; }

View file

@ -1,19 +1,17 @@
"use strict"; // external modules
var shortId = require('shortid')
//external modules
var shortId = require('shortid');
module.exports = function (sequelize, DataTypes) { module.exports = function (sequelize, DataTypes) {
var Temp = sequelize.define("Temp", { var Temp = sequelize.define('Temp', {
id: { id: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true, primaryKey: true,
defaultValue: shortId.generate defaultValue: shortId.generate
}, },
data: { data: {
type: DataTypes.TEXT type: DataTypes.TEXT
} }
}); })
return Temp; return Temp
}; }

View file

@ -1,149 +1,147 @@
"use strict";
// external modules // external modules
var md5 = require("blueimp-md5"); var md5 = require('blueimp-md5')
var Sequelize = require("sequelize"); var Sequelize = require('sequelize')
var scrypt = require('scrypt'); var scrypt = require('scrypt')
// core // core
var logger = require("../logger.js"); var logger = require('../logger.js')
var letterAvatars = require('../letter-avatars.js'); var letterAvatars = require('../letter-avatars.js')
module.exports = function (sequelize, DataTypes) { module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", { var User = sequelize.define('User', {
id: { id: {
type: DataTypes.UUID, type: DataTypes.UUID,
primaryKey: true, primaryKey: true,
defaultValue: Sequelize.UUIDV4 defaultValue: Sequelize.UUIDV4
}, },
profileid: { profileid: {
type: DataTypes.STRING, type: DataTypes.STRING,
unique: true unique: true
}, },
profile: { profile: {
type: DataTypes.TEXT type: DataTypes.TEXT
}, },
history: { history: {
type: DataTypes.TEXT type: DataTypes.TEXT
}, },
accessToken: { accessToken: {
type: DataTypes.STRING type: DataTypes.STRING
}, },
refreshToken: { refreshToken: {
type: DataTypes.STRING type: DataTypes.STRING
}, },
email: { email: {
type: Sequelize.TEXT, type: Sequelize.TEXT,
validate: { validate: {
isEmail: true isEmail: true
} }
}, },
password: { password: {
type: Sequelize.TEXT, type: Sequelize.TEXT,
set: function(value) { set: function (value) {
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString("hex"); var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
this.setDataValue('password', hash); this.setDataValue('password', hash)
} }
}
}, {
instanceMethods: {
verifyPassword: function (attempt) {
if (scrypt.verifyKdfSync(new Buffer(this.password, 'hex'), attempt)) {
return this
} else {
return false
} }
}, { }
instanceMethods: { },
verifyPassword: function(attempt) { classMethods: {
if (scrypt.verifyKdfSync(new Buffer(this.password, "hex"), attempt)) { associate: function (models) {
return this; User.hasMany(models.Note, {
} else { foreignKey: 'ownerId',
return false; constraints: false
} })
} User.hasMany(models.Note, {
}, foreignKey: 'lastchangeuserId',
classMethods: { constraints: false
associate: function (models) { })
User.hasMany(models.Note, { },
foreignKey: "ownerId", getProfile: function (user) {
constraints: false return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null)
}); },
User.hasMany(models.Note, { parseProfile: function (profile) {
foreignKey: "lastchangeuserId", try {
constraints: false profile = JSON.parse(profile)
}); } catch (err) {
}, logger.error(err)
getProfile: function (user) { profile = null
return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null);
},
parseProfile: function (profile) {
try {
var profile = JSON.parse(profile);
} catch (err) {
logger.error(err);
profile = null;
}
if (profile) {
profile = {
name: profile.displayName || profile.username,
photo: User.parsePhotoByProfile(profile),
biggerphoto: User.parsePhotoByProfile(profile, true)
}
}
return profile;
},
parsePhotoByProfile: function (profile, bigger) {
var photo = null;
switch (profile.provider) {
case "facebook":
photo = 'https://graph.facebook.com/' + profile.id + '/picture';
if (bigger) photo += '?width=400';
else photo += '?width=96';
break;
case "twitter":
photo = 'https://twitter.com/' + profile.username + '/profile_image';
if (bigger) photo += '?size=original';
else photo += '?size=bigger';
break;
case "github":
photo = 'https://avatars.githubusercontent.com/u/' + profile.id;
if (bigger) photo += '?s=400';
else photo += '?s=96';
break;
case "gitlab":
photo = profile.avatarUrl;
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400');
else photo = photo.replace(/(\?s=)\d*$/i, '$196');
break;
case "dropbox":
//no image api provided, use gravatar
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value);
if (bigger) photo += '?s=400';
else photo += '?s=96';
break;
case "google":
photo = profile.photos[0].value;
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400');
else photo = photo.replace(/(\?sz=)\d*$/i, '$196');
break;
case "ldap":
//no image api provided,
//use gravatar if email exists,
//otherwise generate a letter avatar
if (profile.emails[0]) {
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]);
if (bigger) photo += '?s=400';
else photo += '?s=96';
} else {
photo = letterAvatars(profile.username);
}
break;
}
return photo;
},
parseProfileByEmail: function (email) {
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email);
return {
name: email.substring(0, email.lastIndexOf("@")),
photo: photoUrl += '?s=96',
biggerphoto: photoUrl += '?s=400'
};
}
} }
}); if (profile) {
profile = {
name: profile.displayName || profile.username,
photo: User.parsePhotoByProfile(profile),
biggerphoto: User.parsePhotoByProfile(profile, true)
}
}
return profile
},
parsePhotoByProfile: function (profile, bigger) {
var photo = null
switch (profile.provider) {
case 'facebook':
photo = 'https://graph.facebook.com/' + profile.id + '/picture'
if (bigger) photo += '?width=400'
else photo += '?width=96'
break
case 'twitter':
photo = 'https://twitter.com/' + profile.username + '/profile_image'
if (bigger) photo += '?size=original'
else photo += '?size=bigger'
break
case 'github':
photo = 'https://avatars.githubusercontent.com/u/' + profile.id
if (bigger) photo += '?s=400'
else photo += '?s=96'
break
case 'gitlab':
photo = profile.avatarUrl
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400')
else photo = photo.replace(/(\?s=)\d*$/i, '$196')
break
case 'dropbox':
// no image api provided, use gravatar
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value)
if (bigger) photo += '?s=400'
else photo += '?s=96'
break
case 'google':
photo = profile.photos[0].value
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
else photo = photo.replace(/(\?sz=)\d*$/i, '$196')
break
case 'ldap':
// no image api provided,
// use gravatar if email exists,
// otherwise generate a letter avatar
if (profile.emails[0]) {
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0])
if (bigger) photo += '?s=400'
else photo += '?s=96'
} else {
photo = letterAvatars(profile.username)
}
break
}
return photo
},
parseProfileByEmail: function (email) {
var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email)
return {
name: email.substring(0, email.lastIndexOf('@')),
photo: photoUrl + '?s=96',
biggerphoto: photoUrl + '?s=400'
}
}
}
})
return User; return User
}; }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,140 +1,137 @@
// external modules // external modules
var DiffMatchPatch = require('diff-match-patch'); var DiffMatchPatch = require('diff-match-patch')
var dmp = new DiffMatchPatch(); var dmp = new DiffMatchPatch()
// core // core
var config = require("../config.js"); var config = require('../config.js')
var logger = require("../logger.js"); var logger = require('../logger.js')
process.on('message', function(data) { process.on('message', function (data) {
if (!data || !data.msg || !data.cacheKey) { if (!data || !data.msg || !data.cacheKey) {
return logger.error('dmp worker error: not enough data'); return logger.error('dmp worker error: not enough data')
} }
switch (data.msg) { switch (data.msg) {
case 'create patch': case 'create patch':
if (!data.hasOwnProperty('lastDoc') || !data.hasOwnProperty('currDoc')) { if (!data.hasOwnProperty('lastDoc') || !data.hasOwnProperty('currDoc')) {
return logger.error('dmp worker error: not enough data on create patch'); return logger.error('dmp worker error: not enough data on create patch')
} }
try { try {
var patch = createPatch(data.lastDoc, data.currDoc); var patch = createPatch(data.lastDoc, data.currDoc)
process.send({ process.send({
msg: 'check', msg: 'check',
result: patch, result: patch,
cacheKey: data.cacheKey cacheKey: data.cacheKey
}); })
} catch (err) { } catch (err) {
logger.error('dmp worker error', err); logger.error('dmp worker error', err)
process.send({ process.send({
msg: 'error', msg: 'error',
error: err, error: err,
cacheKey: data.cacheKey cacheKey: data.cacheKey
}); })
} }
break; break
case 'get revision': case 'get revision':
if (!data.hasOwnProperty('revisions') || !data.hasOwnProperty('count')) { if (!data.hasOwnProperty('revisions') || !data.hasOwnProperty('count')) {
return logger.error('dmp worker error: not enough data on get revision'); return logger.error('dmp worker error: not enough data on get revision')
} }
try { try {
var result = getRevision(data.revisions, data.count); var result = getRevision(data.revisions, data.count)
process.send({ process.send({
msg: 'check', msg: 'check',
result: result, result: result,
cacheKey: data.cacheKey cacheKey: data.cacheKey
}); })
} catch (err) { } catch (err) {
logger.error('dmp worker error', err); logger.error('dmp worker error', err)
process.send({ process.send({
msg: 'error', msg: 'error',
error: err, error: err,
cacheKey: data.cacheKey cacheKey: data.cacheKey
}); })
} }
break; break
} }
}); })
function createPatch(lastDoc, currDoc) { function createPatch (lastDoc, currDoc) {
var ms_start = (new Date()).getTime(); var msStart = (new Date()).getTime()
var diff = dmp.diff_main(lastDoc, currDoc); var diff = dmp.diff_main(lastDoc, currDoc)
var patch = dmp.patch_make(lastDoc, diff); var patch = dmp.patch_make(lastDoc, diff)
patch = dmp.patch_toText(patch); patch = dmp.patch_toText(patch)
var ms_end = (new Date()).getTime(); var msEnd = (new Date()).getTime()
if (config.debug) { if (config.debug) {
logger.info(patch); logger.info(patch)
logger.info((ms_end - ms_start) + 'ms'); logger.info((msEnd - msStart) + 'ms')
} }
return patch; return patch
} }
function getRevision(revisions, count) { function getRevision (revisions, count) {
var ms_start = (new Date()).getTime(); var msStart = (new Date()).getTime()
var startContent = null; var startContent = null
var lastPatch = []; var lastPatch = []
var applyPatches = []; var applyPatches = []
var authorship = []; var authorship = []
if (count <= Math.round(revisions.length / 2)) { if (count <= Math.round(revisions.length / 2)) {
// start from top to target // start from top to target
for (var i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
var revision = revisions[i]; let revision = revisions[i]
if (i == 0) { if (i === 0) {
startContent = revision.content || revision.lastContent; startContent = revision.content || revision.lastContent
} }
if (i != count - 1) { if (i !== count - 1) {
var patch = dmp.patch_fromText(revision.patch); let patch = dmp.patch_fromText(revision.patch)
applyPatches = applyPatches.concat(patch); applyPatches = applyPatches.concat(patch)
} }
lastPatch = revision.patch; lastPatch = revision.patch
authorship = revision.authorship; authorship = revision.authorship
}
// swap DIFF_INSERT and DIFF_DELETE to achieve unpatching
for (var i = 0, l = applyPatches.length; i < l; i++) {
for (var j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
var diff = applyPatches[i].diffs[j];
if (diff[0] == DiffMatchPatch.DIFF_INSERT)
diff[0] = DiffMatchPatch.DIFF_DELETE;
else if (diff[0] == DiffMatchPatch.DIFF_DELETE)
diff[0] = DiffMatchPatch.DIFF_INSERT;
}
}
} else {
// start from bottom to target
var l = revisions.length - 1;
for (var i = l; i >= count - 1; i--) {
var revision = revisions[i];
if (i == l) {
startContent = revision.lastContent;
authorship = revision.authorship;
}
if (revision.patch) {
var patch = dmp.patch_fromText(revision.patch);
applyPatches = applyPatches.concat(patch);
}
lastPatch = revision.patch;
authorship = revision.authorship;
}
} }
try { // swap DIFF_INSERT and DIFF_DELETE to achieve unpatching
var finalContent = dmp.patch_apply(applyPatches, startContent)[0]; for (let i = 0, l = applyPatches.length; i < l; i++) {
} catch (err) { for (let j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
throw new Error(err); var diff = applyPatches[i].diffs[j]
if (diff[0] === DiffMatchPatch.DIFF_INSERT) { diff[0] = DiffMatchPatch.DIFF_DELETE } else if (diff[0] === DiffMatchPatch.DIFF_DELETE) { diff[0] = DiffMatchPatch.DIFF_INSERT }
}
} }
var data = { } else {
content: finalContent, // start from bottom to target
patch: dmp.patch_fromText(lastPatch), var l = revisions.length - 1
authorship: authorship for (var i = l; i >= count - 1; i--) {
}; let revision = revisions[i]
var ms_end = (new Date()).getTime(); if (i === l) {
if (config.debug) { startContent = revision.lastContent
logger.info((ms_end - ms_start) + 'ms'); authorship = revision.authorship
}
if (revision.patch) {
let patch = dmp.patch_fromText(revision.patch)
applyPatches = applyPatches.concat(patch)
}
lastPatch = revision.patch
authorship = revision.authorship
} }
return data; }
try {
var finalContent = dmp.patch_apply(applyPatches, startContent)[0]
} catch (err) {
throw new Error(err)
}
var data = {
content: finalContent,
patch: dmp.patch_fromText(lastPatch),
authorship: authorship
}
var msEnd = (new Date()).getTime()
if (config.debug) {
logger.info((msEnd - msStart) + 'ms')
}
return data
} }
// log uncaught exception // log uncaught exception
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {
logger.error('An uncaught exception has occured.'); logger.error('An uncaught exception has occured.')
logger.error(err); logger.error(err)
logger.error('Process will exit now.'); logger.error('Process will exit now.')
process.exit(1); process.exit(1)
}); })

View file

@ -5,7 +5,7 @@
"main": "app.js", "main": "app.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "npm run-script lint", "test": "node ./node_modules/standard/bin/cmd.js && npm run-script lint",
"lint": "eslint .", "lint": "eslint .",
"dev": "webpack --config webpack.config.js --progress --colors --watch", "dev": "webpack --config webpack.config.js --progress --colors --watch",
"build": "webpack --config webpack.production.js --progress --colors", "build": "webpack --config webpack.production.js --progress --colors",
@ -165,8 +165,15 @@
"optimize-css-assets-webpack-plugin": "^1.3.0", "optimize-css-assets-webpack-plugin": "^1.3.0",
"script-loader": "^0.7.0", "script-loader": "^0.7.0",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"standard": "^9.0.1",
"url-loader": "^0.5.7", "url-loader": "^0.5.7",
"webpack": "^1.14.0", "webpack": "^1.14.0",
"webpack-parallel-uglify-plugin": "^0.2.0" "webpack-parallel-uglify-plugin": "^0.2.0"
},
"standard": {
"ignore": [
"lib/ot",
"public/vendor"
]
} }
} }