Handle additional side effects from events.

This commit is contained in:
Paul 2021-07-29 21:23:06 +01:00
parent b0681dfc99
commit 6531486636
2 changed files with 57 additions and 11 deletions

View file

@ -321,7 +321,7 @@ export class DataStore {
} }
@action @action
packet(packet: ClientboundNotification) { async packet(packet: ClientboundNotification) {
switch (packet.type) { switch (packet.type) {
case "Ready": { case "Ready": {
for (let user of packet.users) { for (let user of packet.users) {
@ -352,6 +352,12 @@ export class DataStore {
} }
case "ChannelGroupJoin": { case "ChannelGroupJoin": {
this.channels.get(packet.id)?.groupJoin(packet.user); this.channels.get(packet.id)?.groupJoin(packet.user);
if (!this.users.has(packet.user)) {
let user = await this.client.users.fetch(packet.user);
this.users.set(packet.user, new User(user));
}
break; break;
} }
case "ChannelGroupLeave": { case "ChannelGroupLeave": {
@ -375,6 +381,13 @@ export class DataStore {
break; break;
} }
case "ServerDelete": { case "ServerDelete": {
let server = this.servers.get(packet.id);
if (server) {
for (let channel of server.channels) {
this.channels.delete(channel);
}
}
this.servers.delete(packet.id); this.servers.delete(packet.id);
break; break;
} }
@ -385,10 +398,32 @@ export class DataStore {
case "ServerMemberJoin": { case "ServerMemberJoin": {
const _id = { server: packet.id, user: packet.user }; const _id = { server: packet.id, user: packet.user };
this.members.set(_id, new Member({ _id })); this.members.set(_id, new Member({ _id }));
if (!this.servers.has(packet.id)) {
let server = await this.client.servers.fetch(packet.id);
this.servers.set(packet.id, new Server(server));
for (let id of server.channels) {
let channel = this.client.channels.get(id);
if (channel) {
this.channels.set(id, new Channel(channel));
}
}
}
if (!this.users.has(packet.user)) {
let user = await this.client.users.fetch(packet.user);
this.users.set(packet.user, new User(user));
}
break; break;
} }
case "ServerMemberLeave": { case "ServerMemberLeave": {
this.members.delete({ server: packet.id, user: packet.user }); this.members.delete({ server: packet.id, user: packet.user });
if (packet.user === this.client.user!._id) {
await this.packet({ type: "ServerDelete", id: packet.id });
}
break; break;
} }
} }

View file

@ -1,12 +1,16 @@
import { ArrowBack } from "@styled-icons/boxicons-regular"; import { ArrowBack } from "@styled-icons/boxicons-regular";
import { autorun } from "mobx";
import { useStore } from "react-redux";
import { useHistory, useParams } from "react-router-dom"; import { useHistory, useParams } from "react-router-dom";
import { Invites } from "revolt.js/dist/api/objects"; import { Invites, Servers } from "revolt.js/dist/api/objects";
import styles from "./Invite.module.scss"; import styles from "./Invite.module.scss";
import { useContext, useEffect, useState } from "preact/hooks"; import { useContext, useEffect, useState } from "preact/hooks";
import { defer } from "../../lib/defer"; import { defer } from "../../lib/defer";
import { useData } from "../../mobx/State";
import RequiresOnline from "../../context/revoltjs/RequiresOnline"; import RequiresOnline from "../../context/revoltjs/RequiresOnline";
import { import {
AppContext, AppContext,
@ -22,6 +26,7 @@ import Overline from "../../components/ui/Overline";
import Preloader from "../../components/ui/Preloader"; import Preloader from "../../components/ui/Preloader";
export default function Invite() { export default function Invite() {
const store = useData();
const history = useHistory(); const history = useHistory();
const client = useContext(AppContext); const client = useContext(AppContext);
const status = useContext(StatusContext); const status = useContext(StatusContext);
@ -115,19 +120,25 @@ export default function Invite() {
`/server/${invite.server_id}/channel/${invite.channel_id}`, `/server/${invite.server_id}/channel/${invite.channel_id}`,
); );
} }
}
const result = await client.joinInvite( const dispose = autorun(() => {
code, let server = store.servers.get(
); invite.server_id,
if (result.type === "Server") {
defer(() => {
history.push(
`/server/${result.server._id}/channel/${result.channel._id}`,
); );
defer(() => {
if (server) {
history.push(
`/server/${server._id}/channel/${invite.channel_id}`,
);
}
});
dispose();
}); });
} }
await client.joinInvite(code);
} catch (err) { } catch (err) {
setError(takeError(err)); setError(takeError(err));
setProcessing(false); setProcessing(false);