From f5c691f37b2ea43e566373995181436d1b6b9dbe Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 24 Mar 2024 20:27:56 +0100 Subject: [PATCH] wip: unbork init progress --- client/js/chan.ts | 34 +++++++++++++++++++++++++++++++++ client/js/store.ts | 28 +-------------------------- client/js/types.d.ts | 7 +++++-- server/models/msg.ts | 2 +- server/server.ts | 6 +++--- shared/types/msg.ts | 14 +++++--------- shared/types/socket-events.d.ts | 4 ++-- 7 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 client/js/chan.ts diff --git a/client/js/chan.ts b/client/js/chan.ts new file mode 100644 index 00000000..00e87efd --- /dev/null +++ b/client/js/chan.ts @@ -0,0 +1,34 @@ +import {ClientChan, ClientMessage} from "./types"; +import {SharedNetworkChan} from "../../shared/types/network"; +import {SharedMsg} from "../../shared/types/msg"; + +export function toClientChan(shared: SharedNetworkChan): ClientChan { + const history: string[] = [""].concat( + shared.messages + .filter((m) => m.self && m.text && m.type === "message") + // TS is too stupid to see the nil guard on filter... so we monkey patch it + .map((m): string => (m.text ? m.text : "")) + .reverse() + .slice(0, 99) + ); + // filter the unused vars + const {messages, totalMessages: __, ...props} = shared; + const channel: ClientChan = { + ...props, + editTopic: false, + pendingMessage: "", + inputHistoryPosition: 0, + historyLoading: false, + scrolledToBottom: true, + usersOutdated: shared.type === "channel" ? true : false, + moreHistoryAvailable: shared.totalMessages > shared.messages.length, + inputHistory: history, + messages: sharedMsgToClientMsg(messages), + }; + return channel; +} + +function sharedMsgToClientMsg(shared: SharedMsg[]): ClientMessage[] { + // TODO: this is a stub for now, we will want to populate client specific stuff here + return shared; +} diff --git a/client/js/store.ts b/client/js/store.ts index 21887327..fa268729 100644 --- a/client/js/store.ts +++ b/client/js/store.ts @@ -3,7 +3,7 @@ import {ActionContext, createStore, Store, useStore as baseUseStore} from "vuex"; import {createSettingsStore} from "./store-settings"; import storage from "./localStorage"; -import type {ClientChan, ClientNetwork, InitClientChan, NetChan, ClientMention} from "./types"; +import type {ClientChan, ClientNetwork, NetChan, ClientMention} from "./types"; import type {InjectionKey} from "vue"; import {SettingsState} from "./settings"; @@ -125,7 +125,6 @@ type Getters = { findNetwork: (state: State) => (uuid: string) => ClientNetwork | null; highlightCount(state: State): number; title(state: State, getters: Omit): string; - initChannel: () => (channel: InitClientChan) => ClientChan; }; // getters without the state argument @@ -196,31 +195,6 @@ const getters: Getters = { return alertEventCount + channelname + appName; }, - initChannel: () => (channel: InitClientChan) => { - // TODO: This should be a mutation - channel.pendingMessage = ""; - channel.inputHistoryPosition = 0; - - channel.inputHistory = [""].concat( - channel.messages - .filter((m) => m.self && m.text && m.type === "message") - .map((m) => m.text) - .reverse() - .slice(0, 99) - ); - channel.historyLoading = false; - channel.scrolledToBottom = true; - channel.editTopic = false; - - channel.moreHistoryAvailable = channel.totalMessages! > channel.messages.length; - delete channel.totalMessages; - - if (channel.type === "channel") { - channel.usersOutdated = true; - } - - return channel as ClientChan; - }, }; type Mutations = { diff --git a/client/js/types.d.ts b/client/js/types.d.ts index e69fbc86..017263ff 100644 --- a/client/js/types.d.ts +++ b/client/js/types.d.ts @@ -5,7 +5,7 @@ import {SharedNetwork} from "../../shared/types/network"; import {SharedUser} from "../../shared/types/user"; import {SharedMention} from "../../shared/models/mention"; import {SharedConfiguration, LockedSharedConfiguration} from "../../shared/types/config"; -import {LinkPreview} from "../../shared/types/msg"; +import {LinkPreview, ClientMessage} from "../../shared/types/msg"; interface LoungeWindow extends Window { g_TheLoungeRemoveLoading?: () => void; @@ -17,10 +17,13 @@ interface LoungeWindow extends Window { type ClientUser = SharedUser; +// we will eventually need to put client specific fields here +// which are not shared with the server +export type ClientMessage = SharedMsg; + type ClientChan = Omit & { moreHistoryAvailable: boolean; editTopic: boolean; - users: ClientUser[]; messages: ClientMessage[]; // these are added in store/initChannel diff --git a/server/models/msg.ts b/server/models/msg.ts index 06420d5c..8d670b6d 100644 --- a/server/models/msg.ts +++ b/server/models/msg.ts @@ -34,7 +34,7 @@ class Msg { raw_modes!: any; when!: Date; whois!: any; - users!: UserInMessage[] | string[]; + users!: string[]; statusmsgGroup!: string; params!: string[]; diff --git a/server/server.ts b/server/server.ts index a009bfb8..6bf23f23 100644 --- a/server/server.ts +++ b/server/server.ts @@ -840,7 +840,7 @@ function initializeClient( // socket.join is a promise depending on the adapter. void socket.join(client.id); - const sendInitEvent = (tokenToSend: string | null) => { + const sendInitEvent = (tokenToSend?: string) => { socket.emit("init", { active: openChannel, networks: client.networks.map((network) => @@ -852,7 +852,7 @@ function initializeClient( }; if (Config.values.public) { - sendInitEvent(null); + sendInitEvent(); } else if (!token) { client.generateToken((newToken) => { token = client.calculateTokenHash(newToken); @@ -863,7 +863,7 @@ function initializeClient( }); } else { client.updateSession(token, getClientIp(socket), socket.request); - sendInitEvent(null); + sendInitEvent(); } } diff --git a/shared/types/msg.ts b/shared/types/msg.ts index a0fc41be..45dcf24e 100644 --- a/shared/types/msg.ts +++ b/shared/types/msg.ts @@ -61,12 +61,12 @@ export type LinkPreview = { export type SharedMsg = { from?: UserInMessage; - id?: number; + id: number; previews?: LinkPreview[]; text?: string; type?: MessageType; self?: boolean; - time?: Date; + time: Date; hostmask?: string; target?: UserInMessage; // TODO: new_nick is only on MessageType.NICK, @@ -92,13 +92,9 @@ export type SharedMsg = { raw_modes?: any; when?: Date; whois?: any; - users?: UserInMessage[] | string[]; + + users: string[]; + statusmsgGroup?: string; params?: string[]; }; - -export type ClientMessage = Omit & { - time: Date; - users: string[]; - id: number; -}; diff --git a/shared/types/socket-events.d.ts b/shared/types/socket-events.d.ts index d4ebeca0..8f6fc3e6 100644 --- a/shared/types/socket-events.d.ts +++ b/shared/types/socket-events.d.ts @@ -81,9 +81,9 @@ interface ServerToClientEvents { "msg:preview": EventHandler<{id: number; chan: number; preview: LinkPreview}>; "msg:special": EventHandler<{chan: number; data?: Record}>; - msg: EventHandler<{msg: ClientMessage; chan: number; highlight?: number; unread?: number}>; + msg: EventHandler<{msg: SharedMsg; chan: number; highlight?: number; unread?: number}>; - init: EventHandler<{active: number; networks: SharedNetwork[]; token: string}>; + init: EventHandler<{active: number; networks: SharedNetwork[]; token?: string}>; "search:results": (response: SearchResponse) => void;