You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2025/03/11 04:06:06 UTC

(answer) branch feat/1.4.5/file updated: feat(siteinfo): add external content display configuration

This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.4.5/file
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/feat/1.4.5/file by this push:
     new 54269334 feat(siteinfo): add external content display configuration
54269334 is described below

commit 54269334b671d6e039e3f8a00b3d6c52d8d882c1
Author: LinkinStars <li...@foxmail.com>
AuthorDate: Tue Mar 11 12:05:31 2025 +0800

    feat(siteinfo): add external content display configuration
---
 internal/controller/siteinfo_controller.go |  3 +++
 internal/install/install_req.go            | 17 +++++++-------
 internal/migrations/init.go                | 32 ++++++++++++++++++--------
 internal/migrations/v25.go                 | 36 +++++++++++++++++++++++++++++-
 internal/schema/siteinfo_schema.go         |  7 ++++++
 5 files changed, 77 insertions(+), 18 deletions(-)

diff --git a/internal/controller/siteinfo_controller.go b/internal/controller/siteinfo_controller.go
index 3ee0e040..a336fb24 100644
--- a/internal/controller/siteinfo_controller.go
+++ b/internal/controller/siteinfo_controller.go
@@ -91,6 +91,9 @@ func (sc *SiteInfoController) GetSiteInfo(ctx *gin.Context) {
 	if err != nil {
 		log.Error(err)
 	}
+	if legal, err := sc.siteInfoService.GetSiteLegal(ctx); err == nil {
+		resp.Legal = &schema.SiteLegalSimpleResp{ExternalContentDisplay: legal.ExternalContentDisplay}
+	}
 
 	handler.HandleResponse(ctx, nil, resp)
 }
