Skip to content

Commit

Permalink
merge branch: 'insert/member-subscriptions'
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Oct 27, 2024
2 parents 86e8424 + df4f657 commit ef4218d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# VITE_API_URL=http://local.revolt.chat:8000
VITE_API_URL=https://app.revolt.chat/api
9 changes: 8 additions & 1 deletion src/controllers/client/jsx/legacy/FileUploads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
});
Expand Down
42 changes: 42 additions & 0 deletions src/pages/channels/Channel.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -163,6 +164,7 @@ const TextChannel = observer(({ channel }: { channel: ChannelI }) => {

const checkUnread = () =>
channel.unread &&
document.hasFocus() &&
channel.client.unreads!.markRead(
channel._id,
channel.last_message_id!,
Expand All @@ -176,6 +178,46 @@ const TextChannel = observer(({ channel }: { channel: ChannelI }) => {
);
}, [channel]);

useEffect(() => {
let lastSubscribed: number | undefined;
function subscribe() {
if (document.hasFocus()) {
if (
!lastSubscribed ||
dayjs().subtract(10, "minutes").isAfter(lastSubscribed)
) {
lastSubscribed = +new Date();
channel.server?.subscribe();
}
}
}

// Trigger logic every minute
const subTimer = setInterval(subscribe, 60e3);
subscribe();

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 (
<AgeGate
type="channel"
Expand Down
41 changes: 37 additions & 4 deletions src/pages/settings/server/Members.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,43 @@ export const Members = ({ server }: Props) => {
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(
Expand Down

0 comments on commit ef4218d

Please sign in to comment.