From 8a8f5bb67043d10b2f6a4c4d271519f4fdfea189 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 10 Feb 2022 12:35:33 -0400 Subject: [PATCH 01/13] migrate sendMessage to ts --- app/definitions/IMessage.ts | 2 ++ app/definitions/IRocketChat.ts | 3 ++- .../{sendMessage.js => sendMessage.ts} | 27 +++++++++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) rename app/lib/methods/{sendMessage.js => sendMessage.ts} (84%) diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index d4c9d32667b..3e1343ff77b 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -54,6 +54,8 @@ export interface ILastMessage { } export interface IMessage { + _id: string; + rid?: string; msg?: string; t?: SubscriptionType; ts: Date; diff --git a/app/definitions/IRocketChat.ts b/app/definitions/IRocketChat.ts index 199cf489112..91d701a5d15 100644 --- a/app/definitions/IRocketChat.ts +++ b/app/definitions/IRocketChat.ts @@ -4,6 +4,7 @@ type TRocketChat = typeof rocketchat; export interface IRocketChat extends TRocketChat { sdk: any; - activeUsersSubTimeout: any; + shareSDK: any; + activeUsersSubTimeout: any; roomsSub: any; } diff --git a/app/lib/methods/sendMessage.js b/app/lib/methods/sendMessage.ts similarity index 84% rename from app/lib/methods/sendMessage.js rename to app/lib/methods/sendMessage.ts index 1a1ff993482..2a35ce7ce49 100644 --- a/app/lib/methods/sendMessage.js +++ b/app/lib/methods/sendMessage.ts @@ -6,12 +6,15 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; +import { IMessage, IRocketChat, TMessageModel, TThreadMessageModel } from '../../definitions'; -const changeMessageStatus = async (id, tmid, status, message) => { +type TMessages = TMessageModel | TThreadMessageModel; + +const changeMessageStatus = async (id: string, tmid: string, status: number, message?: IMessage) => { const db = database.active; const msgCollection = db.get('messages'); const threadMessagesCollection = db.get('thread_messages'); - const successBatch = []; + const successBatch = [] as TMessages[]; const messageRecord = await msgCollection.find(id); successBatch.push( messageRecord.prepareUpdate(m => { @@ -37,7 +40,7 @@ const changeMessageStatus = async (id, tmid, status, message) => { } try { - await db.action(async () => { + await db.write(async () => { await db.batch(...successBatch); }); } catch (error) { @@ -45,30 +48,30 @@ const changeMessageStatus = async (id, tmid, status, message) => { } }; -export async function sendMessageCall(message) { +export async function sendMessageCall(this: IRocketChat, message: TMessageModel) { const { _id, tmid } = message; try { const sdk = this.shareSDK || this.sdk; // RC 0.60.0 const result = await sdk.post('chat.sendMessage', { message }); if (result.success) { - return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message); + return changeMessageStatus(_id, tmid as string, messagesStatus.SENT, result.message); } } catch { // do nothing } - return changeMessageStatus(_id, tmid, messagesStatus.ERROR); + return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); } -export async function resendMessage(message, tmid) { +export async function resendMessage(message: TMessageModel, tmid: string) { const db = database.active; try { - await db.action(async () => { + await db.write(async () => { await message.update(m => { m.status = messagesStatus.TEMP; }); }); - let m = { + let m: Partial = { _id: message.id, rid: message.subscription.id, msg: message.msg @@ -86,7 +89,7 @@ export async function resendMessage(message, tmid) { } } -export default async function (rid, msg, tmid, user, tshow) { +export default async function (rid: string, msg: string, tmid: string, user, tshow: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -147,7 +150,9 @@ export default async function (rid, msg, tmid, user, tshow) { batch.push( threadMessagesCollection.prepareCreate(tm => { tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema); - tm.subscription.id = rid; + if (tm.subscription) { + tm.subscription.id = rid; + } tm.rid = tmid; tm.msg = msg; tm.ts = messageDate; From abba54fba31d6fe3d7bd538b0d2c094b0d5cfea6 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 10 Feb 2022 16:01:12 -0400 Subject: [PATCH 02/13] update interfaces and sendMessage file --- app/definitions/IMessage.ts | 5 +++-- app/definitions/ISubscription.ts | 2 +- app/definitions/IThread.ts | 6 +++--- app/definitions/IThreadMessage.ts | 5 ++--- app/definitions/IUser.ts | 1 + app/lib/methods/sendMessage.ts | 36 ++++++++++++++++++++----------- 6 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index 3e1343ff77b..4fe2d5a4760 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -3,7 +3,8 @@ import { MarkdownAST } from '@rocket.chat/message-parser'; import { IAttachment } from './IAttachment'; import { IReaction } from './IReaction'; -import { SubscriptionType } from './ISubscription'; + +export type MessageType = 'jitsi_call_started' | 'discussion-created' | 'e2e' | 'load_more' | 'rm' | 'uj'; export interface IUserMessage { _id: string; @@ -57,7 +58,7 @@ export interface IMessage { _id: string; rid?: string; msg?: string; - t?: SubscriptionType; + t?: MessageType; ts: Date; u: IUserMessage; alias: string; diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 5f561edfb59..56979abc2d9 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -58,7 +58,7 @@ export interface ISubscription { ignored?: string[]; broadcast?: boolean; prid?: string; - draftMessage?: string; + draftMessage?: string | null; lastThreadSync?: Date; jitsiTimeout?: number; autoTranslate?: boolean; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index 3edefbcadf4..51981ca3bd9 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -2,9 +2,8 @@ import Model from '@nozbe/watermelondb/Model'; import { MarkdownAST } from '@rocket.chat/message-parser'; import { IAttachment } from './IAttachment'; -import { IEditedBy, IUserChannel, IUserMention, IUserMessage } from './IMessage'; +import { IEditedBy, IUserChannel, IUserMention, IUserMessage, MessageType } from './IMessage'; import { IReaction } from './IReaction'; -import { SubscriptionType } from './ISubscription'; export interface IUrl { title: string; @@ -42,7 +41,7 @@ export interface IThreadResult { export interface IThread { id: string; msg?: string; - t?: SubscriptionType; + t?: MessageType; rid: string; _updatedAt?: Date; ts?: Date; @@ -73,6 +72,7 @@ export interface IThread { autoTranslate?: boolean; translations?: any; e2e?: string; + subscription?: { id: string }; } export type TThreadModel = IThread & Model; diff --git a/app/definitions/IThreadMessage.ts b/app/definitions/IThreadMessage.ts index c773e4dc5c4..b391cb96bb0 100644 --- a/app/definitions/IThreadMessage.ts +++ b/app/definitions/IThreadMessage.ts @@ -1,13 +1,12 @@ import Model from '@nozbe/watermelondb/Model'; import { IAttachment } from './IAttachment'; -import { IEditedBy, ITranslations, IUserChannel, IUserMention, IUserMessage } from './IMessage'; +import { IEditedBy, ITranslations, IUserChannel, IUserMention, IUserMessage, MessageType } from './IMessage'; import { IReaction } from './IReaction'; -import { SubscriptionType } from './ISubscription'; export interface IThreadMessage { msg?: string; - t?: SubscriptionType; + t?: MessageType; rid: string; ts: Date; u: IUserMessage; diff --git a/app/definitions/IUser.ts b/app/definitions/IUser.ts index 012ef808714..901b7aaaa02 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -2,6 +2,7 @@ import Model from '@nozbe/watermelondb/Model'; export interface IUser { _id: string; + id: string; name?: string; username: string; avatarETag?: string; diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 2a35ce7ce49..5f2959ea743 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -6,7 +6,15 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; -import { IMessage, IRocketChat, TMessageModel, TThreadMessageModel } from '../../definitions'; +import { + IMessage, + IRocketChat, + IUser, + TMessageModel, + TSubscriptionModel, + TThreadMessageModel, + TThreadModel +} from '../../definitions'; type TMessages = TMessageModel | TThreadMessageModel; @@ -63,7 +71,7 @@ export async function sendMessageCall(this: IRocketChat, message: TMessageModel) return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); } -export async function resendMessage(message: TMessageModel, tmid: string) { +export async function resendMessage(this: IRocketChat, message: TMessageModel, tmid: string) { const db = database.active; try { await db.write(async () => { @@ -71,16 +79,16 @@ export async function resendMessage(message: TMessageModel, tmid: string) { m.status = messagesStatus.TEMP; }); }); - let m: Partial = { + let m = { _id: message.id, rid: message.subscription.id, msg: message.msg - }; + } as TMessageModel; if (tmid) { m = { ...m, tmid - }; + } as TMessageModel; } m = await Encryption.encryptMessage(m); await sendMessageCall.call(this, m); @@ -89,7 +97,7 @@ export async function resendMessage(message: TMessageModel, tmid: string) { } } -export default async function (rid: string, msg: string, tmid: string, user, tshow: boolean) { +export default async function (this: IRocketChat, rid: string, msg: string, tmid: string, user: IUser, tshow: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -97,7 +105,7 @@ export default async function (rid: string, msg: string, tmid: string, user, tsh const threadCollection = db.get('threads'); const threadMessagesCollection = db.get('thread_messages'); const messageId = random(17); - const batch = []; + const batch: (TMessageModel | TThreadMessageModel | TThreadModel | TSubscriptionModel)[] = []; let message = { _id: messageId, @@ -105,11 +113,11 @@ export default async function (rid: string, msg: string, tmid: string, user, tsh msg, tmid, tshow - }; + } as TMessageModel; message = await Encryption.encryptMessage(message); const messageDate = new Date(); - let tMessageRecord; + let tMessageRecord: TMessageModel; // If it's replying to a thread if (tmid) { @@ -119,7 +127,9 @@ export default async function (rid: string, msg: string, tmid: string, user, tsh batch.push( tMessageRecord.prepareUpdate(m => { m.tlm = messageDate; - m.tcount += 1; + if (m.tcount) { + m.tcount += 1; + } }) ); @@ -131,7 +141,9 @@ export default async function (rid: string, msg: string, tmid: string, user, tsh batch.push( threadCollection.prepareCreate(tm => { tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema); - tm.subscription.id = rid; + if (tm.subscription) { + tm.subscription.id = rid; + } tm.tmid = tmid; tm.msg = tMessageRecord.msg; tm.ts = tMessageRecord.ts; @@ -215,7 +227,7 @@ export default async function (rid: string, msg: string, tmid: string, user, tsh } try { - await db.action(async () => { + await db.write(async () => { await db.batch(...batch); }); } catch (e) { From 779c4a165003ad931db7e5cd62d4909b6e29ba0f Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 11 Feb 2022 10:07:22 -0400 Subject: [PATCH 03/13] Update sendMessage.ts --- app/lib/methods/sendMessage.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 5f2959ea743..e6429955623 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -71,7 +71,7 @@ export async function sendMessageCall(this: IRocketChat, message: TMessageModel) return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); } -export async function resendMessage(this: IRocketChat, message: TMessageModel, tmid: string) { +export async function resendMessage(message: TMessageModel, tmid: string) { const db = database.active; try { await db.write(async () => { @@ -91,13 +91,14 @@ export async function resendMessage(this: IRocketChat, message: TMessageModel, t } as TMessageModel; } m = await Encryption.encryptMessage(m); + // @ts-ignore await sendMessageCall.call(this, m); } catch (e) { log(e); } } -export default async function (this: IRocketChat, rid: string, msg: string, tmid: string, user: IUser, tshow: boolean) { +export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -234,7 +235,7 @@ export default async function (this: IRocketChat, rid: string, msg: string, tmid log(e); return; } - + // @ts-ignore await sendMessageCall.call(this, message); } catch (e) { log(e); From 01aacb705fd9e562e74f7dd0d22db850b8226590 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 11 Feb 2022 10:34:03 -0400 Subject: [PATCH 04/13] minor tweaks --- app/definitions/IUser.ts | 1 + app/lib/methods/sendMessage.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/definitions/IUser.ts b/app/definitions/IUser.ts index 901b7aaaa02..aa413b31d29 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -6,6 +6,7 @@ export interface IUser { name?: string; username: string; avatarETag?: string; + token: string; } export type TUserModel = IUser & Model; diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index e6429955623..1cef78f5272 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -98,7 +98,7 @@ export async function resendMessage(message: TMessageModel, tmid: string) { } } -export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow: boolean) { +export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); From 9aa871648491c3b31b21824f138d3794d2bb89de Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 11 Feb 2022 10:52:09 -0400 Subject: [PATCH 05/13] Update sendMessage.ts --- app/views/ShareView/index.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index af8b75b2744..aff1eff33c4 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -29,6 +29,7 @@ import Header from './Header'; import styles from './styles'; import { IAttachment } from './interfaces'; import { ISubscription } from '../../definitions/ISubscription'; +import { IUser } from '../../definitions'; interface IShareViewState { selected: IAttachment; @@ -239,7 +240,7 @@ class ShareView extends Component { // Send text message } else if (text.length) { - await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token }); + await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser); } } catch { // Do nothing From 2c6cef8475155e67cfeb99f94153664caf701f2f Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 11 Feb 2022 11:36:06 -0400 Subject: [PATCH 06/13] migrate getUsersPresence to ts --- ...etUsersPresence.js => getUsersPresence.ts} | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) rename app/lib/methods/{getUsersPresence.js => getUsersPresence.ts} (78%) diff --git a/app/lib/methods/getUsersPresence.js b/app/lib/methods/getUsersPresence.ts similarity index 78% rename from app/lib/methods/getUsersPresence.js rename to app/lib/methods/getUsersPresence.ts index f596e527adb..3bc93fd31a3 100644 --- a/app/lib/methods/getUsersPresence.js +++ b/app/lib/methods/getUsersPresence.ts @@ -1,14 +1,16 @@ import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { IActiveUsers } from '../../reducers/activeUsers'; import { compareServerVersion } from '../utils'; import { store as reduxStore } from '../auxStore'; import { setActiveUsers } from '../../actions/activeUsers'; import { setUser } from '../../actions/login'; import database from '../database'; +import { IRocketChat, IUser } from '../../definitions'; -export function subscribeUsersPresence() { - const serverVersion = reduxStore.getState().server.version; +export function subscribeUsersPresence(this: IRocketChat) { + const serverVersion = reduxStore.getState().server.version as string; // if server is lower than 1.1.0 if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) { @@ -29,10 +31,10 @@ export function subscribeUsersPresence() { this.sdk.subscribe('stream-notify-logged', 'Users:NameChanged'); } -let ids = []; +let ids: string[] = []; -export default async function getUsersPresence() { - const serverVersion = reduxStore.getState().server.version; +export default async function getUsersPresence(this: IRocketChat) { + const serverVersion = reduxStore.getState().server.version as string; const { user: loggedUser } = reduxStore.getState().login; // if server is greather than or equal 1.1.0 @@ -60,8 +62,8 @@ export default async function getUsersPresence() { if (result.success) { const { users } = result; - const activeUsers = ids.reduce((ret, id) => { - const user = users.find(u => u._id === id) ?? { _id: id, status: 'offline' }; + const activeUsers = ids.reduce((ret: IActiveUsers, id) => { + const user = users.find((u: IUser) => u._id === id) ?? { _id: id, status: 'offline' }; const { _id, status, statusText } = user; if (loggedUser && loggedUser.id === _id) { @@ -78,17 +80,17 @@ export default async function getUsersPresence() { const db = database.active; const userCollection = db.get('users'); - users.forEach(async user => { + users.forEach(async (user: IUser) => { try { const userRecord = await userCollection.find(user._id); - await db.action(async () => { + await db.write(async () => { await userRecord.update(u => { Object.assign(u, user); }); }); } catch (e) { // User not found - await db.action(async () => { + await db.write(async () => { await userCollection.create(u => { u._raw = sanitizedRaw({ id: user._id }, userCollection.schema); Object.assign(u, user); @@ -103,8 +105,8 @@ export default async function getUsersPresence() { } } -let usersTimer = null; -export function getUserPresence(uid) { +let usersTimer: number | null = null; +export function getUserPresence(this: IRocketChat, uid: string) { if (!usersTimer) { usersTimer = setTimeout(() => { getUsersPresence.call(this); From b3f98a6de681c5d2f2eaab88cc63964b6395a522 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 11 Feb 2022 11:37:15 -0400 Subject: [PATCH 07/13] Revert "migrate getUsersPresence to ts" This reverts commit 2c6cef8475155e67cfeb99f94153664caf701f2f. --- ...etUsersPresence.ts => getUsersPresence.js} | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) rename app/lib/methods/{getUsersPresence.ts => getUsersPresence.js} (78%) diff --git a/app/lib/methods/getUsersPresence.ts b/app/lib/methods/getUsersPresence.js similarity index 78% rename from app/lib/methods/getUsersPresence.ts rename to app/lib/methods/getUsersPresence.js index 3bc93fd31a3..f596e527adb 100644 --- a/app/lib/methods/getUsersPresence.ts +++ b/app/lib/methods/getUsersPresence.js @@ -1,16 +1,14 @@ import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; -import { IActiveUsers } from '../../reducers/activeUsers'; import { compareServerVersion } from '../utils'; import { store as reduxStore } from '../auxStore'; import { setActiveUsers } from '../../actions/activeUsers'; import { setUser } from '../../actions/login'; import database from '../database'; -import { IRocketChat, IUser } from '../../definitions'; -export function subscribeUsersPresence(this: IRocketChat) { - const serverVersion = reduxStore.getState().server.version as string; +export function subscribeUsersPresence() { + const serverVersion = reduxStore.getState().server.version; // if server is lower than 1.1.0 if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) { @@ -31,10 +29,10 @@ export function subscribeUsersPresence(this: IRocketChat) { this.sdk.subscribe('stream-notify-logged', 'Users:NameChanged'); } -let ids: string[] = []; +let ids = []; -export default async function getUsersPresence(this: IRocketChat) { - const serverVersion = reduxStore.getState().server.version as string; +export default async function getUsersPresence() { + const serverVersion = reduxStore.getState().server.version; const { user: loggedUser } = reduxStore.getState().login; // if server is greather than or equal 1.1.0 @@ -62,8 +60,8 @@ export default async function getUsersPresence(this: IRocketChat) { if (result.success) { const { users } = result; - const activeUsers = ids.reduce((ret: IActiveUsers, id) => { - const user = users.find((u: IUser) => u._id === id) ?? { _id: id, status: 'offline' }; + const activeUsers = ids.reduce((ret, id) => { + const user = users.find(u => u._id === id) ?? { _id: id, status: 'offline' }; const { _id, status, statusText } = user; if (loggedUser && loggedUser.id === _id) { @@ -80,17 +78,17 @@ export default async function getUsersPresence(this: IRocketChat) { const db = database.active; const userCollection = db.get('users'); - users.forEach(async (user: IUser) => { + users.forEach(async user => { try { const userRecord = await userCollection.find(user._id); - await db.write(async () => { + await db.action(async () => { await userRecord.update(u => { Object.assign(u, user); }); }); } catch (e) { // User not found - await db.write(async () => { + await db.action(async () => { await userCollection.create(u => { u._raw = sanitizedRaw({ id: user._id }, userCollection.schema); Object.assign(u, user); @@ -105,8 +103,8 @@ export default async function getUsersPresence(this: IRocketChat) { } } -let usersTimer: number | null = null; -export function getUserPresence(this: IRocketChat, uid: string) { +let usersTimer = null; +export function getUserPresence(uid) { if (!usersTimer) { usersTimer = setTimeout(() => { getUsersPresence.call(this); From e08938ad7859a7c1d38db3b9244f0942a0596058 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 15 Feb 2022 13:03:10 -0400 Subject: [PATCH 08/13] remove @ts-ignore --- app/lib/methods/sendMessage.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 1cef78f5272..ba07f12918c 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -15,6 +15,7 @@ import { TThreadMessageModel, TThreadModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; type TMessages = TMessageModel | TThreadMessageModel; @@ -56,10 +57,9 @@ const changeMessageStatus = async (id: string, tmid: string, status: number, mes } }; -export async function sendMessageCall(this: IRocketChat, message: TMessageModel) { +export async function sendMessageCall(message: TMessageModel) { const { _id, tmid } = message; try { - const sdk = this.shareSDK || this.sdk; // RC 0.60.0 const result = await sdk.post('chat.sendMessage', { message }); if (result.success) { @@ -71,7 +71,7 @@ export async function sendMessageCall(this: IRocketChat, message: TMessageModel) return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); } -export async function resendMessage(message: TMessageModel, tmid: string) { +export async function resendMessage(this: IRocketChat, message: TMessageModel, tmid: string) { const db = database.active; try { await db.write(async () => { @@ -91,14 +91,14 @@ export async function resendMessage(message: TMessageModel, tmid: string) { } as TMessageModel; } m = await Encryption.encryptMessage(m); - // @ts-ignore + await sendMessageCall.call(this, m); } catch (e) { log(e); } } -export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { +export default async function (this: IRocketChat, rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -235,7 +235,7 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs log(e); return; } - // @ts-ignore + await sendMessageCall.call(this, message); } catch (e) { log(e); From b5798b5d1e365bb91bd06484480017e93d21cd12 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 15 Feb 2022 15:36:06 -0400 Subject: [PATCH 09/13] any types for sdk.post --- app/lib/methods/sendMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index ba07f12918c..8c4fa684231 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -61,7 +61,7 @@ export async function sendMessageCall(message: TMessageModel) { const { _id, tmid } = message; try { // RC 0.60.0 - const result = await sdk.post('chat.sendMessage', { message }); + const result = (await sdk.post('chat.sendMessage' as any, { message } as any)) as any; if (result.success) { return changeMessageStatus(_id, tmid as string, messagesStatus.SENT, result.message); } From 5ed1e5323311802aa5afd84a8691052e82c82523 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 15 Feb 2022 16:04:34 -0400 Subject: [PATCH 10/13] remove this --- app/lib/methods/sendMessage.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 8c4fa684231..7085699914d 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -6,15 +6,7 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; -import { - IMessage, - IRocketChat, - IUser, - TMessageModel, - TSubscriptionModel, - TThreadMessageModel, - TThreadModel -} from '../../definitions'; +import { IMessage, IUser, TMessageModel, TSubscriptionModel, TThreadMessageModel, TThreadModel } from '../../definitions'; import sdk from '../rocketchat/services/sdk'; type TMessages = TMessageModel | TThreadMessageModel; @@ -71,7 +63,7 @@ export async function sendMessageCall(message: TMessageModel) { return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); } -export async function resendMessage(this: IRocketChat, message: TMessageModel, tmid: string) { +export async function resendMessage(message: TMessageModel, tmid: string) { const db = database.active; try { await db.write(async () => { @@ -92,13 +84,13 @@ export async function resendMessage(this: IRocketChat, message: TMessageModel, t } m = await Encryption.encryptMessage(m); - await sendMessageCall.call(this, m); + await sendMessageCall(m); } catch (e) { log(e); } } -export default async function (this: IRocketChat, rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { +export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -236,7 +228,7 @@ export default async function (this: IRocketChat, rid: string, msg: string, tmid return; } - await sendMessageCall.call(this, message); + await sendMessageCall(message); } catch (e) { log(e); } From 6f696028d1dd51b8c56db2f0ef3854c7f532d94a Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 16 Feb 2022 20:21:49 -0400 Subject: [PATCH 11/13] minor tweaks --- app/definitions/IRocketChat.ts | 1 - app/lib/methods/sendMessage.ts | 36 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/definitions/IRocketChat.ts b/app/definitions/IRocketChat.ts index 91d701a5d15..654e2c92c0d 100644 --- a/app/definitions/IRocketChat.ts +++ b/app/definitions/IRocketChat.ts @@ -4,7 +4,6 @@ type TRocketChat = typeof rocketchat; export interface IRocketChat extends TRocketChat { sdk: any; - shareSDK: any; activeUsersSubTimeout: any; roomsSub: any; } diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 7085699914d..db8a75b6f44 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -1,4 +1,5 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { Model } from '@nozbe/watermelondb'; import messagesStatus from '../../constants/messagesStatus'; import database from '../database'; @@ -6,16 +7,16 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; -import { IMessage, IUser, TMessageModel, TSubscriptionModel, TThreadMessageModel, TThreadModel } from '../../definitions'; +import { IMessage, IUser, TMessageModel, TThreadMessageModel } from '../../definitions'; import sdk from '../rocketchat/services/sdk'; type TMessages = TMessageModel | TThreadMessageModel; -const changeMessageStatus = async (id: string, tmid: string, status: number, message?: IMessage) => { +const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => { const db = database.active; const msgCollection = db.get('messages'); const threadMessagesCollection = db.get('thread_messages'); - const successBatch = [] as TMessages[]; + const successBatch: TMessages[] = []; const messageRecord = await msgCollection.find(id); successBatch.push( messageRecord.prepareUpdate(m => { @@ -49,21 +50,23 @@ const changeMessageStatus = async (id: string, tmid: string, status: number, mes } }; -export async function sendMessageCall(message: TMessageModel) { +export async function sendMessageCall(message: IMessage) { const { _id, tmid } = message; try { // RC 0.60.0 - const result = (await sdk.post('chat.sendMessage' as any, { message } as any)) as any; + // @ts-ignore + const result = await sdk.post('chat.sendMessage', { message }); if (result.success) { - return changeMessageStatus(_id, tmid as string, messagesStatus.SENT, result.message); + // @ts-ignore + return changeMessageStatus(_id, messagesStatus.SENT, tmid, result.message); } } catch { // do nothing } - return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR); + return changeMessageStatus(_id, messagesStatus.ERROR, tmid); } -export async function resendMessage(message: TMessageModel, tmid: string) { +export async function resendMessage(message: TMessageModel, tmid?: string) { const db = database.active; try { await db.write(async () => { @@ -71,20 +74,20 @@ export async function resendMessage(message: TMessageModel, tmid: string) { m.status = messagesStatus.TEMP; }); }); - let m = { + let m: Partial = { _id: message.id, rid: message.subscription.id, msg: message.msg - } as TMessageModel; + }; if (tmid) { m = { ...m, tmid - } as TMessageModel; + }; } m = await Encryption.encryptMessage(m); - await sendMessageCall(m); + await sendMessageCall(m as IMessage); } catch (e) { log(e); } @@ -98,16 +101,15 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs const threadCollection = db.get('threads'); const threadMessagesCollection = db.get('thread_messages'); const messageId = random(17); - const batch: (TMessageModel | TThreadMessageModel | TThreadModel | TSubscriptionModel)[] = []; + const batch: Model[] = []; - let message = { + const message = await Encryption.encryptMessage({ _id: messageId, rid, msg, tmid, tshow - } as TMessageModel; - message = await Encryption.encryptMessage(message); + }); const messageDate = new Date(); let tMessageRecord: TMessageModel; @@ -228,7 +230,7 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs return; } - await sendMessageCall(message); + await sendMessageCall(message as IMessage); } catch (e) { log(e); } From a6d683af8bfbb7b695cac7197f718868a030f536 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 16 Feb 2022 21:20:18 -0400 Subject: [PATCH 12/13] FIx lint errors --- app/definitions/IThread.ts | 2 +- app/lib/methods/sendMessage.ts | 24 ++++++++++-------------- app/lib/methods/updateMessages.ts | 4 +++- app/utils/room.ts | 4 ++-- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index ca54231e7ff..be5dac1baf0 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -39,7 +39,7 @@ export interface IThread { t?: MessageType; rid: string; _updatedAt?: Date; - ts?: Date; + ts?: string | Date; u?: IUserMessage; alias?: string; parseUrls?: boolean; diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index db8a75b6f44..4e3e8af20d6 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -74,20 +74,14 @@ export async function resendMessage(message: TMessageModel, tmid?: string) { m.status = messagesStatus.TEMP; }); }); - let m: Partial = { + const m = await Encryption.encryptMessage({ _id: message.id, - rid: message.subscription.id, - msg: message.msg - }; - if (tmid) { - m = { - ...m, - tmid - }; - } - m = await Encryption.encryptMessage(m); + rid: message.subscription ? message.subscription.id : '', + msg: message.msg, + ...(tmid && { tmid }) + } as IMessage); - await sendMessageCall(m as IMessage); + await sendMessageCall(m); } catch (e) { log(e); } @@ -109,7 +103,7 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs msg, tmid, tshow - }); + } as IMessage); const messageDate = new Date(); let tMessageRecord: TMessageModel; @@ -185,7 +179,9 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs batch.push( msgCollection.prepareCreate(m => { m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema); - m.subscription.id = rid; + if (m.subscription) { + m.subscription.id = rid; + } m.msg = msg; m.ts = messageDate; m._updatedAt = messageDate; diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index ddfd872218c..f6e52d1ac71 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -105,7 +105,9 @@ export default async function updateMessages({ threadCollection.prepareCreate( protectedFunction((t: TThreadModel) => { t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema); - t.subscription.id = sub.id; + if (t.subscription) { + t.subscription.id = sub.id; + } Object.assign(t, thread); }) ) diff --git a/app/utils/room.ts b/app/utils/room.ts index d39e4049035..4bb8a29b9a0 100644 --- a/app/utils/room.ts +++ b/app/utils/room.ts @@ -22,7 +22,7 @@ export const capitalize = (s: string): string => { return s.charAt(0).toUpperCase() + s.slice(1); }; -export const formatDate = (date: Date): string => +export const formatDate = (date: string | Date): string => moment(date).calendar(null, { lastDay: `[${I18n.t('Yesterday')}]`, sameDay: 'LT', @@ -30,7 +30,7 @@ export const formatDate = (date: Date): string => sameElse: 'L' }); -export const formatDateThreads = (date: Date): string => +export const formatDateThreads = (date: string | Date): string => moment(date).calendar(null, { sameDay: 'LT', lastDay: `[${I18n.t('Yesterday')}] LT`, From c0f8117446e818a4a851be86f7a26a6c11868df5 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 18 Feb 2022 12:15:45 -0400 Subject: [PATCH 13/13] minor tweak --- app/lib/methods/sendMessage.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 4e3e8af20d6..b4bd9bf9e1c 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -7,16 +7,14 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; -import { IMessage, IUser, TMessageModel, TThreadMessageModel } from '../../definitions'; +import { IMessage, IUser, TMessageModel } from '../../definitions'; import sdk from '../rocketchat/services/sdk'; -type TMessages = TMessageModel | TThreadMessageModel; - const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => { const db = database.active; const msgCollection = db.get('messages'); const threadMessagesCollection = db.get('thread_messages'); - const successBatch: TMessages[] = []; + const successBatch: Model[] = []; const messageRecord = await msgCollection.find(id); successBatch.push( messageRecord.prepareUpdate(m => { @@ -50,7 +48,7 @@ const changeMessageStatus = async (id: string, status: number, tmid?: string, me } }; -export async function sendMessageCall(message: IMessage) { +export async function sendMessageCall(message: any) { const { _id, tmid } = message; try { // RC 0.60.0 @@ -226,7 +224,7 @@ export default async function (rid: string, msg: string, tmid: string, user: IUs return; } - await sendMessageCall(message as IMessage); + await sendMessageCall(message); } catch (e) { log(e); }