Remove remaining references to useUser(s).

Add Channel object.
This commit is contained in:
Paul 2021-07-29 16:55:07 +01:00
parent 748713be53
commit 0571c065bd
13 changed files with 126 additions and 54 deletions

View file

@ -9,7 +9,6 @@ import { QueuedMessage } from "../../../redux/reducers/queue";
import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { AppContext } from "../../../context/revoltjs/RevoltClient"; import { AppContext } from "../../../context/revoltjs/RevoltClient";
import { useUser } from "../../../context/revoltjs/hooks";
import { MessageObject } from "../../../context/revoltjs/util"; import { MessageObject } from "../../../context/revoltjs/util";
import Overline from "../../ui/Overline"; import Overline from "../../ui/Overline";

View file

@ -8,7 +8,6 @@ import { TextReact } from "../../../lib/i18n";
import { User } from "../../../mobx"; import { User } from "../../../mobx";
import { useData } from "../../../mobx/State"; import { useData } from "../../../mobx/State";
import { useForceUpdate, useUser } from "../../../context/revoltjs/hooks";
import { MessageObject } from "../../../context/revoltjs/util"; import { MessageObject } from "../../../context/revoltjs/util";
import UserShort from "../user/UserShort"; import UserShort from "../user/UserShort";

View file

