First crack at message receiving

This commit is contained in:
Andrew Ferrazzutti 2021-02-25 23:59:25 -05:00
parent 1b7642ce6d
commit 5d77852d7a
2 changed files with 33 additions and 21 deletions

View File

@ -274,7 +274,7 @@ class MautrixController {
* @return {ChatListInfo} - The info in the element. * @return {ChatListInfo} - The info in the element.
*/ */
parseChatListItem(element, knownId) { parseChatListItem(element, knownId) {
return { return !element.classList.contains("chatList") ? null : {
id: knownId || this.getChatListItemId(element), id: knownId || this.getChatListItemId(element),
name: this.getChatListItemName(element), name: this.getChatListItemName(element),
// TODO icon, but only for groups // TODO icon, but only for groups
@ -294,6 +294,7 @@ class MautrixController {
} }
/** /**
* TODO
* Check if an image has been downloaded. * Check if an image has been downloaded.
* *
* @param {number} id - The ID of the message whose image to check. * @param {number} id - The ID of the message whose image to check.
@ -307,6 +308,7 @@ class MautrixController {
} }
/** /**
* TODO
* Download an image and return it as a data URL. * Download an image and return it as a data URL.
* Used for downloading the blob: URLs in image messages. * Used for downloading the blob: URLs in image messages.
* *
@ -331,18 +333,32 @@ class MautrixController {
* @private * @private
*/ */
_observeChatListMutations(mutations) { _observeChatListMutations(mutations) {
/* TODO // TODO Observe *added/removed* chats, not just new messages
const changedChatIDs = new Set() const changedChatIDs = new Set()
for (const change of mutations) { for (const change of mutations) {
console.debug("Chat list mutation:", change) console.debug("Chat list mutation:", change)
if (!(change.target instanceof Element) if (change.target.id == "_chat_list_body") {
|| change.target.tagName.toLowerCase() === "mws-conversation-list-item-menu") { // TODO
console.debug("Ignoring chat list mutation:", change.target instanceof Element) // These could be new chats, or they're
continue // existing ones that just moved around.
/*
for (const node of change.addedNodes) {
} }
const chat = this.parseChatListItem(change.target.closest("mws-conversation-list-item")) */
}
else if (change.target.tagName == "LI")
{
for (const node of change.addedNodes) {
const chat = this.parseChatListItem(node)
if (chat) {
console.debug("Changed chat list item:", chat) console.debug("Changed chat list item:", chat)
changedChatIDs.add(chat.id) changedChatIDs.add(chat.id)
} else {
console.debug("Could not parse node as a chat list item:", node)
}
}
}
// change.removedNodes tells you which chats that had notifications are now read.
} }
if (changedChatIDs.size > 0) { if (changedChatIDs.size > 0) {
console.debug("Dispatching chat list mutations:", changedChatIDs) console.debug("Dispatching chat list mutations:", changedChatIDs)
@ -350,28 +366,25 @@ class MautrixController {
() => console.debug("Chat list mutations dispatched"), () => console.debug("Chat list mutations dispatched"),
err => console.error("Error dispatching chat list mutations:", err)) err => console.error("Error dispatching chat list mutations:", err))
} }
*/
} }
/** /**
* Add a mutation observer to the given element. * Add a mutation observer to the chat list.
*
* @param {Element} element - The DOM element to add the mutation observer to.
*/ */
addChatListObserver(element) { addChatListObserver() {
if (this.chatListObserver !== null) { if (this.chatListObserver !== null) {
this.removeChatListObserver() this.removeChatListObserver()
} }
this.chatListObserver = new MutationObserver(mutations => { this.chatListObserver = new MutationObserver(mutations => {
/* TODO
try { try {
this._observeChatListMutations(mutations) this._observeChatListMutations(mutations)
} catch (err) { } catch (err) {
console.error("Error observing chat list mutations:", err) console.error("Error observing chat list mutations:", err)
} }
*/
}) })
this.chatListObserver.observe(element, { childList: true, subtree: true }) this.chatListObserver.observe(
document.querySelector("#_chat_list_body"),
{ childList: true, subtree: true })
console.debug("Started chat list observer") console.debug("Started chat list observer")
} }

View File

@ -95,10 +95,8 @@ export default class MessagesPuppeteer {
await this.page.exposeFunction("__mautrixExpiry", this._receiveExpiry.bind(this)) await this.page.exposeFunction("__mautrixExpiry", this._receiveExpiry.bind(this))
await this.page.exposeFunction("__mautrixReceiveMessageID", await this.page.exposeFunction("__mautrixReceiveMessageID",
id => this.sentMessageIDs.add(id)) id => this.sentMessageIDs.add(id))
/* TODO
await this.page.exposeFunction("__mautrixReceiveChanges", await this.page.exposeFunction("__mautrixReceiveChanges",
this._receiveChatListChanges.bind(this)) this._receiveChatListChanges.bind(this))
*/
await this.page.exposeFunction("__chronoParseDate", chrono.parseDate) await this.page.exposeFunction("__chronoParseDate", chrono.parseDate)
// NOTE Must *always* re-login on a browser session, so no need to check if already logged in // NOTE Must *always* re-login on a browser session, so no need to check if already logged in
@ -390,8 +388,8 @@ export default class MessagesPuppeteer {
async startObserving() { async startObserving() {
this.log("Adding chat list observer") this.log("Adding chat list observer")
await this.page.$eval("#_chat_list_body", await this.page.evaluate(
element => window.__mautrixController.addChatListObserver(element)) () => window.__mautrixController.addChatListObserver())
} }
async stopObserving() { async stopObserving() {
@ -502,6 +500,7 @@ export default class MessagesPuppeteer {
async _getMessagesUnsafe(id, minID = 0) { async _getMessagesUnsafe(id, minID = 0) {
// TODO Also handle "decrypting" state // TODO Also handle "decrypting" state
// TODO Handle unloaded messages. Maybe scroll up // TODO Handle unloaded messages. Maybe scroll up
// TODO This will mark the chat as "read"!
await this._switchChat(id) await this._switchChat(id)
this.log("Waiting for messages to load") this.log("Waiting for messages to load")
const messages = await this.page.evaluate( const messages = await this.page.evaluate(