: asapien/signet

commit 2423e010078734ecfe390c2e75937b0074680551
parent 0019c5cd89a25029697285754173f3832894a061
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:40:03 +0530

Task 5: Client enrolment dialog with canvas signature pad

Add enrolment UI to document view:
- Canvas-based signature drawing (pointer events, black on transparent)
- PNG file upload alternative
- Poll /me until enrolled, then reload document
- Show enrolment dialog only when needs_enrolment is true (can_decide ∧ zone ∧ !enrolled)

Update GET /me to return facsimile {width, height} for auto-fit placement.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2 files changed, 77 insertions(+), 22 deletions(-)
M
src/signet-api.c
|
15
++++++++++-----
M
www/app.js
|
84
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
diff --git a/src/signet-api.c b/src/signet-api.c
@@ -315,21 +315,26 @@ h_me(struct kreq *r, PGconn *pg, const struct ident *id)
 {
 	PGresult *res;
 	char buf[512];
-	int enrolled = 0;
+	int enrolled = 0, width = 0, height = 0;
 
-	res = db_q(pg, "SELECT 1 FROM facsimiles WHERE username = $1 AND enrolled_at IS NOT NULL",
+	res = db_q(pg, "SELECT width, height FROM facsimiles WHERE username = $1 AND enrolled_at IS NOT NULL",
 	    1, (const char *[]){ id->user });
 	if (res != NULL) {
-		enrolled = PQntuples(res) == 1;
+		if (PQntuples(res) == 1) {
+			enrolled = 1;
+			width = atoi(PQgetvalue(res, 0, 0));
+			height = atoi(PQgetvalue(res, 0, 1));
+		}
 		PQclear(res);
 	}
 
 	snprintf(buf, sizeof(buf), "{\"user\":\"%s\",\"roles\":{\"drafter\":%s,"
-	    "\"signer\":%s,\"admin\":%s},\"enrolled\":%s}", id->user,
+	    "\"signer\":%s,\"admin\":%s},\"enrolled\":%s,\"facsimile\":{\"width\":%d,\"height\":%d}}",
+	    id->user,
 	    ident_has_role(id, &cf, "drafter") ? "true" : "false",
 	    ident_has_role(id, &cf, "signer") ? "true" : "false",
 	    ident_has_role(id, &cf, "admin") ? "true" : "false",
-	    enrolled ? "true" : "false");
+	    enrolled ? "true" : "false", width, height);
 	send_json(r, KHTTP_200, buf);
 }
 
