From 7d0d29bb8b9f549018119e4da851af88aca0d34c Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 9 Oct 2022 23:23:02 +0300 Subject: [PATCH] add more stubs to matrix implementation --- src/routes/matrix/index.ts | 73 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/src/routes/matrix/index.ts b/src/routes/matrix/index.ts index 66b86f3..a9e6ac3 100644 --- a/src/routes/matrix/index.ts +++ b/src/routes/matrix/index.ts @@ -29,6 +29,19 @@ const roomToChannelId = (room?: any): number | null => { return numberId; }; +const matrixUserProfile = (matrixUserId: string) => { + if (typeof matrixUserId !== "string") return null; + const parts = matrixUserId.split(":", 1); + if (parts.length !== 1) return null; + const suffixedId = parts[0]; + if (!suffixedId.startsWith("@")) return null; + const username = suffixedId.substring(1, suffixedId.length); + if (!username || username.length < 3 || username.length > 32) return null; + + return { + displayname: username + }; +}; interface MatrixSyncCursors { [channel_id: number]: number; @@ -408,10 +421,64 @@ router.get( "/_matrix/client/r0/profile/:userId", authenticateRoute(), (req, res) => { - res.json({ - displayname: req.user.username - }); + res.json(matrixUserProfile(req.params.userId)); } ); +router.get( + "/_matrix/client/r0/profile/:userId/displayname", + authenticateRoute(), + (req, res) => { + res.json(matrixUserProfile(req.params.userId)); + } +); + +router.get( + "/_matrix/client/r0/thirdparty/protocols", + authenticateRoute(), + (req, res) => { + res.json({}); + } +); + +router.get( + "/_matrix/client/r0/rooms/:roomId/state/:eventType", + authenticateRoute(), + (req, res) => { + if (req.params.eventType === "m.room.topic") { + res.json({ + topic: "" + }); + } else if (req.params.eventType === "m.room.avatar") { + res.json({}); + } else { + res.json({ + errcode: "M_BAD_JSON", + error: "Unsupported eventType" + }); + } + } +); + +router.get( + "/_matrix/client/r0/account/3pid", + authenticateRoute(), + (req, res) => { + res.json({ + threepids: [] + }); + } +) + +router.use((req, res, next) => { + //console.log("[matrix debug] [404]"); + //console.log(` -> method: ${req.method}`) + //console.log(` -> path: ${req.path}`); + //console.log(` -> body: ${JSON.stringify(req.body)}`); + res.status(404).json({ + errcode: "M_NOT_FOUND", + error: "Resource or route not found" + }); +}); + export default router;