Fix: invalidate avatars cache entry after posting reply

After writing a reply to a thread and going back to the thread list, the
cache entry was always used, and so the avatars would be wrong if it was
the first time replying to that thread. This fixes it.

Fixed: twpowertools:23
Change-Id: Ibc77e8394bf3bf912d6b6ae11ae23a8fd4b36712
diff --git a/src/common/xhrInterceptors.json5 b/src/common/xhrInterceptors.json5
index cc0699f..6a5a1c5 100644
--- a/src/common/xhrInterceptors.json5
+++ b/src/common/xhrInterceptors.json5
@@ -5,5 +5,10 @@
       urlRegex: "api/ViewForum",
       intercepts: "response",
     },
+    {
+      eventName: "CreateMessageRequest",
+      urlRegex: "api/CreateMessage",
+      intercepts: "request",
+    },
   ],
 }
diff --git a/src/contentScripts/communityConsole/utils/AvatarsDB.js b/src/contentScripts/communityConsole/utils/AvatarsDB.js
index 191940f..05149b5 100644
--- a/src/contentScripts/communityConsole/utils/AvatarsDB.js
+++ b/src/contentScripts/communityConsole/utils/AvatarsDB.js
@@ -2,6 +2,7 @@
 
 const dbName = 'TWPTAvatarsDB';
 const threadListLoadEvent = 'TWPT_ViewForumResponse';
+const createMessageLoadEvent = 'TWPT_CreateMessageRequest';
 // Time after the last use when a cache entry should be deleted (in s):
 const cacheExpirationTime = 4 * 24 * 60 * 60;  // 4 days
 // Probability of running the piece of code to remove unused cache entries after
@@ -15,7 +16,7 @@
   constructor() {
     this.dbPromise = undefined;
     this.openDB();
-    this.setUpInvalidationsHandler();
+    this.setUpInvalidationsHandlers();
   }
 
   openDB() {
@@ -73,12 +74,14 @@
         });
   }
 
-  setUpInvalidationsHandler() {
+  setUpInvalidationsHandlers() {
     window.addEventListener(
-        threadListLoadEvent, e => this.handleInvalidations(e));
+        threadListLoadEvent, e => this.handleInvalidationsByListLoad(e));
+    window.addEventListener(
+        createMessageLoadEvent, e => this.handleInvalidationByNewMessage(e));
   }
 
-  handleInvalidations(e) {
+  handleInvalidationsByListLoad(e) {
     var response = e?.detail?.body;
     var threads = response?.['1']?.['2'];
     if (threads === undefined) {
@@ -128,6 +131,21 @@
     });
   }
 
+  handleInvalidationByNewMessage(e) {
+    var request = e?.detail?.body;
+    var threadId = request?.['2'];
+    if (threadId === undefined) {
+      console.warn(
+          '[threadListAvatars] Thread ID couldn\'t be parsed from the CreateMessage request.');
+      return;
+    }
+
+    console.debug(
+        '[threadListAvatars] Invalidating thread', threadId,
+        'due to intercepting a CreateMessage request for that thread.');
+    return this.invalidateCacheEntryIfExists(threadId);
+  }
+
   // unauthorizedForums methods:
   isForumUnauthorized(forumId) {
     return this.dbPromise.then(db => db.get('unauthorizedForums', forumId))