Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

🐛 Fixed failing Facebook URL validation for group urls #906

Merged
merged 3 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 32 additions & 41 deletions app/controllers/settings/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,13 @@ export default Controller.extend({
let oldUrl = this.get('model.facebook');
let errMessage = '';

// reset errors and validation
this.get('model.errors').remove('facebook');
this.get('model.hasValidated').removeObject('facebook');

if (newUrl === '') {
// Clear out the Facebook url
this.set('model.facebook', '');
this.get('model.errors').remove('facebook');
return;
}

Expand All @@ -197,49 +200,41 @@ export default Controller.extend({
newUrl = oldUrl;
}

// If new url didn't change, exit
if (newUrl === oldUrl) {
this.get('model.errors').remove('facebook');
return;
}
try {
// strip any facebook URLs out
newUrl = newUrl.replace(/(https?:\/\/)?(www\.)?facebook\.com/i, '');

if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-z\d.]+)/i)) {
let username = [];

if (newUrl.match(/(?:facebook\.com\/)(\S+)/)) {
[, username] = newUrl.match(/(?:facebook\.com\/)(\S+)/);
} else {
[, username] = newUrl.match(/(?:https:\/\/|http:\/\/)?(?:www\.)?(?:\w+\.\w+\/+)?(\S+)/mi);
// don't allow any non-facebook urls
if (newUrl.match(/^(http|\/\/)/i)) {
throw 'invalid url';
}

// check if we have a /page/username or without
if (username.match(/^(?:\/)?(pages?\/\S+)/mi)) {
// we got a page url, now save the username without the / in the beginning

[, username] = username.match(/^(?:\/)?(pages?\/\S+)/mi);
} else if (username.match(/^(http|www)|(\/)/) || !username.match(/^([a-z\d.]{1,50})$/mi)) {
errMessage = !username.match(/^([a-z\d.]{1,50})$/mi) ? 'Your Page name is not a valid Facebook Page name' : 'The URL must be in a format like https://www.facebook.com/yourPage';
// strip leading / if we have one then concat to full facebook URL
newUrl = newUrl.replace(/^\//, '');
newUrl = `https://www.facebook.com/${newUrl}`;

this.get('model.errors').add('facebook', errMessage);
this.get('model.hasValidated').pushObject('facebook');
return;
// don't allow URL if it's not valid
if (!validator.isURL(newUrl)) {
throw 'invalid url';
}

newUrl = `https://www.facebook.com/${username}`;

this.get('model.errors').remove('facebook');
this.get('model.hasValidated').pushObject('facebook');

this.set('model.facebook', '');
run.schedule('afterRender', this, function () {
this.set('model.facebook', newUrl);
});
} else {
errMessage = 'The URL must be in a format like '
+ 'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);

} catch (e) {
if (e === 'invalid url') {
errMessage = 'The URL must be in a format like '
+ 'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);
return;
}

throw e;

} finally {
this.get('model.hasValidated').pushObject('facebook');
return;
}
},

Expand All @@ -248,10 +243,13 @@ export default Controller.extend({
let oldUrl = this.get('model.twitter');
let errMessage = '';

// reset errors and validation
this.get('model.errors').remove('twitter');
this.get('model.hasValidated').removeObject('twitter');

if (newUrl === '') {
// Clear out the Twitter url
this.set('model.twitter', '');
this.get('model.errors').remove('twitter');
return;
}

Expand All @@ -260,12 +258,6 @@ export default Controller.extend({
newUrl = oldUrl;
}

// If new url didn't change, exit
if (newUrl === oldUrl) {
this.get('model.errors').remove('twitter');
return;
}

if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-z\d.]+)/i)) {
let username = [];

Expand All @@ -286,7 +278,6 @@ export default Controller.extend({

newUrl = `https://twitter.com/${username}`;

this.get('model.errors').remove('twitter');
this.get('model.hasValidated').pushObject('twitter');

this.set('model.twitter', '');
Expand Down
79 changes: 32 additions & 47 deletions app/controllers/team/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ export default Controller.extend({
let oldUrl = this.get('user.facebook');
let errMessage = '';

// reset errors and validation
this.get('user.errors').remove('facebook');
this.get('user.hasValidated').removeObject('facebook');

if (newUrl === '') {
// Clear out the Facebook url
this.set('user.facebook', '');
this.get('user.errors').remove('facebook');
return;
}

Expand All @@ -258,52 +261,41 @@ export default Controller.extend({
newUrl = oldUrl;
}

// If new url didn't change, exit
if (newUrl === oldUrl) {
this.get('user.errors').remove('facebook');
return;
}
try {
// strip any facebook URLs out
newUrl = newUrl.replace(/(https?:\/\/)?(www\.)?facebook\.com/i, '');

// TODO: put the validation here into a validator
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-z\d.]+)/i)) {
let username = [];

if (newUrl.match(/(?:facebook\.com\/)(\S+)/)) {
[, username] = newUrl.match(/(?:facebook\.com\/)(\S+)/);
} else {
[, username] = newUrl.match(/(?:https:\/\/|http:\/\/)?(?:www\.)?(?:\w+\.\w+\/+)?(\S+)/mi);
// don't allow any non-facebook urls
if (newUrl.match(/^(http|\/\/)/i)) {
throw 'invalid url';
}

// check if we have a /page/username or without
if (username.match(/^(?:\/)?(pages?\/\S+)/mi)) {
// we got a page url, now save the username without the / in the beginning

[, username] = username.match(/^(?:\/)?(pages?\/\S+)/mi);
} else if (username.match(/^(http|www)|(\/)/) || !username.match(/^([a-z\d.]{1,50})$/mi)) {
errMessage = !username.match(/^([a-z\d.]{1,50})$/mi) ? 'Your Username is not a valid Facebook Username' : 'The URL must be in a format like https://www.facebook.com/yourUsername';
// strip leading / if we have one then concat to full facebook URL
newUrl = newUrl.replace(/^\//, '');
newUrl = `https://www.facebook.com/${newUrl}`;

this.get('user.errors').add('facebook', errMessage);
this.get('user.hasValidated').pushObject('facebook');
return;
// don't allow URL if it's not valid
if (!validator.isURL(newUrl)) {
throw 'invalid url';
}

newUrl = `https://www.facebook.com/${username}`;
this.set('user.facebook', newUrl);

this.get('user.errors').remove('facebook');
this.get('user.hasValidated').pushObject('facebook');

// necessary to update the value in the input field
this.set('user.facebook', '');
run.schedule('afterRender', this, function () {
this.set('user.facebook', newUrl);
});
} else {
errMessage = 'The URL must be in a format like '
+ 'https://www.facebook.com/yourUsername';
this.get('user.errors').add('facebook', errMessage);

} catch (e) {
if (e === 'invalid url') {
errMessage = 'The URL must be in a format like '
+ 'https://www.facebook.com/yourPage';
this.get('user.errors').add('facebook', errMessage);
return;
}

throw e;

} finally {
this.get('user.hasValidated').pushObject('facebook');
return;
}
},

Expand All @@ -312,10 +304,13 @@ export default Controller.extend({
let oldUrl = this.get('user.twitter');
let errMessage = '';

// reset errors and validation
this.get('user.errors').remove('twitter');
this.get('user.hasValidated').removeObject('twitter');

if (newUrl === '') {
// Clear out the Twitter url
this.set('user.twitter', '');
this.get('user.errors').remove('twitter');
return;
}

Expand All @@ -324,13 +319,6 @@ export default Controller.extend({
newUrl = oldUrl;
}

// If new url didn't change, exit
if (newUrl === oldUrl) {
this.get('user.errors').remove('twitter');
return;
}

// TODO: put the validation here into a validator
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-z\d.]+)/i)) {
let username = [];

Expand All @@ -350,12 +338,9 @@ export default Controller.extend({
}

newUrl = `https://twitter.com/${username}`;
this.set('user.twitter', newUrl);

this.get('user.errors').remove('twitter');
this.get('user.hasValidated').pushObject('twitter');

// necessary to update the value in the input field
this.set('user.twitter', '');
run.schedule('afterRender', this, function () {
this.set('user.twitter', newUrl);
Expand Down
10 changes: 5 additions & 5 deletions app/templates/team/user.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,35 @@
{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="location"}}
<label for="user-location">Location</label>
{{gh-input user.location type="text" id="user-location" focusOut=(action "validate" "location" target=user) update=(action (mut user.location)) data-test-location-input=true}}
{{gh-error-message errors=user.errors property="location"}}
{{gh-error-message errors=user.errors property="location" data-test-location-error=true}}
<p>Where in the world do you live?</p>
{{/gh-form-group}}

{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="website"}}
<label for="user-website">Website</label>
{{gh-input user.website type="url" id="user-website" autocapitalize="off" autocorrect="off" autocomplete="off" focusOut=(action "validate" "website" target=user) update=(action (mut user.website)) data-test-website-input=true}}
{{gh-error-message errors=user.errors property="website"}}
{{gh-error-message errors=user.errors property="website" data-test-website-error=true}}
<p>Have a website or blog other than this one? Link it!</p>
{{/gh-form-group}}

{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="facebook"}}
<label for="user-facebook">Facebook Profile</label>
<input value={{user.facebook}} oninput={{action (mut _scratchFacebook) value="target.value"}} {{action "validateFacebookUrl" on="focusOut"}} type="url" class="gh-input" id="user-facebook" name="user[facebook]" placeholder="https://www.facebook.com/username" autocorrect="off" data-test-facebook-input=true/>
{{gh-error-message errors=user.errors property="facebook"}}
{{gh-error-message errors=user.errors property="facebook" data-test-facebook-error=true}}
<p>URL of your personal Facebook Profile</p>
{{/gh-form-group}}

{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="twitter"}}
<label for="user-twitter">Twitter Profile</label>
<input value={{user.twitter}} oninput={{action (mut _scratchTwitter) value="target.value"}} {{action "validateTwitterUrl" on="focusOut"}} type="url" class="gh-input" id="user-twitter" name="user[twitter]" placeholder="https://twitter.com/username" autocorrect="off" data-test-twitter-input=true/>
{{gh-error-message errors=user.errors property="twitter"}}
{{gh-error-message errors=user.errors property="twitter" data-test-twitter-error=true}}
<p>URL of your personal Twitter profile</p>
{{/gh-form-group}}

{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="bio" class="bio-container"}}
<label for="user-bio">Bio</label>
{{gh-textarea user.bio id="user-bio" focusOut=(action "validate" "bio" target=user) update=(action (mut user.bio)) data-test-bio-input=true}}
{{gh-error-message errors=user.errors property="bio"}}
{{gh-error-message errors=user.errors property="bio" data-test-bio-error=true}}
<p>
Write about you, in 200 characters or less.
{{gh-count-characters user.bio}}
Expand Down
Loading