subtitle.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/job/main/dm2/model"
  9. filterMdl "go-common/app/service/main/filter/api/grpc/v1"
  10. "go-common/library/database/sql"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _filterAreaSubtitle = "subtitle"
  16. _filterCapacity = 5
  17. _contentSizeLimit = 300
  18. )
  19. // SubtitleFilter .
  20. // 1、只处理状态为审核待检测,发布待检测的数据
  21. // 2、如果是审核待检测的数据,检测完毕,状态改为带审核,删除草稿缓存,删除字幕缓存
  22. // 3、如果检测失败,状态改为审核驳回,并且更新驳回理由 删除缓存
  23. // 4、如果是发布待检测的状态,检测完毕,状态改为发布,更新数据库逻辑,发布表更新,删除字幕缓存
  24. // 5、如果发布检测失败。状态改为审核驳回,并且更新驳回理由。删除缓存
  25. // 6、如果消费失败 数据丢失 容错
  26. func (s *Service) SubtitleFilter(c context.Context, oid int64, subtitleID int64) (err error) {
  27. var (
  28. subtitle *model.Subtitle
  29. )
  30. if subtitle, err = s.dao.GetSubtitle(c, oid, subtitleID); err != nil {
  31. log.Error("params(oid::%v,subtitleID:%v)", oid, subtitleID)
  32. return
  33. }
  34. if subtitle == nil {
  35. log.Error("params(oid:%v,subtitleID:%v not found)", oid, subtitleID)
  36. return
  37. }
  38. switch subtitle.Status {
  39. case model.SubtitleStatusCheckToAudit:
  40. if err = s.checkToAudit(c, subtitle); err != nil {
  41. log.Error("checkToAudit.params(subtitle:%+v),error(%v)", subtitle, err)
  42. return
  43. }
  44. case model.SubtitleStatusCheckPublish:
  45. if err = s.checkToPublish(c, subtitle); err != nil {
  46. log.Error("checkToPublish.params(subtitle:%+v),error(%v)", subtitle, err)
  47. return
  48. }
  49. default:
  50. return
  51. }
  52. return
  53. }
  54. func (s *Service) checkToAudit(c context.Context, subtitle *model.Subtitle) (err error) {
  55. var (
  56. status = model.SubtitleStatusToAudit
  57. hits []string
  58. )
  59. if hits, err = s.checkBfsData(c, subtitle); err != nil {
  60. log.Error("checkBfsData(subtitle:%+v),error(%v)", subtitle, err)
  61. return
  62. }
  63. if len(hits) > 0 {
  64. subtitle.RejectComment = "敏感词:" + strings.Join(hits, ",")
  65. status = model.SubtitleStatusAuditBack
  66. subtitle.PubTime = time.Now().Unix()
  67. }
  68. subtitle.Status = status
  69. if err = s.dao.UpdateSubtitle(c, subtitle); err != nil {
  70. log.Error("UpdateSubtitleStatus(subtitle:%+v),error(%v)", subtitle, err)
  71. return
  72. }
  73. s.dao.DelSubtitleDraftCache(c, subtitle.Oid, subtitle.Type, subtitle.Mid, subtitle.Lan)
  74. s.dao.DelSubtitleCache(c, subtitle.Oid, subtitle.ID)
  75. return
  76. }
  77. func (s *Service) checkToPublish(c context.Context, subtitle *model.Subtitle) (err error) {
  78. var (
  79. status = model.SubtitleStatusPublish
  80. hits []string
  81. )
  82. if hits, err = s.checkBfsData(c, subtitle); err != nil && err != ecode.SubtitleSizeLimit {
  83. log.Error("checkBfsData(subtitle:%+v),error(%v)", subtitle, err)
  84. return
  85. }
  86. if err == ecode.SubtitleSizeLimit {
  87. subtitle.RejectComment = "单条字幕数超过限制"
  88. status = model.SubtitleStatusAuditBack
  89. }
  90. if len(hits) > 0 {
  91. subtitle.RejectComment = "敏感词:" + strings.Join(hits, ",")
  92. status = model.SubtitleStatusAuditBack
  93. }
  94. subtitle.Status = status
  95. if err = s.dao.UpdateSubtitle(c, subtitle); err != nil {
  96. log.Error("UpdateSubtitleStatus(subtitle:%+v),error(%v)", subtitle, err)
  97. return
  98. }
  99. if status == model.SubtitleStatusPublish {
  100. if err = s.auditPass(c, subtitle); err != nil {
  101. log.Error("auditPass(subtitle:%+v),error(%v)", subtitle, err)
  102. return
  103. }
  104. return
  105. }
  106. if err = s.auditReject(c, subtitle); err != nil {
  107. log.Error("auditReject(subtitle:%+v),error(%v)", subtitle, err)
  108. return
  109. }
  110. return
  111. }
  112. // checkBfsData .
  113. func (s *Service) checkBfsData(c context.Context, subtitle *model.Subtitle) (hits []string, err error) {
  114. var (
  115. body *model.SubtitleBody
  116. bs []byte
  117. )
  118. if bs, err = s.dao.BfsData(c, subtitle.SubtitleURL); err != nil {
  119. log.Error("BfsData.params(SubtitleURL:%v),error(%v)", subtitle.SubtitleURL, err)
  120. return
  121. }
  122. body = &model.SubtitleBody{}
  123. if err = json.Unmarshal(bs, body); err != nil {
  124. log.Error("checkToAudit.Unmarshal,error(%v)", err)
  125. return
  126. }
  127. if hits, err = s.checkFilter(c, body); err != nil {
  128. log.Error("checkFilter(body:%+v),error(%v)", body, err)
  129. return
  130. }
  131. return
  132. }
  133. // checkFilter .
  134. func (s *Service) checkFilter(c context.Context, body *model.SubtitleBody) (hits []string, err error) {
  135. var (
  136. msgMap map[string]string
  137. msgMaps []map[string]string
  138. reply *filterMdl.MHitReply
  139. hitMap map[string]struct{}
  140. )
  141. msgMap = make(map[string]string)
  142. for idx, item := range body.Bodys {
  143. if len(item.Content) > _contentSizeLimit {
  144. err = ecode.SubtitleSizeLimit
  145. return
  146. }
  147. msgMap[fmt.Sprint(idx)] = item.Content
  148. if (idx+1)%_filterCapacity == 0 {
  149. msgMaps = append(msgMaps, msgMap)
  150. msgMap = make(map[string]string)
  151. }
  152. }
  153. if len(msgMap) > 0 {
  154. msgMaps = append(msgMaps, msgMap)
  155. }
  156. hitMap = make(map[string]struct{})
  157. for _, msgMap = range msgMaps {
  158. if reply, err = s.filterRPC.MHit(c, &filterMdl.MHitReq{
  159. Area: _filterAreaSubtitle,
  160. MsgMap: msgMap,
  161. }); err != nil {
  162. log.Error("checkFilter(msgMap:%+v),error(%v)", msgMap, err)
  163. return
  164. }
  165. for _, rl := range reply.GetRMap() {
  166. for _, hit := range rl.GetHits() {
  167. hitMap[hit] = struct{}{}
  168. }
  169. }
  170. }
  171. for k := range hitMap {
  172. hits = append(hits, k)
  173. }
  174. return
  175. }
  176. // auditReject subtitle reject
  177. func (s *Service) auditReject(c context.Context, subtitle *model.Subtitle) (err error) {
  178. subtitle.Status = model.SubtitleStatusAuditBack
  179. if err = s.dao.UpdateSubtitle(c, subtitle); err != nil {
  180. log.Error("params(%+v).error(%v)", subtitle, err)
  181. return
  182. }
  183. s.dao.DelSubtitleDraftCache(context.Background(), subtitle.Oid, subtitle.Type, subtitle.Mid, subtitle.Lan)
  184. s.dao.DelSubtitleCache(context.Background(), subtitle.Oid, subtitle.ID)
  185. return
  186. }
  187. // auditPass .
  188. func (s *Service) auditPass(c context.Context, subtitle *model.Subtitle) (err error) {
  189. var (
  190. tx *sql.Tx
  191. subtitlePub *model.SubtitlePub
  192. )
  193. defer func() {
  194. if err != nil {
  195. tx.Rollback()
  196. log.Error("params(subtitle:%+v).err(%v)", subtitle, err)
  197. return
  198. }
  199. if err = tx.Commit(); err != nil {
  200. log.Error("params(subtitle:%+v).err(%v)", subtitle, err)
  201. return
  202. }
  203. }()
  204. subtitle.RejectComment = ""
  205. if tx, err = s.dao.BeginBiliDMTran(c); err != nil {
  206. log.Error("error(%v)", err)
  207. return
  208. }
  209. if err = s.dao.TxUpdateSubtitle(tx, subtitle); err != nil {
  210. log.Error("params(%+v).error(%v)", subtitle, err)
  211. return
  212. }
  213. subtitlePub = &model.SubtitlePub{
  214. Oid: subtitle.Oid,
  215. Type: subtitle.Type,
  216. Lan: subtitle.Lan,
  217. SubtitleID: subtitle.ID,
  218. }
  219. if err = s.dao.TxAddSubtitlePub(tx, subtitlePub); err != nil {
  220. log.Error("params(%+v).error(%v)", subtitlePub, err)
  221. return
  222. }
  223. if err = s.dao.DelSubtitleCache(c, subtitle.Oid, subtitle.ID); err != nil {
  224. log.Error("DelSubtitleCache.params(subtitle:%+v).err(%v)", subtitle, err)
  225. return
  226. }
  227. if err = s.dao.DelVideoSubtitleCache(c, subtitle.Oid, subtitle.Type); err != nil {
  228. log.Error("DelVideoSubtitleCache.params(subtitle:%+v).err(%v)", subtitle, err)
  229. return
  230. }
  231. return
  232. }