diff --git a/app.js b/app.js index db623a5..35408bb 100644 --- a/app.js +++ b/app.js @@ -16,6 +16,7 @@ var formidable = require('formidable'); var morgan = require('morgan'); var passportSocketIo = require("passport.socketio"); var helmet = require('helmet'); +var request = require('request'); //core var config = require("./lib/config.js"); @@ -82,6 +83,9 @@ var sessionStore = new SequelizeStore({ //compression app.use(compression()); +//cookies +app.use(cookieParser()); + // use hsts to tell https users stick to this app.use(helmet.hsts({ maxAge: 31536000 * 1000, // 365 days @@ -438,6 +442,29 @@ app.post('/uploadimage', function (req, res) { } }); }); +//get gitlab parameters +app.get('/gitlab', function (req, res) { + var ret = { baseURL: config.gitlab.baseURL }; + models.User.findById(req.cookies.userid) + .then(function(user) { + ret.accesstoken = user.accessToken; + ret.profileid = user.profileid; + request( + config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken, + function(error, httpResponse, body) { + if (!error && httpResponse.statusCode == 200) { + ret.projects = JSON.parse(body); + return res.send(ret); + } else { + return res.send(ret); + } + } + ); + }).catch(function(err) { + logger.error('user search failed: ' + err); + return response.errorInternalError(res); + }); +}); //get new note app.get("/new", response.newNote); //get publish note diff --git a/lib/auth.js b/lib/auth.js index d495605..ec45eea 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -19,13 +19,23 @@ function callback(accessToken, refreshToken, profile, done) { profileid: profile.id.toString() }, defaults: { - profile: JSON.stringify(profile) + profile: JSON.stringify(profile), + accessToken: accessToken, + refreshToken: refreshToken } }).spread(function(user, created) { if (user) { - if (config.debug) - logger.info('user login: ' + user.id); - return done(null, user); + if(user.accessToken == accessToken){ + if (config.debug) + logger.info('user login: ' + user.id); + return done(null, user); + } + user.accessToken = accessToken; + user.save().then(function(){ + if (config.debug) + logger.info('user login: ' + user.id); + return done(null, user); + }) } }).catch(function(err) { logger.error('auth callback failed: ' + err); diff --git a/lib/models/user.js b/lib/models/user.js index b7ef129..2323dc9 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -23,6 +23,9 @@ module.exports = function (sequelize, DataTypes) { }, history: { type: DataTypes.TEXT + }, + accessToken: { + type: DataTypes.STRING } }, { classMethods: { @@ -75,6 +78,6 @@ module.exports = function (sequelize, DataTypes) { } } }); - + return User; }; \ No newline at end of file diff --git a/lib/response.js b/lib/response.js index 99cd080..bbc08e8 100644 --- a/lib/response.js +++ b/lib/response.js @@ -126,7 +126,11 @@ function responseHackMD(res, note) { twitter: config.twitter, github: config.github, gitlab: config.gitlab, +<<<<<<< HEAD dropbox: config.dropbox +======= + dropbox: config.dropbox, +>>>>>>> 930afdc33738a487bd9e596c5d35bc9f686eaaa1 }); var buf = html; res.writeHead(200, { diff --git a/public/css/index.css b/public/css/index.css index fdfae0a..aaf84a0 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -328,6 +328,11 @@ div[contenteditable]:empty:not(:focus):before{ border-bottom: 1px solid #ccc; } +.snippet-import-or { + text-align: center; + width: 100%; +} + .status-bar { background: #1c1c1e; border-top: 1px solid #343434; diff --git a/public/js/index.js b/public/js/index.js index 6bb50c6..0193b6b 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -243,6 +243,16 @@ var lastInfo = { }; var personalInfo = {}; var onlineUsers = []; +var fileTypes = { + "pl": "perl", + "cgi": "perl", + "js": "javascript", + "php": "php", + "sh": "bash", + "rb": "ruby", + "html": "html", + "py": "python" +}; //editor settings var textit = document.getElementById("textit"); @@ -496,12 +506,14 @@ var ui = { export: { dropbox: $(".ui-save-dropbox"), googleDrive: $(".ui-save-google-drive"), - gist: $(".ui-save-gist") + gist: $(".ui-save-gist"), + snippet: $(".ui-save-snippet") }, import: { dropbox: $(".ui-import-dropbox"), googleDrive: $(".ui-import-google-drive"), gist: $(".ui-import-gist"), + snippet: $(".ui-import-snippet"), clipboard: $(".ui-import-clipboard") }, beta: { @@ -541,6 +553,10 @@ var ui = { codemirrorSizer: $(".ui-edit-area .CodeMirror .CodeMirror-sizer"), codemirrorSizerInner: $(".ui-edit-area .CodeMirror .CodeMirror-sizer > div"), markdown: $(".ui-view-area .markdown-body") + }, + modal: { + snippetImportProjects: $("#snippetImportModalProjects"), + snippetImportSnippets: $("#snippetImportModalSnippets") } }; @@ -1163,6 +1179,40 @@ ui.toolbar.export.googleDrive.click(function (e) { }); //export to gist ui.toolbar.export.gist.attr("href", noteurl + "/gist"); +//export to snippet +ui.toolbar.export.snippet.click(function() { + $.get(serverurl + '/gitlab') + .success(function (data) { + $("#snippetExportModalAccessToken").val(data.accesstoken); + $("#snippetExportModalBaseURL").val(data.baseURL); + $("#snippetExportModalLoading").hide(); + $("#snippetExportModal").modal('toggle'); + $("#snippetExportModalProjects").find('option').remove().end().append(''); + if (data.projects) { + data.projects.sort(function(a,b) { + return (a.path_with_namespace < b.path_with_namespace) ? -1 : ((a.path_with_namespace > b.path_with_namespace) ? 1 : 0); + }); + data.projects.forEach(function(project) { + if (!project.snippets_enabled + || (project.permissions.project_access === null && project.permissions.group_access === null) + || (project.permissions.project_access !== null && project.permissions.project_access.access_level < 20)) + { + return; + } + $(''); + if (data.projects) { + data.projects.sort(function(a,b) { + return (a.path_with_namespace < b.path_with_namespace) ? -1 : ((a.path_with_namespace > b.path_with_namespace) ? 1 : 0); + }); + data.projects.forEach(function(project) { + if (!project.snippets_enabled + || (project.permissions.project_access === null && project.permissions.group_access === null) + || (project.permissions.project_access !== null && project.permissions.project_access.access_level < 20)) + { + return; + } + $(''); + data.forEach(function(snippet) { + $('