From 3a0143ac46ab5e22c08a667fbb00532990b3a7b1 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Fri, 8 Oct 2021 01:11:33 +0800 Subject: [PATCH] [matrix] use encodeURIComponent to handle the url encode --- server/notification-providers/matrix.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/server/notification-providers/matrix.js b/server/notification-providers/matrix.js index eeaa9eb91..c1054fce6 100644 --- a/server/notification-providers/matrix.js +++ b/server/notification-providers/matrix.js @@ -1,6 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); -const Crypto = require('crypto') +const Crypto = require("crypto"); +const { debug } = require("../../src/util"); class Matrix extends NotificationProvider { name = "matrix"; @@ -9,14 +10,18 @@ class Matrix extends NotificationProvider { let okMsg = "Sent Successfully."; const size = 20; - const randomString = Crypto - .randomBytes(size) - .toString('base64') - .slice(0, size); - const roomId = notification - .internalRoomId - .replaceAll(":", "%3A") - .replaceAll("!", "%21"); + const randomString = encodeURIComponent( + Crypto + .randomBytes(size) + .toString("base64") + .slice(0, size) + ); + + debug("Random String: " + randomString); + + const roomId = encodeURIComponent(notification.internalRoomId); + + debug("Matrix Room ID: " + roomId); try { let config = { @@ -29,7 +34,7 @@ class Matrix extends NotificationProvider { "body": msg }; - await axios.put(`${notification.homeserverUrl}/_matrix/client/r0/rooms/${roomId}/send/m.room.message/${randomString}`, data, config) + await axios.put(`${notification.homeserverUrl}/_matrix/client/r0/rooms/${roomId}/send/m.room.message/${randomString}`, data, config); return okMsg; } catch (error) { this.throwGeneralAxiosError(error);