mirror of https://github.com/msgbyte/tailchat
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.
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
3 years ago
|
import type {
|
||
|
ChatMessage,
|
||
|
SendMessagePayload,
|
||
|
SimpleMessagePayload,
|
||
|
} from '../model/message';
|
||
|
import _isNil from 'lodash/isNil';
|
||
|
import _set from 'lodash/set';
|
||
3 years ago
|
import _get from 'lodash/get';
|
||
3 years ago
|
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 };
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 判断消息体内是否有回复信息
|
||
|
*/
|
||
|
hasReply(): ReplyMsgType | false {
|
||
|
const reply = _get(this.payload, ['meta', 'reply']);
|
||
|
if (_isNil(reply)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return reply;
|
||
|
}
|
||
|
|
||
3 years ago
|
setReplyMsg(replyMsg: ReplyMsgType) {
|
||
|
if (_isNil(replyMsg)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
_set(this.payload, ['meta', 'reply'], _pick(replyMsg, replyMsgFields));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 生成待发送的消息体
|
||
|
*/
|
||
|
generatePayload(): SendMessagePayload {
|
||
|
return { ...this.payload };
|
||
|
}
|
||
|
}
|