This is an automated email from the ASF dual-hosted git repository. LinkinStars pushed a commit to branch dev in repository https://gitbox.apache.org/repos/asf/answer.git
commit a0d902687540637d31febc7c923aa1c8c0beb140 Author: LinkinStars <[email protected]> AuthorDate: Thu Aug 20 16:26:57 2026 +0800 fix: hide hidden questions from public answer profiles --- internal/entity/answer_entity.go | 1 + internal/repo/answer/answer_repo.go | 16 ++-- .../repo_test/personal_answer_visibility_test.go | 89 ++++++++++++++++++++++ internal/service/content/question_service.go | 1 + 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/internal/entity/answer_entity.go b/internal/entity/answer_entity.go index 4c9436ec..0e93aa97 100644 --- a/internal/entity/answer_entity.go +++ b/internal/entity/answer_entity.go @@ -70,6 +70,7 @@ type PersonalAnswerPageQueryCond struct { UserID string Order string ShowPending bool + ShowHidden bool } // TableName answer table name diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go index 42e3494a..615d8dfb 100644 --- a/internal/repo/answer/answer_repo.go +++ b/internal/repo/answer/answer_repo.go @@ -394,20 +394,24 @@ func (ar *answerRepo) GetPersonalAnswerPage(ctx context.Context, req *entity.Per UserID: req.UserID, } session := ar.data.DB.Context(ctx) + if !req.ShowHidden { + session = session.Join("INNER", "question", "answer.question_id = question.id"). + And("question.show = ?", entity.QuestionShow) + } switch req.Order { case entity.AnswerSearchOrderByTime: - session = session.OrderBy("created_at desc") + session = session.OrderBy("answer.created_at desc") case entity.AnswerSearchOrderByTimeAsc: - session = session.OrderBy("created_at asc") + session = session.OrderBy("answer.created_at asc") case entity.AnswerSearchOrderByVote: - session = session.OrderBy("vote_count desc") + session = session.OrderBy("answer.vote_count desc") default: - session = session.OrderBy("adopted desc,vote_count desc,created_at asc") + session = session.OrderBy("answer.adopted desc,answer.vote_count desc,answer.created_at asc") } if req.ShowPending { - session = session.And("status != ?", entity.AnswerStatusDeleted) + session = session.And("answer.status != ?", entity.AnswerStatusDeleted) } else { - session = session.And("status = ?", entity.AnswerStatusAvailable) + session = session.And("answer.status = ?", entity.AnswerStatusAvailable) } resp = make([]*entity.Answer, 0) total, err = pager.Help(req.Page, req.PageSize, &resp, cond, session) diff --git a/internal/repo/repo_test/personal_answer_visibility_test.go b/internal/repo/repo_test/personal_answer_visibility_test.go new file mode 100644 index 00000000..f50105aa --- /dev/null +++ b/internal/repo/repo_test/personal_answer_visibility_test.go @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package repo_test + +import ( + "context" + "testing" + + "github.com/apache/answer/internal/entity" + "github.com/apache/answer/internal/repo/answer" + "github.com/apache/answer/internal/repo/question" + "github.com/apache/answer/internal/repo/unique" + "github.com/stretchr/testify/require" +) + +func TestPersonalAnswerPageRespectsHiddenQuestionVisibility(t *testing.T) { + ctx := context.Background() + questionRepo := question.NewQuestionRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) + answerRepo := answer.NewAnswerRepo(testDataSource, nil, nil, nil) + + newQuestion := func(title string, show int) *entity.Question { + q := &entity.Question{ + UserID: "personal-answer-question-owner", + Title: title, + OriginalText: title, + ParsedText: title, + Status: entity.QuestionStatusAvailable, + Show: show, + } + require.NoError(t, questionRepo.AddQuestion(ctx, q)) + return q + } + + visibleQuestion := newQuestion("visible question", entity.QuestionShow) + hiddenQuestion := newQuestion("hidden question", entity.QuestionHide) + answers := []*entity.Answer{ + {QuestionID: visibleQuestion.ID, UserID: "personal-answer-author", OriginalText: "visible answer", ParsedText: "visible answer", Status: entity.AnswerStatusAvailable}, + {QuestionID: hiddenQuestion.ID, UserID: "personal-answer-author", OriginalText: "hidden answer", ParsedText: "hidden answer", Status: entity.AnswerStatusAvailable}, + } + for _, item := range answers { + _, err := testDataSource.DB.Context(ctx).Insert(item) + require.NoError(t, err) + } + t.Cleanup(func() { + for _, item := range answers { + _, _ = testDataSource.DB.Context(ctx).ID(item.ID).Delete(&entity.Answer{}) + } + for _, item := range []*entity.Question{visibleQuestion, hiddenQuestion} { + _, _ = testDataSource.DB.Context(ctx).ID(item.ID).Delete(&entity.Question{}) + } + }) + + publicAnswers, publicTotal, err := answerRepo.GetPersonalAnswerPage(ctx, &entity.PersonalAnswerPageQueryCond{ + Page: 1, + PageSize: 20, + UserID: "personal-answer-author", + }) + require.NoError(t, err) + require.Equal(t, int64(1), publicTotal) + require.Len(t, publicAnswers, 1) + require.Equal(t, visibleQuestion.ID, publicAnswers[0].QuestionID) + + ownerAnswers, ownerTotal, err := answerRepo.GetPersonalAnswerPage(ctx, &entity.PersonalAnswerPageQueryCond{ + Page: 1, + PageSize: 20, + UserID: "personal-answer-author", + ShowHidden: true, + }) + require.NoError(t, err) + require.Equal(t, int64(2), ownerTotal) + require.Len(t, ownerAnswers, 2) +} diff --git a/internal/service/content/question_service.go b/internal/service/content/question_service.go index f7899566..82d6e1e0 100644 --- a/internal/service/content/question_service.go +++ b/internal/service/content/question_service.go @@ -1232,6 +1232,7 @@ func (qs *QuestionService) PersonalAnswerPage(ctx context.Context, req *schema.P cond.Page = req.Page cond.PageSize = req.PageSize cond.ShowPending = req.IsAdmin || req.LoginUserID == cond.UserID + cond.ShowHidden = req.IsAdmin || req.LoginUserID == cond.UserID if req.OrderCond == "newest" { cond.Order = entity.AnswerSearchOrderByTime } else {
