feat: switch to revolt.js@6.0.0 + new revolt-api

This commit is contained in:
Paul Makles
2022-04-09 15:47:04 +01:00
parent dc3925c003
commit b2f4411850
91 changed files with 528 additions and 460 deletions

View File

@@ -337,7 +337,9 @@ export default observer(() => {
<Trash
size={24}
onClick={() =>
client.users.edit({ remove: "StatusText" })
client.users.edit({
remove: ["StatusText"],
})
}
/>
)}

View File

@@ -69,7 +69,7 @@ export default observer(({ channel }: Props) => {
{ max_side: 256 },
true,
)}
remove={() => channel.edit({ remove: "Icon" })}
remove={() => channel.edit({ remove: ["Icon"] })}
defaultPreview={
channel.channel_type === "Group"
? "/assets/group.png"

View File

@@ -1,7 +1,6 @@
import isEqual from "lodash.isequal";
import { observer } from "mobx-react-lite";
import { OverrideField } from "revolt-api/types/_common";
import { Channel } from "revolt.js/dist/maps/Channels";
import { Channel, API } from "revolt.js";
import { useLayoutEffect, useState } from "preact/hooks";
@@ -42,7 +41,7 @@ export default observer(({ channel }: Props) => {
// Keep track of whatever role we're editing right now.
const [selected, setSelected] = useState("default");
const [value, setValue] = useState<OverrideField | number | undefined>(
const [value, setValue] = useState<API.OverrideField | number | undefined>(
undefined,
);
const currentPermission = currentRoles.find(
@@ -64,10 +63,10 @@ export default observer(({ channel }: Props) => {
selected,
typeof currentValue === "number"
? currentValue
: {
: ({
allow: currentValue.a,
deny: currentValue.d,
},
} as any),
);
}

View File

@@ -1,4 +1,4 @@
import { At, Key, Block, ListOl } from "@styled-icons/boxicons-regular";
import { At, Key, Block } from "@styled-icons/boxicons-regular";
import {
Envelope,
HelpCircle,
@@ -8,7 +8,7 @@ import {
} from "@styled-icons/boxicons-solid";
import { observer } from "mobx-react-lite";
import { useHistory } from "react-router-dom";
import { Profile } from "revolt-api/types/Users";
import { API } from "revolt.js";
import styles from "./Panes.module.scss";
import { Text } from "preact-i18n";
@@ -37,7 +37,9 @@ export const Account = observer(() => {
const [email, setEmail] = useState("...");
const [revealEmail, setRevealEmail] = useState(false);
const [profile, setProfile] = useState<undefined | Profile>(undefined);
const [profile, setProfile] = useState<undefined | API.UserProfile>(
undefined,
);
const history = useHistory();
function switchPage(to: string) {
@@ -46,8 +48,8 @@ export const Account = observer(() => {
useEffect(() => {
if (email === "..." && status === ClientStatus.ONLINE) {
client
.req("GET", "/auth/account")
client.api
.get("/auth/account/")
.then((account) => setEmail(account.email));
}

View File

@@ -1,9 +1,9 @@
// ! FIXME: this code is garbage, need to replace
import { Key, Clipboard, Globe, Plus } from "@styled-icons/boxicons-regular";
import { LockAlt, HelpCircle } from "@styled-icons/boxicons-solid";
import type { AxiosError } from "axios";
import { observer } from "mobx-react-lite";
import { Bot } from "revolt-api/types/Bots";
import { Profile as ProfileI } from "revolt-api/types/Users";
import { API } from "revolt.js";
import { User } from "revolt.js/dist/maps/Users";
import styled from "styled-components/macro";
@@ -43,7 +43,7 @@ interface Changes {
name?: string;
public?: boolean;
interactions_url?: string;
remove?: "InteractionsURL";
remove?: "InteractionsURL"[];
}
const BotBadge = styled.div`
@@ -62,7 +62,7 @@ const BotBadge = styled.div`
`;
interface Props {
bot: Bot;
bot: API.Bot;
onDelete(): void;
onUpdate(changes: Changes): void;
}
@@ -75,7 +75,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
_id: bot._id,
username: user.username,
public: bot.public,
interactions_url: bot.interactions_url,
interactions_url: bot.interactions_url as any,
});
const [error, setError] = useState<string | JSX.Element>("");
const [saving, setSaving] = useState(false);
@@ -87,23 +87,21 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
useState<HTMLInputElement | null>(null);
const { writeClipboard, openScreen } = useIntermediate();
const [profile, setProfile] = useState<undefined | ProfileI>(undefined);
const [profile, setProfile] = useState<undefined | API.UserProfile>(
undefined,
);
const refreshProfile = useCallback(() => {
client
.request(
"GET",
`/users/${bot._id}/profile` as "/users/id/profile",
{
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers["x-user-id"];
delete headers["x-session-token"];
return data;
},
client.api
.get(`/users/${bot._id as ""}/profile`, undefined, {
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers?.["x-user-id"];
delete headers?.["x-session-token"];
return data;
},
)
})
.then((profile) => setProfile(profile ?? {}));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user, setProfile]);
@@ -122,14 +120,14 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
const changes: Changes = {};
if (data.username !== user!.username) changes.name = data.username;
if (data.public !== bot.public) changes.public = data.public;
if (data.interactions_url === "") changes.remove = "InteractionsURL";
if (data.interactions_url === "") changes.remove = ["InteractionsURL"];
else if (data.interactions_url !== bot.interactions_url)
changes.interactions_url = data.interactions_url;
setSaving(true);
setError("");
try {
await client.bots.edit(bot._id, changes);
if (changed) await editBotContent(profile?.content);
if (changed) await editBotContent(profile?.content ?? undefined);
onUpdate(changes);
setChanged(false);
setEditMode(false);
@@ -152,19 +150,22 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotAvatar(avatar?: string) {
setSaving(true);
setError("");
await client.request("PATCH", "/users/id", {
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers["x-user-id"];
delete headers["x-session-token"];
return data;
await client.api.patch(
"/users/@me",
avatar ? { avatar } : { remove: ["Avatar"] },
{
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers?.["x-user-id"];
delete headers?.["x-session-token"];
return data;
},
},
data: JSON.stringify(avatar ? { avatar } : { remove: "Avatar" }),
});
);
const res = await client.bots.fetch(bot._id);
if (!avatar) res.user.update({}, "Avatar");
if (!avatar) res.user.update({}, ["Avatar"]);
setUser(res.user);
setSaving(false);
}
@@ -172,20 +173,21 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotBackground(background?: string) {
setSaving(true);
setError("");
await client.request("PATCH", "/users/id", {
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers["x-user-id"];
delete headers["x-session-token"];
return data;
await client.api.patch(
"/users/@me",
background
? { profile: { background } }
: { remove: ["ProfileBackground"] },
{
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers?.["x-user-id"];
delete headers?.["x-session-token"];
return data;
},
},
data: JSON.stringify(
background
? { profile: { background } }
: { remove: "ProfileBackground" },
),
});
);
if (!background) setProfile({ ...profile, background: undefined });
else refreshProfile();
@@ -195,20 +197,19 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotContent(content?: string) {
setSaving(true);
setError("");
await client.request("PATCH", "/users/id", {
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers["x-user-id"];
delete headers["x-session-token"];
return data;
await client.api.patch(
"/users/@me",
content ? { profile: { content } } : { remove: ["ProfileContent"] },
{
headers: { "x-bot-token": bot.token },
transformRequest: (data, headers) => {
// Remove user headers for this request
delete headers?.["x-user-id"];
delete headers?.["x-session-token"];
return data;
},
},
data: JSON.stringify(
content
? { profile: { content } }
: { remove: "ProfileContent" },
),
});
);
if (!content) setProfile({ ...profile, content: undefined });
else refreshProfile();
@@ -333,7 +334,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
_id: bot._id,
username: user!.username,
public: bot.public,
interactions_url: bot.interactions_url,
interactions_url: bot.interactions_url as any,
});
usernameRef!.value = user!.username;
interactionsRef!.value = bot.interactions_url || "";
@@ -521,7 +522,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
export const MyBots = observer(() => {
const client = useClient();
const [bots, setBots] = useState<Bot[] | undefined>(undefined);
const [bots, setBots] = useState<API.Bot[] | undefined>(undefined);
useEffect(() => {
client.bots.fetchOwned().then(({ bots }) => setBots(bots));
@@ -582,7 +583,7 @@ export const MyBots = observer(() => {
changes.interactions_url;
if (
changes.remove ===
"InteractionsURL"
["InteractionsURL"]
)
x.interactions_url = undefined;
}

View File

@@ -81,7 +81,7 @@ export const Notifications = observer(() => {
// tell the server we just subscribed
const json = sub.toJSON();
if (json.keys) {
client.req("POST", "/push/subscribe", {
client.api.post("/push/subscribe", {
endpoint: sub.endpoint,
...(json.keys as {
p256dh: string;
@@ -96,7 +96,7 @@ export const Notifications = observer(() => {
sub?.unsubscribe();
setPushEnabled(false);
client.req("POST", "/push/unsubscribe");
client.api.post("/push/unsubscribe");
}
}
} catch (err) {

View File

@@ -1,7 +1,7 @@
import { Markdown } from "@styled-icons/boxicons-logos";
import { observer } from "mobx-react-lite";
import { useHistory } from "react-router-dom";
import { Profile as ProfileI } from "revolt-api/types/Users";
import { API } from "revolt.js";
import styles from "./Panes.module.scss";
import { Text } from "preact-i18n";
@@ -30,7 +30,9 @@ export const Profile = observer(() => {
const client = useClient();
const history = useHistory();
const [profile, setProfile] = useState<undefined | ProfileI>(undefined);
const [profile, setProfile] = useState<undefined | API.UserProfile>(
undefined,
);
// ! FIXME: temporary solution
// ! we should just announce profile changes through WS
@@ -103,7 +105,7 @@ export const Profile = observer(() => {
behaviour="upload"
maxFileSize={4_000_000}
onUpload={(avatar) => client.users.edit({ avatar })}
remove={() => client.users.edit({ remove: "Avatar" })}
remove={() => client.users.edit({ remove: ["Avatar"] })}
defaultPreview={client.user!.generateAvatarURL(
{ max_side: 256 },
true,
@@ -132,7 +134,7 @@ export const Profile = observer(() => {
}}
remove={async () => {
await client.users.edit({
remove: "ProfileBackground",
remove: ["ProfileBackground"],
});
setProfile({ ...profile, background: undefined });
}}

View File

@@ -11,7 +11,7 @@ import {
} from "@styled-icons/simple-icons";
import relativeTime from "dayjs/plugin/relativeTime";
import { useHistory } from "react-router-dom";
import { SessionInfo } from "revolt-api/types/Auth";
import { API } from "revolt.js";
import { decodeTime } from "ulid";
import styles from "./Panes.module.scss";
@@ -33,7 +33,7 @@ export function Sessions() {
const deviceId =
typeof client.session === "object" ? client.session._id : undefined;
const [sessions, setSessions] = useState<SessionInfo[] | undefined>(
const [sessions, setSessions] = useState<API.SessionInfo[] | undefined>(
undefined,
);
const [attemptingDelete, setDelete] = useState<string[]>([]);
@@ -44,7 +44,7 @@ export function Sessions() {
}
useEffect(() => {
client.req("GET", "/auth/session/all").then((data) => {
client.api.get("/auth/session/all").then((data) => {
data.sort(
(a, b) =>
(b._id === deviceId ? 1 : 0) - (a._id === deviceId ? 1 : 0),
@@ -61,7 +61,7 @@ export function Sessions() {
);
}
function getIcon(session: SessionInfo) {
function getIcon(session: API.SessionInfo) {
const name = session.name;
switch (true) {
case /firefox/i.test(name):
@@ -83,7 +83,7 @@ export function Sessions() {
}
}
function getSystemIcon(session: SessionInfo) {
function getSystemIcon(session: API.SessionInfo) {
const name = session.name;
switch (true) {
case /linux/i.test(name):
@@ -187,9 +187,10 @@ export function Sessions() {
...attemptingDelete,
session._id,
]);
await client.req(
"DELETE",
`/auth/session/${session._id}` as "/auth/session/id",
await client.api.delete(
`/auth/session/${
session._id as ""
}`,
);
setSessions(
sessions?.filter(
@@ -222,10 +223,7 @@ export function Sessions() {
setDelete(del);
for (const id of del) {
await client.req(
"DELETE",
`/auth/session/${id}` as "/auth/session/id",
);
await client.api.delete(`/auth/session/${id as ""}`);
}
setSessions(sessions.filter((x) => x._id === deviceId));

View File

@@ -1,9 +1,7 @@
import { XCircle } from "@styled-icons/boxicons-regular";
import { observer } from "mobx-react-lite";
import { Virtuoso } from "react-virtuoso";
import { Ban } from "revolt-api/types/Servers";
import { User } from "revolt-api/types/Users";
import { Route } from "revolt.js/dist/api/routes";
import { API } from "revolt.js";
import { Server } from "revolt.js/dist/maps/Servers";
import styles from "./Panes.module.scss";
@@ -15,8 +13,8 @@ import IconButton from "../../../components/ui/IconButton";
import Preloader from "../../../components/ui/Preloader";
interface InnerProps {
ban: Ban;
users: Pick<User, "username" | "avatar" | "_id">[];
ban: API.ServerBan;
users: Pick<API.User, "username" | "avatar" | "_id">[];
server: Server;
removeSelf: () => void;
}
@@ -53,9 +51,7 @@ interface Props {
}
export const Bans = observer(({ server }: Props) => {
const [data, setData] = useState<
Route<"GET", "/servers/id/bans">["response"] | undefined
>(undefined);
const [data, setData] = useState<API.BanListResult | undefined>(undefined);
useEffect(() => {
server.fetchBans().then(setData);

View File

@@ -1,9 +1,7 @@
import { Plus, X } from "@styled-icons/boxicons-regular";
import { observer } from "mobx-react-lite";
import { DragDropContext } from "react-beautiful-dnd";
import { TextChannel, VoiceChannel } from "revolt-api/types/Channels";
import { Category } from "revolt-api/types/Servers";
import { Server } from "revolt.js/dist/maps/Servers";
import { Channel, Server, API } from "revolt.js";
import styled, { css } from "styled-components/macro";
import { ulid } from "ulid";
@@ -135,7 +133,7 @@ interface Props {
export const Categories = observer(({ server }: Props) => {
const [status, setStatus] = useState<EditStatus>("saved");
const [categories, setCategories] = useState<Category[]>(
const [categories, setCategories] = useState<API.Category[]>(
server.categories ?? [],
);
@@ -327,12 +325,14 @@ function ListElement({
addChannel,
draggable,
}: {
category: Category;
category: API.Category;
server: Server;
index: number;
setTitle?: (title: string) => void;
deleteSelf?: () => void;
addChannel: (channel: TextChannel | VoiceChannel) => void;
addChannel: (
channel: Channel & { channel_type: "TextChannel" | "VoiceChannel" },
) => void;
draggable?: boolean;
}) {
const { openScreen } = useIntermediate();

View File

@@ -1,8 +1,7 @@
import { XCircle } from "@styled-icons/boxicons-regular";
import { observer } from "mobx-react-lite";
import { Virtuoso } from "react-virtuoso";
import { Invite, ServerInvite } from "revolt-api/types/Invites";
import { Server } from "revolt.js/dist/maps/Servers";
import { API, Server } from "revolt.js";
import styles from "./Panes.module.scss";
import { Text } from "preact-i18n";
@@ -16,7 +15,7 @@ import IconButton from "../../../components/ui/IconButton";
import Preloader from "../../../components/ui/Preloader";
interface InnerProps {
invite: Invite;
invite: API.Invite;
server: Server;
removeSelf: () => void;
}
@@ -52,12 +51,10 @@ interface Props {
}
export const Invites = ({ server }: Props) => {
const [invites, setInvites] = useState<ServerInvite[] | undefined>(
undefined,
);
const [invites, setInvites] = useState<API.Invite[] | undefined>(undefined);
useEffect(() => {
server.fetchInvites().then(setInvites);
server.fetchInvites().then((v) => setInvites(v));
}, [server, setInvites]);
return (

View File

@@ -8,6 +8,7 @@ import { Text } from "preact-i18n";
import { useEffect, useState } from "preact/hooks";
import TextAreaAutoSize from "../../../lib/TextAreaAutoSize";
import { noop } from "../../../lib/js";
import { FileUploader } from "../../../context/revoltjs/FileUploads";
import { getChannelName } from "../../../context/revoltjs/util";
@@ -60,9 +61,9 @@ export const Overview = observer(({ server }: Props) => {
fileType="icons"
behaviour="upload"
maxFileSize={2_500_000}
onUpload={(icon) => server.edit({ icon })}
onUpload={(icon) => server.edit({ icon }).then(noop)}
previewURL={server.generateIconURL({ max_side: 256 }, true)}
remove={() => server.edit({ remove: "Icon" })}
remove={() => server.edit({ remove: ["Icon"] }).then(noop)}
/>
<div className={styles.name}>
<h3>
@@ -117,9 +118,9 @@ export const Overview = observer(({ server }: Props) => {
fileType="banners"
behaviour="upload"
maxFileSize={6_000_000}
onUpload={(banner) => server.edit({ banner })}
onUpload={(banner) => server.edit({ banner }).then(noop)}
previewURL={server.generateBannerURL({ width: 1000 }, true)}
remove={() => server.edit({ remove: "Banner" })}
remove={() => server.edit({ remove: ["Banner"] }).then(noop)}
/>
<hr />
<h3>