mime-chat/src/modules/message.ts
Andrew Gundersen feccda8ec9 message grouping
2021-03-13 11:59:37 -06:00

52 lines
No EOL
1.1 KiB
TypeScript

import { RenderMessage, ClientMessage } from "@/types/message/index";
import { v4 as uuidv4 } from 'uuid';
// Create a RenderMessage object directly from json string.
export function initRenderMessageFromString(messageStr: string) {
// Parse the string.
const m = JSON.parse(messageStr);
// Auto set uid and time.
const currentdate = new Date();
const message: RenderMessage = {
content: m.content,
context: m.context,
modifier: m.modifier,
time: currentdate.getTime(),
uid: uuidv4(),
isChild: "none"
}
return message
}
// Create a RenderMessage object.
export function initRenderMessage(text: string, audio: string, context: string, modifier: string) {
// Auto set uid and time.
const currentdate = new Date();
const message: RenderMessage = {
content: {
text: text,
audio: audio
},
context: context,
modifier: modifier,
time: currentdate.getTime(),
uid: uuidv4(),
isChild: "none"
}
return message
}
export const clientMessage = (text: string, audio: string | boolean, uid: string): ClientMessage => (
{
audio,
text,
uid
}
)