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('Select From Available Projects'); + 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; + } + $('').val(project.id).text(project.path_with_namespace).appendTo("#snippetExportModalProjects"); + }); + $("#snippetExportModalProjects").prop('disabled',false); + } + $("#snippetExportModalLoading").hide(); + }) + .error(function (data) { + showMessageModal(' Import from Snippet', 'Unable to fetch gitlab parameters :(', '', '', false); + }) + .complete(function () { + //na + }); + return false; +}); //import from dropbox ui.toolbar.import.dropbox.click(function () { var options = { @@ -1215,6 +1265,42 @@ function buildImportFromGoogleDrive() { ui.toolbar.import.gist.click(function () { //na }); +//import from snippet +ui.toolbar.import.snippet.click(function () { + $.get(serverurl + '/gitlab') + .success(function (data) { + $("#snippetImportModalAccessToken").val(data.accesstoken); + $("#snippetImportModalBaseURL").val(data.baseURL); + $("#snippetImportModalContent").prop('disabled', false); + $("#snippetImportModalConfirm").prop('disabled', false); + $("#snippetImportModalLoading").hide(); + $("#snippetImportModal").modal('toggle'); + $("#snippetImportModalProjects").find('option').remove().end().append('Select From Available Projects'); + 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; + } + $('').val(project.id).text(project.path_with_namespace).appendTo("#snippetImportModalProjects"); + }); + $("#snippetImportModalProjects").prop('disabled',false); + } + $("#snippetImportModalLoading").hide(); + }) + .error(function (data) { + showMessageModal(' Import from Snippet', 'Unable to fetch gitlab parameters :(', '', '', false); + }) + .complete(function () { + //na + }); + return false; +}); //import from clipboard ui.toolbar.import.clipboard.click(function () { //na @@ -1236,6 +1322,39 @@ ui.toolbar.beta.pdf.attr("download", "").attr("href", noteurl + "/pdf"); //slide ui.toolbar.beta.slide.attr("href", noteurl + "/slide"); +//modal actions +//snippet projects +ui.modal.snippetImportProjects.change(function() { + var accesstoken = $("#snippetImportModalAccessToken").val(), + baseURL = $("#snippetImportModalBaseURL").val(), + project = $("#snippetImportModalProjects").val(); + + $("#snippetImportModalLoading").show(); + $("#snippetImportModalContent").val('/projects/' + project); + $.get(baseURL + '/api/v3/projects/' + project + '/snippets?access_token=' + accesstoken) + .success(function(data) { + $("#snippetImportModalSnippets").find('option').remove().end().append('Select From Available Snippets'); + data.forEach(function(snippet) { + $('').val(snippet.id).text(snippet.title).appendTo($("#snippetImportModalSnippets")); + }); + $("#snippetImportModalLoading").hide(); + $("#snippetImportModalSnippets").prop('disabled',false); + }) + .error(function(err) { + + }) + .complete(function() { + //na + }); +}); +//snippet snippets +ui.modal.snippetImportSnippets.change(function() { + var project = $("#snippetImportModalProjects").val(), + snippet = $("#snippetImportModalSnippets").val(); + + $("#snippetImportModalContent").val($("#snippetImportModalContent").val() + '/snippets/' + snippet); +}) + function scrollToTop() { if (currentMode == modeType.both) { if (editor.getScrollInfo().top != 0) @@ -1382,6 +1501,81 @@ $("#gistImportModalConfirm").click(function () { } }); +// snippet import modal +$("#snippetImportModalClear").click(function () { + $("#snippetImportModalContent").val(''); + $("#snippetImportModalProjects").val('init'); + $("#snippetImportModalSnippets").val('init'); + $("#snippetImportModalSnippets").prop('disabled',true); +}); +$("#snippetImportModalConfirm").click(function () { + var snippeturl = $("#snippetImportModalContent").val(); + if (!snippeturl) return; + $('#snippetImportModal').modal('hide'); + $("#snippetImportModalContent").val(''); + if (!/^.+\/snippets\/.+$/.test(snippeturl)) { + showMessageModal(' Import from Snippet', 'Not a valid Snippet URL :(', '', '', false); + } else { + ui.spinner.show(); + var accessToken = '?access_token=' + $("#snippetImportModalAccessToken").val(); + var fullURL = $("#snippetImportModalBaseURL").val() + '/api/v3' + snippeturl; + $.get(fullURL + accessToken) + .success(function(data) { + var content = '# ' + (data.title || "Snippet Import"); + var fileInfo = data.file_name.split('.'); + fileInfo[1] = (fileInfo[1]) ? fileInfo[1] : "md"; + $.get(fullURL + '/raw' + accessToken) + .success(function (raw) { + if (raw) { + content += "\n\n"; + if (fileInfo[1] != "md") { + content += "```" + fileTypes[fileInfo[1]] + "\n"; + } + content += raw; + if (fileInfo[1] != "md") { + content += "\n```"; + } + replaceAll(content); + } + }) + .error(function (data) { + showMessageModal(' Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false); + }) + .complete(function () { + ui.spinner.hide(); + }); + }) + .error(function (data) { + showMessageModal(' Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false); + }); + } +}); + +//snippet export modal +$("#snippetExportModalConfirm").click(function() { + var accesstoken = $("#snippetExportModalAccessToken").val(), + baseURL = $("#snippetExportModalBaseURL").val(), + data = { + title: $("#snippetExportModalTitle").val(), + file_name: $("#snippetExportModalFileName").val(), + code: editor.getValue(), + visibility_level: $("#snippetExportModalVisibility").val() + }; + + $("#snippetExportModalLoading").show(); + var fullURL = baseURL + '/api/v3/projects/' + $("#snippetExportModalProjects").val() + '/snippets?access_token=' + accesstoken; + $.post(fullURL + , data + , function(ret) { + $("#snippetExportModalLoading").hide(); + $('#snippetExportModal').modal('hide'); + var redirect = baseURL + '/' + $("#snippetExportModalProjects option[value='" + $("#snippetExportModalProjects").val() + "']").text() + '/snippets/' + ret.id; + showMessageModal(' Export to Snippet', 'Export Successful!', redirect, 'View Snippet Here', true); + } + , 'json' + ); +}); + function parseToEditor(data) { var parsed = toMarkdown(data); if (parsed) diff --git a/public/views/body.ejs b/public/views/body.ejs index 11bccfe..5e183dc 100644 --- a/public/views/body.ejs +++ b/public/views/body.ejs @@ -151,5 +151,88 @@ + + + + + + × + + Import from Snippet + + + + + + + Project: + + Select From Available Projects + + + + Snippet + + Select From Available Snippets + + + + OR + + + + + + + + + + + + × + + Export to Snippet + + + + + + + Title: + + + + File Name: + + + + Project: + + Select From Available Projects + + + + Visibility: + + Select Visibility Level + Private + Internal + + + + + + + + <%- include signin-modal %> <%- include help-modal %> \ No newline at end of file diff --git a/public/views/header.ejs b/public/views/header.ejs index f45d631..8610b86 100644 --- a/public/views/header.ejs +++ b/public/views/header.ejs @@ -42,6 +42,10 @@ Gist <% } %> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + Snippet + + <% } %> Import Dropbox @@ -50,6 +54,10 @@ Gist + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + Snippet + + <% } %> Clipboard @@ -127,6 +135,10 @@ Gist <% } %> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + Snippet + + <% } %> Import Dropbox @@ -135,6 +147,10 @@ Gist + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + Snippet + + <% } %> Clipboard
OR