fix(extra-info): handle successive message loads and promoted messages
When loading a thread, some messages might be collapsed and are loaded
in subsequent requests. This CL handles this case by holding an array of
messages which grows each time the user expands a gap of messages.
Furthermore, this CL also considers promoted messages for the message
list (before this, the extra info chips were added to promoted messages
only if they were loaded outside of the promoted messages structure as
well).
Bug: twpowertools:93
Change-Id: I8b6f4e8f4a97c7f5e4cdde52b6b773b9631fbe57
diff --git a/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js b/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
index ba7b906..c44d7f8 100644
--- a/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
+++ b/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
@@ -10,12 +10,6 @@
const kCurrentInfoExpiresInMs = kTimeoutInMs * 1.5;
export default class ThreadInfoHandler extends ResponseEventBasedInfoHandler {
- constructor() {
- super();
-
- this.thread = undefined;
- }
-
getEvent() {
return kViewThreadResponse;
}
@@ -27,14 +21,44 @@
};
}
- async isInfoCurrent(injectionDetails) {
- this.thread = new ThreadModel(this.info.body?.[1]);
+ setUpDefaultInfoValue() {
+ this.info = {
+ thread: new ThreadModel(),
+ messages: [],
+ id: -1,
+ timestamp: 0,
+ };
+ }
+ updateInfoWithNewValue(e) {
+ const newThread = new ThreadModel(e.detail.body?.[1]);
+ if (newThread.getId() != this.info.thread.getId()) {
+ this.info.messages = [];
+ }
+
+ const newMessages = newThread.getAllMessagesList();
+ this.updateRecordedMessages(newMessages);
+
+ this.info.thread = newThread;
+ this.info.id = e.detail.id;
+ this.info.timestamp = Date.now();
+ }
+
+ updateRecordedMessages(newMessages) {
+ const nonUpdatedMessages = this.info.messages.filter(message => {
+ return !newMessages.some(newMessage => {
+ return message.getId() == newMessage.getId();
+ });
+ });
+ this.info.messages = nonUpdatedMessages.concat(newMessages);
+ }
+
+ async isInfoCurrent(injectionDetails) {
const currentPage = this.parseThreadUrl();
const isCurrentThread =
Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs &&
- this.thread.getId() == currentPage.thread &&
- this.thread.getForumId() == currentPage.forum;
+ this.info.thread.getId() == currentPage.thread &&
+ this.info.thread.getForumId() == currentPage.forum;
const isMessageNode = injectionDetails.isMessageNode;
const messageNode = injectionDetails.messageNode;
@@ -53,8 +77,8 @@
currentThreadContainsMessage(messageNode) {
const messageId = MessageExtraInfoService.getMessageIdFromNode(messageNode);
- const message = MessageExtraInfoService.getMessageFromThreadModel(
- messageId, this.thread);
+ const message = MessageExtraInfoService.getMessageFromList(
+ messageId, this.info.messages);
return message !== undefined;
}
}