diff --git a/internal/install/install_req.go b/internal/install/install_req.go
index 8b2db0a8..c35917ab 100644
--- a/internal/install/install_req.go
+++ b/internal/install/install_req.go
@@ -96,14 +96,15 @@ type InitEnvironmentResp struct {
 
 // InitBaseInfoReq init base info request
 type InitBaseInfoReq struct {
-	Language      string `validate:"required,gt=0,lte=30" json:"lang"`
-	SiteName      string `validate:"required,sanitizer,gt=0,lte=30" json:"site_name"`
-	SiteURL       string `validate:"required,gt=0,lte=512,url" json:"site_url"`
-	ContactEmail  string `validate:"required,email,gt=0,lte=500" json:"contact_email"`
-	AdminName     string `validate:"required,gt=3,lte=30" json:"name"`
-	AdminPassword string `validate:"required,gte=8,lte=32" json:"password"`
-	AdminEmail    string `validate:"required,email,gt=0,lte=500" json:"email"`
-	LoginRequired bool   `json:"login_required"`
+	Language               string `validate:"required,gt=0,lte=30" json:"lang"`
+	SiteName               string `validate:"required,sanitizer,gt=0,lte=30" json:"site_name"`
+	SiteURL                string `validate:"required,gt=0,lte=512,url" json:"site_url"`
+	ContactEmail           string `validate:"required,email,gt=0,lte=500" json:"contact_email"`
+	AdminName              string `validate:"required,gt=3,lte=30" json:"name"`
+	AdminPassword          string `validate:"required,gte=8,lte=32" json:"password"`
+	AdminEmail             string `validate:"required,email,gt=0,lte=500" json:"email"`
+	LoginRequired          bool   `json:"login_required"`
+	ExternalContentDisplay string `validate:"required,oneof=always_display ask_before_display" json:"external_content_display"`
 }
 
 func (r *InitBaseInfoReq) Check() (errFields []*validator.FormErrorField, err error) {
diff --git a/internal/migrations/init.go b/internal/migrations/init.go
index c51d0b72..e36c960b 100644
--- a/internal/migrations/init.go
+++ b/internal/migrations/init.go
@@ -50,14 +50,15 @@ func NewMentor(ctx context.Context, engine *xorm.Engine, data *InitNeedUserInput
 }
 
 type InitNeedUserInputData struct {
-	Language      string
-	SiteName      string
-	SiteURL       string
-	ContactEmail  string
-	AdminName     string
-	AdminPassword string
-	AdminEmail    string
-	LoginRequired bool
+	Language               string
+	SiteName               string
+	SiteURL                string
+	ContactEmail           string
+	AdminName              string
+	AdminPassword          string
+	AdminEmail             string
+	LoginRequired          bool
+	ExternalContentDisplay string
 }
 
 func (m *Mentor) InitDB() error {
@@ -79,6 +80,7 @@ func (m *Mentor) InitDB() error {
 	m.do("init site info user config", m.initSiteInfoUsersConfig)
 	m.do("init site info privilege rank", m.initSiteInfoPrivilegeRank)
 	m.do("init site info write", m.initSiteInfoWrite)
+	m.do("init site info legal", m.initSiteInfoLegalConfig)
 	m.do("init default content", m.initDefaultContent)
 	m.do("init default badges", m.initDefaultBadges)
 	return m.err
@@ -185,7 +187,7 @@ func (m *Mentor) initSiteInfoGeneralData() {
 }
 
 func (m *Mentor) initSiteInfoLoginConfig() {
-	loginConfig := map[string]bool{
+	loginConfig := map[string]interface{}{
 		"allow_new_registrations":   true,
 		"allow_email_registrations": true,
 		"allow_password_login":      true,
@@ -199,6 +201,18 @@ func (m *Mentor) initSiteInfoLoginConfig() {
 	})
 }
 
+func (m *Mentor) initSiteInfoLegalConfig() {
+	legalConfig := map[string]interface{}{
+		"external_content_display": m.userData.ExternalContentDisplay,
+	}
+	legalConfigDataBytes, _ := json.Marshal(legalConfig)
+	_, m.err = m.engine.Context(m.ctx).Insert(&entity.SiteInfo{
+		Type:    "legal",
+		Content: string(legalConfigDataBytes),
+		Status:  1,
+	})
+}
+
 func (m *Mentor) initSiteInfoThemeConfig() {
 	themeConfig := `{"theme":"default","theme_config":{"default":{"navbar_style":"colored","primary_color":"#0033ff"}}}`
 	_, m.err = m.engine.Context(m.ctx).Insert(&entity.SiteInfo{
diff --git a/internal/migrations/v25.go b/internal/migrations/v25.go
index a0e1f1c2..560a852a 100644
--- a/internal/migrations/v25.go
+++ b/internal/migrations/v25.go
@@ -21,11 +21,45 @@ package migrations
 
 import (
 	"context"
+	"encoding/json"
+	"fmt"
 
 	"github.com/apache/answer/internal/entity"
 	"xorm.io/xorm"
 )
 
 func addFileRecord(ctx context.Context, x *xorm.Engine) error {
-	return x.Context(ctx).Sync(new(entity.FileRecord))
+	if err := x.Context(ctx).Sync(new(entity.FileRecord)); err != nil {
+		return err
+	}
+
+	// Set default external_content_display to always_display
+	legalInfo := &entity.SiteInfo{Type: "legal"}
+	exist, err := x.Context(ctx).Get(legalInfo)
+	if err != nil {
+		return fmt.Errorf("get legal config failed: %w", err)
+	}
+	legalConfig := make(map[string]interface{})
+	if exist {
+		if err := json.Unmarshal([]byte(legalInfo.Content), &legalConfig); err != nil {
+			return fmt.Errorf("unmarshal legal config failed: %w", err)
+		}
+	}
+	legalConfig["external_content_display"] = "always_display"
+	legalConfigBytes, _ := json.Marshal(legalConfig)
+	if exist {
+		legalInfo.Content = string(legalConfigBytes)
+		_, err = x.Context(ctx).ID(legalInfo.ID).Cols("content").Update(legalInfo)
+		if err != nil {
+			return fmt.Errorf("update legal config failed: %w", err)
+		}
+	} else {
+		legalInfo.Content = string(legalConfigBytes)
+		legalInfo.Status = 1
+		_, err = x.Context(ctx).Insert(legalInfo)
+		if err != nil {
+			return fmt.Errorf("insert legal config failed: %w", err)
+		}
+	}
+	return nil
 }
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index d71ea263..7e0c408f 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -118,6 +118,7 @@ type SiteLegalReq struct {
 	TermsOfServiceParsedText   string `json:"terms_of_service_parsed_text"`
 	PrivacyPolicyOriginalText  string `json:"privacy_policy_original_text"`
 	PrivacyPolicyParsedText    string `json:"privacy_policy_parsed_text"`
+	ExternalContentDisplay     string `validate:"required,oneof=always_display ask_before_display" json:"external_content_display"`
 }
 
 // GetSiteLegalInfoReq site site legal request
@@ -237,6 +238,11 @@ type SiteWriteResp SiteWriteReq
 // SiteLegalResp site write response
 type SiteLegalResp SiteLegalReq
 
+// SiteLegalSimpleResp site write response
+type SiteLegalSimpleResp struct {
+	ExternalContentDisplay string `validate:"required,oneof=always_display ask_before_display" json:"external_content_display"`
+}
+
 // SiteSeoResp site write response
 type SiteSeoResp SiteSeoReq
 
@@ -251,6 +257,7 @@ type SiteInfoResp struct {
 	SiteSeo       *SiteSeoResp           `json:"site_seo"`
 	SiteUsers     *SiteUsersResp         `json:"site_users"`
 	Write         *SiteWriteResp         `json:"site_write"`
+	Legal         *SiteLegalSimpleResp   `json:"site_legal"`
 	Version       string                 `json:"version"`
 	Revision      string                 `json:"revision"`
 }