From 3ad1c40cea898a87631932183e150ff179391c19 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 7 Jun 2023 14:25:06 +0800 Subject: [PATCH 1/5] fix notificationInviteUser --- internal/service/question_service.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/service/question_service.go b/internal/service/question_service.go index cf46c580..fa92a750 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -589,13 +589,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 } From 3378cbff694adb85ebfba1e3d11dc26d2411b9ef Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:39:25 +0800 Subject: [PATCH 2/5] fix tag count upgrade --- internal/migrations/v13.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) } From 3092954e68d71860e8c47766fee8f37d383902e1 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Thu, 8 Jun 2023 10:30:26 +0800 Subject: [PATCH 3/5] fix post-title --- cmd/wire_gen.go | 4 +- internal/repo/collection/collection_repo.go | 4 ++ internal/service/question_common/question.go | 58 +++++++++++++++++++ internal/service/question_service.go | 50 +--------------- internal/service/siteinfo/siteinfo_service.go | 6 ++ 5 files changed, 71 insertions(+), 51 deletions(-) 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/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 fa92a750..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" @@ -1347,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 } From cacf5e309f6f12656ede66a2cfa6233c82528794 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:02:39 +0800 Subject: [PATCH 4/5] fix sitemap --- .../controller/template_render/question.go | 20 +++++++++++++++++++ ui/template/sitemap.xml | 4 ++++ 2 files changed, 24 insertions(+) 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/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 }} From 8e63d64f8445695ede37c261454b67fe2fb55c80 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sun, 11 Jun 2023 10:26:33 +0300 Subject: [PATCH 5/5] fix(internal/build): Using forward slash with embed fs --- internal/cli/build.go | 64 +++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/internal/cli/build.go b/internal/cli/build.go index b6c74f96..33826d24 100644 --- a/internal/cli/build.go +++ b/internal/cli/build.go @@ -4,6 +4,8 @@ import ( "bytes" "embed" "fmt" + "io" + "io/fs" "os" "os/exec" "path" @@ -286,34 +288,56 @@ func mergeI18nFiles(b *buildingMaterial) (err error) { } func copyDirEntries(sourceFs embed.FS, sourceDir string, targetDir string) (err error) { - entries, err := ui.Build.ReadDir(sourceDir) - if err != nil { - return err - } - err = dir.CreateDirIfNotExist(targetDir) if err != nil { return err } - for _, entry := range entries { - if entry.IsDir() { - err = copyDirEntries(sourceFs, filepath.Join(sourceDir, entry.Name()), filepath.Join(targetDir, entry.Name())) + err = fs.WalkDir(sourceFs, sourceDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + // Convert the path to use forward slashes, important because we use embedded FS which always uses forward slashes + path = filepath.ToSlash(path) + + // Construct the absolute path for the source file/directory + srcPath := filepath.Join(sourceDir, path) + + // Construct the absolute path for the destination file/directory + dstPath := filepath.Join(targetDir, path) + + if d.IsDir() { + // Create the directory in the destination + err := os.MkdirAll(dstPath, d.Type()) + if err != nil { + return err + } + } else { + // Open the source file + srcFile, err := sourceFs.Open(srcPath) + if err != nil { + return err + } + defer srcFile.Close() + + // Create the destination file + dstFile, err := os.Create(dstPath) + if err != nil { + return err + } + defer dstFile.Close() + + // Copy the file contents + _, err = io.Copy(dstFile, srcFile) if err != nil { return err } - continue } - file, err := sourceFs.ReadFile(filepath.Join(sourceDir, entry.Name())) - if err != nil { - return err - } - filename := filepath.Join(targetDir, entry.Name()) - err = os.WriteFile(filename, file, 0666) - if err != nil { - return err - } - } - return nil + + return nil + }) + + return err } func buildBinary(b *buildingMaterial) (err error) {