Chore: Migrate methods/getThreadName to typescript (#3707)

* chore: change getThreadName to typescript

* chore: change types after merge develop into current

* chore: minor tweak
This commit is contained in:
Alex Junior 2022-02-22 13:01:35 -03:00 committed by GitHub
parent c5cc192dc9
commit cbfca132c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 26 deletions

View File

@ -1,5 +1,5 @@
export interface IAttachment {
ts: Date;
ts: string | Date;
title: string;
type: string;
description: string;

View File

@ -63,7 +63,7 @@ export interface IMessage {
_id: string;
rid: string;
msg?: string;
id?: string;
id: string;
t?: MessageType;
ts: string | Date;
u: IUserMessage;
@ -74,7 +74,7 @@ export interface IMessage {
emoji?: string;
attachments?: IAttachment[];
urls?: IUrl[];
_updatedAt: Date;
_updatedAt: string | Date;
status?: number;
pinned?: boolean;
starred?: boolean;
@ -83,10 +83,10 @@ export interface IMessage {
role?: string;
drid?: string;
dcount?: number;
dlm?: Date;
dlm?: string | Date;
tmid?: string;
tcount?: number;
tlm?: Date;
tlm?: string | Date;
replies?: string[];
mentions?: IUserMention[];
channels?: IUserChannel[];

View File

@ -31,7 +31,7 @@ export interface ISubscription {
v?: IVisitor;
f: boolean;
t: SubscriptionType;
ts: Date;
ts: string | Date;
ls: Date;
name: string;
fname?: string;

View File

@ -13,6 +13,7 @@ interface IFileThread {
}
export interface IThreadResult {
id: string;
_id: string;
rid: string;
ts: string | Date;
@ -23,13 +24,13 @@ export interface IThreadResult {
attachments?: IAttachment[];
md?: MarkdownAST;
u: IUserMessage;
_updatedAt: Date;
_updatedAt: string | Date;
urls?: IUrl[];
mentions?: IUserMention[];
channels?: IUserChannel[];
replies?: string[];
tcount?: number;
tlm?: Date;
tlm?: string | Date;
}
export interface IThread {
@ -38,7 +39,7 @@ export interface IThread {
msg?: string;
t?: MessageType;
rid: string;
_updatedAt?: Date;
_updatedAt?: string | Date;
ts?: string | Date;
u?: IUserMessage;
alias?: string;
@ -56,10 +57,10 @@ export interface IThread {
role?: string;
drid?: string;
dcount?: number | string;
dlm?: number;
dlm?: string | Date;
tmid?: string;
tcount?: number | string;
tlm?: string;
tlm?: string | Date;
replies?: string[];
mentions?: IUserMention[];
channels?: IUserChannel[];

View File

@ -6,6 +6,7 @@ import { IReaction } from './IReaction';
import { IUrl } from './IUrl';
export interface IThreadMessage {
id: string;
_id: string;
tmsg?: string;
msg?: string;
@ -20,7 +21,7 @@ export interface IThreadMessage {
emoji?: string;
attachments?: IAttachment[];
urls?: IUrl[];
_updatedAt?: Date;
_updatedAt?: string | Date;
status?: number;
pinned?: boolean;
starred?: boolean;
@ -29,10 +30,10 @@ export interface IThreadMessage {
role?: string;
drid?: string;
dcount?: number;
dlm?: Date;
dlm?: string | Date;
tmid?: string;
tcount?: number;
tlm?: Date;
tlm?: string | Date;
replies?: string[];
mentions?: IUserMention[];
channels?: IUserChannel[];

View File

@ -443,7 +443,7 @@ class Encryption {
};
// Decrypt a message
decryptMessage = async (message: Partial<IMessage>) => {
decryptMessage = async (message: Pick<IMessage, 't' | 'e2e' | 'rid' | 'msg' | 'tmsg'>) => {
const { t, e2e } = message;
// Prevent create a new instance if this room was encrypted sometime ago

View File

@ -1,6 +1,7 @@
import RocketChat from '../rocketchat';
import { IMessage } from '../../definitions';
const getSingleMessage = (messageId: string) =>
const getSingleMessage = (messageId: string): Promise<IMessage> =>
new Promise(async (resolve, reject) => {
try {
const result = await RocketChat.getSingleMessage(messageId);

View File

@ -6,11 +6,12 @@ import { getThreadById } from '../database/services/Thread';
import log from '../../utils/log';
import { Encryption } from '../encryption';
import getSingleMessage from './getSingleMessage';
import { IThread, TThreadModel } from '../../definitions';
const buildThreadName = thread => thread.msg || thread?.attachments?.[0]?.title;
const buildThreadName = (thread: IThread): string | undefined => thread.msg || thread?.attachments?.[0]?.title;
const getThreadName = async (rid, tmid, messageId) => {
let tmsg;
const getThreadName = async (rid: string, tmid: string, messageId: string): Promise<string | undefined> => {
let tmsg: string | undefined;
try {
const db = database.active;
const threadCollection = db.get('threads');
@ -18,7 +19,7 @@ const getThreadName = async (rid, tmid, messageId) => {
const threadRecord = await getThreadById(tmid);
if (threadRecord) {
tmsg = buildThreadName(threadRecord);
await db.action(async () => {
await db.write(async () => {
await messageRecord?.update(m => {
m.tmsg = tmsg;
});
@ -27,11 +28,11 @@ const getThreadName = async (rid, tmid, messageId) => {
let thread = await getSingleMessage(tmid);
thread = await Encryption.decryptMessage(thread);
tmsg = buildThreadName(thread);
await db.action(async () => {
await db.write(async () => {
await db.batch(
threadCollection?.prepareCreate(t => {
threadCollection?.prepareCreate((t: TThreadModel) => {
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
t.subscription.id = rid;
if (t.subscription) t.subscription.id = rid;
Object.assign(t, thread);
}),
messageRecord?.prepareUpdate(m => {

View File

@ -105,9 +105,7 @@ export default async function updateMessages({
threadCollection.prepareCreate(
protectedFunction((t: TThreadModel) => {
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
if (t.subscription) {
t.subscription.id = sub.id;
}
if (t.subscription) t.subscription.id = sub.id;
Object.assign(t, thread);
})
)