Skip to content

Commit 0d06785

Browse files
committed
Merge pull request OpenUserJS#901 from Martii/Issue-819adaptiveEncodingUrlsAndStaticUris
Fix some bugs with encoding Auto-merge with several hours of testing on dev with some local pro
2 parents ec98e84 + 9ff620f commit 0d06785

File tree

12 files changed

+291
-106
lines changed

12 files changed

+291
-106
lines changed

controllers/admin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ exports.adminUserUpdate = function (aReq, aRes, aNext) {
195195

196196
// Make sure the change is reflected in the session store
197197
updateSessions(aReq, aUser, function (aErr, aSess) {
198-
aRes.redirect(user.userPageUrl);
198+
aRes.redirect(user.userPageUri);
199199
});
200200
});
201201
});
@@ -497,6 +497,6 @@ exports.authAsUser = function (aReq, aRes, aNext) {
497497

498498
aReq.session.user = user;
499499

500-
aRes.redirect(encodeURI(user.userPageUrl)); // NOTE: Watchpoint
500+
aRes.redirect(user.userPageUri);
501501
});
502502
};

controllers/auth.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ exports.callback = function (aReq, aRes, aNext) {
172172
var username = aReq.session.username;
173173
var newstrategy = aReq.session.newstrategy;
174174
var strategyInstance = null;
175-
var doneUrl = aReq.session.user ? '/user/preferences' : '/';
175+
var doneUri = aReq.session.user ? '/user/preferences' : '/';
176176

177177
// The callback was called improperly
178178
if (!strategy || !username) {
@@ -231,7 +231,7 @@ exports.callback = function (aReq, aRes, aNext) {
231231
console.error(chalk.red('`User` not found'));
232232
}
233233

234-
aRes.redirect(doneUrl + (doneUrl === '/' ? 'register' : '') + '?authfail');
234+
aRes.redirect(doneUri + (doneUri === '/' ? 'register' : '') + '?authfail');
235235
return;
236236
}
237237

@@ -263,16 +263,16 @@ exports.callback = function (aReq, aRes, aNext) {
263263
addSession(aReq, aUser, function () {
264264
if (newstrategy && newstrategy !== strategy) {
265265
// Allow a user to link to another account
266-
aRes.redirect('/auth/' + newstrategy);
266+
aRes.redirect('/auth/' + newstrategy); // NOTE: Watchpoint... careful with encoding
267267
return;
268268
} else {
269269
// Delete the username that was temporarily stored
270270
delete aReq.session.username;
271271
delete aReq.session.newstrategy;
272-
doneUrl = aReq.session.redirectTo;
272+
doneUri = aReq.session.redirectTo;
273273
delete aReq.session.redirectTo;
274274

275-
aRes.redirect(doneUrl);
275+
aRes.redirect(doneUri);
276276
return;
277277
}
278278
});

controllers/discussion.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,10 @@ exports.createTopic = function (aReq, aRes, aNext) {
525525
return;
526526
}
527527

528-
aRes.redirect(encodeURI(aDiscussion.path
529-
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
528+
aRes.redirect(aDiscussion.path.split('/').map(function (aStr) {
529+
return encodeURIComponent(aStr);
530+
}).join('/')
531+
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : ''));
530532
});
531533
};
532534

@@ -560,8 +562,10 @@ exports.createComment = function (aReq, aRes, aNext) {
560562
}
561563

562564
postComment(authedUser, aDiscussion, content, false, function (aErr, aDiscussion) {
563-
aRes.redirect(encodeURI(aDiscussion.path
564-
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
565+
aRes.redirect(aDiscussion.path.split('/').map(function (aStr) {
566+
return encodeURIComponent(aStr);
567+
}).join('/') +
568+
(aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : ''));
565569
});
566570
});
567571
};

