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.

feat(theme): add theme and custom css html interface

LinkinStar87bb3c72b0d6
diff --git a/internal/base/constant/constant.go b/internal/base/constant/constant.go
index 270354a0..a1d09667 100644
--- a/internal/base/constant/constant.go
+++ b/internal/base/constant/constant.go
@@ -56,13 +56,15 @@ var (
)
const (
- SiteTypeGeneral = "general"
- SiteTypeInterface = "interface"
- SiteTypeBranding = "branding"
- SiteTypeWrite = "write"
- SiteTypeLegal = "legal"
- SiteTypeSeo = "seo"
- SiteTypeLogin = "login"
+ SiteTypeGeneral = "general"
+ SiteTypeInterface = "interface"
+ SiteTypeBranding = "branding"
+ SiteTypeWrite = "write"
+ SiteTypeLegal = "legal"
+ SiteTypeSeo = "seo"
+ SiteTypeLogin = "login"
+ SiteTypeCustomCssHTML = "css-html"
+ SiteTypeTheme = "theme"
)
func ExistInPathIgnore(name string) bool {
diff --git a/internal/controller/siteinfo_controller.go b/internal/controller/siteinfo_controller.go
index f527bb82..82d23733 100644
--- a/internal/controller/siteinfo_controller.go
+++ b/internal/controller/siteinfo_controller.go
@@ -50,6 +50,11 @@ func (sc *SiteinfoController) GetSiteInfo(ctx *gin.Context) {
if err != nil {
log.Error(err)
}
+
+ resp.Theme, err = sc.siteInfoService.GetSiteTheme(ctx)
+ if err != nil {
+ log.Error(err)
+ }
handler.HandleResponse(ctx, nil, resp)
}
diff --git a/internal/controller_backyard/siteinfo_controller.go b/internal/controller_backyard/siteinfo_controller.go
index 81ca62cc..09a8bba3 100644
--- a/internal/controller_backyard/siteinfo_controller.go
+++ b/internal/controller_backyard/siteinfo_controller.go
@@ -113,6 +113,32 @@ func (sc *SiteInfoController) GetSiteLogin(ctx *gin.Context) {
handler.HandleResponse(ctx, err, resp)
}
+// GetSiteCustomCssHTML get site info custom html css config
+// @Summary get site info custom html css config
+// @Description get site info custom html css config
+// @Security ApiKeyAuth
+// @Tags admin
+// @Produce json
+// @Success 200 {object} handler.RespBody{data=schema.SiteCustomCssHTMLResp}
+// @Router /answer/admin/api/siteinfo/custom-css-html [get]
+func (sc *SiteInfoController) GetSiteCustomCssHTML(ctx *gin.Context) {
+ resp, err := sc.siteInfoService.GetSiteCustomCssHTML(ctx)
+ handler.HandleResponse(ctx, err, resp)
+}
+
+// GetSiteTheme get site info theme config
+// @Summary get site info theme config
+// @Description get site info theme config
+// @Security ApiKeyAuth
+// @Tags admin
+// @Produce json
+// @Success 200 {object} handler.RespBody{data=schema.SiteThemeResp}
+// @Router /answer/admin/api/siteinfo/theme [get]
+func (sc *SiteInfoController) GetSiteTheme(ctx *gin.Context) {
+ resp, err := sc.siteInfoService.GetSiteTheme(ctx)
+ handler.HandleResponse(ctx, err, resp)
+}
+
// GetRobots get site robots information
// @Summary get site robots information
// @Description get site robots information
@@ -257,6 +283,42 @@ func (sc *SiteInfoController) UpdateSiteLogin(ctx *gin.Context) {
handler.HandleResponse(ctx, err, nil)
}
+// UpdateSiteCustomCssHTML update site custom css html config
+// @Summary update site custom css html config
+// @Description update site custom css html config
+// @Security ApiKeyAuth
+// @Tags admin
+// @Produce json
+// @Param data body schema.SiteCustomCssHTMLReq true "login info"
+// @Success 200 {object} handler.RespBody{}
+// @Router /answer/admin/api/siteinfo/custom-css-html [put]
+func (sc *SiteInfoController) UpdateSiteCustomCssHTML(ctx *gin.Context) {
+ req := &schema.SiteCustomCssHTMLReq{}
+ if handler.BindAndCheck(ctx, req) {
+ return
+ }
+ err := sc.siteInfoService.SaveSiteCustomCssHTML(ctx, req)
+ handler.HandleResponse(ctx, err, nil)
+}
+
+// SaveSiteTheme update site custom css html config
+// @Summary update site custom css html config
+// @Description update site custom css html config
+// @Security ApiKeyAuth
+// @Tags admin
+// @Produce json
+// @Param data body schema.SiteThemeReq true "login info"
+// @Success 200 {object} handler.RespBody{}
+// @Router /answer/admin/api/siteinfo/theme [put]
+func (sc *SiteInfoController) SaveSiteTheme(ctx *gin.Context) {
+ req := &schema.SiteThemeReq{}
+ if handler.BindAndCheck(ctx, req) {
+ return
+ }
+ err := sc.siteInfoService.SaveSiteTheme(ctx, req)
+ handler.HandleResponse(ctx, err, nil)
+}
+
// GetSMTPConfig get smtp config
// @Summary GetSMTPConfig get smtp config
// @Description GetSMTPConfig get smtp config
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index be309d0c..44aaec56 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -46,6 +46,7 @@ var migrations = []Migration{
NewMigration("add recommend and reserved tag fields", addTagRecommendedAndReserved),
NewMigration("add activity timeline", addActivityTimeline),
NewMigration("add user role", addRoleFeatures),
+ NewMigration("add theme and private mode", addThemeAndPrivateMode),
}
// GetCurrentDBVersion returns the current db version
diff --git a/internal/migrations/v5.go b/internal/migrations/v5.go
new file mode 100644
index 00000000..6bae0353
--- /dev/null
+++ b/internal/migrations/v5.go
@@ -0,0 +1,22 @@
+package migrations
+
+import (
+ "encoding/json"
+
+ "github.com/answerdev/answer/internal/entity"
+ "xorm.io/xorm"
+)
+
+func addThemeAndPrivateMode(x *xorm.Engine) error {
+ loginConfig := map[string]bool{
+ "allow_new_registrations": true,
+ "login_required": false,
+ }
+ loginConfigDataBytes, _ := json.Marshal(loginConfig)
+ _, err := x.InsertOne(&entity.SiteInfo{
+ Type: "login",
+ Content: string(loginConfigDataBytes),
+ Status: 1,
+ })
+ return err
+}
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 552ae049..ff111e54 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -255,12 +255,16 @@ func (a *AnswerAPIRouter) RegisterAnswerCmsAPIRouter(r *gin.RouterGroup) {
r.GET("/siteinfo/legal", a.siteInfoController.GetSiteLegal)
r.GET("/siteinfo/seo", a.siteInfoController.GetSeo)
r.GET("/siteinfo/login", a.siteInfoController.GetSiteLogin)
+ r.GET("/siteinfo/custom-css-html", a.siteInfoController.GetSiteCustomCssHTML)
+ r.GET("/siteinfo/theme", a.siteInfoController.GetSiteTheme)
r.PUT("/siteinfo/general", a.siteInfoController.UpdateGeneral)
r.PUT("/siteinfo/interface", a.siteInfoController.UpdateInterface)
r.PUT("/siteinfo/branding", a.siteInfoController.UpdateBranding)
r.PUT("/siteinfo/write", a.siteInfoController.UpdateSiteWrite)
r.PUT("/siteinfo/legal", a.siteInfoController.UpdateSiteLegal)
r.PUT("/siteinfo/login", a.siteInfoController.UpdateSiteLogin)
+ r.PUT("/siteinfo/custom-css-html", a.siteInfoController.UpdateSiteCustomCssHTML)
+ r.PUT("/siteinfo/theme", a.siteInfoController.SaveSiteTheme)
r.PUT("/siteinfo/seo", a.siteInfoController.UpdateSeo)
r.GET("/setting/smtp", a.siteInfoController.GetSMTPConfig)
r.PUT("/setting/smtp", a.siteInfoController.UpdateSMTPConfig)
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index a7d19e75..113cc70a 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -88,6 +88,20 @@ type SiteLoginReq struct {
LoginRequired bool `json:"login_required"`
}
+// SiteCustomCssHTMLReq site custom css html
+type SiteCustomCssHTMLReq struct {
+ CustomHead string `validate:"omitempty,gt=0,lte=65536" json:"custom_head"`
+ CustomCss string `validate:"omitempty,gt=0,lte=65536" json:"custom_css"`
+ CustomHeader string `validate:"omitempty,gt=0,lte=65536" json:"custom_header"`
+ CustomFooter string `validate:"omitempty,gt=0,lte=65536" json:"custom_footer"`
+}
+
+// SiteThemeReq site theme config
+type SiteThemeReq struct {
+ Theme string `validate:"required,gt=0,lte=255" json:"theme"`
+ ThemeConfig map[string]interface{} `validate:"omitempty" json:"theme_config"`
+}
+
// SiteGeneralResp site general response
type SiteGeneralResp SiteGeneralReq
@@ -100,12 +114,19 @@ type SiteBrandingResp SiteBrandingReq
// SiteLoginResp site login response
type SiteLoginResp SiteLoginReq
+// SiteCustomCssHTMLResp site custom css html response
+type SiteCustomCssHTMLResp SiteCustomCssHTMLReq
+
+// SiteThemeResp site theme response
+type SiteThemeResp SiteThemeReq
+
// SiteWriteResp site write response
type SiteWriteResp SiteWriteReq
// SiteLegalResp site write response
type SiteLegalResp SiteLegalReq
+// SiteSeoResp site write response
type SiteSeoResp SiteSeoReq
// SiteInfoResp get site info response
@@ -114,6 +135,7 @@ type SiteInfoResp struct {
Interface *SiteInterfaceResp `json:"interface"`
Branding *SiteBrandingResp `json:"branding"`
Login *SiteLoginResp `json:"login"`
+ Theme *SiteThemeResp `json:"theme"`
}
type TemplateSiteInfoResp struct {
General *SiteGeneralResp `json:"general"`
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go
index d0e088bc..cdcad1f5 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -85,6 +85,16 @@ func (s *SiteInfoService) GetSiteLogin(ctx context.Context) (resp *schema.SiteLo
return s.siteInfoCommonService.GetSiteLogin(ctx)
}
+// GetSiteCustomCssHTML get site custom css html config
+func (s *SiteInfoService) GetSiteCustomCssHTML(ctx context.Context) (resp *schema.SiteCustomCssHTMLResp, err error) {
+ return s.siteInfoCommonService.GetSiteCustomCssHTML(ctx)
+}
+
+// GetSiteTheme get site theme config
+func (s *SiteInfoService) GetSiteTheme(ctx context.Context) (resp *schema.SiteThemeResp, err error) {
+ return s.siteInfoCommonService.GetSiteTheme(ctx)
+}
+
func (s *SiteInfoService) SaveSiteGeneral(ctx context.Context, req schema.SiteGeneralReq) (err error) {
req.FormatSiteUrl()
var (
@@ -187,6 +197,28 @@ func (s *SiteInfoService) SaveSiteLogin(ctx context.Context, req *schema.SiteLog
return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeLogin, data)
}
+// SaveSiteCustomCssHTML save site custom html configuration
+func (s *SiteInfoService) SaveSiteCustomCssHTML(ctx context.Context, req *schema.SiteCustomCssHTMLReq) (err error) {
+ content, _ := json.Marshal(req)
+ data := &entity.SiteInfo{
+ Type: constant.SiteTypeCustomCssHTML,
+ Content: string(content),
+ Status: 1,
+ }
+ return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeCustomCssHTML, data)
+}
+
+// SaveSiteTheme save site custom html configuration
+func (s *SiteInfoService) SaveSiteTheme(ctx context.Context, req *schema.SiteThemeReq) (err error) {
+ content, _ := json.Marshal(req)
+ data := &entity.SiteInfo{
+ Type: constant.SiteTypeTheme,
+ Content: string(content),
+ Status: 1,
+ }
+ return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeTheme, data)
+}
+
// GetSMTPConfig get smtp config
func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
resp *schema.GetSMTPConfigResp, err error,
diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.go
index 1925cb8c..cdb498ca 100644
--- a/internal/service/siteinfo_common/siteinfo_service.go
+++ b/internal/service/siteinfo_common/siteinfo_service.go
@@ -80,6 +80,24 @@ func (s *SiteInfoCommonService) GetSiteLogin(ctx context.Context) (resp *schema.
return resp, nil
}
+// GetSiteCustomCssHTML get site custom css html config
+func (s *SiteInfoCommonService) GetSiteCustomCssHTML(ctx context.Context) (resp *schema.SiteCustomCssHTMLResp, err error) {
+ resp = &schema.SiteCustomCssHTMLResp{}
+ if err = s.getSiteInfoByType(ctx, constant.SiteTypeCustomCssHTML, resp); err != nil {
+ return nil, err
+ }
+ return resp, nil
+}
+
+// GetSiteTheme get site theme
+func (s *SiteInfoCommonService) GetSiteTheme(ctx context.Context) (resp *schema.SiteThemeResp, err error) {
+ resp = &schema.SiteThemeResp{}
+ if err = s.getSiteInfoByType(ctx, constant.SiteTypeTheme, resp); err != nil {
+ return nil, err
+ }
+ return resp, nil
+}
+
func (s *SiteInfoCommonService) getSiteInfoByType(ctx context.Context, siteType string, resp interface{}) (err error) {
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, siteType)
if err != nil {