124 lines
3.1 KiB
TypeScript
124 lines
3.1 KiB
TypeScript
export enum MessageType {
|
|
ACK = 0,
|
|
TEXT = 1,
|
|
ID_RESPONSE = 3,
|
|
TYPING = 4,
|
|
CURRENTLY_TYPING = 5,
|
|
}
|
|
|
|
export type ServerMessage = {
|
|
__ctx?: string;
|
|
type: MessageType;
|
|
date: number;
|
|
};
|
|
export function isServerMessage(
|
|
obj: unknown,
|
|
type?: MessageType
|
|
): obj is ServerMessage {
|
|
if (typeof obj !== "object") return false;
|
|
if (obj === null) return false;
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "type") ||
|
|
typeof (obj as { type: unknown }).type !== "number" ||
|
|
(type === undefined &&
|
|
!Object.hasOwnProperty.call(
|
|
MessageType,
|
|
(obj as { type: number }).type
|
|
)) ||
|
|
(type !== undefined && (obj as { type: number }).type !== type)
|
|
) {
|
|
return false;
|
|
}
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "date") ||
|
|
typeof (obj as { date: unknown }).date !== "number"
|
|
) {
|
|
return false;
|
|
}
|
|
if (
|
|
Object.hasOwnProperty.call(obj, "__ctx") &&
|
|
typeof (obj as { __ctx: unknown }).__ctx !== "string" &&
|
|
typeof (obj as { __ctx: unknown }).__ctx !== "undefined"
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export type TextMessage = ServerMessage & {
|
|
type: MessageType.TEXT;
|
|
author: string;
|
|
content: string;
|
|
};
|
|
export function isTextMessage(obj: unknown): obj is TextMessage {
|
|
if (!isServerMessage(obj, MessageType.TEXT)) return false;
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "author") ||
|
|
typeof (obj as ServerMessage & { author: unknown }).author !== "string"
|
|
) {
|
|
return false;
|
|
}
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "author") ||
|
|
typeof (obj as ServerMessage & { content: unknown }).content !== "string"
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export type AckMessage = ServerMessage & {
|
|
type: MessageType.ACK;
|
|
};
|
|
export function isAckMessage(obj: unknown): obj is AckMessage {
|
|
return isServerMessage(obj, MessageType.ACK);
|
|
}
|
|
|
|
export type IdResponseMessage = ServerMessage & {
|
|
type: MessageType.ID_RESPONSE;
|
|
authorId: string;
|
|
};
|
|
export function isIdResponseMessage(obj: unknown): obj is IdResponseMessage {
|
|
if (!isServerMessage(obj, MessageType.ID_RESPONSE)) return false;
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "authorId") ||
|
|
typeof (obj as ServerMessage & { authorId: unknown }).authorId !== "string"
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export type TypingMessage = ServerMessage & {
|
|
type: MessageType.TYPING;
|
|
};
|
|
export function isTypingMessage(obj: unknown): obj is TypingMessage {
|
|
return isServerMessage(obj, MessageType.TYPING);
|
|
}
|
|
|
|
export type CurrentlyTypingMessage = ServerMessage & {
|
|
type: MessageType.CURRENTLY_TYPING;
|
|
currently: string[];
|
|
};
|
|
export function isCurrentlyTypingMessage(
|
|
obj: unknown
|
|
): obj is CurrentlyTypingMessage {
|
|
if (!isServerMessage(obj, MessageType.CURRENTLY_TYPING)) {
|
|
return false;
|
|
}
|
|
if (
|
|
!Object.hasOwnProperty.call(obj, "currently") ||
|
|
typeof obj !== "object" ||
|
|
!(
|
|
(obj as ServerMessage & { currently: unknown }).currently instanceof Array
|
|
) ||
|
|
Array.prototype.some.call(
|
|
(obj as ServerMessage & { currently: unknown[] }).currently,
|
|
(element) => typeof element !== "string"
|
|
)
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|