search.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. "go-common/app/job/openplatform/article/dao"
  7. "go-common/app/job/openplatform/article/model"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. "github.com/jaytaylor/html2text"
  11. )
  12. func (s *Service) updateSearchArt(c context.Context, art *model.Article) (err error) {
  13. if art == nil {
  14. return
  15. }
  16. a := artmdl.Meta{Attributes: art.Attributes}
  17. if a.AttrVal(artmdl.AttrBitNoDistribute) {
  18. return
  19. }
  20. searchArt := &model.SearchArticle{Article: *art}
  21. var (
  22. group *errgroup.Group
  23. errCtx context.Context
  24. )
  25. group, errCtx = errgroup.WithContext(c)
  26. group.Go(func() (err error) {
  27. if searchArt.Content, err = s.dao.ArticleContent(errCtx, art.ID); err != nil {
  28. return
  29. }
  30. searchArt.Content, err = extractText(searchArt.Content)
  31. return
  32. })
  33. group.Go(func() (err error) {
  34. var stat *artmdl.StatMsg
  35. if stat, err = s.dao.Stat(errCtx, art.ID); (err != nil) || (stat == nil) {
  36. return
  37. }
  38. searchArt.StatsDisLike = *stat.Dislike
  39. searchArt.StatsFavorite = *stat.Favorite
  40. searchArt.StatsLikes = *stat.Like
  41. searchArt.StatsReply = *stat.Reply
  42. searchArt.StatsShare = *stat.Share
  43. searchArt.StatsView = *stat.View
  44. searchArt.StatsCoin = *stat.Coin
  45. return
  46. })
  47. group.Go(func() (err error) {
  48. searchArt.Tags, err = s.tags(errCtx, art.ID)
  49. return
  50. })
  51. group.Go(func() (err error) {
  52. searchArt.Keywords, err = s.dao.Keywords(errCtx, art.ID)
  53. return
  54. })
  55. if err = group.Wait(); err == nil {
  56. err = s.dao.UpdateSearch(c, searchArt)
  57. }
  58. if err != nil {
  59. dao.PromError("search:更新文章")
  60. log.Error("updateSearchArt(%+v) err: %+v", searchArt, err)
  61. return
  62. }
  63. log.Info("success updateSearchArt(id: %v, title: %v)", searchArt.ID, searchArt.Title)
  64. return
  65. }
  66. func (s *Service) delSearchArt(c context.Context, aid int64) (err error) {
  67. err = s.dao.DelSearch(c, aid)
  68. return
  69. }
  70. func extractText(html string) (res string, err error) {
  71. ops := html2text.Options{PrettyTables: false}
  72. res, err = html2text.FromString(html, ops)
  73. res = strings.Replace(res, "*", "", -1)
  74. return
  75. }
  76. func (s *Service) updateSearchStats(c context.Context, stat *artmdl.StatMsg) (err error) {
  77. if err = s.dao.UpdateSearchStats(c, stat); err != nil {
  78. log.Error("updateSearchStats(%v) err: %+v", stat.String(), err)
  79. return
  80. }
  81. log.Info("updateSearchStats(%v) success", stat.String())
  82. return
  83. }