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;
|
||||
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 (obj === null) return false;
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "type") ||
|
||||
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;
|
||||
}
|
||||
|
@ -29,15 +37,14 @@ export function isServerMessage(obj: unknown): obj is ServerMessage {
|
|||
|
||||
export type TextMessage = ServerMessage & {
|
||||
type: MessageType.TEXT;
|
||||
author: string;
|
||||
author: number;
|
||||
content: string;
|
||||
};
|
||||
export function isTextMessage(obj: unknown): obj is TextMessage {
|
||||
if (!isServerMessage(obj)) return false;
|
||||
if (obj.type !== MessageType.TEXT) return false;
|
||||
if (!isServerMessage(obj, MessageType.TEXT)) return false;
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "author") ||
|
||||
typeof (obj as ServerMessage & { author: unknown }).author !== "string"
|
||||
typeof (obj as ServerMessage & { author: unknown }).author !== "number"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
@ -54,7 +61,5 @@ export type AckMessage = ServerMessage & {
|
|||
type: MessageType.ACK;
|
||||
};
|
||||
export function isAckMessage(obj: unknown): obj is AckMessage {
|
||||
if (!isServerMessage(obj)) return false;
|
||||
if (obj.type !== MessageType.ACK) return false;
|
||||
return true;
|
||||
return isServerMessage(obj, MessageType.ACK);
|
||||
}
|
||||
|
|
Reference in a new issue