@ably/chat
Version:
Ably Chat is a set of purpose-built APIs for a host of chat features enabling you to create 1:1, 1:Many, Many:1 and Many:Many chat rooms for any scale. It is designed to meet a wide range of chat use cases, such as livestreams, in-game communication, cust
47 lines (39 loc) • 1.45 kB
text/typescript
import * as Ably from 'ably';
import { DefaultRoomReaction, RoomReaction, RoomReactionHeaders, RoomReactionMetadata } from './room-reaction.js';
interface ReactionPayload {
data?: {
name?: string;
metadata?: RoomReactionMetadata;
};
clientId?: string;
timestamp: number;
extras?: {
headers?: RoomReactionHeaders;
};
}
/**
* Parses a room reaction from an inbound message.
* @param message The inbound message containing the reaction data.
* @param clientId The client ID of the user.
* @returns The parsed room reaction.
*/
export const parseRoomReaction = (message: Ably.InboundMessage, clientId?: string): RoomReaction => {
const reactionCreatedMessage = message as ReactionPayload;
// Use empty string if type is missing or invalid
const name =
reactionCreatedMessage.data?.name && typeof reactionCreatedMessage.data.name === 'string'
? reactionCreatedMessage.data.name
: '';
// Use empty string if clientId is missing
const messageClientId = reactionCreatedMessage.clientId ?? '';
// Use current time if timestamp is missing
const timestamp = reactionCreatedMessage.timestamp ? new Date(reactionCreatedMessage.timestamp) : new Date();
return new DefaultRoomReaction(
name,
messageClientId,
timestamp,
clientId ? clientId === messageClientId : false,
reactionCreatedMessage.data?.metadata ?? {},
reactionCreatedMessage.extras?.headers ?? {},
);
};