diff options
author | Cheng-Han, Wu | 2016-05-15 11:15:02 +0800 |
---|---|---|
committer | Cheng-Han, Wu | 2016-05-15 11:15:02 +0800 |
commit | b956ada24bca26bc39769e20f45f4ecdebf42398 (patch) | |
tree | f359a76b86dbeef0ca39577e18d75806c2d05316 | |
parent | 22f7c4bdcb97ab9ce41fea61975783a3cf0fdb97 (diff) | |
parent | 8a01b7242c24f1bdfbf6361a5c6b63e8123f904a (diff) |
Merge branch 'jccrofty30-gitlab_snippets'
Diffstat (limited to '')
-rw-r--r-- | app.js | 27 | ||||
-rw-r--r-- | lib/auth.js | 18 | ||||
-rw-r--r-- | lib/models/user.js | 5 | ||||
-rw-r--r-- | lib/response.js | 4 | ||||
-rw-r--r-- | public/css/index.css | 5 | ||||
-rw-r--r-- | public/js/index.js | 196 | ||||
-rw-r--r-- | public/views/body.ejs | 83 | ||||
-rw-r--r-- | public/views/header.ejs | 16 |
8 files changed, 348 insertions, 6 deletions
@@ -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 d495605a..ec45eea3 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 b7ef1295..2323dc92 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 99cd080a..bbc08e80 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 fdfae0ac..aaf84a04 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 6bb50c68..0193b6bc 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('<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>'); + 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; + } + $('<option>').val(project.id).text(project.path_with_namespace).appendTo("#snippetExportModalProjects"); + }); + $("#snippetExportModalProjects").prop('disabled',false); + } + $("#snippetExportModalLoading").hide(); + }) + .error(function (data) { + showMessageModal('<i class="fa fa-gitlab"></i> 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('<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>'); + 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; + } + $('<option>').val(project.id).text(project.path_with_namespace).appendTo("#snippetImportModalProjects"); + }); + $("#snippetImportModalProjects").prop('disabled',false); + } + $("#snippetImportModalLoading").hide(); + }) + .error(function (data) { + showMessageModal('<i class="fa fa-gitlab"></i> 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('<option value="init" selected="selected" disabled="disabled">Select From Available Snippets</option>'); + data.forEach(function(snippet) { + $('<option>').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('<i class="fa fa-github"></i> 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('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false); + }) + .complete(function () { + ui.spinner.hide(); + }); + }) + .error(function (data) { + showMessageModal('<i class="fa fa-gitlab"></i> 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('<i class="fa fa-gitlab"></i> 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 11bccfef..5e183dc3 100644 --- a/public/views/body.ejs +++ b/public/views/body.ejs @@ -151,5 +151,88 @@ </div> </div> </div> +<!-- snippet import modal --> +<div class="modal fade" id="snippetImportModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> + </button> + <h4 class="modal-title" id="myModalLabel">Import from Snippet</h4> + </div> + <div class="modal-body"> + <input type="hidden" id="snippetImportModalAccessToken" /> + <input type="hidden" id="snippetImportModalBaseURL" /> + <div class="ui-field-contain" style="display:table;margin-bottom:10px;width:100%;"> + <div style="display:table-row;margin-bottom:5px;"> + <label style="display:table-cell;">Project:</label> + <select class="form-control" id="snippetImportModalProjects" style="display:table-cell;" disabled="disabled"> + <option value="init" selected="selected" disabled="disabled">Select From Available Projects</option> + </select> + </div> + <div style="display:table-row;"> + <label style="display:table-cell;">Snippet</label> + <select class="form-control" id="snippetImportModalSnippets" style="display:table-cell;" disabled="disabled"> + <option value="init" selected="selected" disabled="disabled">Select From Available Snippets</option> + </select> + </div> + </div> + <p class="snippet-import-or">OR</p> + <input type="url" class="form-control" placeholder="/projects/:id/snippets/:snippet_id" id="snippetImportModalContent" disabled="disabled"> + </div> + <div class="modal-footer"> + <span id="snippetImportModalLoading"><i class="fa fa-refresh fa-spin"></i></span> + <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> + <button type="button" class="btn btn-danger" id="snippetImportModalClear">Clear</button> + <button type="button" class="btn btn-primary" id="snippetImportModalConfirm" disabled="disabled">Import</button> + </div> + </div> + </div> +</div> +<!-- snippet export modal --> +<div class="modal fade" id="snippetExportModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="modal-header"> + <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span> + </button> + <h4 class="modal-title" id="myModalLabel">Export to Snippet</h4> + </div> + <div class="modal-body"> + <input type="hidden" id="snippetExportModalAccessToken" /> + <input type="hidden" id="snippetExportModalBaseURL" /> + <div class="ui-field-contain" style="display:table;margin-bottom:10px;width:100%;"> + <div style="display:table-row;margin-bottom:5px;"> + <label style="display:table-cell;">Title:</label> + <input class="form-control" placeholder="new snippet" type="text" id="snippetExportModalTitle" /> + </div> + <div style="display:table-row;margin-bottom:5px;"> + <label style="display:table-cell;">File Name:</label> + <input class="form-control" placeholder="new_snippet.md" type="text" id="snippetExportModalFileName" /> + </div> + <div style="display:table-row;margin-bottom:5px;"> + <label style="display:table-cell;">Project:</label> + <select class="form-control" id="snippetExportModalProjects" style="display:table-cell;"> + <option value="init" selected="selected" disabled="disabled">Select From Available Projects</option> + </select> + </div> + <div style="display:table-row;margin-bottom:5px;"> + <label style="display:table-cell;">Visibility:</label> + <select class="form-control" id="snippetExportModalVisibility" style="display:table-cell;"> + <option value="" selected="selected" disabled="disabled">Select Visibility Level</option> + <option value="0">Private</option> + <option value="10">Internal</option> + </select> + </div> + </div> + </div> + <div class="modal-footer"> + <span id="snippetExportModalLoading"><i class="fa fa-refresh fa-spin"></i></span> + <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> + <button type="button" class="btn btn-primary" id="snippetExportModalConfirm">Export</button> + </div> + </div> + </div> +</div> <%- 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 f45d6311..8610b861 100644 --- a/public/views/header.ejs +++ b/public/views/header.ejs @@ -42,6 +42,10 @@ <li role="presentation"><a role="menuitem" class="ui-save-gist" tabindex="-1" href="#" target="_blank"><i class="fa fa-github fa-fw"></i> Gist</a> </li> <% } %> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + <li role="presentation"><a role="menuitem" class="ui-save-snippet" href="#" data-toggle="modal" data-target="#snippetExportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> + </li> + <% } %> <li class="divider"></li> <li class="dropdown-header">Import</li> <li role="presentation"><a role="menuitem" class="ui-import-dropbox" tabindex="-1" href="#" target="_self"><i class="fa fa-dropbox fa-fw"></i> Dropbox</a> @@ -50,6 +54,10 @@ </li> <li role="presentation"><a role="menuitem" class="ui-import-gist" href="#" data-toggle="modal" data-target="#gistImportModal"><i class="fa fa-github fa-fw"></i> Gist</a> </li> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + <li role="presentation"><a role="menuitem" class="ui-import-snippet" href="#" data-toggle="modal" data-target="#snippetImportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> + </li> + <% } %> <li role="presentation"><a role="menuitem" class="ui-import-clipboard" href="#" data-toggle="modal" data-target="#clipboardModal"><i class="fa fa-clipboard fa-fw"></i> Clipboard</a> </li> <li class="divider"></li> @@ -127,6 +135,10 @@ <li role="presentation"><a role="menuitem" class="ui-save-gist" tabindex="-1" href="#" target="_blank"><i class="fa fa-github fa-fw"></i> Gist</a> </li> <% } %> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + <li role="presentation"><a role="menuitem" class="ui-save-snippet" href="#" data-toggle="modal" data-target="#snippetExportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> + </li> + <% } %> <li class="divider"></li> <li class="dropdown-header">Import</li> <li role="presentation"><a role="menuitem" class="ui-import-dropbox" tabindex="-1" href="#" target="_self"><i class="fa fa-dropbox fa-fw"></i> Dropbox</a> @@ -135,6 +147,10 @@ </li> <li role="presentation"><a role="menuitem" class="ui-import-gist" href="#" data-toggle="modal" data-target="#gistImportModal"><i class="fa fa-github fa-fw"></i> Gist</a> </li> + <% if(typeof gitlab !== 'undefined' && gitlab) { %> + <li role="presentation"><a role="menuitem" class="ui-import-snippet" href="#" data-toggle="modal" data-target="#snippetImportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> + </li> + <% } %> <li role="presentation"><a role="menuitem" class="ui-import-clipboard" href="#" data-toggle="modal" data-target="#clipboardModal"><i class="fa fa-clipboard fa-fw"></i> Clipboard</a> </li> <li class="divider"></li> |