creative_list.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "time"
  6. "go-common/app/interface/openplatform/article/dao"
  7. "go-common/app/interface/openplatform/article/model"
  8. filter "go-common/app/service/main/filter/model/rpc"
  9. "go-common/library/database/sql"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. // const _novel = 16
  15. const _positionStep = 1000
  16. // CreativeUpLists up list
  17. func (s *Service) CreativeUpLists(c context.Context, mid int64) (novel bool, lists []*model.CreativeList, err error) {
  18. if err = s.checkPrivilege(c, mid); err != nil {
  19. return
  20. }
  21. // var count int64
  22. // if count, err = s.dao.CreativeCountArticles(c, mid, s.novelCIDs()); err != nil {
  23. // return
  24. // }
  25. // novel = count > 0
  26. novel = true
  27. ls, err := s.dao.CreativeUpLists(c, mid)
  28. if err != nil {
  29. return
  30. }
  31. sortLists(ls)
  32. var ids []int64
  33. for _, l := range ls {
  34. ids = append(ids, l.ID)
  35. }
  36. arts, err := s.dao.CreativeListsArticles(c, ids)
  37. if err != nil {
  38. return
  39. }
  40. for _, l := range ls {
  41. lists = append(lists, &model.CreativeList{List: l, Total: len(arts[l.ID])})
  42. }
  43. return
  44. }
  45. func sortLists(lists []*model.List) {
  46. sort.Slice(lists, func(i, j int) bool {
  47. it := int64(lists[i].Ctime)
  48. jt := int64(lists[j].Ctime)
  49. if int64(lists[i].UpdateTime) > it {
  50. it = int64(lists[i].UpdateTime)
  51. }
  52. if int64(lists[j].UpdateTime) > jt {
  53. jt = int64(lists[j].UpdateTime)
  54. }
  55. return it > jt
  56. })
  57. }
  58. func (s *Service) filter(c context.Context, content string) (res string, err error) {
  59. arg := filter.ArgFilter{Area: "article", Message: content}
  60. var filterRes *filter.FilterRes
  61. if filterRes, err = s.filterRPC.FilterArea(c, &arg); err != nil {
  62. dao.PromError("creative:过滤服务")
  63. log.Errorv(c, log.KV("log", "s.filterRPC.Filter"), log.KV("content", content), log.KV("err", err))
  64. res = content
  65. return
  66. }
  67. res = filterRes.Result
  68. return
  69. }
  70. // CreativeAddList add list
  71. func (s *Service) CreativeAddList(c context.Context, mid int64, name string, summary, imageURL string) (res *model.List, err error) {
  72. if err = s.checkPrivilege(c, mid); err != nil {
  73. return
  74. }
  75. if lists, _ := s.dao.CreativeUpLists(c, mid); len(lists) >= s.c.Article.ListLimit {
  76. err = ecode.ArtMaxListErr
  77. return
  78. }
  79. var newName, newSummary string
  80. newName, _ = s.filter(c, name)
  81. if newName != name {
  82. log.Infov(c, log.KV("log", "filter list"), log.KV("old", name), log.KV("new", newName))
  83. }
  84. if summary != "" {
  85. newSummary, _ = s.filter(c, summary)
  86. if newSummary != summary {
  87. log.Infov(c, log.KV("log", "filter list summary"), log.KV("old", summary), log.KV("new", newSummary))
  88. }
  89. }
  90. name = newName
  91. summary = newSummary
  92. var ok bool
  93. if name, ok = s.checkTitle(name); !ok || name == "" {
  94. log.Errorv(c, log.KV("log", "CreativeAddList"), log.KV("name", name), log.KV("mid", mid))
  95. err = ecode.ArtListNameErr
  96. return
  97. }
  98. id, err := s.dao.CreativeListAdd(c, mid, name, imageURL, summary, xtime.Time(0), 0)
  99. if err != nil {
  100. return
  101. }
  102. res, err = s.dao.RawList(c, id)
  103. cache.Save(func() {
  104. s.dao.RebuildUpListsCache(context.TODO(), mid)
  105. })
  106. return
  107. }
  108. // CreativeDelList del list
  109. func (s *Service) CreativeDelList(c context.Context, mid int64, id int64) (err error) {
  110. if err = s.checkPrivilege(c, mid); err != nil {
  111. return
  112. }
  113. if _, err = s.checkList(c, mid, id); err != nil {
  114. return
  115. }
  116. arts, _ := s.dao.CreativeListArticles(c, id)
  117. err = s.dao.CreativeListDel(c, id)
  118. if err != nil {
  119. return
  120. }
  121. err = s.dao.CreativeListDelAllArticles(c, id)
  122. // del list cache/articles cache/article list cache
  123. cache.Save(func() {
  124. c := context.TODO()
  125. var aids []int64
  126. for _, a := range arts {
  127. aids = append(aids, a.ID)
  128. }
  129. s.deleteArtsListCache(c, aids...)
  130. s.deleteListArtsCache(c, id)
  131. s.deleteListCache(c, id)
  132. s.dao.RebuildUpListsCache(context.TODO(), mid)
  133. })
  134. return
  135. }
  136. // func (s *Service) novelCIDs() (cids []int64) {
  137. // for _, a := range s.categoriesReverseMap[_novel] {
  138. // cids = append(cids, a.ID)
  139. // }
  140. // return
  141. // }
  142. func (s *Service) creativeNotAddListArticles(c context.Context, mid int64) (res []*model.ListArtMeta, err error) {
  143. // cids := s.novelCIDs()
  144. arts, err := s.dao.CreativeCategoryArticles(c, mid)
  145. if err != nil {
  146. return
  147. }
  148. lists, err := s.dao.CreativeUpLists(c, mid)
  149. if err != nil {
  150. return
  151. }
  152. var ids []int64
  153. for _, l := range lists {
  154. ids = append(ids, l.ID)
  155. }
  156. listsArts, err := s.dao.CreativeListsArticles(c, ids)
  157. if err != nil {
  158. return
  159. }
  160. exists := make(map[int64]bool)
  161. for _, la := range listsArts {
  162. for _, a := range la {
  163. exists[a.ID] = true
  164. }
  165. }
  166. for _, art := range arts {
  167. if !exists[art.ID] {
  168. res = append(res, art)
  169. }
  170. }
  171. return
  172. }
  173. // CreativeCanAddArticles can added passed articles
  174. func (s *Service) CreativeCanAddArticles(c context.Context, mid int64) (res []*model.ListArtMeta, err error) {
  175. if err = s.checkPrivilege(c, mid); err != nil {
  176. return
  177. }
  178. arts, err := s.creativeNotAddListArticles(c, mid)
  179. if err != nil {
  180. return
  181. }
  182. for _, art := range arts {
  183. if art.IsNormal() {
  184. res = append(res, art)
  185. }
  186. }
  187. sort.Slice(res, func(i, j int) bool {
  188. return res[i].PublishTime > res[j].PublishTime
  189. })
  190. return
  191. }
  192. // CreativeListAllArticles get read list articles
  193. func (s *Service) CreativeListAllArticles(c context.Context, mid, id int64) (list *model.List, arts []*model.ListArtMeta, err error) {
  194. if err = s.checkPrivilege(c, mid); err != nil {
  195. return
  196. }
  197. list, err = s.checkList(c, mid, id)
  198. if err != nil {
  199. return
  200. }
  201. list.Read, _ = s.dao.CacheListReadCount(c, id)
  202. arts, err = s.rawListArticles(c, id)
  203. return
  204. }
  205. func (s *Service) checkList(c context.Context, mid, id int64) (list *model.List, err error) {
  206. if id == 0 {
  207. return
  208. }
  209. list, err = s.dao.RawList(c, id)
  210. if err != nil {
  211. return
  212. }
  213. if list == nil {
  214. err = ecode.NothingFound
  215. return
  216. }
  217. if list.Mid != mid {
  218. err = ecode.ArtCreationMIDErr
  219. return
  220. }
  221. return
  222. }
  223. // CreativeUpdateListArticles update list articles
  224. func (s *Service) CreativeUpdateListArticles(c context.Context, listID int64, name, imageURL, summary string, onlyList bool, mid int64, aids []int64) (list *model.List, err error) {
  225. if err = s.checkPrivilege(c, mid); err != nil {
  226. return
  227. }
  228. list, err = s.checkList(c, mid, listID)
  229. if err != nil {
  230. return
  231. }
  232. list, err = s.CreativeUpdateList(c, listID, name, imageURL, summary, list.PublishTime, list.Words)
  233. if err != nil {
  234. return
  235. }
  236. if onlyList {
  237. cache.Save(func() {
  238. s.updateListCache(c, listID)
  239. })
  240. return
  241. }
  242. if len(aids) > s.c.Article.ListArtsLimit {
  243. err = ecode.ArtAddListLimitErr
  244. return
  245. }
  246. existArts, err := s.dao.CreativeListArticles(c, listID)
  247. if err != nil {
  248. return
  249. }
  250. existsMap := make(map[int64]bool)
  251. for _, a := range existArts {
  252. existsMap[a.ID] = true
  253. }
  254. // 过滤非自己的文章
  255. metas, err := s.CreativeCanAddArticles(c, mid)
  256. if err != nil {
  257. return
  258. }
  259. metasMap := make(map[int64]*model.ListArtMeta)
  260. for _, m := range metas {
  261. metasMap[m.ID] = m
  262. }
  263. var newAids []int64
  264. for _, aid := range aids {
  265. if existsMap[aid] || (metasMap[aid] != nil) {
  266. newAids = append(newAids, aid)
  267. }
  268. }
  269. aids = newAids
  270. // 计算排序
  271. updated, deleted := calculateListArtPosition(existArts, aids)
  272. log.Info("creative: update list update(%v) deleted(%v)", len(updated), len(deleted))
  273. var tx *sql.Tx
  274. if tx, err = s.dao.BeginTran(c); err != nil {
  275. log.Error("tx.BeginTran() error(%+v)", err)
  276. return
  277. }
  278. defer func() {
  279. if err != nil {
  280. if err1 := tx.Rollback(); err1 != nil {
  281. log.Error("tx.Rollback() error(%+v)", err1)
  282. }
  283. return
  284. }
  285. if err = tx.Commit(); err != nil {
  286. dao.PromError("creative:修改文集")
  287. log.Error("tx.Commit() error(%+v)", err)
  288. return
  289. }
  290. err = s.updateListInfo(c, listID)
  291. cache.Save(func() {
  292. s.dao.CreativeListUpdateTime(context.TODO(), listID, time.Now())
  293. s.RebuildListCache(context.TODO(), listID)
  294. s.deleteArtsListCache(context.TODO(), deleted...)
  295. })
  296. }()
  297. for _, id := range deleted {
  298. if err = s.dao.TxDelListArticle(c, tx, listID, id); err != nil {
  299. return
  300. }
  301. }
  302. for _, a := range updated {
  303. if err = s.dao.TxAddListArticle(c, tx, listID, a.ID, a.Position); err != nil {
  304. return
  305. }
  306. }
  307. return
  308. }
  309. func calculateListArtPosition(src []*model.ListArtMeta, dest []int64) (update []*model.ListArtMeta, delete []int64) {
  310. srcMap := make(map[int64]*model.ListArtMeta)
  311. for _, m := range src {
  312. srcMap[m.ID] = m
  313. }
  314. destMap := make(map[int64]bool)
  315. for _, id := range dest {
  316. destMap[id] = true
  317. }
  318. var newSrc []*model.ListArtMeta
  319. for _, m := range src {
  320. if !destMap[m.ID] {
  321. delete = append(delete, m.ID)
  322. continue
  323. }
  324. newSrc = append(newSrc, m)
  325. }
  326. if len(newSrc) == len(dest) {
  327. equal := true
  328. for i, d := range dest {
  329. if newSrc[i].ID != d {
  330. equal = false
  331. break
  332. }
  333. }
  334. if equal {
  335. return
  336. }
  337. }
  338. if len(dest) == 0 {
  339. return
  340. }
  341. for i, id := range dest {
  342. pos := (i + 1) * _positionStep
  343. if (srcMap[id] != nil) && (srcMap[id].Position == pos) {
  344. continue
  345. }
  346. update = append(update, &model.ListArtMeta{ID: id, Position: pos})
  347. }
  348. return
  349. }
  350. // CreativeUpdateList update list
  351. func (s *Service) CreativeUpdateList(c context.Context, id int64, name, imageURL, summary string, publishTime xtime.Time, words int64) (res *model.List, err error) {
  352. var newName string
  353. newName, _ = s.filter(c, name)
  354. if newName != name {
  355. log.Infov(c, log.KV("log", "filter title"), log.KV("old", name), log.KV("new", newName))
  356. }
  357. name = newName
  358. var ok bool
  359. if name, ok = s.checkTitle(name); !ok || name == "" {
  360. log.Errorv(c, log.KV("log", "CreativeUpdateList"), log.KV("name", name), log.KV("id", id))
  361. err = ecode.ArtListNameErr
  362. return
  363. }
  364. if summary != "" {
  365. summary, _ = s.filter(c, summary)
  366. }
  367. if err = s.dao.CreativeListUpdate(c, id, name, imageURL, summary, publishTime, words); err != nil {
  368. return
  369. }
  370. res, err = s.dao.RawList(c, id)
  371. if err != nil {
  372. return
  373. }
  374. cache.Save(func() {
  375. s.dao.AddCacheList(context.TODO(), res.ID, res)
  376. })
  377. return
  378. }
  379. // creativeAddArticleList set article list
  380. func (s *Service) creativeAddArticleList(c context.Context, mid, listID, articleID int64, onlyPass bool) (err error) {
  381. if listID == 0 {
  382. return
  383. }
  384. log.Infov(c, log.KV("log", "creativeSetArticleList"), log.KV("list_id", listID), log.KV("article_id", articleID), log.KV("mid", mid))
  385. defer func() {
  386. if err != nil {
  387. log.Errorv(c, log.KV("log", "creativeSetArticleList"), log.KV("list_id", listID), log.KV("article_id", articleID), log.KV("mid", mid), log.KV("err", err))
  388. }
  389. }()
  390. if _, err = s.checkList(c, mid, listID); err != nil {
  391. return
  392. }
  393. var can bool
  394. if can, err = s.checkArticleCanAddList(c, mid, articleID, onlyPass); (err != nil) || !can {
  395. log.Errorv(c, log.KV("log", "checkArticleCanAddList"), log.KV("mid", mid), log.KV("aid", articleID), log.KV("err", err), log.KV("error", err))
  396. err = ecode.ArtArtAddListErr
  397. return
  398. }
  399. arts, _ := s.dao.CreativeListArticles(c, listID)
  400. if len(arts) >= s.c.Article.ListArtsLimit {
  401. err = ecode.ArtAddListLimitErr
  402. return
  403. }
  404. position := _positionStep
  405. if len(arts) > 0 {
  406. position = arts[len(arts)-1].Position + _positionStep
  407. }
  408. err = s.dao.AddListArticle(c, listID, articleID, position)
  409. if err != nil {
  410. return
  411. }
  412. err = s.updateListInfo(c, listID)
  413. if err != nil {
  414. return
  415. }
  416. cache.Save(func() {
  417. s.dao.CreativeListUpdateTime(context.TODO(), listID, time.Now())
  418. // only update passed art
  419. s.RebuildListCache(context.TODO(), listID)
  420. })
  421. return
  422. }
  423. // CreativeUpdateArticleList update article list
  424. func (s *Service) CreativeUpdateArticleList(c context.Context, mid, aid, listID int64, onlyPass bool) (err error) {
  425. if err = s.checkPrivilege(c, mid); err != nil {
  426. return
  427. }
  428. if listID > 0 {
  429. _, err = s.checkList(c, mid, listID)
  430. if err != nil {
  431. return
  432. }
  433. }
  434. // check article
  435. meta, err := s.dao.AllArticleMeta(c, aid)
  436. if err != nil {
  437. return
  438. }
  439. if meta == nil {
  440. err = ecode.NothingFound
  441. return
  442. }
  443. if meta.Author.Mid != mid {
  444. err = ecode.ArtCreationMIDErr
  445. return
  446. }
  447. // get article list
  448. lists, err := s.dao.RawArtsListID(c, []int64{aid})
  449. if err != nil {
  450. return
  451. }
  452. oldListID := lists[aid]
  453. if oldListID == listID {
  454. return
  455. }
  456. defer func() {
  457. if err == nil {
  458. err = s.dao.CreativeListUpdateTime(c, listID, time.Now())
  459. }
  460. }()
  461. s.deleteArtsListCache(c, aid)
  462. if oldListID > 0 {
  463. err = s.dao.DelListArticle(c, oldListID, aid)
  464. if err != nil {
  465. return
  466. }
  467. err = s.updateListInfo(c, oldListID)
  468. if err != nil {
  469. return
  470. }
  471. cache.Save(func() {
  472. s.RebuildListCache(context.TODO(), oldListID)
  473. })
  474. }
  475. if listID > 0 {
  476. err = s.creativeAddArticleList(c, mid, listID, aid, onlyPass)
  477. }
  478. return
  479. }
  480. func (s *Service) checkArticleCanAddList(c context.Context, mid, aid int64, onlyPass bool) (res bool, err error) {
  481. metas, err := s.creativeNotAddListArticles(c, mid)
  482. if err != nil {
  483. log.Errorv(c, log.KV("log", "checkArticleCanAddList"), log.KV("mid", mid), log.KV("aid", aid), log.KV("err", err))
  484. return
  485. }
  486. metasMap := make(map[int64]*model.ListArtMeta)
  487. for _, m := range metas {
  488. metasMap[m.ID] = m
  489. }
  490. if onlyPass {
  491. res = (metasMap[aid] != nil) && metasMap[aid].IsNormal()
  492. return
  493. }
  494. res = metasMap[aid] != nil
  495. return
  496. }
  497. // updateListInfo update list words and publish_time
  498. func (s *Service) updateListInfo(c context.Context, id int64) (err error) {
  499. list, err := s.dao.RawList(c, id)
  500. if err != nil {
  501. return
  502. }
  503. if list == nil {
  504. err = ecode.NothingFound
  505. return
  506. }
  507. metas, err := s.dao.RawListArts(c, id)
  508. if err != nil {
  509. return
  510. }
  511. var words, pt int64
  512. for _, meta := range metas {
  513. if meta.IsNormal() {
  514. words += meta.Words
  515. if int64(meta.PublishTime) > pt {
  516. pt = int64(meta.PublishTime)
  517. }
  518. }
  519. }
  520. err = s.dao.CreativeListUpdate(c, id, list.Name, list.ImageURL, list.Summary, xtime.Time(pt), words)
  521. return
  522. }
  523. // RefreshList refresh list info and cache use in api
  524. func (s *Service) RefreshList(c context.Context, id int64) (err error) {
  525. if err = s.updateListInfo(c, id); err != nil {
  526. return
  527. }
  528. return s.RebuildListCache(c, id)
  529. }