Loading packages/client/src/components/Chat/OpenChatButton.tsx +2 −6 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ import { useAppContext } from "../../contexts/AppContext"; const OpenChatButton = () => { const { config } = useAppContext(); const { notificationCount } = useChatContext(); const { notificationCount, doLogin } = useChatContext(); return ( <Badge Loading @@ -13,11 +13,7 @@ const OpenChatButton = () => { color="danger" size="sm" > {config?.chat?.element_host && ( <Button as={Link} href={config.chat.element_host} target="_blank"> Chat </Button> )} {config?.chat?.element_host && <Button onPress={doLogin}>Chat</Button>} </Badge> ); }; Loading packages/client/src/contexts/ChatContext.tsx +60 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ import { useState, } from "react"; import { useAppContext } from "./AppContext"; import { toast } from "react-toastify"; interface IMatrixUser { userId: string; Loading Loading @@ -38,6 +39,12 @@ export const ChatContext = ({ children }: PropsWithChildren) => { return; } if (user?.userId) { console.log("[ChatContext#doLogin] user logged in, opening element..."); window.open(config.chat.element_host); return; } const redirectUrl = window.location.protocol + "//" + window.location.host + "/chat_callback"; Loading Loading @@ -104,6 +111,59 @@ export const ChatContext = ({ children }: PropsWithChildren) => { console.log("[Chat] access token has been acquired"); setUser({ userId }); toast.success("Logged into chat"); checkIfInGeneral(); }; const checkIfInGeneral = async () => { const generalAlias = config?.chat.general_alias; if (!generalAlias) { console.log("[ChatContext#checkIfInGeneral] no general alias in config"); return; } const accessToken = localStorage.getItem("matrix.access_token"); if (!accessToken) return; const joinReq = await fetch( `https://${config.chat.matrix_homeserver}/_matrix/client/v3/join/${generalAlias}`, { method: "POST", headers: { Authorization: "Bearer " + accessToken, "Content-Type": "application/json", }, body: JSON.stringify({ reason: "Auto-joined via Canvas client", }), } ); const joinRes = await joinReq.json(); console.log( "[ChatContext#checkIfInGeneral] auto-join general response", joinRes ); if (joinReq.status === 200) { toast.success(`Joined chat ${decodeURIComponent(generalAlias)}!`); } else if (joinReq.status === 403) { toast.error( "Failed to join general chat! " + joinRes.errcode + " - " + joinRes.error ); } else if (joinReq.status === 429) { toast.warn("Auto-join general chat got ratelimited"); } else { toast.error( "Failed to join general chat! " + joinRes.errcode + " - " + joinRes.error ); } }; const checkForNotifs = async () => { Loading packages/lib/src/net.ts +5 −0 Original line number Diff line number Diff line Loading @@ -106,6 +106,11 @@ export type ClientConfig = { * @example https://chat.fediverse.events */ element_host: string; /** * URI encoded alias * @example %23canvas-general:aftermath.gg */ general_alias: string; }; }; Loading packages/server/src/lib/SocketServer.ts +1 −0 Original line number Diff line number Diff line Loading @@ -72,6 +72,7 @@ const getClientConfig = (): ClientConfig => { enabled: true, matrix_homeserver: process.env.MATRIX_HOMESERVER, element_host: process.env.ELEMENT_HOST, general_alias: process.env.MATRIX_GENERAL_ALIAS, }, }; }; Loading packages/server/src/types.ts +1 −0 Original line number Diff line number Diff line Loading @@ -54,6 +54,7 @@ declare global { MATRIX_HOMESERVER: string; ELEMENT_HOST: string; MATRIX_GENERAL_ALIAS: string; } } } Loading
packages/client/src/components/Chat/OpenChatButton.tsx +2 −6 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ import { useAppContext } from "../../contexts/AppContext"; const OpenChatButton = () => { const { config } = useAppContext(); const { notificationCount } = useChatContext(); const { notificationCount, doLogin } = useChatContext(); return ( <Badge Loading @@ -13,11 +13,7 @@ const OpenChatButton = () => { color="danger" size="sm" > {config?.chat?.element_host && ( <Button as={Link} href={config.chat.element_host} target="_blank"> Chat </Button> )} {config?.chat?.element_host && <Button onPress={doLogin}>Chat</Button>} </Badge> ); }; Loading
packages/client/src/contexts/ChatContext.tsx +60 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ import { useState, } from "react"; import { useAppContext } from "./AppContext"; import { toast } from "react-toastify"; interface IMatrixUser { userId: string; Loading Loading @@ -38,6 +39,12 @@ export const ChatContext = ({ children }: PropsWithChildren) => { return; } if (user?.userId) { console.log("[ChatContext#doLogin] user logged in, opening element..."); window.open(config.chat.element_host); return; } const redirectUrl = window.location.protocol + "//" + window.location.host + "/chat_callback"; Loading Loading @@ -104,6 +111,59 @@ export const ChatContext = ({ children }: PropsWithChildren) => { console.log("[Chat] access token has been acquired"); setUser({ userId }); toast.success("Logged into chat"); checkIfInGeneral(); }; const checkIfInGeneral = async () => { const generalAlias = config?.chat.general_alias; if (!generalAlias) { console.log("[ChatContext#checkIfInGeneral] no general alias in config"); return; } const accessToken = localStorage.getItem("matrix.access_token"); if (!accessToken) return; const joinReq = await fetch( `https://${config.chat.matrix_homeserver}/_matrix/client/v3/join/${generalAlias}`, { method: "POST", headers: { Authorization: "Bearer " + accessToken, "Content-Type": "application/json", }, body: JSON.stringify({ reason: "Auto-joined via Canvas client", }), } ); const joinRes = await joinReq.json(); console.log( "[ChatContext#checkIfInGeneral] auto-join general response", joinRes ); if (joinReq.status === 200) { toast.success(`Joined chat ${decodeURIComponent(generalAlias)}!`); } else if (joinReq.status === 403) { toast.error( "Failed to join general chat! " + joinRes.errcode + " - " + joinRes.error ); } else if (joinReq.status === 429) { toast.warn("Auto-join general chat got ratelimited"); } else { toast.error( "Failed to join general chat! " + joinRes.errcode + " - " + joinRes.error ); } }; const checkForNotifs = async () => { Loading
packages/lib/src/net.ts +5 −0 Original line number Diff line number Diff line Loading @@ -106,6 +106,11 @@ export type ClientConfig = { * @example https://chat.fediverse.events */ element_host: string; /** * URI encoded alias * @example %23canvas-general:aftermath.gg */ general_alias: string; }; }; Loading
packages/server/src/lib/SocketServer.ts +1 −0 Original line number Diff line number Diff line Loading @@ -72,6 +72,7 @@ const getClientConfig = (): ClientConfig => { enabled: true, matrix_homeserver: process.env.MATRIX_HOMESERVER, element_host: process.env.ELEMENT_HOST, general_alias: process.env.MATRIX_GENERAL_ALIAS, }, }; }; Loading
packages/server/src/types.ts +1 −0 Original line number Diff line number Diff line Loading @@ -54,6 +54,7 @@ declare global { MATRIX_HOMESERVER: string; ELEMENT_HOST: string; MATRIX_GENERAL_ALIAS: string; } } }