diff --git a/src/stores/GatewayConnectionStore.ts b/src/stores/GatewayConnectionStore.ts index c5f9d87..3d4cf6b 100644 --- a/src/stores/GatewayConnectionStore.ts +++ b/src/stores/GatewayConnectionStore.ts @@ -32,6 +32,7 @@ import { GatewayReceivePayload, GatewaySendPayload, GatewayTypingStartDispatchData, + GatewayUserUpdateDispatchData, PresenceUpdateStatus, Snowflake, } from "@spacebarchat/spacebar-api-types/v9"; @@ -151,6 +152,8 @@ export default class GatewayConnectionStore { this.dispatchHandlers.set(GatewayDispatchEvents.PresenceUpdate, this.onPresenceUpdate); this.dispatchHandlers.set(GatewayDispatchEvents.TypingStart, this.onTypingStart); + + this.dispatchHandlers.set(GatewayDispatchEvents.UserUpdate, this.onUserUpdate); } private onopen = () => { @@ -721,4 +724,8 @@ export default class GatewayConnectionStore { channel.typingIds.get(data.user_id)?.(); } }; + + private onUserUpdate = (data: GatewayUserUpdateDispatchData) => { + this.app.users.update(data); + }; } diff --git a/src/stores/UserStore.ts b/src/stores/UserStore.ts index 6227b94..c8a7b6e 100644 --- a/src/stores/UserStore.ts +++ b/src/stores/UserStore.ts @@ -1,4 +1,9 @@ -import { Routes, type APIUser, type Snowflake } from "@spacebarchat/spacebar-api-types/v9"; +import { + GatewayUserUpdateDispatchData, + Routes, + type APIUser, + type Snowflake, +} from "@spacebarchat/spacebar-api-types/v9"; import { ObservableMap, action, computed, observable } from "mobx"; import useLogger from "../hooks/useLogger"; import AppStore from "./AppStore"; @@ -22,6 +27,11 @@ export default class UserStore { users.forEach((user) => this.add(user)); } + @action + update(user: APIUser | GatewayUserUpdateDispatchData) { + this.users.get(user.id)?.update(user); + } + @action get(id: string) { return this.users.get(id); diff --git a/src/stores/objects/User.ts b/src/stores/objects/User.ts index cceb9f9..f8a3631 100644 --- a/src/stores/objects/User.ts +++ b/src/stores/objects/User.ts @@ -1,7 +1,7 @@ import { Snowflake } from "@spacebarchat/spacebar-api-types/globals"; -import type { APIUser, PublicUser } from "@spacebarchat/spacebar-api-types/v9"; +import type { APIUser, GatewayUserUpdateDispatchData, PublicUser } from "@spacebarchat/spacebar-api-types/v9"; import { CDNRoutes, DefaultUserAvatarAssets, ImageFormat } from "@spacebarchat/spacebar-api-types/v9"; -import { makeObservable, observable } from "mobx"; +import { action, makeObservable, observable } from "mobx"; import REST from "../../utils/REST"; export default class User { @@ -48,6 +48,11 @@ export default class User { makeObservable(this); } + @action + update(member: APIUser | GatewayUserUpdateDispatchData) { + Object.assign(this, member); + } + /** * Gets the users default avatar url * @returns The URL to the user's default avatar.