You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/client/shared/utils/message-helper.ts

48 lines
1.0 KiB
TypeScript

import type {
ChatMessage,
SendMessagePayload,
SimpleMessagePayload,
} from '../model/message';
import _isNil from 'lodash/isNil';
import _set from 'lodash/set';
import _get from 'lodash/get';
import _pick from 'lodash/pick';
const replyMsgFields = ['_id', 'content', 'author'] as const;
export type ReplyMsgType = Pick<ChatMessage, typeof replyMsgFields[number]>;
export class MessageHelper {
private payload: SendMessagePayload;
constructor(origin: SimpleMessagePayload) {
this.payload = { ...origin };
}
/**
*
*/
hasReply(): ReplyMsgType | false {
const reply = _get(this.payload, ['meta', 'reply']);
if (_isNil(reply)) {
return false;
}
return reply;
}
setReplyMsg(replyMsg: ReplyMsgType) {
if (_isNil(replyMsg)) {
return;
}
_set(this.payload, ['meta', 'reply'], _pick(replyMsg, replyMsgFields));
}
/**
*
*/
generatePayload(): SendMessagePayload {
return { ...this.payload };
}
}