diff --git a/website_mail_turnstile/README.rst b/website_mail_turnstile/README.rst new file mode 100644 index 0000000000..4505c53c54 --- /dev/null +++ b/website_mail_turnstile/README.rst @@ -0,0 +1,98 @@ +================================= +Website Mail Cloudflare Turnstile +================================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:022d2b4a526ab003d56321a848bfdb6cbb9b9a73dbd9873c79c8c861cfb47044 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/18.0/website_mail_turnstile + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-18-0/website-18-0-website_mail_turnstile + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/website&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +The module currently covers the follow widget provided by +``website_mail``. + +**Table of contents** + +.. contents:: + :local: + +Use Cases / Context +=================== + +When ``website_cf_turnstile`` is installed, requests to +``/website_mail/follow`` are validated by Turnstile. + +However, the follow widget provided by ``website_mail`` does not send +the ``turnstile_captcha`` token, causing follow and unfollow operations +to fail with Cloudflare validation enabled. + +Usage +===== + +Install this module together with ``website_mail`` and +``website_cf_turnstile``. + +Once installed, follow and unfollow actions, such as forum topic +subscriptions, will work correctly when Cloudflare Turnstile is enabled. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__: + + - Pilar Vargas + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/website `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_mail_turnstile/__init__.py b/website_mail_turnstile/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/website_mail_turnstile/__manifest__.py b/website_mail_turnstile/__manifest__.py new file mode 100644 index 0000000000..ac695f8b28 --- /dev/null +++ b/website_mail_turnstile/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2026 Tecnativa - Pilar Vargas +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +{ + "name": "Website Mail Cloudflare Turnstile", + "summary": "Adds Cloudflare Turnstile support to website mail follow actions", + "version": "18.0.1.0.0", + "category": "Website", + "website": "https://github.com/OCA/website", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "LGPL-3", + "depends": ["website_mail", "website_cf_turnstile"], + "assets": { + "web.assets_frontend": [ + "website_mail_turnstile/static/src/js/website_mail_turnstile.esm.js", + ], + }, +} diff --git a/website_mail_turnstile/pyproject.toml b/website_mail_turnstile/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/website_mail_turnstile/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/website_mail_turnstile/readme/CONTEXT.md b/website_mail_turnstile/readme/CONTEXT.md new file mode 100644 index 0000000000..75349ec6d1 --- /dev/null +++ b/website_mail_turnstile/readme/CONTEXT.md @@ -0,0 +1,6 @@ +When ``website_cf_turnstile`` is installed, requests to +``/website_mail/follow`` are validated by Turnstile. + +However, the follow widget provided by ``website_mail`` does not send the +``turnstile_captcha`` token, causing follow and unfollow operations to fail +with Cloudflare validation enabled. \ No newline at end of file diff --git a/website_mail_turnstile/readme/CONTRIBUTORS.md b/website_mail_turnstile/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..df940da7c6 --- /dev/null +++ b/website_mail_turnstile/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Tecnativa](https://www.tecnativa.com): + - Pilar Vargas diff --git a/website_mail_turnstile/readme/DESCRIPTION.md b/website_mail_turnstile/readme/DESCRIPTION.md new file mode 100644 index 0000000000..26065e940d --- /dev/null +++ b/website_mail_turnstile/readme/DESCRIPTION.md @@ -0,0 +1 @@ +The module currently covers the follow widget provided by ``website_mail``. \ No newline at end of file diff --git a/website_mail_turnstile/readme/USAGE.md b/website_mail_turnstile/readme/USAGE.md new file mode 100644 index 0000000000..e5edb9ebe1 --- /dev/null +++ b/website_mail_turnstile/readme/USAGE.md @@ -0,0 +1,5 @@ +Install this module together with ``website_mail`` and +``website_cf_turnstile``. + +Once installed, follow and unfollow actions, such as forum topic +subscriptions, will work correctly when Cloudflare Turnstile is enabled. \ No newline at end of file diff --git a/website_mail_turnstile/static/description/index.html b/website_mail_turnstile/static/description/index.html new file mode 100644 index 0000000000..acde2e6388 --- /dev/null +++ b/website_mail_turnstile/static/description/index.html @@ -0,0 +1,444 @@ + + + + + +Website Mail Cloudflare Turnstile + + + +
+

Website Mail Cloudflare Turnstile

+ + +

Beta License: LGPL-3 OCA/website Translate me on Weblate Try me on Runboat

+

The module currently covers the follow widget provided by +website_mail.

+

Table of contents

+ +
+

Use Cases / Context

+

When website_cf_turnstile is installed, requests to +/website_mail/follow are validated by Turnstile.

+

However, the follow widget provided by website_mail does not send +the turnstile_captcha token, causing follow and unfollow operations +to fail with Cloudflare validation enabled.

+
+
+

Usage

+

Install this module together with website_mail and +website_cf_turnstile.

+

Once installed, follow and unfollow actions, such as forum topic +subscriptions, will work correctly when Cloudflare Turnstile is enabled.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/website project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/website_mail_turnstile/static/src/js/website_mail_turnstile.esm.js b/website_mail_turnstile/static/src/js/website_mail_turnstile.esm.js new file mode 100644 index 0000000000..2284f58bcc --- /dev/null +++ b/website_mail_turnstile/static/src/js/website_mail_turnstile.esm.js @@ -0,0 +1,127 @@ +import {_t} from "@web/core/l10n/translation"; +import publicWidget from "@web/legacy/js/public/public_widget"; +import {renderToElement} from "@web/core/utils/render"; +import {rpc} from "@web/core/network/rpc"; +import {session} from "@web/session"; + +publicWidget.registry.follow.include({ + /** + * @override + */ + start() { + const res = this._super(...arguments); + if (!session.turnstile_site_key || this.editableMode) { + return res; + } + if (!this.el.querySelector(".s_turnstile_container")) { + const turnstileNodes = this._addTurnstile("website_mail_follow"); + turnstileNodes?.appendTo($(this.el)); + this._renderTurnstile(turnstileNodes); + } + return res; + }, + + _addTurnstile(action) { + const mode = + new URLSearchParams(window.location.search).get("cf") === "show" + ? "always" + : "interaction-only"; + const turnstileContainer = renderToElement( + "website_cf_turnstile.turnstile_container", + { + action: action, + appearance: mode, + additionalClasses: "position-absolute top-0 start-0", + beforeInteractiveGlobalCallback: "turnstileFollowBecomeVisible", + errorGlobalCallback: "throwTurnstileErrorCode", + executeGlobalCallback: "turnstileFollowSuccess", + expiredCallback: "turnstileFollowExpired", + sitekey: session.turnstile_site_key, + style: "display: none; width: 0; height: 0; overflow: hidden;", + } + ); + let toInsert = $(turnstileContainer); + globalThis.throwTurnstileErrorCode = function (code) { + const error = new Error("Turnstile Error"); + error.code = code; + throw error; + }; + if (!window.turnstile?.render) { + const turnstileScript = renderToElement( + "website_cf_turnstile.turnstile_remote_script" + ); + toInsert = toInsert.add($(turnstileScript)); + } + return toInsert; + }, + + _renderTurnstile(turnstileNodes) { + const nodes = turnstileNodes.toArray(); + const turnstileContainer = nodes.find((node) => + node.classList.contains("s_turnstile_container") + ); + const turnstileScript = nodes.find( + (node) => node.id === "s_turnstile_remote_script" + ); + if (turnstileScript) { + return; + } + if ( + window.turnstile?.render && + turnstileContainer && + !turnstileContainer.querySelector("iframe") + ) { + window.turnstile.render(turnstileContainer); + } + }, + + /** + * @override + */ + async _onClick(ev) { + const $jsFollow = $(ev.currentTarget).closest(".js_follow"); + const $email = $jsFollow.find(".js_follow_email"); + if ($email.length && !$email.val().match(/.+@.+/)) { + $jsFollow + .addClass("o_has_error") + .find(".form-control, .form-select") + .addClass("is-invalid"); + return false; + } + $jsFollow + .removeClass("o_has_error") + .find(".form-control, .form-select") + .removeClass("is-invalid"); + const email = $email.length ? $email.val() : false; + if (email || this.isUser) { + const tokenCaptcha = await this._recaptcha.getToken("website_mail_follow"); + const token = tokenCaptcha.token; + if (tokenCaptcha.error) { + this.notification.add(tokenCaptcha.error, { + type: "danger", + title: _t("Error"), + sticky: true, + }); + return false; + } + rpc("/website_mail/follow", { + id: Number($jsFollow.data("id")), + object: $jsFollow.data("object"), + message_is_follower: $jsFollow.attr("data-follow") || "off", + email: email, + recaptcha_token_response: token, + turnstile_captcha: this.$el + .find("input[name='turnstile_captcha']") + .val(), + }).then((follow) => { + this._toggleSubscription(follow, email, $jsFollow); + const turnstileContainer = this.el.querySelector( + ".s_turnstile_container" + ); + if (window.turnstile?.reset && turnstileContainer) { + window.turnstile.reset(turnstileContainer); + } + }); + } + }, +});