parser inside the model

This commit is contained in:
Reinaldo Neto 2023-02-02 12:34:04 -03:00
parent eb9f973e47
commit 4ea8aabe6b
4 changed files with 48 additions and 45 deletions

View File

@ -147,7 +147,10 @@ export interface IMessage extends IMessageFromServer {
editedAt?: string | Date; editedAt?: string | Date;
} }
export type TMessageModel = IMessage & Model; export type TMessageModel = IMessage &
Model & {
asPlain?: () => IMessage;
};
export type TAnyMessageModel = TMessageModel | TThreadModel | TThreadMessageModel; export type TAnyMessageModel = TMessageModel | TThreadModel | TThreadMessageModel;
export type TTypeMessages = IMessageFromServer | ILoadMoreMessage | IMessage; export type TTypeMessages = IMessageFromServer | ILoadMoreMessage | IMessage;

View File

@ -85,4 +85,47 @@ export default class Message extends Model {
@json('md', sanitizer) md; @json('md', sanitizer) md;
@field('comment') comment; @field('comment') comment;
asPlain() {
return {
id: this.id,
rid: this.subscription.id,
msg: this.msg,
t: this.t,
ts: this.ts,
u: this.u,
alias: this.alias,
parseUrls: this.parseUrls,
groupable: this.groupable,
avatar: this.avatar,
emoji: this.emoji,
attachments: this.attachments,
urls: this.urls,
_updatedAt: this._updatedAt,
status: this.status,
pinned: this.pinned,
starred: this.starred,
editedBy: this.editedBy,
reactions: this.reactions,
role: this.role,
drid: this.drid,
dcount: this.dcount,
dlm: this.dlm,
tmid: this.tmid,
tcount: this.tcount,
tlm: this.tlm,
replies: this.replies,
mentions: this.mentions,
channels: this.channels,
unread: this.unread,
autoTranslate: this.autoTranslate,
translations: this.translations,
tmsg: this.tmsg,
blocks: this.blocks,
e2e: this.e2e,
tshow: this.tshow,
md: this.md,
comment: this.comment
};
}
} }

View File

@ -1,8 +1,7 @@
import { TMessageModel } from '../../../definitions'; import { TMessageModel } from '../../../definitions';
import { parseModelMessageToMessage } from './parseModelMessageToMessage';
export const mapMessageFromDB = (messageModel: TMessageModel) => { export const mapMessageFromDB = (messageModel: TMessageModel) => {
const parsedMessage = parseModelMessageToMessage(messageModel); const parsedMessage = messageModel.asPlain!();
return { return {
...parsedMessage, ...parsedMessage,
ts: new Date(parsedMessage.ts), ts: new Date(parsedMessage.ts),

View File

@ -1,42 +0,0 @@
import { IMessage, TMessageModel } from '../../../definitions';
export const parseModelMessageToMessage = (messageModel: TMessageModel): IMessage =>
({
msg: messageModel.msg,
t: messageModel.t,
ts: messageModel.ts,
u: messageModel.u,
subscription: messageModel.subscription,
alias: messageModel.alias,
parseUrls: messageModel.parseUrls,
groupable: messageModel.groupable,
avatar: messageModel.avatar,
emoji: messageModel.emoji,
attachments: messageModel.attachments,
urls: messageModel.urls,
_updatedAt: messageModel._updatedAt,
status: messageModel.status,
pinned: messageModel.pinned,
starred: messageModel.starred,
editedBy: messageModel.editedBy,
reactions: messageModel.reactions,
role: messageModel.role,
drid: messageModel.drid,
dcount: messageModel.dcount,
dlm: messageModel.dlm,
tmid: messageModel.tmid,
tcount: messageModel.tcount,
tlm: messageModel.tlm,
replies: messageModel.replies,
mentions: messageModel.mentions,
channels: messageModel.channels,
unread: messageModel.unread,
autoTranslate: messageModel.autoTranslate,
translations: messageModel.translations,
tmsg: messageModel.tmsg,
blocks: messageModel.blocks,
e2e: messageModel.e2e,
tshow: messageModel.tshow,
md: messageModel.md,
comment: messageModel.comment
} as IMessage);