2022-11-03 11:12:20 +08:00
|
|
|
package siteinfo
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-11-03 10:33:48 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/constant"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
2022-11-02 15:25:27 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/entity"
|
|
|
|
"github.com/answerdev/answer/internal/schema"
|
|
|
|
"github.com/answerdev/answer/internal/service/export"
|
|
|
|
"github.com/answerdev/answer/internal/service/siteinfo_common"
|
2022-11-15 11:24:39 +08:00
|
|
|
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
|
2022-10-20 16:38:56 +08:00
|
|
|
"github.com/jinzhu/copier"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/segmentfault/pacman/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SiteInfoService struct {
|
2022-11-15 11:24:39 +08:00
|
|
|
siteInfoRepo siteinfo_common.SiteInfoRepo
|
|
|
|
emailService *export.EmailService
|
|
|
|
tagCommonService *tagcommon.TagCommonService
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:24:39 +08:00
|
|
|
func NewSiteInfoService(
|
|
|
|
siteInfoRepo siteinfo_common.SiteInfoRepo,
|
|
|
|
emailService *export.EmailService,
|
|
|
|
tagCommonService *tagcommon.TagCommonService) *SiteInfoService {
|
2022-09-27 17:59:05 +08:00
|
|
|
return &SiteInfoService{
|
2022-11-15 11:24:39 +08:00
|
|
|
siteInfoRepo: siteInfoRepo,
|
|
|
|
emailService: emailService,
|
|
|
|
tagCommonService: tagCommonService,
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 10:33:48 +08:00
|
|
|
// GetSiteGeneral get site info general
|
|
|
|
func (s *SiteInfoService) GetSiteGeneral(ctx context.Context) (resp *schema.SiteGeneralResp, err error) {
|
|
|
|
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeGeneral)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
if !exist {
|
2022-11-03 10:33:48 +08:00
|
|
|
return nil, errors.BadRequest(reason.SiteInfoNotFound)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 10:33:48 +08:00
|
|
|
resp = &schema.SiteGeneralResp{}
|
|
|
|
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
|
|
|
|
return resp, nil
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 10:33:48 +08:00
|
|
|
// GetSiteInterface get site info interface
|
|
|
|
func (s *SiteInfoService) GetSiteInterface(ctx context.Context) (resp *schema.SiteInterfaceResp, err error) {
|
|
|
|
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
if !exist {
|
2022-11-03 10:33:48 +08:00
|
|
|
return nil, errors.BadRequest(reason.SiteInfoNotFound)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-11-03 10:33:48 +08:00
|
|
|
resp = &schema.SiteInterfaceResp{}
|
|
|
|
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
|
|
|
|
return resp, nil
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-11-14 15:17:49 +08:00
|
|
|
// GetSiteBranding get site info branding
|
|
|
|
func (s *SiteInfoService) GetSiteBranding(ctx context.Context) (resp *schema.SiteBrandingReq, err error) {
|
|
|
|
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeBranding)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
return nil, errors.BadRequest(reason.SiteInfoNotFound)
|
|
|
|
}
|
|
|
|
resp = &schema.SiteBrandingReq{}
|
|
|
|
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSiteWrite get site info write
|
2022-11-15 11:24:39 +08:00
|
|
|
func (s *SiteInfoService) GetSiteWrite(ctx context.Context) (resp *schema.SiteWriteResp, err error) {
|
2022-11-14 15:17:49 +08:00
|
|
|
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeWrite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
return nil, errors.BadRequest(reason.SiteInfoNotFound)
|
|
|
|
}
|
2022-11-15 11:24:39 +08:00
|
|
|
resp = &schema.SiteWriteResp{}
|
2022-11-14 15:17:49 +08:00
|
|
|
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
|
2022-11-15 11:24:39 +08:00
|
|
|
|
|
|
|
resp.RecommendTags, err = s.tagCommonService.GetSiteWriteRecommendTag(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.ReservedTags, err = s.tagCommonService.GetSiteWriteReservedTag(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-14 15:17:49 +08:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
func (s *SiteInfoService) SaveSiteGeneral(ctx context.Context, req schema.SiteGeneralReq) (err error) {
|
2022-11-10 11:00:26 +08:00
|
|
|
req.FormatSiteUrl()
|
2022-09-27 17:59:05 +08:00
|
|
|
var (
|
|
|
|
siteType = "general"
|
|
|
|
content []byte
|
|
|
|
)
|
2022-11-01 15:25:44 +08:00
|
|
|
content, _ = json.Marshal(req)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
data := entity.SiteInfo{
|
|
|
|
Type: siteType,
|
|
|
|
Content: string(content),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.siteInfoRepo.SaveByType(ctx, siteType, &data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SiteInfoService) SaveSiteInterface(ctx context.Context, req schema.SiteInterfaceReq) (err error) {
|
|
|
|
var (
|
2022-11-02 15:25:27 +08:00
|
|
|
siteType = "interface"
|
|
|
|
themeExist bool
|
|
|
|
content []byte
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// check theme
|
|
|
|
for _, theme := range schema.GetThemeOptions {
|
|
|
|
if theme.Value == req.Theme {
|
|
|
|
themeExist = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !themeExist {
|
|
|
|
err = errors.BadRequest(reason.ThemeNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check language
|
2022-11-02 15:25:27 +08:00
|
|
|
if !translator.CheckLanguageIsValid(req.Language) {
|
2022-09-27 17:59:05 +08:00
|
|
|
err = errors.BadRequest(reason.LangNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
content, _ = json.Marshal(req)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
data := entity.SiteInfo{
|
|
|
|
Type: siteType,
|
|
|
|
Content: string(content),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.siteInfoRepo.SaveByType(ctx, siteType, &data)
|
|
|
|
return
|
|
|
|
}
|
2022-10-20 16:38:56 +08:00
|
|
|
|
2022-11-14 11:50:00 +08:00
|
|
|
// SaveSiteBranding save site branding information
|
|
|
|
func (s *SiteInfoService) SaveSiteBranding(ctx context.Context, req *schema.SiteBrandingReq) (err error) {
|
|
|
|
content, _ := json.Marshal(req)
|
|
|
|
data := &entity.SiteInfo{
|
|
|
|
Type: constant.SiteTypeBranding,
|
|
|
|
Content: string(content),
|
|
|
|
Status: 1,
|
|
|
|
}
|
|
|
|
return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeBranding, data)
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:17:49 +08:00
|
|
|
// SaveSiteWrite save site configuration about write
|
|
|
|
func (s *SiteInfoService) SaveSiteWrite(ctx context.Context, req *schema.SiteWriteReq) (err error) {
|
2022-11-15 11:24:39 +08:00
|
|
|
err = s.tagCommonService.SetSiteWriteRecommendTag(ctx, req.RecommendTags, req.RequiredTag, req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.tagCommonService.SetSiteWriteReservedTag(ctx, req.ReservedTags, req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:17:49 +08:00
|
|
|
content, _ := json.Marshal(req)
|
|
|
|
data := &entity.SiteInfo{
|
|
|
|
Type: constant.SiteTypeWrite,
|
|
|
|
Content: string(content),
|
|
|
|
Status: 1,
|
|
|
|
}
|
|
|
|
return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeWrite, data)
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:38:56 +08:00
|
|
|
// GetSMTPConfig get smtp config
|
|
|
|
func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
|
2022-11-01 15:25:44 +08:00
|
|
|
resp *schema.GetSMTPConfigResp, err error,
|
|
|
|
) {
|
2022-10-20 16:38:56 +08:00
|
|
|
emailConfig, err := s.emailService.GetEmailConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp = &schema.GetSMTPConfigResp{}
|
|
|
|
_ = copier.Copy(resp, emailConfig)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSMTPConfig get smtp config
|
|
|
|
func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) {
|
2022-10-21 17:04:41 +08:00
|
|
|
oldEmailConfig, err := s.emailService.GetEmailConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = copier.Copy(oldEmailConfig, req)
|
|
|
|
|
|
|
|
err = s.emailService.SetEmailConfig(oldEmailConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(req.TestEmailRecipient) > 0 {
|
2022-10-24 12:04:12 +08:00
|
|
|
title, body, err := s.emailService.TestTemplate(ctx)
|
2022-10-21 17:04:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go s.emailService.Send(ctx, req.TestEmailRecipient, title, body, "", "")
|
|
|
|
}
|
|
|
|
return
|
2022-10-20 16:38:56 +08:00
|
|
|
}
|