Message author as id instead of name
Also add optional MessageType as parameter for isServerMessage
This commit is contained in:
parent
9fc4e8f80d
commit
7746e3ebdd
1 changed files with 14 additions and 9 deletions
|
@ -7,13 +7,21 @@ export type ServerMessage = {
|
||||||
type: MessageType;
|
type: MessageType;
|
||||||
date: number;
|
date: number;
|
||||||
};
|
};
|
||||||
export function isServerMessage(obj: unknown): obj is ServerMessage {
|
export function isServerMessage(
|
||||||
|
obj: unknown,
|
||||||
|
type?: MessageType
|
||||||
|
): obj is ServerMessage {
|
||||||
if (typeof obj !== "object") return false;
|
if (typeof obj !== "object") return false;
|
||||||
if (obj === null) return false;
|
if (obj === null) return false;
|
||||||
if (
|
if (
|
||||||
!Object.hasOwnProperty.call(obj, "type") ||
|
!Object.hasOwnProperty.call(obj, "type") ||
|
||||||
typeof (obj as { type: unknown }).type !== "number" ||
|
typeof (obj as { type: unknown }).type !== "number" ||
|
||||||
!Object.hasOwnProperty.call(MessageType, (obj as { type: number }).type)
|
(type === undefined &&
|
||||||
|
!Object.hasOwnProperty.call(
|
||||||
|
MessageType,
|
||||||
|
(obj as { type: number }).type
|
||||||
|
)) ||
|
||||||
|
(type !== undefined && (obj as { type: number }).type !== type)
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -29,15 +37,14 @@ export function isServerMessage(obj: unknown): obj is ServerMessage {
|
||||||
|
|
||||||
export type TextMessage = ServerMessage & {
|
export type TextMessage = ServerMessage & {
|
||||||
type: MessageType.TEXT;
|
type: MessageType.TEXT;
|
||||||
author: string;
|
author: number;
|
||||||
content: string;
|
content: string;
|
||||||
};
|
};
|
||||||
export function isTextMessage(obj: unknown): obj is TextMessage {
|
export function isTextMessage(obj: unknown): obj is TextMessage {
|
||||||
if (!isServerMessage(obj)) return false;
|
if (!isServerMessage(obj, MessageType.TEXT)) return false;
|
||||||
if (obj.type !== MessageType.TEXT) return false;
|
|
||||||
if (
|
if (
|
||||||
!Object.hasOwnProperty.call(obj, "author") ||
|
!Object.hasOwnProperty.call(obj, "author") ||
|
||||||
typeof (obj as ServerMessage & { author: unknown }).author !== "string"
|
typeof (obj as ServerMessage & { author: unknown }).author !== "number"
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +61,5 @@ export type AckMessage = ServerMessage & {
|
||||||
type: MessageType.ACK;
|
type: MessageType.ACK;
|
||||||
};
|
};
|
||||||
export function isAckMessage(obj: unknown): obj is AckMessage {
|
export function isAckMessage(obj: unknown): obj is AckMessage {
|
||||||
if (!isServerMessage(obj)) return false;
|
return isServerMessage(obj, MessageType.ACK);
|
||||||
if (obj.type !== MessageType.ACK) return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue