Add ID_RESPONSE Message, to let users know their authorID

This value should be provided when sending messages, but will not be
trusted by the server.
This commit is contained in:
Tobias Berger 2022-01-08 17:22:17 +01:00
parent 4e626c8fdf
commit 4c698768de

View file

@ -1,6 +1,7 @@
export enum MessageType { export enum MessageType {
ACK = 0, ACK = 0,
TEXT = 1, TEXT = 1,
ID_RESPONSE = 3,
} }
export type ServerMessage = { export type ServerMessage = {
@ -71,3 +72,18 @@ export type AckMessage = ServerMessage & {
export function isAckMessage(obj: unknown): obj is AckMessage { export function isAckMessage(obj: unknown): obj is AckMessage {
return isServerMessage(obj, MessageType.ACK); 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;
}