2024-03-24 19:27:56 +00:00
|
|
|
import {ClientChan, ClientMessage} from "./types";
|
|
|
|
import {SharedNetworkChan} from "../../shared/types/network";
|
2024-05-02 06:21:51 +00:00
|
|
|
import {SharedMsg, MessageType} from "../../shared/types/msg";
|
|
|
|
import {ChanType} from "../../shared/types/chan";
|
2024-03-24 19:27:56 +00:00
|
|
|
|
|
|
|
export function toClientChan(shared: SharedNetworkChan): ClientChan {
|
|
|
|
const history: string[] = [""].concat(
|
|
|
|
shared.messages
|
2024-05-02 06:21:51 +00:00
|
|
|
.filter((m) => m.self && m.text && m.type === MessageType.MESSAGE)
|
2024-03-24 19:27:56 +00:00
|
|
|
// 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
|
2024-04-01 20:27:45 +00:00
|
|
|
const {messages, totalMessages: _, ...props} = shared;
|
2024-03-24 19:27:56 +00:00
|
|
|
const channel: ClientChan = {
|
|
|
|
...props,
|
|
|
|
editTopic: false,
|
|
|
|
pendingMessage: "",
|
|
|
|
inputHistoryPosition: 0,
|
|
|
|
historyLoading: false,
|
|
|
|
scrolledToBottom: true,
|
2024-04-07 16:04:08 +00:00
|
|
|
users: [],
|
2024-05-02 06:21:51 +00:00
|
|
|
usersOutdated: shared.type === ChanType.CHANNEL ? true : false,
|
2024-03-24 19:27:56 +00:00
|
|
|
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;
|
|
|
|
}
|