controllers/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,16 @@ exports.register = function (aReq, aRes) {
297297

298298
exports.logout = function (aReq, aRes) {
299299
var authedUser = aReq.session.user;
300-
var redirectUrl = getRedirect(aReq);
300+
var redirectUri = getRedirect(aReq); // NOTE: Watchpoint
301301

302302
if (!authedUser) {
303-
aRes.redirect(redirectUrl);
303+
aRes.redirect(redirectUri);
304304
return;
305305
}
306306

307307
User.findOne({ _id: authedUser._id }, function (aErr, aUser) {
308308
removeSession(aReq, aUser, function () {
309-
aRes.redirect(redirectUrl);
309+
aRes.redirect(redirectUri);
310310
});
311311
});
312312
};

controllers/issue.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,14 @@ exports.open = function (aReq, aRes, aNext) {
380380
discussionLib.postTopic(authedUser, category.slug, topic, content, true,
381381
function (aDiscussion) {
382382
if (!aDiscussion) {
383-
aRes.redirect('/' + encodeURI(category) + '/open');
383+
aRes.redirect('/' + category.slugUri + '/open');
384384
return;
385385
}
386386

387-
aRes.redirect(encodeURI(aDiscussion.path +
388-
(aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
387+
aRes.redirect(aDiscussion.path.split('/').map(function (aStr) {
388+
return encodeURIComponent(aStr);
389+
}).join('/') +
390+
(aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : ''));
389391
}
390392
);
391393
} else {
@@ -439,8 +441,10 @@ exports.comment = function (aReq, aRes, aNext) {
439441

440442
discussionLib.postComment(authedUser, aIssue, content, false,
441443
function (aErr, aDiscussion) {
442-
aRes.redirect(encodeURI(aDiscussion.path
443-
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
444+
aRes.redirect(aDiscussion.path.split('/').map(function (aStr) {
445+
return encodeURIComponent(aStr);
446+
}).join('/') +
447+
(aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : ''));
444448
});
445449
});
446450
});
@@ -487,8 +491,10 @@ exports.changeStatus = function (aReq, aRes, aNext) {
487491

488492
if (changed) {
489493
aIssue.save(function (aErr, aDiscussion) {
490-
aRes.redirect(encodeURI(aDiscussion.path
491-
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
494+
aRes.redirect(aDiscussion.path.split('/').map(function (aStr) {
495+
return encodeURIComponent(aStr);
496+
}).join('/') +
497+
(aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : ''));
492498
});
493499
} else {
494500
aNext();

controllers/script.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,15 @@ exports.edit = function (aReq, aRes, aNext) {
467467
if (aReq.body.remove) {
468468
// POST
469469
scriptStorage.deleteScript(aScript.installName, function () {
470-
aRes.redirect(authedUser.userScriptListPageUrl);
470+
aRes.redirect(authedUser.userScriptListPageUri);
471471
});
472472
} else if (typeof aReq.body.about !== 'undefined') {
473473
// POST
474474
aScript.about = aReq.body.about;
475475
scriptGroups = (aReq.body.groups || '');
476476
scriptGroups = scriptGroups.split(/,/);
477477
addScriptToGroups(aScript, scriptGroups, function () {
478-
aRes.redirect(script.scriptPageUrl);
478+
aRes.redirect(script.scriptPageUri);
479479
});
480480
} else {
481481
// GET
@@ -495,20 +495,20 @@ exports.edit = function (aReq, aRes, aNext) {
495495
// Script voting
496496
exports.vote = function (aReq, aRes, aNext) {
497497
//
498-
var url = aReq._parsedUrl.pathname.split('/');
498+
var uri = aReq._parsedUrl.pathname.split('/');
499499
var vote = aReq.params.vote;
500500
var unvote = false;
501501

502502
var isLib = aReq.params.isLib;
503503
var installNameBase = scriptStorage.getInstallNameBase(aReq);
504504

505505
// ---
506-
if (url.length > 5) {
507-
url.pop();
506+
if (uri.length > 5) {
507+
uri.pop();
508508
}
509-
url.shift();
510-
url.shift();
511-
url = '/' + url.join('/');
509+
uri.shift();
510+
uri.shift();
511+
uri = '/' + uri.join('/');
512512

513513
if (vote === 'up') {
514514
vote = true;
@@ -517,7 +517,7 @@ exports.vote = function (aReq, aRes, aNext) {
517517
} else if (vote === 'unvote') {
518518
unvote = true;
519519
} else {
520-
aRes.redirect(url);
520+
aRes.redirect(uri);
521521
return;
522522
}
523523

@@ -530,7 +530,7 @@ exports.vote = function (aReq, aRes, aNext) {
530530

531531
// ---
532532
if (aErr || !aScript) {
533-
aRes.redirect(url);
533+
aRes.redirect(uri);
534534
return;
535535
}
536536

@@ -543,15 +543,15 @@ exports.vote = function (aReq, aRes, aNext) {
543543
function saveScript() {
544544
if (!flags) {
545545
aScript.save(function (aErr, aScript) {
546-
aRes.redirect(url);
546+
aRes.redirect(uri);
547547
});
548548
return;
549549
}
550550

551551
flagLib.getAuthor(aScript, function (aAuthor) {
552552
flagLib.saveContent(Script, aScript, aAuthor, flags,
553553
function (aFlagged) {
554-
aRes.redirect(url);
554+
aRes.redirect(uri);
555555
});
556556
});
557557
}
@@ -565,7 +565,7 @@ exports.vote = function (aReq, aRes, aNext) {
565565
}
566566

567567
if (authedUser._id == aScript._authorId || (!aVoteModel && unvote)) {
568-
aRes.redirect(url);
568+
aRes.redirect(uri);
569569
return;
570570
} else if (!aVoteModel) {
571571
aVoteModel = new Vote({

controllers/scriptStorage.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var RepoManager = require('../libs/repoManager');
2626

2727
var cleanFilename = require('../libs/helpers').cleanFilename;
2828
var findDeadorAlive = require('../libs/remove').findDeadorAlive;
29+
var encode = require('../libs/helpers').encode;
2930

3031
//--- Configuration inclusions
3132
var userRoles = require('../models/userRoles.json');
@@ -88,6 +89,9 @@ function getInstallNameBase(aReq, aOptions) {
8889
base = encodeURIComponent(username) + '/' + encodeURIComponent(scriptname);
8990
break;
9091

92+
case 'url':
93+
base = encode(username) + '/' + encode(scriptname);
94+
9195
default:
9296
base = username + '/' + scriptname;
9397
}
@@ -733,7 +737,11 @@ exports.webhook = function (aReq, aRes) {
733737
payload.commits.forEach(function (aCommit) {
734738
aCommit.modified.forEach(function (aFilename) {
735739
if (aFilename.substr(-8) === '.user.js') {
736-
repo[aFilename] = '/' + encodeURI(aFilename);
740+
console.log([
741+
'Webhook filename:',
742+
' ' + aFilename
743+
].join('\n')); // TODO: After some data collected, reaffirm and remove this
744+
repo[aFilename] = '/' + encodeURI(aFilename); // NOTE: Watchpoint
737745
}
738746
});
739747
});

0 commit comments

Comments
 (0)