From fe5fe522ab1b29fb58f632539183ed96199131e4 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Fri, 26 Apr 2024 18:31:34 -0500 Subject: [PATCH] stub handler for identity registration --- backend/src/matrix.js | 22 ++++++++++++++++++++-- frontend/src/Widget.jsx | 19 +++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/backend/src/matrix.js b/backend/src/matrix.js index 3ae7401..e7b615b 100644 --- a/backend/src/matrix.js +++ b/backend/src/matrix.js @@ -67,7 +67,7 @@ const start = async () => { // Leave targetRoomId uninitialized for now } - async function handleCommand(roomId, event) { + const handleCommand = async (roomId, event) => { // Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us) if (event.content?.msgtype !== 'm.text') return; if (event.sender === await client.getUserId()) return; @@ -101,11 +101,29 @@ const start = async () => { // Not found } } - } + }; + + const handleRegisterIdentity = async (roomId, event) => { + // const { message, signature } = event.content; + await client.replyNotice(roomId, event, 'Received request to register identity'); + }; // Before we start the bot, register our command handler client.on('room.message', handleCommand); + // Handler for custom events + client.on('room.event', (roomId, event) => { + // Note that state events can also be sent down this listener too + if (event.state_key !== undefined) return; // state event + + switch (event.type) { + case 'io.dgov.identity.register': + handleRegisterIdentity(roomId, event); + break; + default: + } + }); + client.start().then(() => { console.log('Bot started!'); outboundQueue.resume(); diff --git a/frontend/src/Widget.jsx b/frontend/src/Widget.jsx index 7b8ccd7..c1733d5 100644 --- a/frontend/src/Widget.jsx +++ b/frontend/src/Widget.jsx @@ -368,13 +368,20 @@ function Widget() { setShowViewPost(true); }, [setViewPost, setShowViewPost]); - // TODO: Sign and send a message to the forum-api bot / to a room in matrix - const registerMatrixIdentity = async () => { - await widgetApi.current.sendRoomEvent('m.room.message', { - body: 'test message', - msgtype: 'm.text', + const registerMatrixIdentity = useCallback(async () => { + const message = new Date().toISOString(); + + const signature = await provider.request({ + method: 'personal_sign', + params: [message, account], }); - }; + + // TODO: Sign and send a message + await widgetApi.current.sendRoomEvent('io.dgov.identity.register', { + message, + signature, + }); + }, [provider, account]); /* -------------------------------------------------------------------------------- */ /* --------------------------- END UI ACTIONS ------------------------------------- */