diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 26355097..1c237435 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -165,7 +165,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, answerCommon := answercommon.NewAnswerCommon(answerRepo) metaRepo := meta.NewMetaRepo(dataData) metaService := meta2.NewMetaService(metaRepo) - questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configService) + questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configService, dataData) collectionService := service.NewCollectionService(collectionRepo, collectionGroupRepo, questionCommon) collectionController := controller.NewCollectionController(collectionService) answerActivityRepo := activity.NewAnswerActivityRepo(dataData, activityRepo, userRankRepo) @@ -193,7 +193,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, reasonService := reason2.NewReasonService(reasonRepo) reasonController := controller.NewReasonController(reasonService) themeController := controller_admin.NewThemeController() - siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configService) + siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configService, questionCommon) siteInfoController := controller_admin.NewSiteInfoController(siteInfoService) siteinfoController := controller.NewSiteinfoController(siteInfoCommonService) notificationRepo := notification.NewNotificationRepo(dataData) diff --git a/internal/controller/template_render/question.go b/internal/controller/template_render/question.go index fc0e5bab..6504d7e9 100644 --- a/internal/controller/template_render/question.go +++ b/internal/controller/template_render/question.go @@ -25,6 +25,11 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) { log.Error("get site general failed:", err) return } + siteInfo, err := t.siteInfoService.GetSiteSeo(ctx) + if err != nil { + log.Error("get site GetSiteSeo failed:", err) + return + } sitemapInfo := &schema.SiteMapList{} infoStr, err := t.data.Cache.GetString(ctx, schema.SitemapCachekey) @@ -32,6 +37,10 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) { log.Errorf("get Cache failed: %s", err) return } + hasTitle := false + if siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitle || siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitleByShortID { + hasTitle = true + } if err = json.Unmarshal([]byte(infoStr), sitemapInfo); err != nil { log.Errorf("get sitemap info failed: %s", err) return @@ -45,6 +54,7 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) { "xmlHeader": template.HTML(``), "list": sitemapInfo.QuestionIDs, "general": general, + "hastitle": hasTitle, }, ) } else { @@ -68,6 +78,15 @@ func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error log.Error("get site general failed:", err) return err } + siteInfo, err := t.siteInfoService.GetSiteSeo(ctx) + if err != nil { + log.Error("get site GetSiteSeo failed:", err) + return err + } + hasTitle := false + if siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitle || siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitleByShortID { + hasTitle = true + } cachekey := fmt.Sprintf(schema.SitemapPageCachekey, page) infoStr, err := t.data.Cache.GetString(ctx, cachekey) @@ -85,6 +104,7 @@ func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error "xmlHeader": template.HTML(``), "list": sitemapInfo.PageData, "general": general, + "hastitle": hasTitle, }, ) return nil diff --git a/internal/migrations/v13.go b/internal/migrations/v13.go index 3576abb2..36b2df5e 100644 --- a/internal/migrations/v13.go +++ b/internal/migrations/v13.go @@ -224,7 +224,7 @@ func updateTagCount(x *xorm.Engine) error { } } else { tag.QuestionCount = 0 - if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil { + if _, err = x.Cols("question_count").Update(tag, &entity.Tag{ID: tag.ID}); err != nil { log.Errorf("update %+v tag failed: %s", tag.ID, err) return fmt.Errorf("update tag failed: %w", err) } diff --git a/internal/repo/collection/collection_repo.go b/internal/repo/collection/collection_repo.go index 6c05a38d..f21a323a 100644 --- a/internal/repo/collection/collection_repo.go +++ b/internal/repo/collection/collection_repo.go @@ -10,6 +10,7 @@ import ( "github.com/answerdev/answer/internal/entity" collectioncommon "github.com/answerdev/answer/internal/service/collection_common" "github.com/answerdev/answer/internal/service/unique" + "github.com/answerdev/answer/pkg/uid" "github.com/segmentfault/pacman/errors" "xorm.io/xorm" ) @@ -148,6 +149,9 @@ func (cr *collectionRepo) GetCollectionPage(ctx context.Context, page, pageSize // SearchObjectCollected check object is collected or not func (cr *collectionRepo) SearchObjectCollected(ctx context.Context, userID string, objectIds []string) (map[string]bool, error) { collectedMap := make(map[string]bool) + for k, object_id := range objectIds { + objectIds[k] = uid.DeShortID(object_id) + } list, err := cr.SearchByObjectIDsAndUser(ctx, userID, objectIds) if err != nil { err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.go index 8d96ab43..b0ccc4cc 100644 --- a/internal/service/question_common/question.go +++ b/internal/service/question_common/question.go @@ -3,9 +3,12 @@ package questioncommon import ( "context" "encoding/json" + "fmt" + "math" "time" "github.com/answerdev/answer/internal/base/constant" + "github.com/answerdev/answer/internal/base/data" "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/service/activity_common" @@ -64,6 +67,7 @@ type QuestionCommon struct { AnswerCommon *answercommon.AnswerCommon metaService *meta.MetaService configService *config.ConfigService + data *data.Data } func NewQuestionCommon(questionRepo QuestionRepo, @@ -76,6 +80,8 @@ func NewQuestionCommon(questionRepo QuestionRepo, answerCommon *answercommon.AnswerCommon, metaService *meta.MetaService, configService *config.ConfigService, + data *data.Data, + ) *QuestionCommon { return &QuestionCommon{ questionRepo: questionRepo, @@ -88,6 +94,7 @@ func NewQuestionCommon(questionRepo QuestionRepo, AnswerCommon: answerCommon, metaService: metaService, configService: configService, + data: data, } } @@ -543,6 +550,57 @@ func (as *QuestionCommon) RemoveAnswer(ctx context.Context, id string) (err erro return as.answerRepo.RemoveAnswer(ctx, id) } +func (qs *QuestionCommon) SitemapCron(ctx context.Context) { + data := &schema.SiteMapList{} + questionNum, err := qs.questionRepo.GetQuestionCount(ctx) + if err != nil { + log.Error("GetQuestionCount error", err) + return + } + if questionNum <= schema.SitemapMaxSize { + questionIDList, err := qs.questionRepo.GetQuestionIDsPage(ctx, 0, int(questionNum)) + if err != nil { + log.Error("GetQuestionIDsPage error", err) + return + } + data.QuestionIDs = questionIDList + + } else { + nums := make([]int, 0) + totalpages := int(math.Ceil(float64(questionNum) / float64(schema.SitemapMaxSize))) + for i := 1; i <= totalpages; i++ { + siteMapPagedata := &schema.SiteMapPageList{} + nums = append(nums, i) + questionIDList, err := qs.questionRepo.GetQuestionIDsPage(ctx, i, int(schema.SitemapMaxSize)) + if err != nil { + log.Error("GetQuestionIDsPage error", err) + return + } + siteMapPagedata.PageData = questionIDList + if setCacheErr := qs.SetCache(ctx, fmt.Sprintf(schema.SitemapPageCachekey, i), siteMapPagedata); setCacheErr != nil { + log.Errorf("set sitemap cron SetCache failed: %s", setCacheErr) + } + } + data.MaxPageNum = nums + } + if setCacheErr := qs.SetCache(ctx, schema.SitemapCachekey, data); setCacheErr != nil { + log.Errorf("set sitemap cron SetCache failed: %s", setCacheErr) + } +} + +func (qs *QuestionCommon) SetCache(ctx context.Context, cachekey string, info interface{}) error { + infoStr, err := json.Marshal(info) + if err != nil { + return errors.InternalServer(reason.UnknownError).WithError(err).WithStack() + } + + err = qs.data.Cache.SetString(ctx, cachekey, string(infoStr), schema.DashBoardCacheTime) + if err != nil { + return errors.InternalServer(reason.UnknownError).WithError(err).WithStack() + } + return nil +} + func (qs *QuestionCommon) ShowListFormat(ctx context.Context, data *entity.Question) *schema.QuestionInfo { return qs.ShowFormat(ctx, data) } diff --git a/internal/service/question_service.go b/internal/service/question_service.go index cf46c580..378b27df 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -3,7 +3,6 @@ package service import ( "encoding/json" "fmt" - "math" "strings" "time" @@ -589,13 +588,16 @@ func (qs *QuestionService) UpdateQuestionInviteUser(ctx context.Context, req *sc //send notification oldInviteUserIDsStr := originQuestion.InviteUserID oldInviteUserIDs := make([]string, 0) + needSendNotificationUserIDs := make([]string, 0) if oldInviteUserIDsStr != "" { err = json.Unmarshal([]byte(oldInviteUserIDsStr), &oldInviteUserIDs) if err == nil { - needSendNotificationUserIDs := converter.ArrayNotInArray(oldInviteUserIDs, inviteUserIDs) - go qs.notificationInviteUser(ctx, needSendNotificationUserIDs, originQuestion.ID, originQuestion.Title, req.UserID) + needSendNotificationUserIDs = converter.ArrayNotInArray(oldInviteUserIDs, inviteUserIDs) } + } else { + needSendNotificationUserIDs = inviteUserIDs } + go qs.notificationInviteUser(ctx, needSendNotificationUserIDs, originQuestion.ID, originQuestion.Title, req.UserID) return nil } @@ -1344,52 +1346,5 @@ func (qs *QuestionService) changeQuestionToRevision(ctx context.Context, questio } func (qs *QuestionService) SitemapCron(ctx context.Context) { - data := &schema.SiteMapList{} - questionNum, err := qs.questionRepo.GetQuestionCount(ctx) - if err != nil { - log.Error("GetQuestionCount error", err) - return - } - if questionNum <= schema.SitemapMaxSize { - questionIDList, err := qs.questionRepo.GetQuestionIDsPage(ctx, 0, int(questionNum)) - if err != nil { - log.Error("GetQuestionIDsPage error", err) - return - } - data.QuestionIDs = questionIDList - - } else { - nums := make([]int, 0) - totalpages := int(math.Ceil(float64(questionNum) / float64(schema.SitemapMaxSize))) - for i := 1; i <= totalpages; i++ { - siteMapPagedata := &schema.SiteMapPageList{} - nums = append(nums, i) - questionIDList, err := qs.questionRepo.GetQuestionIDsPage(ctx, i, int(schema.SitemapMaxSize)) - if err != nil { - log.Error("GetQuestionIDsPage error", err) - return - } - siteMapPagedata.PageData = questionIDList - if setCacheErr := qs.SetCache(ctx, fmt.Sprintf(schema.SitemapPageCachekey, i), siteMapPagedata); setCacheErr != nil { - log.Errorf("set sitemap cron SetCache failed: %s", setCacheErr) - } - } - data.MaxPageNum = nums - } - if setCacheErr := qs.SetCache(ctx, schema.SitemapCachekey, data); setCacheErr != nil { - log.Errorf("set sitemap cron SetCache failed: %s", setCacheErr) - } -} - -func (qs *QuestionService) SetCache(ctx context.Context, cachekey string, info interface{}) error { - infoStr, err := json.Marshal(info) - if err != nil { - return errors.InternalServer(reason.UnknownError).WithError(err).WithStack() - } - - err = qs.data.Cache.SetString(ctx, cachekey, string(infoStr), schema.DashBoardCacheTime) - if err != nil { - return errors.InternalServer(reason.UnknownError).WithError(err).WithStack() - } - return nil + qs.questioncommon.SitemapCron(ctx) } diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go index b8cb276e..5d0fc294 100644 --- a/internal/service/siteinfo/siteinfo_service.go +++ b/internal/service/siteinfo/siteinfo_service.go @@ -13,6 +13,7 @@ import ( "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/config" "github.com/answerdev/answer/internal/service/export" + questioncommon "github.com/answerdev/answer/internal/service/question_common" "github.com/answerdev/answer/internal/service/siteinfo_common" tagcommon "github.com/answerdev/answer/internal/service/tag_common" "github.com/answerdev/answer/pkg/uid" @@ -28,6 +29,7 @@ type SiteInfoService struct { emailService *export.EmailService tagCommonService *tagcommon.TagCommonService configService *config.ConfigService + questioncommon *questioncommon.QuestionCommon } func NewSiteInfoService( @@ -36,6 +38,8 @@ func NewSiteInfoService( emailService *export.EmailService, tagCommonService *tagcommon.TagCommonService, configService *config.ConfigService, + questioncommon *questioncommon.QuestionCommon, + ) *SiteInfoService { plugin.RegisterGetSiteURLFunc(func() string { generalSiteInfo, err := siteInfoCommonService.GetSiteGeneral(context.Background()) @@ -52,6 +56,7 @@ func NewSiteInfoService( emailService: emailService, tagCommonService: tagCommonService, configService: configService, + questioncommon: questioncommon, } } @@ -295,6 +300,7 @@ func (s *SiteInfoService) SaveSeo(ctx context.Context, req schema.SiteSeoReq) (e } else { uid.ShortIDSwitch = false } + s.questioncommon.SitemapCron(ctx) return } diff --git a/ui/template/sitemap.xml b/ui/template/sitemap.xml index b763c084..d4bc2ab2 100644 --- a/ui/template/sitemap.xml +++ b/ui/template/sitemap.xml @@ -2,7 +2,11 @@ {{ range .list }} + {{if $.hastitle}} {{$.general.SiteUrl}}/questions/{{.ID}}/{{.Title}} + {{else}} + {{$.general.SiteUrl}}/questions/{{.ID}} + {{end}} {{.UpdateTime}} {{ end }}