diff --git a/www/app.js b/www/app.js
@@ -598,23 +598,73 @@ async function showDoc(id) {
       const comment = h('textarea', { rows: 3, placeholder: 'Comment (required unless signing off)' });
       const currentParty = doc.current_party > 0 && doc.route.parties ? doc.route.parties[doc.current_party - 1] : null;
       const hasZone = currentParty && doc.layout && doc.layout.imprints && doc.layout.imprints.some((i) => i.type === 'signature_zone' && i.party === currentParty.name);
-      const placementInput = hasZone ? h('input', { type: 'hidden', id: 'f-placement', value: '' }) : null;
-      const decide = (decision) => act(async () => {
-        const fields = { decision, comment: comment.value };
-        if (hasZone && decision === 'approve') {
-          const pl = document.getElementById('f-placement').value;
-          if (!pl) throw new Error('Placement required for signature zones');
-          fields.placement = pl;
-        }
-        return api(`/docs/${id}/decide`, { method: 'POST', fields });
-      }, 'Recorded');
-      side.append(h('h2', {}, 'Your decision'), comment);
-      if (placementInput) side.append(h('p', { class: 'notice' }, 'Draw your signature placement in the zone on the page'));
-      if (placementInput) side.append(placementInput);
-      side.append(h('div', { class: 'actions' },
-        h('button', { class: 'primary', onclick: () => decide('approve') }, 'Sign off'),
-        h('button', { onclick: () => decide('correction') }, 'Correction required'),
-        h('button', { class: 'danger', onclick: () => { if (confirm('Disapprove this document? This is final.')) decide('disapprove'); } }, 'Disapprove')));
+      const needsEnrolment = hasZone && !app.me.enrolled;
+
+      if (needsEnrolment) {
+        /* Show enrolment dialog */
+        const canvas = h('canvas', { width: 400, height: 200, style: 'border: 1px solid #ccc; display: block; margin: 1rem 0' });
+        const fileInput = h('input', { type: 'file', accept: 'image/png', style: 'margin: 1rem 0' });
+        let isDrawing = false;
+        let ctx = canvas.getContext('2d');
+        ctx.fillStyle = 'transparent';
+        ctx.fillRect(0, 0, canvas.width, canvas.height);
+        ctx.strokeStyle = '#000';
+        ctx.lineWidth = 2;
+
+        const drawStart = (e) => { isDrawing = true; const rect = canvas.getBoundingClientRect(); ctx.beginPath(); ctx.moveTo(e.clientX - rect.left, e.clientY - rect.top); };
+        const drawMove = (e) => { if (!isDrawing) return; const rect = canvas.getBoundingClientRect(); ctx.lineTo(e.clientX - rect.left, e.clientY - rect.top); ctx.stroke(); };
+        const drawEnd = () => { isDrawing = false; };
+        canvas.addEventListener('mousedown', drawStart);
+        canvas.addEventListener('mousemove', drawMove);
+        canvas.addEventListener('mouseup', drawEnd);
+        canvas.addEventListener('mouseout', drawEnd);
+
+        const enrol = async () => {
+          let blob;
+          if (fileInput.files[0]) {
+            blob = fileInput.files[0];
+          } else {
+            blob = await new Promise((res) => canvas.toBlob(res, 'image/png'));
+          }
+          await api('/me/facsimile', { method: 'POST', fields: { file: blob } });
+          toast('Signature enrolled. Waiting for activation…');
+          /* Poll /me until enrolled */
+          for (let i = 0; i < 30; i++) {
+            await new Promise((r) => setTimeout(r, 500));
+            const me = await api('/me');
+            if (me.enrolled) {
+              toast('Ready to sign!');
+              location.hash = '#/doc/' + id; /* Reload the page */
+              return;
+            }
+          }
+          throw new Error('Enrolment timeout');
+        };
+
+        side.append(h('h2', {}, 'Enrol your signature'),
+          h('p', {}, 'Draw your signature in the box below or upload a PNG file'),
+          canvas, fileInput,
+          h('div', { class: 'actions' }, h('button', { class: 'primary', onclick: () => act(enrol, null, false) }, 'Enrol signature')));
+      } else {
+        /* Show decision UI */
+        const placementInput = hasZone ? h('input', { type: 'hidden', id: 'f-placement', value: '' }) : null;
+        const decide = (decision) => act(async () => {
+          const fields = { decision, comment: comment.value };
+          if (hasZone && decision === 'approve') {
+            const pl = document.getElementById('f-placement').value;
+            if (!pl) throw new Error('Placement required for signature zones');
+            fields.placement = pl;
+          }
+          return api(`/docs/${id}/decide`, { method: 'POST', fields });
+        }, 'Recorded');
+        side.append(h('h2', {}, 'Your decision'), comment);
+        if (placementInput) side.append(h('p', { class: 'notice' }, 'Draw your signature placement in the zone on the page'));
+        if (placementInput) side.append(placementInput);
+        side.append(h('div', { class: 'actions' },
+          h('button', { class: 'primary', onclick: () => decide('approve') }, 'Sign off'),
+          h('button', { onclick: () => decide('correction') }, 'Correction required'),
+          h('button', { class: 'danger', onclick: () => { if (confirm('Disapprove this document? This is final.')) decide('disapprove'); } }, 'Disapprove')));
+      }
     }
   }
   side.append(...(historyList() || []));