answer/internal/schema/siteinfo_schema.go

350 lines
13 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package schema
import (
"context"
"fmt"
"net/mail"
"net/url"
2023-04-12 10:35:40 +08:00
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/base/validator"
"github.com/segmentfault/pacman/errors"
)
2022-09-27 17:59:05 +08:00
// SiteGeneralReq site general request
type SiteGeneralReq struct {
2023-02-15 15:51:12 +08:00
Name string `validate:"required,sanitizer,gt=1,lte=128" form:"name" json:"name"`
ShortDescription string `validate:"omitempty,sanitizer,gt=3,lte=255" form:"short_description" json:"short_description"`
Description string `validate:"omitempty,sanitizer,gt=3,lte=2000" form:"description" json:"description"`
SiteUrl string `validate:"required,sanitizer,gt=1,lte=512,url" form:"site_url" json:"site_url"`
ContactEmail string `validate:"required,sanitizer,gt=1,lte=512,email" form:"contact_email" json:"contact_email"`
2022-09-27 17:59:05 +08:00
}
func (r *SiteGeneralReq) FormatSiteUrl() {
parsedUrl, err := url.Parse(r.SiteUrl)
if err != nil {
return
}
r.SiteUrl = fmt.Sprintf("%s://%s", parsedUrl.Scheme, parsedUrl.Host)
}
2022-09-27 17:59:05 +08:00
// SiteInterfaceReq site interface request
type SiteInterfaceReq struct {
2023-04-17 18:04:28 +08:00
Language string `validate:"required,gt=1,lte=128" form:"language" json:"language"`
TimeZone string `validate:"required,gt=1,lte=128" form:"time_zone" json:"time_zone"`
2022-09-27 17:59:05 +08:00
}
2022-11-14 11:50:00 +08:00
// SiteBrandingReq site branding request
type SiteBrandingReq struct {
2023-01-11 11:19:55 +08:00
Logo string `validate:"omitempty,gt=0,lte=512" form:"logo" json:"logo"`
2022-11-14 11:50:00 +08:00
MobileLogo string `validate:"omitempty,gt=0,lte=512" form:"mobile_logo" json:"mobile_logo"`
2023-01-11 11:19:55 +08:00
SquareIcon string `validate:"omitempty,gt=0,lte=512" form:"square_icon" json:"square_icon"`
2022-11-14 11:50:00 +08:00
Favicon string `validate:"omitempty,gt=0,lte=512" form:"favicon" json:"favicon"`
}
2022-11-14 15:17:49 +08:00
// SiteWriteReq site write request
type SiteWriteReq struct {
RequiredTag bool `validate:"omitempty" form:"required_tag" json:"required_tag"`
2022-11-16 14:39:58 +08:00
RecommendTags []string `validate:"omitempty" form:"recommend_tags" json:"recommend_tags"`
ReservedTags []string `validate:"omitempty" form:"reserved_tags" json:"reserved_tags"`
2022-11-15 11:24:39 +08:00
UserID string `json:"-"`
2022-11-14 15:17:49 +08:00
}
// SiteLegalReq site branding request
type SiteLegalReq struct {
TermsOfServiceOriginalText string `json:"terms_of_service_original_text"`
TermsOfServiceParsedText string `json:"terms_of_service_parsed_text"`
PrivacyPolicyOriginalText string `json:"privacy_policy_original_text"`
PrivacyPolicyParsedText string `json:"privacy_policy_parsed_text"`
}
// GetSiteLegalInfoReq site site legal request
type GetSiteLegalInfoReq struct {
InfoType string `validate:"required,oneof=tos privacy" form:"info_type"`
}
func (r *GetSiteLegalInfoReq) IsTOS() bool {
return r.InfoType == "tos"
}
func (r *GetSiteLegalInfoReq) IsPrivacy() bool {
return r.InfoType == "privacy"
}
// GetSiteLegalInfoResp get site legal info response
type GetSiteLegalInfoResp struct {
TermsOfServiceOriginalText string `json:"terms_of_service_original_text,omitempty"`
TermsOfServiceParsedText string `json:"terms_of_service_parsed_text,omitempty"`
PrivacyPolicyOriginalText string `json:"privacy_policy_original_text,omitempty"`
PrivacyPolicyParsedText string `json:"privacy_policy_parsed_text,omitempty"`
}
// SiteUsersReq site users config request
type SiteUsersReq struct {
2023-05-15 11:20:50 +08:00
DefaultAvatar string `validate:"required,oneof=system gravatar" json:"default_avatar"`
GravatarBaseURL string `json:"gravatar_base_url"`
AllowUpdateDisplayName bool `json:"allow_update_display_name"`
AllowUpdateUsername bool `json:"allow_update_username"`
AllowUpdateAvatar bool `json:"allow_update_avatar"`
AllowUpdateBio bool `json:"allow_update_bio"`
AllowUpdateWebsite bool `json:"allow_update_website"`
AllowUpdateLocation bool `json:"allow_update_location"`
}
// SiteLoginReq site login request
type SiteLoginReq struct {
AllowNewRegistrations bool `json:"allow_new_registrations"`
AllowEmailRegistrations bool `json:"allow_email_registrations"`
LoginRequired bool `json:"login_required"`
AllowEmailDomains []string `json:"allow_email_domains"`
}
// SiteCustomCssHTMLReq site custom css html
type SiteCustomCssHTMLReq struct {
2023-04-17 17:59:53 +08:00
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"`
CustomSideBar string `validate:"omitempty,gt=0,lte=65536" json:"custom_sidebar"`
}
// 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"`
}
type SiteSeoReq struct {
PermaLink int `validate:"required,lte=4,gte=0" form:"permalink" json:"permalink"`
Robots string `validate:"required" form:"robots" json:"robots"`
}
func (s *SiteSeoResp) IsShortLink() bool {
return s.PermaLink == constant.PermaLinkQuestionIDAndTitleByShortID ||
s.PermaLink == constant.PermaLinkQuestionIDByShortID
}
2022-09-27 17:59:05 +08:00
// SiteGeneralResp site general response
type SiteGeneralResp SiteGeneralReq
// SiteInterfaceResp site interface response
type SiteInterfaceResp SiteInterfaceReq
2022-11-14 11:50:00 +08:00
// SiteBrandingResp site branding response
type SiteBrandingResp SiteBrandingReq
// SiteLoginResp site login response
type SiteLoginResp SiteLoginReq
// SiteCustomCssHTMLResp site custom css html response
type SiteCustomCssHTMLResp SiteCustomCssHTMLReq
// SiteUsersResp site users response
type SiteUsersResp SiteUsersReq
// SiteThemeResp site theme response
type SiteThemeResp struct {
ThemeOptions []*ThemeOption `json:"theme_options"`
Theme string `json:"theme"`
ThemeConfig map[string]interface{} `json:"theme_config"`
}
func (s *SiteThemeResp) TrTheme(ctx context.Context) {
la := handler.GetLangByCtx(ctx)
for _, option := range s.ThemeOptions {
tr := translator.Tr(la, option.Value)
// if tr is equal the option value means not found translation, so use the original label
if tr != option.Value {
option.Label = tr
}
}
}
// ThemeOption get label option
type ThemeOption struct {
Label string `json:"label"`
Value string `json:"value"`
}
2022-11-14 15:17:49 +08:00
// SiteWriteResp site write response
type SiteWriteResp SiteWriteReq
// SiteLegalResp site write response
type SiteLegalResp SiteLegalReq
// SiteSeoResp site write response
2022-12-05 17:18:56 +08:00
type SiteSeoResp SiteSeoReq
2022-11-14 11:50:00 +08:00
// SiteInfoResp get site info response
2022-09-27 17:59:05 +08:00
type SiteInfoResp struct {
General *SiteGeneralResp `json:"general"`
Interface *SiteInterfaceResp `json:"interface"`
Branding *SiteBrandingResp `json:"branding"`
Login *SiteLoginResp `json:"login"`
Theme *SiteThemeResp `json:"theme"`
CustomCssHtml *SiteCustomCssHTMLResp `json:"custom_css_html"`
SiteSeo *SiteSeoResp `json:"site_seo"`
SiteUsers *SiteUsersResp `json:"site_users"`
Version string `json:"version"`
2023-03-24 11:49:23 +08:00
Revision string `json:"revision"`
2022-09-27 17:59:05 +08:00
}
2022-11-30 18:10:38 +08:00
type TemplateSiteInfoResp struct {
2022-12-14 17:09:33 +08:00
General *SiteGeneralResp `json:"general"`
Interface *SiteInterfaceResp `json:"interface"`
Branding *SiteBrandingResp `json:"branding"`
SiteSeo *SiteSeoResp `json:"site_seo"`
2022-12-14 17:09:33 +08:00
CustomCssHtml *SiteCustomCssHTMLResp `json:"custom_css_html"`
Title string
Year string
Canonical string
JsonLD string
Keywords string
Description string
2022-11-30 18:10:38 +08:00
}
2022-10-20 16:38:56 +08:00
// UpdateSMTPConfigReq get smtp config request
type UpdateSMTPConfigReq struct {
FromEmail string `validate:"omitempty,gt=0,lte=256" json:"from_email"`
FromName string `validate:"omitempty,gt=0,lte=256" json:"from_name"`
SMTPHost string `validate:"omitempty,gt=0,lte=256" json:"smtp_host"`
SMTPPort int `validate:"omitempty,min=1,max=65535" json:"smtp_port"`
Encryption string `validate:"omitempty,oneof=SSL" json:"encryption"` // "" SSL
SMTPUsername string `validate:"omitempty,gt=0,lte=256" json:"smtp_username"`
SMTPPassword string `validate:"omitempty,gt=0,lte=256" json:"smtp_password"`
SMTPAuthentication bool `validate:"omitempty" json:"smtp_authentication"`
TestEmailRecipient string `validate:"omitempty,email" json:"test_email_recipient"`
2022-10-20 16:38:56 +08:00
}
func (r *UpdateSMTPConfigReq) Check() (errField []*validator.FormErrorField, err error) {
_, err = mail.ParseAddress(r.FromName)
if err == nil {
return append(errField, &validator.FormErrorField{
ErrorField: "from_name",
ErrorMsg: reason.SMTPConfigFromNameCannotBeEmail,
}), errors.BadRequest(reason.SMTPConfigFromNameCannotBeEmail)
}
return nil, nil
}
2022-10-20 16:38:56 +08:00
// GetSMTPConfigResp get smtp config response
type GetSMTPConfigResp struct {
FromEmail string `json:"from_email"`
FromName string `json:"from_name"`
SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"`
Encryption string `json:"encryption"` // "" SSL
SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"`
SMTPAuthentication bool `json:"smtp_authentication"`
2022-10-20 16:38:56 +08:00
}
// GetManifestJsonResp get manifest json response
type GetManifestJsonResp struct {
ManifestVersion int `json:"manifest_version"`
Version string `json:"version"`
2023-03-24 11:49:23 +08:00
Revision string `json:"revision"`
ShortName string `json:"short_name"`
Name string `json:"name"`
Icons map[string]string `json:"icons"`
StartUrl string `json:"start_url"`
Display string `json:"display"`
ThemeColor string `json:"theme_color"`
BackgroundColor string `json:"background_color"`
}
2023-04-11 10:27:18 +08:00
2023-04-12 10:35:40 +08:00
const (
// PrivilegeLevel1 low
PrivilegeLevel1 PrivilegeLevel = 1
// PrivilegeLevel2 medium
PrivilegeLevel2 PrivilegeLevel = 2
// PrivilegeLevel3 high
PrivilegeLevel3 PrivilegeLevel = 3
)
type PrivilegeLevel int
type PrivilegeOptions []*PrivilegeOption
func (p PrivilegeOptions) Choose(level PrivilegeLevel) (option *PrivilegeOption) {
for _, op := range p {
if op.Level == level {
return op
}
}
return nil
}
2023-04-12 10:35:40 +08:00
// GetPrivilegesConfigResp get privileges config response
2023-04-11 10:27:18 +08:00
type GetPrivilegesConfigResp struct {
2023-04-12 10:35:40 +08:00
Options []*PrivilegeOption `json:"options"`
SelectedLevel PrivilegeLevel `json:"selected_level"`
2023-04-11 10:27:18 +08:00
}
2023-04-12 10:35:40 +08:00
// PrivilegeOption privilege option
type PrivilegeOption struct {
Level PrivilegeLevel `json:"level"`
2023-04-17 18:04:28 +08:00
LevelDesc string `json:"level_desc"`
2023-04-12 10:35:40 +08:00
Privileges []*constant.Privilege `json:"privileges"`
}
// UpdatePrivilegesConfigReq update privileges config request
2023-04-11 10:27:18 +08:00
type UpdatePrivilegesConfigReq struct {
2023-04-12 10:35:40 +08:00
Level PrivilegeLevel `validate:"required,min=1,max=3" json:"level"`
}
var (
DefaultPrivilegeOptions PrivilegeOptions
2023-04-12 10:35:40 +08:00
privilegeOptionsLevelMapping = map[string][]int{
constant.RankQuestionAddKey: {1, 1, 1},
constant.RankAnswerAddKey: {1, 1, 1},
constant.RankCommentAddKey: {1, 1, 1},
constant.RankReportAddKey: {1, 1, 1},
constant.RankCommentVoteUpKey: {1, 1, 1},
constant.RankLinkUrlLimitKey: {1, 10, 10},
constant.RankQuestionVoteUpKey: {1, 8, 15},
constant.RankAnswerVoteUpKey: {1, 8, 15},
2023-04-12 10:35:40 +08:00
constant.RankQuestionVoteDownKey: {125, 125, 125},
constant.RankAnswerVoteDownKey: {125, 125, 125},
constant.RankInviteSomeoneToAnswerKey: {1, 500, 1000},
2023-04-12 10:35:40 +08:00
constant.RankTagAddKey: {1, 750, 1500},
constant.RankTagEditKey: {1, 50, 100},
constant.RankQuestionEditKey: {1, 100, 200},
constant.RankAnswerEditKey: {1, 100, 200},
constant.RankQuestionEditWithoutReviewKey: {1, 1000, 2000},
constant.RankAnswerEditWithoutReviewKey: {1, 1000, 2000},
constant.RankQuestionAuditKey: {1, 1000, 2000},
constant.RankAnswerAuditKey: {1, 1000, 2000},
constant.RankTagAuditKey: {1, 2500, 5000},
constant.RankTagEditWithoutReviewKey: {1, 10000, 20000},
constant.RankTagSynonymKey: {1, 10000, 20000},
}
)
func init() {
2023-04-17 18:04:28 +08:00
DefaultPrivilegeOptions = append(DefaultPrivilegeOptions, &PrivilegeOption{
Level: PrivilegeLevel1,
LevelDesc: reason.PrivilegeLevel1Desc,
}, &PrivilegeOption{
Level: PrivilegeLevel2,
LevelDesc: reason.PrivilegeLevel2Desc,
}, &PrivilegeOption{
Level: PrivilegeLevel3,
LevelDesc: reason.PrivilegeLevel3Desc,
})
for _, option := range DefaultPrivilegeOptions {
2023-04-12 10:35:40 +08:00
for _, privilege := range constant.RankAllPrivileges {
if len(privilegeOptionsLevelMapping[privilege.Key]) == 0 {
continue
}
2023-04-17 18:04:28 +08:00
option.Privileges = append(option.Privileges, &constant.Privilege{
2023-04-12 10:35:40 +08:00
Label: privilege.Label,
2023-04-17 18:04:28 +08:00
Value: privilegeOptionsLevelMapping[privilege.Key][option.Level-1],
2023-04-12 10:35:40 +08:00
Key: privilege.Key,
})
}
}
2023-04-11 10:27:18 +08:00
}