Skip to content

Commit 4384079

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 4384079

4 files changed

Lines changed: 33 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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,16 @@ func (qs *QuestionCommon) FormatQuestionsPage(
367367
formattedQuestions = make([]*schema.QuestionPageResp, 0)
368368
questionIDs := make([]string, 0)
369369
userIDs := make([]string, 0)
370+
371+
isShortLink := qs.isShortLinkEnabled(ctx)
372+
370373
for _, questionInfo := range questionList {
374+
questionID := questionInfo.ID
375+
if isShortLink {
376+
questionID = uid.EnShortID(questionInfo.ID)
377+
}
371378
t := &schema.QuestionPageResp{
372-
ID: questionInfo.ID,
379+
ID: questionID,
373380
CreatedAt: questionInfo.CreatedAt.Unix(),
374381
Title: questionInfo.Title,
375382
UrlTitle: htmltext.UrlTitle(questionInfo.Title),
@@ -444,7 +451,7 @@ func (qs *QuestionCommon) FormatQuestionsPage(
444451
}
445452

446453
for _, item := range formattedQuestions {
447-
tags, ok := tagsMap[item.ID]
454+
tags, ok := tagsMap[uid.DeShortID(item.ID)]
448455
if ok {
449456
item.Tags = tags
450457
} else {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package questioncommon
2+
3+
import "context"
4+
5+
// isShortLinkEnabled returns true if the site is configured to use short IDs in URLs.
6+
func (qs *QuestionCommon) isShortLinkEnabled(ctx context.Context) bool {
7+
if qs.siteInfoService == nil {
8+
return false
9+
}
10+
seo, err := qs.siteInfoService.GetSiteSeo(ctx)
11+
if err != nil {
12+
return false
13+
}
14+
return seo.IsShortLink()
15+
}

0 commit comments

Comments
 (0)