From e028cc982dc309b88ee55216f15176bc46139225 Mon Sep 17 00:00:00 2001
From: Yang Wong <yang wang>
Date: Wed, 6 Dec 2023 17:13:44 +0100
Subject: [PATCH 1/2] fix issue:Disable unlisted post from showing up in
 profile

---
 ui/src/pages/Users/Personal/index.tsx | 60 ++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 10 deletions(-)

diff --git a/ui/src/pages/Users/Personal/index.tsx b/ui/src/pages/Users/Personal/index.tsx
index 2d205a42a..fc6250dac 100644
--- a/ui/src/pages/Users/Personal/index.tsx
+++ b/ui/src/pages/Users/Personal/index.tsx
@@ -17,19 +17,20 @@
  * under the License.
  */
 
-import { FC } from 'react';
+import { FC, useState, useEffect } from 'react';
 import { Row, Col } from 'react-bootstrap';
 import { useTranslation } from 'react-i18next';
 import { useParams, useSearchParams, Link } from 'react-router-dom';
 
-import { usePageTags } from '@/hooks';
-import { Pagination, FormatTime, Empty } from '@/components';
-import { loggedUserInfoStore } from '@/stores';
 import {
   usePersonalInfoByName,
   usePersonalTop,
   usePersonalListByTabName,
+  questionDetail,
 } from '@/services';
+import { usePageTags } from '@/hooks';
+import { Pagination, FormatTime, Empty } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
 import type { UserInfoRes } from '@/common/interface';
 
 import {
@@ -66,7 +67,46 @@ const Personal: FC = () => {
     },
     tabName,
   );
