Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

update short id

aichy12658b5087ec5ba
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 1b1879e6..eac2b48d 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -160,6 +160,14 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
id := ctx.Param("id")
title := ctx.Param("title")
titleIsAnswerID := false
+ NeedChangeShortID := false
+ isShortID := uid.IsShortID(id)
+ if uid.ShortIDSwitch {
+ if !isShortID {
+ id = uid.EnShortID(id)
+ NeedChangeShortID = true
+ }
+ }
objectType, objectTypeerr := obj.GetObjectTypeStrByObjectID(uid.DeShortID(title))
if objectTypeerr == nil {
@@ -167,21 +175,23 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
titleIsAnswerID = true
}
}
+
url = fmt.Sprintf("%s/questions/%s", siteInfo.General.SiteUrl, id)
if siteInfo.SiteSeo.PermaLink == schema.PermaLinkQuestionID {
+ if len(ctx.Request.URL.Query()) > 0 {
+ url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
+ }
+ if NeedChangeShortID {
+ return true, url
+ }
//not have title
if titleIsAnswerID || len(title) == 0 {
return false, ""
}
- if len(ctx.Request.URL.Query()) > 0 {
- url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
- }
+
return true, url
} else {
- //have title
- if len(title) > 0 && !titleIsAnswerID && correctTitle {
- return false, ""
- }
+
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
if err != nil {
tc.Page404(ctx)
@@ -195,6 +205,13 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
if len(ctx.Request.URL.Query()) > 0 {
url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
}
+ //have title
+ if len(title) > 0 && !titleIsAnswerID && correctTitle {
+ if NeedChangeShortID {
+ return true, url
+ }
+ return false, ""
+ }
return true, url
}
}
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index dd66fa4f..fa3aa515 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -13,10 +13,10 @@ import (
"github.com/segmentfault/pacman/errors"
)
-const PermaLinkQuestionIDAndTitle = 1
-const PermaLinkQuestionID = 2
-const PermaLinkQuestionIDAndTitleByShortID = 3
-const PermaLinkQuestionIDByShortID = 4
+const PermaLinkQuestionIDAndTitle = 1 // /questions/10010000000000001/post-title
+const PermaLinkQuestionID = 2 // /questions/10010000000000001
+const PermaLinkQuestionIDAndTitleByShortID = 3 // /questions/11/post-title
+const PermaLinkQuestionIDByShortID = 4 // /questions/11
// SiteGeneralReq site general request
type SiteGeneralReq struct {
diff --git a/pkg/uid/sid.go b/pkg/uid/sid.go
index a8880d6b..edcf27be 100644
--- a/pkg/uid/sid.go
+++ b/pkg/uid/sid.go
@@ -106,3 +106,14 @@ func DeShortID(sid string) string {
}
return sid
}
+
+func IsShortID(id string) bool {
+ num, err := strconv.ParseInt(id, 10, 64)
+ if err != nil {
+ return true
+ }
+ if num < 10000000000000000 {
+ return true
+ }
+ return false
+}