Flatten threads: remove further info about parents

This CL removes information from the ViewThread and CreateMessage
responses which indicate that the nested messages have a parent message.

This is done because although this information is not used now to render
the replies, it might be used in the future by the Community Console
code and this might break the feature.

Bug: twpowertools:156

Change-Id: Ie32ce2b112f15395c2f15d5d6b9b5cde4cc64768
diff --git a/src/models/Message.js b/src/models/Message.js
index baebd14..7aae392 100644
--- a/src/models/Message.js
+++ b/src/models/Message.js
@@ -54,6 +54,11 @@
     return this.data[1]?.[37] ?? null;
   }
 
+  clearParentMessageId() {
+    if (!this.data[1]) return;
+    delete this.data[1][37];
+  }
+
   isComment() {
     return !!this.getParentMessageId;
   }
diff --git a/src/xhrInterceptor/responseModifiers/createMessageRemoveParentRef.js b/src/xhrInterceptor/responseModifiers/createMessageRemoveParentRef.js
new file mode 100644
index 0000000..a127975
--- /dev/null
+++ b/src/xhrInterceptor/responseModifiers/createMessageRemoveParentRef.js
@@ -0,0 +1,16 @@
+const createMessageRemoveParentRef = {
+  urlRegex: /api\/CreateMessage/i,
+  featureGated: true,
+  features: ['flattenthreads', 'flattenthreads_switch_enabled'],
+  isEnabled(options) {
+    return options['flattenthreads'] &&
+        options['flattenthreads_switch_enabled'];
+  },
+  async interceptor(_request, response) {
+    // Remove parent_message_id value (field 37)
+    delete response[37];
+    return response;
+  }
+};
+
+export default createMessageRemoveParentRef;
diff --git a/src/xhrInterceptor/responseModifiers/flattenThread.js b/src/xhrInterceptor/responseModifiers/flattenThread.js
index 23db6eb..53828eb 100644
--- a/src/xhrInterceptor/responseModifiers/flattenThread.js
+++ b/src/xhrInterceptor/responseModifiers/flattenThread.js
@@ -2,7 +2,7 @@
 import GapModel from '../../models/Gap.js';
 import MessageModel from '../../models/Message.js';
 
-const loadMoreThread = {
+const flattenThread = {
   urlRegex: /api\/ViewThread/i,
   featureGated: true,
   features: ['flattenthreads', 'flattenthreads_switch_enabled'],
@@ -42,6 +42,9 @@
     });
     mogs.forEach(m => m.setPayload(m.newPayload));
 
+    // Clear parent_message_id fields
+    mogs.forEach(m => m.clearParentMessageId());
+
     // Sort the messages by date
     mogs.sort((a, b) => {
       const c = a instanceof MessageModel ? a.getCreatedMicroseconds() :
@@ -54,6 +57,10 @@
 
     response[1][40] = mogs.map(mog => mog.toRawMessageOrGap());
 
+    // Set last_message to the last message after sorting
+    if (response[1]?.[17]?.[3])
+      response[1][17][3] = response[1][40].slice(-1)?.[1];
+
     // Set num_messages to the updated value, since we've flattened the replies.
     response[1][8] = response[1][40].length;
     return response;
@@ -76,8 +83,7 @@
     else
       prevId = parentId;
 
-    const prevMessage =
-        prevId ? mogs.find(m => m.getId() == prevId) : null;
+    const prevMessage = prevId ? mogs.find(m => m.getId() == prevId) : null;
 
     return {
       isComment: true,
@@ -93,4 +99,4 @@
   }
 };
 
-export default loadMoreThread;
+export default flattenThread;
diff --git a/src/xhrInterceptor/responseModifiers/index.js b/src/xhrInterceptor/responseModifiers/index.js
index 43ffe6c..40494eb 100644
--- a/src/xhrInterceptor/responseModifiers/index.js
+++ b/src/xhrInterceptor/responseModifiers/index.js
@@ -3,10 +3,12 @@
 
 import loadMoreThread from './loadMoreThread.js';
 import flattenThread from './flattenThread.js';
+import createMessageRemoveParentRef from './createMessageRemoveParentRef.js';
 
 export const responseModifiers = [
   loadMoreThread,
   flattenThread,
+  createMessageRemoveParentRef,
 ];
 
 // Content script target