Skip to content

Commit

Permalink
Merge pull request #226 from Martii/allowPrefixedMetadataKeys
Browse files Browse the repository at this point in the history
Add basic support for key prefixing

Merging for sizzle... he can test and deploy but we're lagging behind a bit here.
  • Loading branch information
Marti Martz committed Jun 29, 2014
2 parents 7afb77f + c9e5e0e commit 7bee4c4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
90 changes: 69 additions & 21 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,39 @@ exports.sendMeta = function (req, res, next) {
Script.findOne({ installName: caseInsensitive(installName) },
function (err, script) {
var meta = null;
var name = null;
var data = null;
var prefix = null;
var key = null;
var whitespace = ' ';

if (!script) { return next(); }

res.set('Content-Type', 'text/javascript; charset=UTF-8');
meta = script.meta;

res.write('// ==UserScript==\n');
Object.keys(meta).reverse().forEach(function (key) {
if (meta[key] instanceof Array) {
meta[key].forEach(function (value) {
res.write('// @' + key + ' ' + value + '\n');
Object.keys(meta).reverse().forEach(function (name) {
if (meta[name] instanceof Array) {
meta[name].forEach(function (value) {
res.write('// @' + name + (value ? whitespace + value : '') + '\n');
});
} else if (meta[name] instanceof Object) {
prefix = name;
for (key in meta[name]) {
data = meta[prefix][key];
if (data instanceof Array) {
data.forEach(function (value) {
res.write('// @' + prefix + ':' + key + (value ? whitespace + value : '') + '\n');
});
}
else {
res.write('// @' + prefix + ':' + key + (data ? whitespace + data : '') + '\n');
}
}
} else {
res.write('// @' + key + ' ' + meta[key] + '\n');
data = meta[name];
res.write('// @' + name + (data ? whitespace + data : '') + '\n');
}
});
res.end('// ==/UserScript==\n');
Expand All @@ -157,18 +176,22 @@ function parseMeta(aString) {
var re = /\/\/ @(\S+)(?:\s+(.*))?/;
var headers = {};
var name = null;
var prefix = null;
var key = null;
var value = null;
var line = null;
var lineMatches = null;
var lines = {};
var unique = {
var uniques = {
'name': true,
'namespace': true,
'description': true,
'version': true,
'author': true
'oujs:author': true
};
var unique = null;
var one = null;
var matches = null;

lines = aString.split(/[\r\n]+/).filter(function (e, i, a) {
return (e.match(re));
Expand All @@ -187,16 +210,41 @@ function parseMeta(aString) {
name = 'homepageURL';
break;
}
if (!headers[name] || unique[name]) {
headers[name] = value || '';
} else {
if (!(headers[name] instanceof Array)) {
headers[name] = [headers[name]];
name = name.split(/:/).reverse();
key = name[0];
prefix = name[1];
if (key) {
if (prefix) {
if (!headers[prefix]) {
headers[prefix] = {};
}
header = headers[prefix];
unique = {};
for (one in uniques) {
matches = one.match(/(.*):(.*)$/);
if (uniques[one] && matches && matches[1] === prefix) {
unique[matches[2]] = true;
}
}
} else {
header = headers;
unique = {};
for (one in uniques) {
if (uniques[one] && !/:/.test(one)) {
unique[one] = true;
}
}
}
if (!header[key] || unique[key]) {
header[key] = value || '';
} else {
if (!(header[key] instanceof Array)) {
header[key] = [header[key]];
}
header[key].push(value || '');
}
headers[name].push(value || '');
}
}

return headers;
}
exports.parseMeta = parseMeta;
Expand Down Expand Up @@ -242,14 +290,14 @@ exports.storeScript = function (user, meta, buf, callback, update) {
// Can't install a script without a @name (maybe replace with random value)
if (!scriptName) { return callback(null); }

if (!isLibrary && meta.author
&& meta.author != user.name && meta.collaborator) {
collaborators = meta.collaborator;
if (!isLibrary && meta.oujs && meta.oujs.author
&& meta.oujs.author != user.name && meta.oujs.collaborator) {
collaborators = meta.oujs.collaborator;
if ((typeof collaborators === 'string'
&& collaborators === user.name)
|| (collaborators instanceof Array
&& collaborators.indexOf(user.name) > -1)) {
installName = meta.author + '/';
installName = meta.oujs.author + '/';
} else {
collaborators = null;
}
Expand Down Expand Up @@ -300,9 +348,9 @@ exports.storeScript = function (user, meta, buf, callback, update) {
});
} else {
if (!script.isLib) {
if (collaborators && (script.meta.author != meta.author
|| JSON.stringify(script.meta.collaborator) !=
JSON.stringify(meta.collaborator))) {
if (collaborators && (script.meta.oujs && script.meta.oujs.author != meta.oujs.author
|| (script.meta.oujs && JSON.stringify(script.meta.oujs.collaborator) !=
JSON.stringify(meta.oujs.collaborator)))) {
return callback(null);
}
script.meta = meta;
Expand Down
8 changes: 4 additions & 4 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1362,11 +1362,11 @@ function getExistingScript(req, options, authedUser, callback) {

if (!script) { return callback(null); }

if (script.meta.collaborator) {
if (typeof script.meta.collaborator === 'string') {
collaborators.push(script.meta.collaborator);
if (script.meta.oujs && script.meta.oujs.collaborator) {

This comment has been minimized.

Copy link
@Martii

Martii Jun 29, 2014

Member

Present here #229

if (typeof script.meta.oujs.collaborator === 'string') {
collaborators.push(script.meta.oujs.collaborator);
} else {
collaborators = script.meta.collaborator;
collaborators = script.meta.oujs.collaborator;
}
}

Expand Down
8 changes: 4 additions & 4 deletions views/pages/newScriptPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ <h4 class="list-group-item-heading"><code>@supportURL url</code></h4>
<p class="list-group-item-text">A url of http, https or mailto protocol. Specially formatted on the script page. Last value shown.</p>
</div>
<div class="list-group-item inactive">
<h4 class="list-group-item-heading"><code>@author {{#authedUser}}{{authedUser.name}}{{/authedUser}}{{^authedUser}}username{{/authedUser}}</code></h4>
<p class="list-group-item-text">The author of the script. Required for <a href="#collaboration">Collaboration</a>. Only the author may edit. Last value shown. This key is subject to change.</p>
<h4 class="list-group-item-heading"><code>@oujs:author {{#authedUser}}{{authedUser.name}}{{/authedUser}}{{^authedUser}}username{{/authedUser}}</code></h4>
<p class="list-group-item-text">The author of the script. Required to enable <a href="#collaboration">Collaboration</a>. Last value shown.</p>
</div>
<div class="list-group-item inactive">
<h4 class="list-group-item-heading"><code>@collaborator username</code></h4>
<h4 class="list-group-item-heading"><code>@oujs:collaborator username</code></h4>
<p class="list-group-item-text">Can be defined multiple times. Required for <a href="#collaboration">Collaboration</a>. All values shown in reverse order. This key is subject to change.</p>
</div>
</div>
Expand All @@ -63,7 +63,7 @@ <h4 class="list-group-item-heading"><code>@collaborator username</code></h4>
<h3>Collaboration</h3>
<div class="panel panel-default">
<div class="panel-body">
<p>To allow other users to upload modified versions of your script to your account, add <code>@author Zren</code> to the metadata of your script, along with <code>@collaborator username</code> for every user you wish to give write access. Only the real author of the script can modify these metadata keys. Collaborators cannot use GitHub integration to update the script. Add them as collaborators to the GitHub repo instead.</p>
<p>To allow other users to upload modified versions of your script to your account, add <code>@oujs:author Zren</code> to the metadata of your script, along with <code>@oujs:collaborator username</code> for every user you wish to give write access. Only the real author of the script can modify these metadata keys. Collaborators cannot use GitHub integration to update the script. Add them as collaborators to the GitHub repo instead.</p>
</div>
</div>
{{/newUserJS}}
Expand Down

8 comments on commit 7bee4c4

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

@sizzlemctwizzle
Retested everything affected by these 3 prs and appears to be okay to go for your final testing and deploy. TIA

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

@sizzlemctwizzle
Did this get deployed? ... everything else from the other 2 prs seems to be okay on pro... e.g. I tried pasting a new script in... legacy keys are still being used... tried uploading a script... legacy keys are still being used. Test pro script

@sizzlemctwizzle
Copy link
Member

Choose a reason for hiding this comment

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

Yeah it got deployed. On this page I see @oujs:author sizzle so it definitely got deployed.

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

Well now it's broken on dev master too (already deleted that branch too grr)... I tested/coded this for several hours right before sleep and it was working earlier today... so it shouldn't have broken... possible merge conflict foobar... but I don't think that was one of them.

@sizzlemctwizzle
Copy link
Member

Choose a reason for hiding this comment

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

Confirmed. It doesn't work. I should have tested myself before deploying, but I wanted to get a bugfix in quickly so I just pulled master.

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

oujs - Meta View is showing that it's not filtering those anymore on https://openuserjs.org/scripts/Marti/RFC_2606%C2%A73_-_license_and_licence_%28recovered%29_Unit_Test ... so that portion appears to be working. e.g. they wouldn't show up with the prefixes if the parseMeta routine was flawed...

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

Think I've found the culprit... part of a commit disappeared somehow

@Martii
Copy link
Member

@Martii Martii commented on 7bee4c4 Jun 29, 2014

Choose a reason for hiding this comment

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

Should be fixered in #229

Please sign in to comment.