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

Improvements to twitter & facebook handling #6848

Merged
merged 4 commits into from
May 17, 2016
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
128 changes: 79 additions & 49 deletions core/client/app/controllers/settings/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,48 +125,65 @@ export default Controller.extend(SettingsSaveMixin, {
let oldUrl = this.get('model.facebook');
let errMessage = '';

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

// _scratchFacebook will be null unless the user has input something
if (!newUrl) {
newUrl = oldUrl;
}

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

if (!newUrl.match(/(^https:\/\/www\.facebook\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://www.facebook.com/${username}`;
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-z\d\.]+)/i)) {
let username = [];

this.set('model.facebook', newUrl);
if (newUrl.match(/(?:facebook\.com\/)(\S+)/)) {
[ , username ] = newUrl.match(/(?:facebook\.com\/)(\S+)/);
} else {
[ , username ] = newUrl.match(/(?:https\:\/\/|http\:\/\/)?(?:www\.)?(?:\w+\.\w+\/+)?(\S+)/mi);
}

this.get('model.errors').remove('facebook');
this.get('model.hasValidated').pushObject('facebook');
// 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\.]{5,50})$/mi)) {
errMessage = !username.match(/^([a-z\d\.]{5,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';

// User input is validated
return this.save().then(() => {
this.set('model.facebook', '');
run.schedule('afterRender', this, function () {
this.set('model.facebook', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);
this.get('model.hasValidated').pushObject('facebook');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);
this.get('model.hasValidated').pushObject('facebook');
return;
}

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

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

// User input is validated
return this.save().then(() => {
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);
this.get('model.hasValidated').pushObject('facebook');
return;
}
},

Expand All @@ -175,48 +192,61 @@ export default Controller.extend(SettingsSaveMixin, {
let oldUrl = this.get('model.twitter');
let errMessage = '';

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

// _scratchTwitter will be null unless the user has input something
if (!newUrl) {
newUrl = oldUrl;
}

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

if (!newUrl.match(/(^https:\/\/twitter\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://twitter.com/${username}`;
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-z\d\.]+)/i)) {
let username = [];

this.set('model.twitter', newUrl);
if (newUrl.match(/(?:twitter\.com\/)(\S+)/)) {
[ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/);
} else {
[username] = newUrl.match(/([^/]+)\/?$/mi);
}

this.get('model.errors').remove('twitter');
this.get('model.hasValidated').pushObject('twitter');
// check if username starts with http or www and show error if so
if (username.match(/^(http|www)|(\/)/) || !username.match(/^[a-z\d\.\_]{1,15}$/mi)) {
errMessage = !username.match(/^[a-z\d\.\_]{1,15}$/mi) ? 'Your Username is not a valid Twitter Username' : 'The URL must be in a format like https://twitter.com/yourUsername';

// User input is validated
return this.save().then(() => {
this.set('model.twitter', '');
run.schedule('afterRender', this, function () {
this.set('model.twitter', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('model.errors').add('twitter', errMessage);
this.get('model.hasValidated').pushObject('twitter');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('model.errors').add('twitter', errMessage);
this.get('model.hasValidated').pushObject('twitter');
return;
}

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

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

// User input is validated
return this.save().then(() => {
this.set('model.twitter', '');
run.schedule('afterRender', this, function () {
this.set('model.twitter', newUrl);
});
});
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('model.errors').add('twitter', errMessage);
this.get('model.hasValidated').pushObject('twitter');
return;
}
}
}
Expand Down
130 changes: 80 additions & 50 deletions core/client/app/controllers/team/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,50 +249,67 @@ export default Controller.extend({
let oldUrl = this.get('user.facebook');
let errMessage = '';

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

// _scratchFacebook will be null unless the user has input something
if (!newUrl) {
newUrl = oldUrl;
}

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

// TODO: put the validation here into a validator
if (!newUrl.match(/(^https:\/\/www\.facebook\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://www.facebook.com/${username}`;
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-z\d\.]+)/i)) {
let username = [];

this.set('user.facebook', newUrl);
if (newUrl.match(/(?:facebook\.com\/)(\S+)/)) {
[ , username ] = newUrl.match(/(?:facebook\.com\/)(\S+)/);
} else {
[ , username ] = newUrl.match(/(?:https\:\/\/|http\:\/\/)?(?:www\.)?(?:\w+\.\w+\/+)?(\S+)/mi);
}

this.get('user.errors').remove('facebook');
this.get('user.hasValidated').pushObject('facebook');
// 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\.]{5,50})$/mi)) {
errMessage = !username.match(/^([a-z\d\.]{5,50})$/mi) ? 'Your Username is not a valid Facebook Username' : 'The URL must be in a format like https://www.facebook.com/yourUsername';

// User input is validated
invoke(this, 'save').then(() => {
// necessary to update the value in the input field
this.set('user.facebook', '');
run.schedule('afterRender', this, function () {
this.set('user.facebook', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourUsername';
this.get('user.errors').add('facebook', errMessage);
this.get('user.hasValidated').pushObject('facebook');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourUsername';
this.get('user.errors').add('facebook', errMessage);
this.get('user.hasValidated').pushObject('facebook');
return;
}

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

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

// User input is validated
invoke(this, 'save').then(() => {
// 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);
this.get('user.hasValidated').pushObject('facebook');
return;
}
},

Expand All @@ -301,50 +318,63 @@ export default Controller.extend({
let oldUrl = this.get('user.twitter');
let errMessage = '';

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

// _scratchTwitter will be null unless the user has input something
if (!newUrl) {
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(/(^https:\/\/twitter\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://twitter.com/${username}`;
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-z\d\.]+)/i)) {
let username = [];

this.set('user.twitter', newUrl);
if (newUrl.match(/(?:twitter\.com\/)(\S+)/)) {
[ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/);
} else {
[username] = newUrl.match(/([^/]+)\/?$/mi);
}

this.get('user.errors').remove('twitter');
this.get('user.hasValidated').pushObject('twitter');
// check if username starts with http or www and show error if so
if (username.match(/^(http|www)|(\/)/) || !username.match(/^[a-z\d\.\_]{1,15}$/mi)) {
errMessage = !username.match(/^[a-z\d\.\_]{1,15}$/mi) ? 'Your Username is not a valid Twitter Username' : 'The URL must be in a format like https://twitter.com/yourUsername';

// User input is validated
invoke(this, 'save').then(() => {
// necessary to update the value in the input field
this.set('user.twitter', '');
run.schedule('afterRender', this, function () {
this.set('user.twitter', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('user.errors').add('twitter', errMessage);
this.get('user.hasValidated').pushObject('twitter');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('user.errors').add('twitter', errMessage);
this.get('user.hasValidated').pushObject('twitter');
return;
}

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

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

// User input is validated
invoke(this, 'save').then(() => {
// necessary to update the value in the input field
this.set('user.twitter', '');
run.schedule('afterRender', this, function () {
this.set('user.twitter', newUrl);
});
});
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('user.errors').add('twitter', errMessage);
this.get('user.hasValidated').pushObject('twitter');
return;
}
},

Expand Down
4 changes: 2 additions & 2 deletions core/client/app/mirage/fixtures/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default [
updated_at: '2016-05-08T15:20:25.953Z',
updated_by: 1,
uuid: 'd4387e5c-3230-46dd-a89b-0d8a40365c35',
value: ''
value: 'test'
},
{
created_at: '2016-05-05T15:40:12.134Z',
Expand All @@ -199,7 +199,7 @@ export default [
updated_at: '2016-05-08T15:20:25.954Z',
updated_by: 1,
uuid: '5130441f-e4c7-4750-9692-a22d841ab049',
value: ''
value: '@test'
},
{
key: 'availableThemes',
Expand Down
2 changes: 1 addition & 1 deletion core/client/app/models/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default Model.extend(ValidationEngine, {
availableThemes: attr(),
ghost_head: attr('string'),
ghost_foot: attr('string'),
facebook: attr('string'),
facebook: attr('facebook-url-user'),
twitter: attr('twitter-url-user'),
labs: attr('string'),
navigation: attr('navigation-settings'),
Expand Down
4 changes: 2 additions & 2 deletions core/client/app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default Model.extend(ValidationEngine, {
async: false
}),
count: attr('raw'),
facebook: attr('string'),
twitter: attr('string'),
facebook: attr('facebook-url-user'),
twitter: attr('twitter-url-user'),

ghostPaths: service(),
ajax: service(),
Expand Down
Loading