about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-06-02T09·48+0300
committertazjin <tazjin@tvl.su>2023-06-06T11·43+0000
commit6f0ddbac06a9a72908b8b0b955cd743d3ae82378 (patch)
tree9092b069b2c07ff037e3dc3d839b2e50d2b81fe1
parentb2ff63586acbdcd8e1c8159cac2c09724306eda6 (diff)
feat(corp/rih): display & load captcha element inside the form r/6233
Change-Id: Ifd0f85d9e4f785c4cb1ae56ae67e6d999ff43c85
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8694
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
-rw-r--r--corp/rih/Cargo.lock1
-rw-r--r--corp/rih/Cargo.toml5
-rw-r--r--corp/rih/index.html5
-rw-r--r--corp/rih/src/home.html14
-rw-r--r--corp/rih/src/main.rs21
5 files changed, 39 insertions, 7 deletions
diff --git a/corp/rih/Cargo.lock b/corp/rih/Cargo.lock
index 3ed11c4ef2..7e472e5fe1 100644
--- a/corp/rih/Cargo.lock
+++ b/corp/rih/Cargo.lock
@@ -1085,6 +1085,7 @@ dependencies = [
  "fuzzy-matcher",
  "getrandom",
  "gloo",
+ "js-sys",
  "rand",
  "rust_iso3166",
  "serde",
diff --git a/corp/rih/Cargo.toml b/corp/rih/Cargo.toml
index 16221b7a55..2dd8cb7b97 100644
--- a/corp/rih/Cargo.toml
+++ b/corp/rih/Cargo.toml
@@ -7,14 +7,15 @@ edition = "2021"
 
 [dependencies]
 fuzzy-matcher = "0.3.7"
+getrandom = { version = "0.2", features = ["js"] }
 gloo = "0.8"
+js-sys = "0.3"
+rand = "0.8"
 rust_iso3166 = "0.1.10"
 serde_json = "1.0"
 serde_urlencoded = "*" # pinned by yew
 yew = { version = "0.20", features = ["csr"] }
 yew-router = "0.17"
-rand = "0.8"
-getrandom = { version = "0.2", features = ["js"] }
 
 # needs to be in sync with nixpkgs
 wasm-bindgen = "= 0.2.84"
diff --git a/corp/rih/index.html b/corp/rih/index.html
index b7d1803f11..4f0f8f3604 100644
--- a/corp/rih/index.html
+++ b/corp/rih/index.html
@@ -11,7 +11,8 @@
     <!-- Bootstrap -->
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
+
+    <!-- Captcha setup -->
+    <script src="https://smartcaptcha.yandexcloud.net/captcha.js" defer></script>
   </head>
-  <body>
-  </body>
 </html>
diff --git a/corp/rih/src/home.html b/corp/rih/src/home.html
index 7f26144979..95a77877d3 100644
--- a/corp/rih/src/home.html
+++ b/corp/rih/src/home.html
@@ -1,5 +1,17 @@
 html! {
 <main>
+  <script>
+    {r#"function captchaOnload(sitekey) {
+      if (window.smartCaptcha) {
+        const container = document.getElementById('captcha-container');
+        const widgetId = window.smartCaptcha.render(container, {
+            sitekey: sitekey,
+            hl: 'en',
+        });
+      }
+    }"#}
+  </script>
+
   <div class="container px-4 pt-5 my-5 text-center">
     <div class="row">
       <div class="col-7 ms-auto">
@@ -156,6 +168,8 @@ html! {
             <div id="personalDetailsHelp" class="form-text">{"Any specific places where you'd like to live? Would you be moving with family? Any other assistance required?"}</div>
           </div>
 
+          <div id="captcha-container" class="smart-captcha mb-3" style="height: 100px" />
+
           <button type="submit" class="btn btn-primary" disabled=true>{"Submit"}</button>
           <p class="pt-2"><i>{"This page is still under construction! Please reach out at contact@ if you have any questions."}</i></p>
         </form>
diff --git a/corp/rih/src/main.rs b/corp/rih/src/main.rs
index 7360a6979d..2fed9dfd8f 100644
--- a/corp/rih/src/main.rs
+++ b/corp/rih/src/main.rs
@@ -1,8 +1,7 @@
 use fuzzy_matcher::skim::SkimMatcherV2;
 use fuzzy_matcher::FuzzyMatcher;
 use gloo::console;
-use gloo::history::BrowserHistory;
-use gloo::history::History;
+use gloo::history::{BrowserHistory, History};
 use gloo::storage::{LocalStorage, Storage};
 use rand::seq::IteratorRandom;
 use rand::thread_rng;
@@ -10,12 +9,21 @@ use serde::{Deserialize, Serialize};
 use static_markdown::markdown;
 use std::collections::BTreeSet;
 use wasm_bindgen::closure::Closure;
-use wasm_bindgen::JsCast;
+use wasm_bindgen::{JsCast, JsValue};
 use web_sys::{HtmlInputElement, HtmlTextAreaElement, KeyboardEvent};
 use yew::html::Scope;
 use yew::prelude::*;
 use yew_router::prelude::*;
 
+/// Form submission is protected with a captcha. The development
+/// version of the captcha does not do domain checking and works on
+/// `localhost` as well.
+#[cfg(debug_assertions)]
+const CAPTCHA_KEY: &'static str = "ysc1_K7iOi3FSmsyO8pZGu8Im2iQClCtPsVx7jSRyhyCV435a732c";
+
+#[cfg(not(debug_assertions))]
+const CAPTCHA_KEY: &'static str = "ysc1_a3LVlaDRDMwU8CLSZ0WKENTI2exyOxz5J2c6x28P5339d410";
+
 /// This code ends up being compiled for the native and for the
 /// webassembly architectures during the build & test process.
 /// However, the `rust_iso3166` crate exposes a different API (!)
@@ -388,6 +396,13 @@ impl Component for App {
             Route::NotFound => todo!(),
         }
     }
+
+    fn rendered(&mut self, _: &Context<Self>, first_render: bool) {
+        if first_render {
+            let func = js_sys::Function::new_with_args("key", "captchaOnload(key)");
+            let _ = func.call1(&JsValue::NULL, &JsValue::from_str(CAPTCHA_KEY));
+        }
+    }
 }
 
 fn main() {