@ -13,7 +13,7 @@ import { useRenderState } from "../../../../lib/renderer/Singleton";
import { useData } from "../../../../mobx/State"; import { useData } from "../../../../mobx/State";
import { useForceUpdate, useUser } from "../../../../context/revoltjs/hooks"; import { useClient } from "../../../../context/revoltjs/RevoltClient";
import { mapMessage, MessageObject } from "../../../../context/revoltjs/util"; import { mapMessage, MessageObject } from "../../../../context/revoltjs/util";
import Markdown from "../../../markdown/Markdown"; import Markdown from "../../../markdown/Markdown";
@ -124,7 +124,7 @@ export const ReplyBase = styled.div<{
`; `;
export const MessageReply = observer(({ index, channel, id }: Props) => { export const MessageReply = observer(({ index, channel, id }: Props) => {
const ctx = useForceUpdate(); const client = useClient();
const view = useRenderState(channel); const view = useRenderState(channel);
if (view?.type !== "RENDER") return null; if (view?.type !== "RENDER") return null;
@ -138,7 +138,7 @@ export const MessageReply = observer(({ index, channel, id }: Props) => {
if (m) { if (m) {
setMessage(m); setMessage(m);
} else { } else {
ctx.client.channels client.channels
.fetchMessage(channel, id) .fetchMessage(channel, id)
.then((m) => setMessage(mapMessage(m))); .then((m) => setMessage(mapMessage(m)));
} }
@ -178,8 +178,7 @@ export const MessageReply = observer(({ index, channel, id }: Props) => {
<div <div
className="content" className="content"
onClick={() => { onClick={() => {
const obj = const obj = client.channels.get(channel);
ctx.client.channels.get(channel);
if (obj?.channel_type === "TextChannel") { if (obj?.channel_type === "TextChannel") {
history.push( history.push(
`/server/${obj.server}/channel/${obj._id}/${message._id}`, `/server/${obj.server}/channel/${obj._id}/${message._id}`,

View file

@ -17,8 +17,6 @@ import { useRenderState } from "../../../../lib/renderer/Singleton";
import { useData } from "../../../../mobx/State"; import { useData } from "../../../../mobx/State";
import { Reply } from "../../../../redux/reducers/queue"; import { Reply } from "../../../../redux/reducers/queue";
import { useUsers } from "../../../../context/revoltjs/hooks";
import IconButton from "../../../ui/IconButton"; import IconButton from "../../../ui/IconButton";
import Markdown from "../../../markdown/Markdown"; import Markdown from "../../../markdown/Markdown";

View file

@ -15,7 +15,6 @@ import {
AppContext, AppContext,
useClient, useClient,
} from "../../../../context/revoltjs/RevoltClient"; } from "../../../../context/revoltjs/RevoltClient";
import { useUsers } from "../../../../context/revoltjs/hooks";
import { Username } from "../../user/UserShort"; import { Username } from "../../user/UserShort";

View file

@ -23,11 +23,7 @@ import { Unreads } from "../../../redux/reducers/unreads";
import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { AppContext } from "../../../context/revoltjs/RevoltClient"; import { AppContext } from "../../../context/revoltjs/RevoltClient";
import { import { useDMs, useForceUpdate } from "../../../context/revoltjs/hooks";
useDMs,
useForceUpdate,
useUsers,
} from "../../../context/revoltjs/hooks";
import Category from "../../ui/Category"; import Category from "../../ui/Category";
import placeholderSVG from "../items/placeholder.svg"; import placeholderSVG from "../items/placeholder.svg";

View file

@ -21,7 +21,6 @@ import {
HookContext, HookContext,
useChannel, useChannel,
useForceUpdate, useForceUpdate,
useUsers,
} from "../../../context/revoltjs/hooks"; } from "../../../context/revoltjs/hooks";
import CollapsibleSection from "../../common/CollapsibleSection"; import CollapsibleSection from "../../common/CollapsibleSection";

View file

@ -31,7 +31,6 @@ import {
useChannels, useChannels,
useForceUpdate, useForceUpdate,
useUserPermission, useUserPermission,
useUsers,
} from "../../revoltjs/hooks"; } from "../../revoltjs/hooks";
import { useIntermediate } from "../Intermediate"; import { useIntermediate } from "../Intermediate";

View file

@ -77,18 +77,6 @@ function useObject(
: map.toArray(); : map.toArray();
} }
export function useUser(id?: string, context?: HookContext) {
if (typeof id === "undefined") return;
return useObject("users", id, context) as Readonly<Users.User> | undefined;
}
export function useUsers(ids?: string[], context?: HookContext) {
return useObject("users", ids, context) as (
| Readonly<Users.User>
| undefined
)[];
}
export function useChannel(id?: string, context?: HookContext) { export function useChannel(id?: string, context?: HookContext) {
if (typeof id === "undefined") return; if (typeof id === "undefined") return;
return useObject("channels", id, context) as return useObject("channels", id, context) as

View file

@ -9,8 +9,8 @@ import {
action, action,
extendObservable, extendObservable,
} from "mobx"; } from "mobx";
import { Attachment, Users } from "revolt.js/dist/api/objects"; import { Attachment, Channels, Users } from "revolt.js/dist/api/objects";
import { RemoveUserField } from "revolt.js/dist/api/routes"; import { RemoveChannelField, RemoveUserField } from "revolt.js/dist/api/routes";
import { ClientboundNotification } from "revolt.js/dist/websocket/notifications"; import { ClientboundNotification } from "revolt.js/dist/websocket/notifications";
type Nullable<T> = T | null; type Nullable<T> = T | null;
@ -42,7 +42,7 @@ export class User {
} }
@action update(data: Partial<Users.User>, clear?: RemoveUserField) { @action update(data: Partial<Users.User>, clear?: RemoveUserField) {
const apply = (key: keyof Users.User) => { const apply = (key: string) => {
// This code has been tested. // This code has been tested.
// @ts-expect-error // @ts-expect-error
if (data[key] && !isEqual(this[key], data[key])) { if (data[key] && !isEqual(this[key], data[key])) {
@ -62,6 +62,7 @@ export class User {
} }
} }
apply("username");
apply("avatar"); apply("avatar");
apply("badges"); apply("badges");
apply("status"); apply("status");
@ -70,8 +71,110 @@ export class User {
} }
} }
export class Channel {
_id: string;
type: Channels.Channel["channel_type"];
// Direct Message
active: Nullable<boolean> = null;
// Group
owner: Nullable<string> = null;
// Server
server: Nullable<string> = null;
// Permissions
permissions: Nullable<number> = null;
default_permissions: Nullable<number> = null;
role_permissions: Nullable<{ [key: string]: number }> = null;
// Common
name: Nullable<string> = null;
icon: Nullable<Attachment> = null;
description: Nullable<string> = null;
recipients: Nullable<string[]> = null;
last_message: Nullable<string | Channels.LastMessage> = null;
constructor(data: Channels.Channel) {
this._id = data._id;
this.type = data.channel_type;
switch (data.channel_type) {
case "DirectMessage": {
this.active = toNullable(data.active);
this.recipients = toNullable(data.recipients);
this.last_message = toNullable(data.last_message);
break;
}
case "Group": {
this.recipients = toNullable(data.recipients);
this.name = toNullable(data.name);
this.owner = toNullable(data.owner);
this.description = toNullable(data.description);
this.last_message = toNullable(data.last_message);
this.icon = toNullable(data.icon);
this.permissions = toNullable(data.permissions);
break;
}
case "TextChannel":
case "VoiceChannel": {
this.server = toNullable(data.server);
this.name = toNullable(data.name);
this.description = toNullable(data.description);
this.icon = toNullable(data.icon);
this.default_permissions = toNullable(data.default_permissions);
this.role_permissions = toNullable(data.role_permissions);
if (data.channel_type === "TextChannel") {
this.last_message = toNullable(data.last_message);
}
break;
}
}
makeAutoObservable(this);
}
@action update(
data: Partial<Channels.Channel>,
clear?: RemoveChannelField,
) {
const apply = (key: string) => {
// This code has been tested.
// @ts-expect-error
if (data[key] && !isEqual(this[key], data[key])) {
// @ts-expect-error
this[key] = data[key];
}
};
switch (clear) {
case "Description":
this.description = null;
break;
case "Icon":
this.icon = null;
break;
}
apply("active");
apply("owner");
apply("permissions");
apply("default_permissions");
apply("role_permissions");
apply("name");
apply("icon");
apply("description");
apply("recipients");
apply("last_message");
}
}
export class DataStore { export class DataStore {
@observable users = new Map<string, User>(); @observable users = new Map<string, User>();
@observable channels = new Map<string, Channel>();
constructor() { constructor() {
makeAutoObservable(this); makeAutoObservable(this);
@ -84,6 +187,11 @@ export class DataStore {
for (let user of packet.users) { for (let user of packet.users) {
this.users.set(user._id, new User(user)); this.users.set(user._id, new User(user));
} }
for (let channel of packet.channels) {
this.channels.set(channel._id, new Channel(channel));
}
break; break;
} }
case "UserUpdate": { case "UserUpdate": {

View file

@ -9,11 +9,6 @@ import {
ClientStatus, ClientStatus,
StatusContext, StatusContext,
} from "../context/revoltjs/RevoltClient"; } from "../context/revoltjs/RevoltClient";
import {
useChannels,
useForceUpdate,
useUser,
} from "../context/revoltjs/hooks";
import Header from "../components/ui/Header"; import Header from "../components/ui/Header";
@ -32,13 +27,9 @@ export default function Open() {
); );
} }
const ctx = useForceUpdate();
const channels = useChannels(undefined, ctx);
const user = useUser(id, ctx);
useEffect(() => { useEffect(() => {
if (id === "saved") { if (id === "saved") {
for (const channel of channels) { for (const channel of client.channels.toArray()) {
if (channel?.channel_type === "SavedMessages") { if (channel?.channel_type === "SavedMessages") {
history.push(`/channel/${channel._id}`); history.push(`/channel/${channel._id}`);
return; return;
@ -53,8 +44,11 @@ export default function Open() {
return; return;
} }
let user = client.users.get(id);
if (user) { if (user) {
const channel: string | undefined = channels.find( const channel: string | undefined = client.channels
.toArray()
.find(
(channel) => (channel) =>
channel?.channel_type === "DirectMessage" && channel?.channel_type === "DirectMessage" &&
channel.recipients.includes(id), channel.recipients.includes(id),

View file

@ -9,11 +9,7 @@ import { useEffect, useState } from "preact/hooks";
import { useData } from "../../../mobx/State"; import { useData } from "../../../mobx/State";
import { useClient } from "../../../context/revoltjs/RevoltClient"; import { useClient } from "../../../context/revoltjs/RevoltClient";
import { import { useChannels, useForceUpdate } from "../../../context/revoltjs/hooks";
useChannels,
useForceUpdate,
useUsers,
} from "../../../context/revoltjs/hooks";
import { getChannelName } from "../../../context/revoltjs/util"; import { getChannelName } from "../../../context/revoltjs/util";
import UserIcon from "../../../components/common/user/UserIcon"; import UserIcon from "../../../components/common/user/UserIcon";
@ -34,7 +30,6 @@ export const Invites = observer(({ server }: Props) => {
const channels = useChannels(invites?.map((x) => x.channel) ?? [], ctx); const channels = useChannels(invites?.map((x) => x.channel) ?? [], ctx);
const store = useData(); const store = useData();
const client = useClient();
const users = invites?.map((invite) => store.users.get(invite.creator)); const users = invites?.map((invite) => store.users.get(invite.creator));
useEffect(() => { useEffect(() => {

View file

@ -10,7 +10,6 @@ import { useEffect, useState } from "preact/hooks";
import { useData } from "../../../mobx/State"; import { useData } from "../../../mobx/State";
import { useClient } from "../../../context/revoltjs/RevoltClient"; import { useClient } from "../../../context/revoltjs/RevoltClient";
import { useForceUpdate, useUsers } from "../../../context/revoltjs/hooks";
import UserIcon from "../../../components/common/user/UserIcon"; import UserIcon from "../../../components/common/user/UserIcon";
import Button from "../../../components/ui/Button"; import Button from "../../../components/ui/Button";