Merge branch 'gitlab_snippets' of https://github.com/jccrofty30/hackmd into jccrofty30-gitlab_snippets

# Conflicts:
#	lib/response.js
This commit is contained in:
Cheng-Han, Wu 2016-05-15 11:02:15 +08:00
commit 8a01b7242c
8 changed files with 348 additions and 6 deletions

27
app.js
View file

@ -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

View file

@ -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);

View file

@ -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;
};

View file

@ -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, {

View file

@ -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;

View file

@ -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)

View file

@ -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">&times;</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">&times;</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 %>

View file

@ -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>