From 3749c03cebbf262dfa00ce14fc08c1ceaf36bd38 Mon Sep 17 00:00:00 2001 From: "Robin Lejeune (role)" Date: Tue, 2 May 2023 08:52:42 +0000 Subject: [PATCH] [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= - /people// - /p/- 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) --- .../src/snippets/s_facebook_page/000.js | 6 +++- .../src/snippets/s_facebook_page/options.js | 32 ++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/addons/website/static/src/snippets/s_facebook_page/000.js b/addons/website/static/src/snippets/s_facebook_page/000.js index 159a5196ff2..c2a973e2ed4 100644 --- a/addons/website/static/src/snippets/s_facebook_page/000.js +++ b/addons/website/static/src/snippets/s_facebook_page/000.js @@ -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); diff --git a/addons/website/static/src/snippets/s_facebook_page/options.js b/addons/website/static/src/snippets/s_facebook_page/options.js index beae6e31734..a4ec7d8dda4 100644 --- a/addons/website/static/src/snippets/s_facebook_page/options.js +++ b/addons/website/static/src/snippets/s_facebook_page/options.js @@ -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/ + // - http://www.facebook.com/ + // - www.facebook.com/ + // - facebook.com/profile.php?id= + // - www.facebook.com/- - NB: the name doesn't matter + // - www.fb.com/people// - same + // - m.facebook.com/p/- - 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\/)?[^/?#]+-)(?[0-9]{15,16}))|(?[\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(); },