Skip to content

Commit e481a48

Browse files
committed
fix: use short IDs for question IDs in page list response
FormatQuestionsPage was returning raw long IDs instead of short IDs when short ID mode is enabled. Also fix the tag map lookup to use DeShortID so it works correctly after the ID has been encoded.
1 parent fca80ab commit e481a48

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

internal/base/handler/short_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/apache/answer/internal/base/constant"
2626
)
2727

28-
// GetEnableShortID get language from header
28+
// GetEnableShortID gets the ShortID enable flag from context
2929
func GetEnableShortID(ctx context.Context) bool {
3030
flag, ok := ctx.Value(constant.ShortIDContextKey).(bool)
3131
if ok {

internal/cli/build.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os/exec"
2929
"path"
3030
"path/filepath"
31+
"runtime"
3132
"strings"
3233
"text/template"
3334

@@ -61,7 +62,7 @@ func main() {
6162
`
6263
goModTpl = `module answer
6364
64-
go 1.23
65+
go 1.25
6566
`
6667
)
6768

@@ -241,14 +242,10 @@ func movePluginToVendor(b *buildingMaterial) (err error) {
241242

242243
// copyUIFiles copy ui files from answer module to tmp dir
243244
func copyUIFiles(b *buildingMaterial) (err error) {
244-
goListCmd := b.newExecCmd("go", "list", "-mod=mod", "-m", "-f", "{{.Dir}}", "github.com/apache/answer")
245-
buf := new(bytes.Buffer)
246-
goListCmd.Stdout = buf
247-
if err = goListCmd.Run(); err != nil {
248-
return fmt.Errorf("failed to run go list: %w", err)
245+
answerDir, err := os.Getwd()
246+
if err != nil {
247+
return fmt.Errorf("failed to get current working directory: %w", err)
249248
}
250-
251-
answerDir := strings.TrimSpace(buf.String())
252249
goModUIDir := filepath.Join(answerDir, "ui")
253250
localUIBuildDir := filepath.Join(b.tmpDir, "vendor/github.com/apache/answer/ui/")
254251
// The node_modules folder generated during development will interfere packaging, so it needs to be ignored.
@@ -495,9 +492,9 @@ func formatUIPluginsDirName(dirPath string) {
495492
func buildBinary(b *buildingMaterial) (err error) {
496493
versionInfo := b.originalAnswerInfo
497494
cmdPkg := "github.com/apache/answer/cmd"
498-
ldflags := fmt.Sprintf("-X %s.Version=%s -X %s.Revision=%s -X %s.Time=%s",
499-
cmdPkg, versionInfo.Version, cmdPkg, versionInfo.Revision, cmdPkg, versionInfo.Time)
500-
err = b.newExecCmd("go", "build",
495+
ldflags := fmt.Sprintf("-s -w -X %s.Version=%s -X %s.Revision=%s -X %s.Time=%s -X %s.GoVersion=%s -extldflags -static",
496+
cmdPkg, versionInfo.Version, cmdPkg, versionInfo.Revision, cmdPkg, versionInfo.Time, cmdPkg, runtime.Version())
497+
err = b.newExecCmd("go", "build", "-trimpath",
501498
"-ldflags", ldflags, "-o", b.outputPath, ".").Run()
502499
if err != nil {
503500
return err

internal/service/question_common/question.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,23 @@ func (qs *QuestionCommon) FormatQuestionsPage(
367367
formattedQuestions = make([]*schema.QuestionPageResp, 0)
368368
questionIDs := make([]string, 0)
369369
userIDs := make([]string, 0)
370+
371+
var isShortLink bool
372+
if qs.siteInfoService != nil {
373+
if seo, err := qs.siteInfoService.GetSiteSeo(ctx); err == nil {
374+
isShortLink = seo.IsShortLink()
375+
}
376+
} else {
377+
isShortLink = handler.GetEnableShortID(ctx)
378+
}
379+
370380
for _, questionInfo := range questionList {
381+
questionID := questionInfo.ID
382+
if isShortLink {
383+
questionID = uid.EnShortID(questionInfo.ID)
384+
}
371385
t := &schema.QuestionPageResp{
372-
ID: questionInfo.ID,
386+
ID: questionID,
373387
CreatedAt: questionInfo.CreatedAt.Unix(),
374388
Title: questionInfo.Title,
375389
UrlTitle: htmltext.UrlTitle(questionInfo.Title),
@@ -444,7 +458,7 @@ func (qs *QuestionCommon) FormatQuestionsPage(
444458
}
445459

446460
for _, item := range formattedQuestions {
447-
tags, ok := tagsMap[item.ID]
461+
tags, ok := tagsMap[uid.DeShortID(item.ID)]
448462
if ok {
449463
item.Tags = tags
450464
} else {

0 commit comments

Comments
 (0)