[FIX] website: expand Facebook snippet URL validation

Some Facebook pages have a different URL pattern than what we currently
accept for the snippet. Namely:
- /profile.php?id=<id>
- /people/<name>/<id>
- /p/<name>-<id>

This commit adds these to the accepted patterns and adjusts the call to
the Facebook page plugins with the right format.

task-3147321

closes odoo/odoo#124250

X-original-commit: f3e82c3842f44aaf10cb1a56a90883b5f5338af1
Signed-off-by: Outagant Mehdi (mou) <mou@odoo.com>
This commit is contained in:
Robin Lejeune (role)
2023-06-08 14:32:40 +02:00
parent fcdd2acc5a
commit 3749c03ceb
2 changed files with 29 additions and 9 deletions
@@ -16,10 +16,14 @@ const FacebookPageWidget = publicWidget.Widget.extend({
this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive();
var params = pick(this.$el[0].dataset, 'href', 'height', 'tabs', 'small_header', 'hide_cover');
const params = pick(this.$el[0].dataset, 'href', 'id', 'height', 'tabs', 'small_header', 'hide_cover');
if (!params.href) {
return def;
}
if (params.id) {
params.href = `https://www.facebook.com/${params.id}`;
}
delete params.id;
params.width = utils.confine(Math.floor(this.$el.width()), 180, 500);
var src = $.param.querystring('https://www.facebook.com/plugins/page.php', params);
@@ -14,6 +14,7 @@ options.registry.facebookPage = options.Class.extend({
var defaults = {
href: '',
id: '',
height: 215,
width: 350,
tabs: '',
@@ -135,18 +136,33 @@ options.registry.facebookPage = options.Class.extend({
*/
_checkURL: function () {
const defaultURL = 'https://www.facebook.com/Odoo';
const match = this.fbData.href.match(/^(?:https?:\/\/)?(?:www\.)?(?:fb|facebook)\.com\/(?:([\w.]+)|[^/?#]+-([0-9]{15,16}))(?:$|[/?# ])/);
// Patterns matched by the regex (all relate to existing pages,
// in spite of the URLs containing "profile.php" or "people"):
// - https://www.facebook.com/<pagewithaname>
// - http://www.facebook.com/<page.with.a.name>
// - www.facebook.com/<fbid>
// - facebook.com/profile.php?id=<fbid>
// - www.facebook.com/<name>-<fbid> - NB: the name doesn't matter
// - www.fb.com/people/<name>/<fbid> - same
// - m.facebook.com/p/<name>-<fbid> - same
// The regex is kept as a huge one-liner for performance as it is
// compiled once on script load. The only way to split it on several
// lines is with the RegExp constructor, which is compiled on runtime.
const match = this.fbData.href.match(/^(https?:\/\/)?((www\.)?(fb|facebook)|(m\.)?facebook)\.com\/(((profile\.php\?id=|people\/[^/?#]+\/|(p\/)?[^/?#]+-)(?<id>[0-9]{15,16}))|(?<nameid>[\w.]+))($|[/?# ])/);
if (match) {
// Check if the page exists on Facebook or not
return new Promise((resolve, reject) => $.ajax({
url: 'https://graph.facebook.com/' + (match[2] || match[1]) + '/picture',
success: () => resolve(),
error: () => {
const pageId = match.groups.nameid || match.groups.id;
return fetch(`https://graph.facebook.com/${pageId}/picture`)
.then((res) => {
if (res.ok) {
this.fbData.id = pageId;
} else {
this.fbData.id = "";
this.fbData.href = defaultURL;
resolve();
},
}));
}
});
}
this.fbData.id = "";
this.fbData.href = defaultURL;
return Promise.resolve();
},