From 7086aa69a6a62a63cba97fdeb564480bc39da339 Mon Sep 17 00:00:00 2001
From: PhilReact <philliplangmartinez@gmail.com>
Date: Fri, 27 Sep 2024 21:45:16 +0300
Subject: [PATCH] added the ability to read ui group chat msgs

---
 src/components/Chat/ChatGroup.tsx      | 24 ++++++++++++++----------
 src/components/Chat/ChatList.tsx       |  1 -
 src/qdn/encryption/group-encryption.ts | 21 +++++++++++++++++++++
 3 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/src/components/Chat/ChatGroup.tsx b/src/components/Chat/ChatGroup.tsx
index 34da8a5..bd1ddc8 100644
--- a/src/components/Chat/ChatGroup.tsx
+++ b/src/components/Chat/ChatGroup.tsx
@@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useMemo, useReducer, useRef, useState }
 import { CreateCommonSecret } from './CreateCommonSecret'
 import { reusableGet } from '../../qdn/publish/pubish'
 import { uint8ArrayToObject } from '../../backgroundFunctions/encryption'
-import { base64ToUint8Array, objectToBase64 } from '../../qdn/encryption/group-encryption'
+import { base64ToUint8Array, decodeBase64ForUIChatMessages, objectToBase64 } from '../../qdn/encryption/group-encryption'
 import {  ChatContainerComp } from './ChatContainer'
 import { ChatList } from './ChatList'
 import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css";
@@ -20,6 +20,7 @@ import ShortUniqueId from "short-unique-id";
 import { ReplyPreview } from './MessageItem'
 import { ExitIcon } from '../../assets/Icons/ExitIcon'
 import { RESOURCE_TYPE_NUMBER_GROUP_CHAT_REACTIONS } from '../../constants/resourceTypes'
+import { isExtMsg } from '../../background'
 
 
 const uid = new ShortUniqueId({ length: 5 });
@@ -113,22 +114,25 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
             secretKeyObject: secretKey
         }}, (response) => {
             if (!response?.error) {
-              processWithNewMessages(response?.map((item)=> {
+              const filterUImessages = encryptedMessages.filter((item)=> !isExtMsg(item.data))
+              const decodedUIMessages = decodeBase64ForUIChatMessages(filterUImessages)
+
+              const combineUIAndExtensionMsgs = [...decodedUIMessages, ...response]
+              processWithNewMessages(combineUIAndExtensionMsgs?.map((item)=> {
                 return {
                   ...item,
                   ...(item?.decryptedData || {})
                 }
               }), selectedGroup)
-              res(response)
+              res(combineUIAndExtensionMsgs)
               if(hasInitialized.current){
                
-                const formatted = response.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
-                  
+                const formatted = combineUIAndExtensionMsgs.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
                   return {
                     ...item,
                     id: item.signature,
                     text: item?.decryptedData?.message || "",
-                    repliedTo: item?.decryptedData?.repliedTo,
+                    repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
                     unread: item?.sender === myAddress ? false : !!item?.chatReference ? false : true
                   }
                 } )
@@ -138,7 +142,7 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
                 setChatReferences((prev) => {
                   let organizedChatReferences = { ...prev };
                 
-                  response
+                  combineUIAndExtensionMsgs
                     .filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === 'reaction')
                     .forEach((item) => {
                       try {
@@ -200,12 +204,12 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
                 
                 
               } else {
-                const formatted = response.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
+                const formatted = combineUIAndExtensionMsgs.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
                   return {
                     ...item,
                     id: item.signature,
                     text: item?.decryptedData?.message || "",
-                    repliedTo: item?.decryptedData?.repliedTo,
+                    repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
                     unread:  false
                   }
                 } )
@@ -214,7 +218,7 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
                 setChatReferences((prev) => {
                   let organizedChatReferences = { ...prev };
                 
-                  response
+                  combineUIAndExtensionMsgs
                     .filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === 'reaction')
                     .forEach((item) => {
                       try {
diff --git a/src/components/Chat/ChatList.tsx b/src/components/Chat/ChatList.tsx
index afaff35..6efa2a6 100644
--- a/src/components/Chat/ChatList.tsx
+++ b/src/components/Chat/ChatList.tsx
@@ -103,7 +103,6 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
 
   const rowRenderer = (index) => {
     let message = messages[index];
-  
     let replyIndex = messages.findIndex((msg)=> msg?.signature === message?.repliedTo)
     let reply
     let reactions = null
diff --git a/src/qdn/encryption/group-encryption.ts b/src/qdn/encryption/group-encryption.ts
index c87f98c..8e35385 100644
--- a/src/qdn/encryption/group-encryption.ts
+++ b/src/qdn/encryption/group-encryption.ts
@@ -185,6 +185,27 @@ export const encryptSingle = async ({ data64, secretKeyObject, typeNumber = 1 }:
   
 	return finalEncryptedData;
   };
+
+
+export const decodeBase64ForUIChatMessages = (messages)=> {
+	console.log('messages', messages)
+	let msgs = []
+	for(const msg of messages){
+		try {
+			const decoded = atob(msg?.data);
+			const parseDecoded = JSON.parse(decoded)
+			if(parseDecoded?.messageText){
+				msgs.push({
+					...msg,
+					...parseDecoded
+				})
+			}
+		} catch (error) {
+			
+		}
+	}
+	return msgs
+}