Fix problems with outgoing messages

1. Don't set the "own message" promise to null on resolve, as other
   places may still be awaiting on it afterwards
2. Retry pressing "Enter" to send a message if it doesn't work on the
   first try, which can happen for some reason
This commit is contained in:
Andrew Ferrazzutti 2021-07-02 13:58:36 -04:00
parent e1b822bd52
commit 69028c6edd
2 changed files with 23 additions and 6 deletions

View File

@ -95,6 +95,7 @@ class MautrixController {
this.pinAppearObserver = null this.pinAppearObserver = null
this.ownID = null this.ownID = null
this.ownMsgPromise = Promise.resolve(-1)
this._promiseOwnMsgReset() this._promiseOwnMsgReset()
} }
@ -474,13 +475,20 @@ class MautrixController {
}, timeoutLimitMillis) }, timeoutLimitMillis)
} }
/**
* Check if we're waiting for a Matrix-sent message to resolve.
*/
_isWaitingForOwnMessage() {
return !!this.promiseOwnMsgResolve
}
/** /**
* Wait for a user-sent message to finish getting sent. * Wait for a user-sent message to finish getting sent.
* *
* @return {Promise<number>} - The ID of the sent message. * @return {Promise<number>} - The ID of the sent message.
*/ */
async waitForOwnMessage() { async waitForOwnMessage() {
return this.ownMsgPromise ? await this.ownMsgPromise : -1 return await this.ownMsgPromise
} }
/** /**
@ -867,7 +875,7 @@ class MautrixController {
addChatListObserver() { addChatListObserver() {
this.removeChatListObserver() this.removeChatListObserver()
this.chatListObserver = new MutationObserver(async (mutations) => { this.chatListObserver = new MutationObserver(async (mutations) => {
if (this.ownMsgPromise) { if (this._isWaitingForOwnMessage()) {
// Wait for pending sent messages to be resolved before responding to mutations // Wait for pending sent messages to be resolved before responding to mutations
try { try {
await this.ownMsgPromise await this.ownMsgPromise
@ -1213,8 +1221,7 @@ class MautrixController {
} }
_observeOwnMessage(ownMsg) { _observeOwnMessage(ownMsg) {
if (!this.ownMsgPromise) { if (!this._isWaitingForOwnMessage()) {
// Not waiting for a pending sent message
return null return null
} }
@ -1299,7 +1306,6 @@ class MautrixController {
} }
_promiseOwnMsgReset() { _promiseOwnMsgReset() {
this.ownMsgPromise = null
this.promiseOwnMsgSuccessSelector = null this.promiseOwnMsgSuccessSelector = null
this.promiseOwnMsgFailureSelector = null this.promiseOwnMsgFailureSelector = null
this.promiseOwnMsgResolve = null this.promiseOwnMsgResolve = null

View File

@ -790,7 +790,18 @@ export default class MessagesPuppeteer {
// Setting its innerText directly works fine though... // Setting its innerText directly works fine though...
await input.click() await input.click()
await input.evaluate((e, text) => e.innerText = text, text) await input.evaluate((e, text) => e.innerText = text, text)
while (true) {
await input.press("Enter") await input.press("Enter")
try {
await this.page.waitForFunction(
e => e.innerText == "",
{timeout: 500},
input)
break
} catch (e) {
this.error(`Failed to press Enter when sending message, try again (${e})`)
}
}
}) })
return await this._waitForSentMessage(chatID) return await this._waitForSentMessage(chatID)