-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Chore: Migrate sendMessage to TS #3712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8a8f5bb
migrate sendMessage to ts
gerzonc abba54f
update interfaces and sendMessage file
gerzonc 01cbc1b
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc 779c4a1
Update sendMessage.ts
gerzonc 01aacb7
minor tweaks
gerzonc 9aa8716
Update sendMessage.ts
gerzonc 2c6cef8
migrate getUsersPresence to ts
gerzonc b3f98a6
Revert "migrate getUsersPresence to ts"
gerzonc e08938a
remove @ts-ignore
gerzonc 47143f8
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc 3eb083c
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc b5798b5
any types for sdk.post
gerzonc 3302ff6
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc 5ed1e53
remove this
gerzonc 6f69602
minor tweaks
gerzonc 473b1eb
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc a6d683a
FIx lint errors
gerzonc 4bbb368
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
diegolmello 8b00855
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
gerzonc c0f8117
minor tweak
gerzonc 933996d
Merge branch 'develop' into chore.migrate-sendmessage-to-ts
diegolmello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,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 sdk from '../rocketchat/services/sdk'; | ||
|
|
||
| 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[]; | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
| const messageRecord = await msgCollection.find(id); | ||
| successBatch.push( | ||
| messageRecord.prepareUpdate(m => { | ||
|
|
@@ -37,33 +41,32 @@ const changeMessageStatus = async (id, tmid, status, message) => { | |
| } | ||
|
|
||
| try { | ||
| await db.action(async () => { | ||
| await db.write(async () => { | ||
| await db.batch(...successBatch); | ||
| }); | ||
| } catch (error) { | ||
| // Do nothing | ||
| } | ||
| }; | ||
|
|
||
| export async function sendMessageCall(message) { | ||
| 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 }); | ||
| const result = (await sdk.post('chat.sendMessage' as any, { message } as any)) as any; | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
| if (result.success) { | ||
| return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message); | ||
| return changeMessageStatus(_id, tmid as string, messagesStatus.SENT, result.message); | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
| } | ||
| } 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) { | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
| const db = database.active; | ||
| try { | ||
| await db.action(async () => { | ||
| await db.write(async () => { | ||
| await message.update(m => { | ||
| m.status = messagesStatus.TEMP; | ||
| }); | ||
|
|
@@ -72,41 +75,42 @@ export async function resendMessage(message, tmid) { | |
| _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); | ||
|
|
||
| await sendMessageCall(m); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| } | ||
|
|
||
| export default async function (rid, msg, tmid, user, tshow) { | ||
| export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use tm.u = {
_id: user.id || '1',
username: user.username,
name: user.name
}; |
||
| try { | ||
| const db = database.active; | ||
| const subsCollection = db.get('subscriptions'); | ||
| const msgCollection = db.get('messages'); | ||
| const threadCollection = db.get('threads'); | ||
| const threadMessagesCollection = db.get('thread_messages'); | ||
| const messageId = random(17); | ||
| const batch = []; | ||
| const batch: (TMessageModel | TThreadMessageModel | TThreadModel | TSubscriptionModel)[] = []; | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
|
|
||
| let message = { | ||
| _id: messageId, | ||
| rid, | ||
| msg, | ||
| tmid, | ||
| tshow | ||
| }; | ||
| } as TMessageModel; | ||
|
gerzonc marked this conversation as resolved.
Outdated
|
||
| message = await Encryption.encryptMessage(message); | ||
|
|
||
| const messageDate = new Date(); | ||
| let tMessageRecord; | ||
| let tMessageRecord: TMessageModel; | ||
|
|
||
| // If it's replying to a thread | ||
| if (tmid) { | ||
|
|
@@ -116,7 +120,9 @@ export default async function (rid, msg, tmid, user, tshow) { | |
| batch.push( | ||
| tMessageRecord.prepareUpdate(m => { | ||
| m.tlm = messageDate; | ||
| m.tcount += 1; | ||
| if (m.tcount) { | ||
| m.tcount += 1; | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
|
|
@@ -128,7 +134,9 @@ export default async function (rid, msg, tmid, user, tshow) { | |
| 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; | ||
|
|
@@ -147,7 +155,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; | ||
|
|
@@ -210,15 +220,15 @@ export default async function (rid, msg, tmid, user, tshow) { | |
| } | ||
|
|
||
| try { | ||
| await db.action(async () => { | ||
| await db.write(async () => { | ||
| await db.batch(...batch); | ||
| }); | ||
| } catch (e) { | ||
| log(e); | ||
| return; | ||
| } | ||
|
|
||
| await sendMessageCall.call(this, message); | ||
| await sendMessageCall(message); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.