Skip to content

Front End Data Structures

dsims01 edited this page Jan 29, 2019 · 6 revisions

The admin and index front-ends both have to save and update a conversation state. This page will document the data structure that will save that state.

Data Structures

For a given message, we store the role of the sender (user vs admin), the contents of the message, and the time at which it was sent:

messageObject : { 
    role: 'admin' | 'user',
    message: string, 
    timestamp: Date 
}

The user front-end contains one chat, so it is represented by an array of message objects, a string to identify the user, a boolean to represent whether the admin has accepted the chat, as well as a boolean to represent whether the stream is active or not. We call this object the messageStream:

messageStream: {
    messages: [messageObject],
    userId: string,
    accepted: boolean,
    active: boolean,
    typing: boolean,
    icon: string,
    alert: boolean,
    currentMessage: string
}

The admin front-end can contain multiple chats:

chats: [messageStream]

Interface

To create a new chat with a given user, we use the function newChat. Given a user identifier (of type string), this function creates a new chat with the given icon for that user if the identifier is unique or and logs an error if it is a duplicate user

function newChat(userId, icon)

To create a message object, we use the function createMessage. Given a role and a message string, this function appends creates a new messageObject that can be sent to addMessage.

function createMessage(role, messageString) 

To append a message to a specific chat, we use the function addMessage. Given a user identifier (of type string) and a messageObject, this function appends the message object to that user's chat if it exists or logs an error if that user chat doesn't exist

function addMessage(userId, messageObject) 

If the user has disconnected and we want to deactivate a chat, we use the function disconnectChat. Given a userId, this function changes the active state of the chat with that user to false.

function disconnectChat(userId)

If the admin has accepted a chat , we use the function acceptChat. Given a userId, this function changes the accepted state of the chat with that user to true.

function acceptChat(userId)

Example Usage

// Creates new chat with a user called 'chat1'
newChat('chat1');

// Creating a new chat with a duplicate user name will result in an error
newChat('chat1');

// Adds a message saying 'hello' from the 'chat1' user to the chat 
messageObject = createMessage('user', 'hello');
addMessage('chat1', messageObject);

// Adding a message to a chat that does not exists will also result in an error
addMessage('nonexistentChat', messageObject);
Clone this wiki locally