Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned bin urls #554

Merged
merged 5 commits into from
Apr 6, 2013
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/db/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,5 +392,18 @@ module.exports = utils.inherit(Object, {
}
fn(null);
});
},
isOwnerOf: function (params, fn) {
var values = [
params.name,
params.url
], sql = templates.isOwnerOf;

this.connection.query(sql, values, function (err, result) {
if (err) {
return fn(err);
}
fn(null, { found: !!result.length, isowner: result.length ? result[0].owner === 1 : false });
});
}
});
3 changes: 2 additions & 1 deletion lib/db/sql_templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"getUserForForgotToken": "SELECT `ownership`.*, expires FROM `ownership` INNER JOIN `forgot_tokens` ON `name` = `owner_name` WHERE `token` = ? AND `forgot_tokens`.`expires` >= ?",
"setForgotToken": "INSERT INTO `forgot_tokens` (`owner_name`, `token`, `expires`, `created`) VALUES (?, ?, ?, ?)",
"deleteExpiredForgotToken": "DELETE FROM `forgot_tokens` WHERE `expires` <= ? OR `token`=? OR `owner_name`=?",
"reportBin": "UPDATE `sandbox` SET `reported`=? WHERE `url`=? AND `revision`=? AND `active`='y'"
"reportBin": "UPDATE `sandbox` SET `reported`=? WHERE `url`=? AND `revision`=? AND `active`='y'",
"isOwnerOf": "SELECT name=? as `owner` FROM `owners` WHERE `url`=? AND `revision`=1"
}
20 changes: 19 additions & 1 deletion lib/db/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ module.exports = utils.inherit(Object, {
if (result) {
result = _this.convertUserDates(result);
}
fn(null, result);
fn(null, result);
}
});
},
Expand Down Expand Up @@ -450,5 +450,23 @@ module.exports = utils.inherit(Object, {
}
fn(null, results);
});
},
isOwnerOf: function (params, fn) {
var values = [
params.name,
params.url
], sql = templates.isOwnerOf;

this.connection.run(sql, values, function (err, result) {
if (err) {
return fn(err);
}
if (typeof result === 'undefined') {
return fn(null, { found: false });
} else {
return fn(null, { found: true, isowner: result[0].owner === 1 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs the same safety checking as the MySQL version? (isowner: result.length ? result[0].owner === 1 : false)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, IIRC if the length is zero, then the result is undefined, so if we're in that line, there will be an array of results. Not terribly clear though - since you'd expect it to be the same as the mysql version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(that said, a test wouldn't be amiss)

}
});
}

});
24 changes: 19 additions & 5 deletions lib/handlers/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,34 @@ module.exports = Observable.extend({
if (req.param('method') === 'save') {
params = utils.extract(req.body, 'html', 'css', 'javascript', 'settings');
params.url = req.bin.url;
params.revision = req.bin.revision + 1;
params.revision = parseInt(req.params.rev, 10) || 1, //req.bin.revision;
params.summary = utils.titleForBin(params);

this.validateBin(params, function (err) {
if (err) {
return next(err);
}

_this.models.bin.createRevision(params, function (err, result) {
if (err) {
return next(err);
var username = req.session.user ? req.session.user.name : undefined;

_this.models.user.isOwnerOf(username, params, function (err, result) {
console.log('isOwnerOf', username, params, result);
var method = 'create';

if (result.isowner || result.found === false) {
method = 'createRevision';
params.revision = req.bin.revision + 1; // bump the revision from the *latest*
} else {
delete params.revision;
}

_this.completeCreateBin(result, req, res, next);
_this.models.bin[method](params, function (err, result) {
if (err) {
return next(err);
}

_this.completeCreateBin(result, req, res, next);
});
});
});
} else if (req.param('method') === 'update') {
Expand Down
8 changes: 8 additions & 0 deletions lib/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ module.exports = Observable.extend({
};
this.store.getBinsByUser(id, fn);
},
isOwnerOf: function (username, bin, fn) {
var params = {
name: username,
url: bin.url,
revision: bin.revision
};
this.store.isOwnerOf(params, fn);
},
setBinOwner: function (id, bin, fn) {
var params = {
name: id,
Expand Down
2 changes: 1 addition & 1 deletion lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var methods = [
'reportBin',
'getAllOwners',
'getOwnersBlock',

'isOwnerOf',

'populateOwners'
];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"name": "jsbin",
"description": "Collaborative JavaScript Debugging App",
"main": "./lib/app",
"version": "3.2.24",
"version": "3.2.25",
"preferGlobal": "true",
"homepage": "http://jsbin.com",
"bin": "./bin/jsbin",
Expand Down
2 changes: 1 addition & 1 deletion public/js/chrome/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// if a gist has been requested, lazy load the gist library and plug it in
if (/gist\/\d+/.test(window.location.pathname) && (!sessionStorage.getItem('javascript') && !sessionStorage.getItem('html'))) {
if (/gist(\/.*)?\/\d+/.test(window.location.pathname) && (!sessionStorage.getItem('javascript') && !sessionStorage.getItem('html'))) {
window.editors = editors; // needs to be global when the callback triggers to set the content
loadGist = function () {
$.getScript('/js/chrome/gist.js', function () {
Expand Down
4 changes: 2 additions & 2 deletions public/js/editors/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var editorModes = {
processing: 'text/x-csrc'
};

var badChars = new RegExp('\u200B', 'g');
var badChars = new RegExp('[\u200B\u0080-\u00a0]', 'g');

if (jsbin.settings.editor.tabMode === 'default') {
CodeMirror.keyMap.basic.Tab = undefined;
Expand Down Expand Up @@ -369,7 +369,7 @@ Panel.prototype = {
if (content === undefined) content = '';
this.controlButton.toggleClass('hasContent', !!content.trim().length);
this.codeSet = true;
this.editor.setCode(content);
this.editor.setCode(content.replace(badChars, ''));
}
},
codeSet: false,
Expand Down