From 61304f18c276a3d6385cf536e6a280479a7f09ce Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 19 Jun 2024 17:40:59 +0100 Subject: [PATCH 1/3] feat: implement support for Subscribe event --- .env | 4 +-- external/revolt.js | 2 +- src/pages/channels/Channel.tsx | 40 ++++++++++++++++++++++++++ src/pages/settings/server/Members.tsx | 41 ++++++++++++++++++++++++--- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/.env b/.env index c8b232c2b..c13d07196 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ # VITE_API_URL=https://api.revolt.chat -VITE_API_URL=https://app.revolt.chat/api +# VITE_API_URL=https://app.revolt.chat/api # VITE_API_URL=http://local.revolt.chat:8000 -# VITE_API_URL=https://revolt.chat/api +VITE_API_URL=https://revolt.chat/api VITE_THEMES_URL=https://themes.revolt.chat diff --git a/external/revolt.js b/external/revolt.js index 783c8d1a3..3fbd54281 160000 --- a/external/revolt.js +++ b/external/revolt.js @@ -1 +1 @@ -Subproject commit 783c8d1a3e3ad6071d268b18077569262ea61602 +Subproject commit 3fbd54281924e4778ed141de23305a8faaa31516 diff --git a/src/pages/channels/Channel.tsx b/src/pages/channels/Channel.tsx index 3545fd554..c60de5840 100644 --- a/src/pages/channels/Channel.tsx +++ b/src/pages/channels/Channel.tsx @@ -163,6 +163,7 @@ const TextChannel = observer(({ channel }: { channel: ChannelI }) => { const checkUnread = () => channel.unread && + document.hasFocus() && channel.client.unreads!.markRead( channel._id, channel.last_message_id!, @@ -176,6 +177,45 @@ const TextChannel = observer(({ channel }: { channel: ChannelI }) => { ); }, [channel]); + useEffect(() => { + let lastSubscribed: number | undefined; + function subscribe() { + if (document.hasFocus()) { + const tenMinutesAgo = new Date(); + tenMinutesAgo.setMinutes(tenMinutesAgo.getMinutes() - 10); + + if (!lastSubscribed || +tenMinutesAgo > lastSubscribed) { + channel.server?.subscribe(); + lastSubscribed = +tenMinutesAgo; + } + } + } + + // Trigger logic every minute + const subTimer = setInterval(subscribe, 60e3); + + function onFocus() { + // Mark channel as read if it's unread + if (channel.unread) { + channel.client.unreads!.markRead( + channel._id, + channel.last_message_id!, + true, + ); + } + + // Subscribe to channel if expired + subscribe(); + } + + addEventListener("focus", onFocus); + + return () => { + removeEventListener("focus", onFocus); + clearInterval(subTimer); + }; + }, [channel]); + return ( { const [query, setQuery] = useState(""); useEffect(() => { - server - .fetchMembers() - .then((data) => data.members) - .then(setData); + function fetch() { + server + .fetchMembers() + .then((data) => data.members) + .then(setData); + } + + fetch(); + + // Members may be invalidated if we stop receiving events + // This is not very accurate, this should be tracked within + // revolt.js so we know the true validity. + let valid = true, + invalidationTimer: number; + + function waitToInvalidate() { + invalidationTimer = setTimeout(() => { + valid = false; + }, 15 * 60e3) as never; // 15 minutes + } + + function cancelInvalidation() { + if (!valid) { + fetch(); + valid = true; + } + + clearTimeout(invalidationTimer); + } + + addEventListener("blur", waitToInvalidate); + addEventListener("focus", cancelInvalidation); + + return () => { + removeEventListener("blur", waitToInvalidate); + removeEventListener("focus", cancelInvalidation); + }; }, [server, setData]); const members = useMemo( From c972e6813f953eb8f53045fd54bfd6d4e67ab401 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 19 Jun 2024 18:16:30 +0100 Subject: [PATCH 2/3] fix: should always call subscribe --- .env.production | 2 ++ src/pages/channels/Channel.tsx | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 .env.production diff --git a/.env.production b/.env.production new file mode 100644 index 000000000..327e06008 --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +# VITE_API_URL=http://local.revolt.chat:8000 +VITE_API_URL=https://app.revolt.chat/api diff --git a/src/pages/channels/Channel.tsx b/src/pages/channels/Channel.tsx index c60de5840..62857c798 100644 --- a/src/pages/channels/Channel.tsx +++ b/src/pages/channels/Channel.tsx @@ -1,5 +1,6 @@ import { Hash } from "@styled-icons/boxicons-regular"; import { Ghost } from "@styled-icons/boxicons-solid"; +import dayjs from "dayjs"; import { reaction } from "mobx"; import { observer } from "mobx-react-lite"; import { Redirect, useParams } from "react-router-dom"; @@ -181,18 +182,19 @@ const TextChannel = observer(({ channel }: { channel: ChannelI }) => { let lastSubscribed: number | undefined; function subscribe() { if (document.hasFocus()) { - const tenMinutesAgo = new Date(); - tenMinutesAgo.setMinutes(tenMinutesAgo.getMinutes() - 10); - - if (!lastSubscribed || +tenMinutesAgo > lastSubscribed) { + if ( + !lastSubscribed || + dayjs().subtract(10, "minutes").isAfter(lastSubscribed) + ) { + lastSubscribed = +new Date(); channel.server?.subscribe(); - lastSubscribed = +tenMinutesAgo; } } } // Trigger logic every minute const subTimer = setInterval(subscribe, 60e3); + subscribe(); function onFocus() { // Mark channel as read if it's unread From df4f6578f76ae316064fd6074147710044609e22 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sun, 27 Oct 2024 21:59:16 +0000 Subject: [PATCH 3/3] feat: add session token to file uploads --- src/controllers/client/jsx/legacy/FileUploads.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/controllers/client/jsx/legacy/FileUploads.tsx b/src/controllers/client/jsx/legacy/FileUploads.tsx index ec36bb6f1..e2a44322d 100644 --- a/src/controllers/client/jsx/legacy/FileUploads.tsx +++ b/src/controllers/client/jsx/legacy/FileUploads.tsx @@ -12,7 +12,7 @@ import { IconButton, Preloader } from "@revoltchat/ui"; import { determineFileSize } from "../../../../lib/fileSize"; import { modalController } from "../../../modals/ModalController"; -import { useClient } from "../../ClientController"; +import { clientController, useClient } from "../../ClientController"; import { takeError } from "../error"; type BehaviourType = @@ -67,9 +67,16 @@ export async function uploadFile( const formData = new FormData(); formData.append("file", file); + const client = clientController.getActiveSession()?.client; + const sesToken = + typeof client?.session === "string" + ? client.session + : client?.session?.token; + const res = await Axios.post(`${autumnURL}/${tag}`, formData, { headers: { "Content-Type": "multipart/form-data", + "X-Session-Token": sesToken, }, ...config, });