-  const { count = 0, list = [] } = listData?.[tabName] || {};
+  const { list = [] } = listData?.[tabName] || {};
+  const [filteredList, setFilteredList] = useState<any[]>([]);
+  const currentUserId = sessionUser?.id;
+  console.log(sessionUser);
+  useEffect(() => {
+    const fetchQuestionDetails = async () => {
+      let completedList;
+
+      if (tabName === 'bookmarks') {
+        completedList = list.map((item) => ({ ...item, questionInfo: item }));
+      } else {
+        const promises = list.map((item) => {
+          const questionId = item.question_id;
+          return questionDetail(questionId)
+            .then((questionInfo) => ({ ...item, questionInfo }))
+            .catch((error) => {
+              console.error(`error_ID: ${questionId}`, error);
+              return null;
+            });
+        });
+
+        completedList = await Promise.all(promises);
+      }
+
+      const filtered = completedList.filter((item) => {
+        if (!item) return false;
+        const userId = item.questionInfo?.user_info?.id;
+        const showValue = item.questionInfo?.show;
+        return !(showValue === 2 && userId !== currentUserId);
+      });
+
+      setFilteredList(filtered);
+    };
+
+    if (list.length) {
+      fetchQuestionDetails();
+    }
+  }, [list, currentUserId, tabName]);
+
+  const count = filteredList.length;
 
   let pageTitle = '';
   if (userInfo?.username) {
@@ -115,15 +155,15 @@ const Personal: FC = () => {
             visible={tabName !== 'overview'}
             tabName={tabName}
           />
-          <Answers data={list} visible={tabName === 'answers'} />
+          <Answers data={filteredList} visible={tabName === 'answers'} />
           <DefaultList
-            data={list}
+            data={filteredList}
             tabName={tabName}
             visible={tabName === 'questions' || tabName === 'bookmarks'}
           />
-          <Reputation data={list} visible={tabName === 'reputation'} />
-          <Comments data={list} visible={tabName === 'comments'} />
-          <Votes data={list} visible={tabName === 'votes'} />
+          <Reputation data={filteredList} visible={tabName === 'reputation'} />
+          <Comments data={filteredList} visible={tabName === 'comments'} />
+          <Votes data={filteredList} visible={tabName === 'votes'} />
           {!list?.length && !isLoading && <Empty />}
 
           {count > 0 && (

From f3c4efd6846b9530212994226fb1594ecbe722c9 Mon Sep 17 00:00:00 2001
From: Yang Wong <yang wang>
Date: Mon, 11 Dec 2023 00:14:14 +0100
Subject: [PATCH 2/2] fix the unlisted post show problem in user page by adding
 filter

---
 ui/src/pages/Users/Personal/index.tsx | 133 +++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 25 deletions(-)

diff --git a/ui/src/pages/Users/Personal/index.tsx b/ui/src/pages/Users/Personal/index.tsx
index fc6250dac..0d5612b9c 100644
--- a/ui/src/pages/Users/Personal/index.tsx
+++ b/ui/src/pages/Users/Personal/index.tsx
@@ -27,6 +27,7 @@ import {
   usePersonalTop,
   usePersonalListByTabName,
   questionDetail,
+  getAnswers,
 } from '@/services';
 import { usePageTags } from '@/hooks';
 import { Pagination, FormatTime, Empty } from '@/components';
@@ -54,7 +55,8 @@ const Personal: FC = () => {
   const { t } = useTranslation('translation', { keyPrefix: 'personal' });
   const sessionUser = loggedUserInfoStore((state) => state.user);
   const isSelf = sessionUser?.username === username;
-
+  const currentUserId = sessionUser?.id;
+  const roleId = sessionUser?.role_id;
   const { data: userInfo } = usePersonalInfoByName(username);
   const { data: topData } = usePersonalTop(username, tabName);
 
@@ -67,44 +69,125 @@ const Personal: FC = () => {
     },
     tabName,
   );
+  const getFullQuestionInfo = async (list) => {
+    const promises = list.map((item) => {
+      const questionId = item.question_id;
+      return questionDetail(questionId)
+        .then((questionInfo) => ({ ...item, questionInfo }))
+        .catch((error) => {
+          console.error(`error_ID: ${questionId}`, error);
+          return null;
+        });
+    });
+    const completedList = await Promise.all(promises);
+    return completedList;
+  };
+
+  const getAnswersUserId = async (list) => {
+    const promises = list.map((item) =>
+      getAnswers({
+        order: order === 'updated' ? order : 'default',
+        question_id: item.question_id,
+        page: 1,
+        page_size: 999,
+      })
+        .then((res) => ({
+          questionId: item.question_id,
+          user_ids: res.list.map((answer) => answer.user_info.id),
+        }))
+        .catch((error) => {
+          console.error(
+            `Error fetching answers for question ID: ${item.question_id}`,
+            error,
+          );
+          return { questionId: item.question_id, user_ids: [] };
+        }),
+    );
+
+    const results = await Promise.all(promises);
+    const answersMap = new Map();
+    results.forEach(({ questionId, user_ids }) =>
+      answersMap.set(questionId, user_ids),
+    );
+    return answersMap;
+  };
+  const getFiltered = (completedList, answersMap) => {
+    const filtered = completedList.filter((item) => {
+      if (!item) return false;
+      const userId = item.questionInfo?.user_info?.id;
+      const showValue = item.questionInfo?.show;
+      const userAnswered = answersMap
+        .get(item.question_id)
+        ?.includes(currentUserId);
+      return showValue !== 2 || userId === currentUserId || userAnswered;
+    });
+    return filtered;
+  };
   const { list = [] } = listData?.[tabName] || {};
+  const top = topData ?? { answer: [], question: [] };
+  console.log('top data', topData);
+  console.log('list data', list);
   const [filteredList, setFilteredList] = useState<any[]>([]);
-  const currentUserId = sessionUser?.id;
-  console.log(sessionUser);
+  const [filteredTop, setFilteredTop] = useState<{
+    answer: any[];
+    question: any[];
+  }>({ answer: [], question: [] });
+  useEffect(() => {
+    const fetchTop = async () => {
+      if (roleId === 2) {
+        setFilteredTop(top);
+        return;
+      }
+      const completedAnswers = await getFullQuestionInfo(top.answer);
+      const completedQuestion = await getFullQuestionInfo(top.question);
+      const answersAnswerMap = await getAnswersUserId(completedAnswers);
+      const questionsAnswerMap = await getAnswersUserId(completedQuestion);
+      const filteredAnswer = getFiltered(completedAnswers, answersAnswerMap);
+      const filteredQuestion = getFiltered(
+        completedQuestion,
+        questionsAnswerMap,
+      );
+      const answerQuestionIds = filteredAnswer.map((answer) => {
+        return answer.question_id;
+      });
+      const questionQuestionIds = filteredQuestion.map((question) => {
+        return question.question_id;
+      });
+      const filtered = {
+        answer: top.answer.filter((answer) =>
+          answerQuestionIds.includes(answer.question_id),
+        ),
+        question: top.question.filter((question) =>
+          questionQuestionIds.includes(question.question_id),
+        ),
+      };
+      setFilteredTop(filtered);
+    };
+    if (top.answer.length || top.question.length) {
+      fetchTop();
+    }
+  }, [top, currentUserId, roleId]);
   useEffect(() => {
     const fetchQuestionDetails = async () => {
+      if (roleId === 2) {
+        setFilteredList(list);
+        return;
+      }
       let completedList;
-
       if (tabName === 'bookmarks') {
         completedList = list.map((item) => ({ ...item, questionInfo: item }));
       } else {
-        const promises = list.map((item) => {
-          const questionId = item.question_id;
-          return questionDetail(questionId)
-            .then((questionInfo) => ({ ...item, questionInfo }))
-            .catch((error) => {
-              console.error(`error_ID: ${questionId}`, error);
-              return null;
-            });
-        });
-
-        completedList = await Promise.all(promises);
+        completedList = await getFullQuestionInfo(list);
       }
-
-      const filtered = completedList.filter((item) => {
-        if (!item) return false;
-        const userId = item.questionInfo?.user_info?.id;
-        const showValue = item.questionInfo?.show;
-        return !(showValue === 2 && userId !== currentUserId);
-      });
-
+      const answersMap = await getAnswersUserId(completedList);
+      const filtered = getFiltered(completedList, answersMap);
       setFilteredList(filtered);
     };
 
     if (list.length) {
       fetchQuestionDetails();
     }
-  }, [list, currentUserId, tabName]);
+  }, [list, currentUserId, tabName, roleId]);
 
   const count = filteredList.length;
 
@@ -147,7 +230,7 @@ const Personal: FC = () => {
           <Overview
             visible={tabName === 'overview'}
             introduction={userInfo?.bio_html || ''}
-            data={topData}
+            data={filteredTop}
           />
           <ListHead
             count={tabName === 'reputation' ? Number(userInfo?.rank) : count}