reply.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "go-common/app/admin/main/reply/model"
  10. accmdl "go-common/app/service/main/account/api"
  11. "go-common/app/service/main/archive/api"
  12. arcmdl "go-common/app/service/main/archive/model/archive"
  13. rlmdl "go-common/app/service/main/relation/model"
  14. "go-common/library/database/sql"
  15. "go-common/library/ecode"
  16. "go-common/library/log"
  17. "go-common/library/net/metadata"
  18. "go-common/library/queue/databus/report"
  19. "go-common/library/sync/errgroup"
  20. xtime "go-common/library/time"
  21. )
  22. func (s *Service) reply(c context.Context, oid, rpID int64) (rp *model.Reply, err error) {
  23. if rp, err = s.dao.Reply(c, oid, rpID); err != nil {
  24. return
  25. }
  26. if rp == nil {
  27. err = ecode.ReplyNotExist
  28. return
  29. }
  30. if rp.Content, err = s.dao.ReplyContent(c, oid, rpID); err != nil {
  31. return
  32. }
  33. if rp.Content == nil {
  34. err = ecode.ReplyNotExist
  35. }
  36. return
  37. }
  38. func (s *Service) replies(c context.Context, oids, rpIDs []int64) (res map[int64]*model.Reply, err error) {
  39. res, missIDs, err := s.dao.RepliesCache(c, rpIDs)
  40. if err != nil {
  41. return
  42. }
  43. if len(missIDs) > 0 {
  44. var (
  45. rps map[int64]*model.Reply
  46. rcs map[int64]*model.ReplyContent
  47. miss []*model.Reply
  48. missOids []int64
  49. )
  50. for _, missID := range missIDs {
  51. for i := range rpIDs {
  52. if rpIDs[i] == missID {
  53. missOids = append(missOids, oids[i])
  54. break
  55. }
  56. }
  57. }
  58. if rps, err = s.dao.Replies(c, missOids, missIDs); err != nil {
  59. return
  60. }
  61. if rcs, err = s.dao.ReplyContents(c, missOids, missIDs); err != nil {
  62. return
  63. }
  64. for id, rp := range rps {
  65. rp.Content = rcs[id]
  66. res[id] = rp
  67. miss = append(miss, rp)
  68. }
  69. s.cache.Do(c, func(ctx context.Context) {
  70. s.dao.AddReplyCache(ctx, miss...)
  71. })
  72. }
  73. return
  74. }
  75. // ReplySearch return reply result from search.
  76. func (s *Service) ReplySearch(c context.Context, sp *model.SearchParams, page, pageSize int64) (res *model.SearchResult, err error) {
  77. if res, err = s.dao.SearchReplyV3(c, sp, page, pageSize); err != nil {
  78. log.Error("s.dao.SearchReplyV3(%+v,%d,%d) error(%v) ", sp, page, pageSize, err)
  79. return
  80. }
  81. adMap := make(map[int64]*model.SearchAdminLog)
  82. var ids []int64
  83. for i := range res.Result {
  84. ids = append(ids, res.Result[i].ID)
  85. }
  86. if adres, err := s.dao.SearchAdminLog(c, ids); err == nil {
  87. for _, data := range adres {
  88. adMap[data.ReplyID] = data
  89. }
  90. }
  91. filters := make(map[int64]string, len(res.Result))
  92. links := make(map[int64]string, len(res.Result))
  93. titles := make(map[int64]string)
  94. var mids []int64
  95. for _, data := range res.Result {
  96. if log, ok := adMap[data.ID]; ok && log != nil {
  97. data.AdminID = log.AdminID
  98. data.AdminName = log.AdminName
  99. data.OpCtime = log.CTime
  100. data.Opremark = log.Remark
  101. data.Opresult = log.Result
  102. }
  103. mids = append(mids, data.Mid)
  104. data.OidStr = strconv.FormatInt(data.Oid, 10)
  105. // reply filtered
  106. if len(data.Attr) > 0 {
  107. for _, attr := range data.Attr {
  108. if attr == 4 {
  109. filters[data.ID] = data.Message
  110. }
  111. }
  112. }
  113. // show title for top reply
  114. if sp.Attr == "1" && data.Title == "" {
  115. var link string
  116. data.Title, link, _ = s.TitleLink(c, data.Oid, int32(data.Type))
  117. data.RedirectURL = fmt.Sprintf("%s#reply%d", link, data.ID)
  118. } else {
  119. links[data.Oid] = ""
  120. if int32(data.Type) == model.SubTypeArchive {
  121. titles[data.Oid] = ""
  122. }
  123. }
  124. }
  125. ip := metadata.String(c, metadata.RemoteIP)
  126. stasMap, err := s.relationSvc.Stats(c, &rlmdl.ArgMids{Mids: mids, RealIP: ip})
  127. if err == nil {
  128. for i, data := range res.Result {
  129. if stat, ok := stasMap[data.Mid]; ok {
  130. res.Result[i].Stat = stat
  131. }
  132. }
  133. } else {
  134. log.Error("relationSvc.Stats error(%v)", err)
  135. }
  136. s.linkByOids(c, links, sp.Type)
  137. s.titlesByOids(c, titles)
  138. s.dao.FilterContents(c, filters)
  139. for _, data := range res.Result {
  140. if content := filters[data.ID]; content != "" {
  141. data.Message = content
  142. }
  143. if data.RedirectURL == "" {
  144. if link := links[data.Oid]; link != "" {
  145. data.RedirectURL = fmt.Sprintf("%s#reply%d", link, data.ID)
  146. }
  147. }
  148. if int32(data.Type) == model.SubTypeArchive && data.Title == "" {
  149. if title := titles[data.Oid]; title != "" {
  150. data.Title = title
  151. }
  152. }
  153. }
  154. return
  155. }
  156. func (s *Service) titlesByOids(c context.Context, titles map[int64]string) (err error) {
  157. var aids []int64
  158. for oid := range titles {
  159. aids = append(aids, oid)
  160. }
  161. arg := &arcmdl.ArgAids2{
  162. Aids: aids,
  163. }
  164. var m map[int64]*api.Arc
  165. m, err = s.arcSrv.Archives3(c, arg)
  166. for oid := range m {
  167. titles[oid] = m[oid].Title
  168. }
  169. return
  170. }
  171. func (s *Service) linkByOids(c context.Context, oids map[int64]string, typ int32) (err error) {
  172. if len(oids) == 0 {
  173. return
  174. }
  175. if typ == model.SubTypeActivity {
  176. err = s.dao.TopicsLink(c, oids, false)
  177. } else {
  178. for oid := range oids {
  179. var link string
  180. switch typ {
  181. case model.SubTypeTopic:
  182. link = fmt.Sprintf("https://www.bilibili.com/topic/%d.html", oid)
  183. case model.SubTypeArchive:
  184. link = fmt.Sprintf("https://www.bilibili.com/video/av%d", oid)
  185. case model.SubTypeForbiden:
  186. link = fmt.Sprintf("https://www.bilibili.com/blackroom/ban/%d", oid)
  187. case model.SubTypeNotice:
  188. link = fmt.Sprintf("https://www.bilibili.com/blackroom/notice/%d", oid)
  189. case model.SubTypeActArc:
  190. _, link, err = s.dao.ActivitySub(c, oid)
  191. if err != nil {
  192. return
  193. }
  194. case model.SubTypeArticle:
  195. link = fmt.Sprintf("https://www.bilibili.com/read/cv%d", oid)
  196. case model.SubTypeLiveVideo:
  197. link = fmt.Sprintf("https://vc.bilibili.com/video/%d", oid)
  198. case model.SubTypeLiveAct:
  199. _, link, err = s.dao.LiveActivityTitle(c, oid)
  200. if err != nil {
  201. return
  202. }
  203. case model.SubTypeLivePicture:
  204. link = fmt.Sprintf("https://h.bilibili.com/ywh/%d", oid)
  205. case model.SubTypeCredit:
  206. link = fmt.Sprintf("https://www.bilibili.com/judgement/case/%d", oid)
  207. case model.SubTypeDynamic:
  208. link = fmt.Sprintf("https://t.bilibili.com/%d", oid)
  209. default:
  210. return
  211. }
  212. oids[oid] = link
  213. }
  214. }
  215. return
  216. }
  217. // AdminEditReply edit reply content by admin.
  218. func (s *Service) AdminEditReply(c context.Context, adminID int64, adName string, oid, rpID int64, tp int32, msg, remark string) (err error) {
  219. rp, err := s.reply(c, oid, rpID)
  220. if err != nil {
  221. return
  222. }
  223. if rp.IsDeleted() {
  224. err = ecode.ReplyDeleted
  225. return
  226. }
  227. now := time.Now()
  228. if _, err = s.dao.UpReplyContent(c, oid, rpID, msg, now); err != nil {
  229. log.Error("s.content.UpMessage(%d, %d, %s, %v), err is (%v)", oid, rpID, msg, now, err)
  230. return
  231. }
  232. if err = s.dao.DelReplyCache(c, rpID); err != nil {
  233. log.Error("dao.AddReplyCache(%+v,%s) rpid(%d) error(%v)", rp, msg, err)
  234. }
  235. s.addAdminLog(c, rp.Oid, rp.ID, adminID, rp.Type, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperEdit, "已修改评论内容", remark, now)
  236. s.cache.Do(c, func(ctx context.Context) {
  237. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, rp.State)
  238. })
  239. report.Manager(&report.ManagerInfo{
  240. UID: adminID,
  241. Uname: adName,
  242. Business: 41,
  243. Type: int(tp),
  244. Oid: rp.Oid,
  245. Ctime: now,
  246. Action: model.ReportActionReplyEdit,
  247. Index: []interface{}{
  248. rp.ID,
  249. rp.State,
  250. rp.State,
  251. },
  252. Content: map[string]interface{}{"remark": remark},
  253. })
  254. return
  255. }
  256. // AddTop add a top reply.
  257. func (s *Service) AddTop(c context.Context, adid int64, adName string, oid, rpID int64, typ int32, act uint32) (err error) {
  258. rp, err := s.reply(c, oid, rpID)
  259. if err != nil {
  260. return
  261. }
  262. if rp.IsFolded() {
  263. return ecode.ReplyFolded
  264. }
  265. if rp.Root != 0 {
  266. log.Error("add top reply illegal reply(oid:%v,type:%v,:rpID:%v) not root", oid, typ, rpID, err)
  267. return
  268. }
  269. sub, err := s.subject(c, oid, typ)
  270. if err != nil {
  271. log.Error("s.subject(%d,%d),err:%v", oid, typ)
  272. return
  273. }
  274. if act == model.AttrYes && sub.AttrVal(model.SubAttrTopAdmin) == model.AttrYes {
  275. err = ecode.ReplyHaveTop
  276. log.Error("Repeat to add top reply(%d,%d,%d,%d) ", rp.ID, rp.Oid, typ, sub.Attr)
  277. return
  278. }
  279. sub.AttrSet(act, model.SubAttrTopAdmin)
  280. err = sub.TopSet(rpID, 0, act)
  281. if err != nil {
  282. log.Error("sub.TopSet(%d,%d,%d) failed!err:=%v ", rp.ID, rp.Oid, 0, err)
  283. return
  284. }
  285. rp.AttrSet(act, model.AttrTopAdmin)
  286. now := time.Now()
  287. tx, err := s.dao.BeginTran(c)
  288. if err != nil {
  289. return
  290. }
  291. if _, err = s.dao.TxUpReplyAttr(tx, oid, rpID, rp.Attr, now); err != nil {
  292. tx.Rollback()
  293. return
  294. }
  295. if _, err = s.dao.TxUpSubAttr(tx, oid, typ, sub.Attr, now); err != nil {
  296. tx.Rollback()
  297. return
  298. }
  299. if _, err = s.dao.TxUpSubMeta(tx, sub.Oid, sub.Type, sub.Meta, now); err != nil {
  300. tx.Rollback()
  301. log.Error("dao.TxUpMeta(oid:%d,tp:%d) err(%v)", sub.Oid, sub.Type, err)
  302. return
  303. }
  304. if err = tx.Commit(); err != nil {
  305. return
  306. }
  307. if act == model.AttrYes {
  308. s.dao.DelIndexBySort(c, rp, model.SortByCount)
  309. s.dao.DelIndexBySort(c, rp, model.SortByLike)
  310. } else if act == model.AttrNo && rp.IsNormal() {
  311. s.addReplyIndex(c, rp)
  312. }
  313. s.dao.AddTopCache(c, rp)
  314. s.dao.AddReplyCache(c, rp)
  315. s.dao.DelSubjectCache(c, rp.Oid, rp.Type)
  316. report.Manager(&report.ManagerInfo{
  317. UID: adid,
  318. Uname: adName,
  319. Business: 41,
  320. Type: int(typ),
  321. Oid: oid,
  322. Ctime: now,
  323. Action: model.ReportActionReplyTop,
  324. Index: []interface{}{sub.Mid, act, rpID},
  325. })
  326. if act == model.AttrYes {
  327. s.pubEvent(c, "top", 0, sub, rp, nil)
  328. } else if act == model.AttrNo {
  329. s.pubEvent(c, "untop", 0, sub, rp, nil)
  330. }
  331. //add admin log and search log
  332. if act == model.AttrYes {
  333. s.addAdminLog(c, rp.Oid, rp.ID, adid, rp.Type, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperSubTop, "管理员置顶评论", "", time.Now())
  334. s.cache.Do(c, func(ctx context.Context) {
  335. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, rp.State)
  336. })
  337. } else {
  338. s.addAdminLog(c, rp.Oid, rp.ID, adid, rp.Type, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperSubTop, "管理员取消置顶评论", "", time.Now())
  339. s.cache.Do(c, func(ctx context.Context) {
  340. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, model.StateNormal)
  341. })
  342. }
  343. return
  344. }
  345. // CallbackDeleteReply delete reply by admin.
  346. func (s *Service) CallbackDeleteReply(ctx context.Context, adminID int64, oid, rpID int64, ftime int64, typ int32, moral int32, adminName, remark string, reason, freason int32) (err error) {
  347. now := time.Now()
  348. sub, rp, err := s.delReply(ctx, oid, rpID, model.StateDelAdmin, now)
  349. if err != nil {
  350. if ecode.ReplyDeleted.Equal(err) && rp.IsDeleted() {
  351. err = nil
  352. } else {
  353. log.Error("delReply(%d,%d) error(%v)", oid, rpID, err)
  354. return err
  355. }
  356. }
  357. s.delCache(ctx, sub, rp)
  358. s.cache.Do(ctx, func(ctx context.Context) {
  359. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  360. defer cancel()
  361. var rpt *model.Report
  362. if rpt, _ = s.dao.Report(ctx, oid, rpID); rpt != nil {
  363. if rpt.State == model.ReportStateNew || rpt.State == model.ReportStateNew2 {
  364. rpt.MTime = xtime.Time(now.Unix())
  365. if rpt.State == model.ReportStateNew {
  366. rpt.State = model.ReportStateDelete1
  367. } else if rpt.State == model.ReportStateNew2 {
  368. rpt.State = model.ReportStateDelete2
  369. }
  370. if _, err = s.dao.UpReportsState(ctx, []int64{rpt.Oid}, []int64{rpt.RpID}, rpt.State, now); err != nil {
  371. log.Error("s.dao.UpdateReport(%+v) error(%v)", rpt, err)
  372. }
  373. state := model.StateDelAdmin
  374. s.pubSearchReport(ctx, map[int64]*model.Report{rpt.RpID: rpt}, &state)
  375. }
  376. }
  377. s.pubEvent(context.Background(), model.EventReportDel, rpt.Mid, sub, rp, rpt)
  378. report.Manager(&report.ManagerInfo{
  379. UID: adminID,
  380. Uname: adminName,
  381. Business: 41,
  382. Type: int(typ),
  383. Oid: rp.Oid,
  384. Ctime: now,
  385. Action: model.ReportActionReplyDel,
  386. Index: []interface{}{
  387. rp.ID,
  388. rp.State,
  389. model.StateDelAdmin,
  390. },
  391. Content: map[string]interface{}{
  392. "moral": moral,
  393. "notify": false,
  394. "ftime": ftime,
  395. "freason": freason,
  396. "reason": reason,
  397. "remark": remark,
  398. },
  399. })
  400. rps := make(map[int64]*model.Reply)
  401. rps[rp.ID] = rp
  402. s.addAdminLogs(ctx, rps, adminID, typ, model.AdminIsNew, model.AdminIsReport, model.AdminOperDelete, fmt.Sprintf("已删除并封禁%s/扣除%d节操", forbidResult(ftime), moral), remark, now)
  403. s.pubSearchReply(ctx, rps, model.StateDelAdmin)
  404. })
  405. return
  406. }
  407. // AdminDeleteReply delete reply by admin.
  408. func (s *Service) AdminDeleteReply(c context.Context, adminID int64, oids, rpIDs []int64, ftime int64, typ int32, moral int32, notify bool, adminName, remark string, reason, freason int32) (err error) {
  409. err = s.adminDeleteReply(c, adminID, oids, rpIDs, ftime, typ, moral, notify, adminName, remark, reason, freason)
  410. return
  411. }
  412. func (s *Service) adminDeleteReply(c context.Context, adminID int64, oids, rpIDs []int64, ftime int64, typ int32, moral int32, notify bool, adminName, remark string, reason, freason int32) (err error) {
  413. var (
  414. lk sync.Mutex
  415. rps = make(map[int64]*model.Reply)
  416. now = time.Now()
  417. )
  418. wg := errgroup.Group{}
  419. wg.GOMAXPROCS(4)
  420. for idx := range oids {
  421. i := idx
  422. wg.Go(func() (err error) {
  423. var sub *model.Subject
  424. var rp *model.Reply
  425. // 针对大忽悠事件 特殊用户删除评论不让删
  426. var (
  427. tp int32
  428. ok bool
  429. exsits bool
  430. )
  431. tp, ok = s.oids[oids[i]]
  432. if ok && tp == typ {
  433. _, exsits = s.ads[adminName]
  434. if exsits {
  435. return
  436. }
  437. }
  438. sub, rp, err = s.delReply(c, oids[i], rpIDs[i], model.StateDelAdmin, now)
  439. if err != nil {
  440. if ecode.ReplyDeleted.Equal(err) && rp.IsDeleted() {
  441. err = nil
  442. } else {
  443. log.Error("delReply(%d,%d) error(%v)", oids[i], rpIDs[i], err)
  444. return err
  445. }
  446. }
  447. if rp.IsFolded() {
  448. s.marker.Do(c, func(ctx context.Context) {
  449. s.handleFolded(ctx, rp)
  450. })
  451. }
  452. s.delCache(c, sub, rp)
  453. s.pubEvent(c, "reply_del", 0, sub, rp, nil)
  454. rpt, _ := s.dao.Report(c, oids[i], rpIDs[i])
  455. if rpt != nil {
  456. if rpt.State == model.ReportStateNew || rpt.State == model.ReportStateNew2 {
  457. rpt.MTime = xtime.Time(now.Unix())
  458. if rpt.State == model.ReportStateNew {
  459. rpt.State = model.ReportStateDelete1
  460. } else if rpt.State == model.ReportStateNew2 {
  461. rpt.State = model.ReportStateDelete2
  462. }
  463. if _, err = s.dao.UpReportsState(c, []int64{rpt.Oid}, []int64{rpt.RpID}, rpt.State, now); err != nil {
  464. log.Error("s.dao.UpdateReport(%+v) error(%v)", rpt, err)
  465. }
  466. }
  467. }
  468. s.cache.Do(c, func(ctx context.Context) {
  469. // 针对大忽悠事件的特殊推送
  470. if _, ok := s.ads[adminName]; ok {
  471. if e := s.NotifyTroll(ctx, rp.Mid); e != nil {
  472. log.Warn("notify-troll error (%v)", e)
  473. }
  474. }
  475. s.dao.DelReport(ctx, rp.Oid, rp.ID)
  476. if rpt != nil {
  477. rpt.ReplyCtime = rp.CTime
  478. state := model.StateDelAdmin
  479. s.pubSearchReport(ctx, map[int64]*model.Report{rpt.RpID: rpt}, &state)
  480. }
  481. s.moralAndNotify(ctx, rp, moral, notify, rp.Mid, adminID, adminName, remark, reason, freason, ftime, false)
  482. })
  483. lk.Lock()
  484. rps[rp.ID] = rp
  485. lk.Unlock()
  486. report.Manager(&report.ManagerInfo{
  487. UID: adminID,
  488. Uname: adminName,
  489. Business: 41,
  490. Type: int(typ),
  491. Oid: rp.Oid,
  492. Ctime: now,
  493. Action: model.ReportActionReplyDel,
  494. Index: []interface{}{
  495. rp.ID,
  496. rp.State,
  497. model.StateDelAdmin,
  498. },
  499. Content: map[string]interface{}{
  500. "moral": moral,
  501. "notify": notify,
  502. "ftime": ftime,
  503. "freason": freason,
  504. "reason": reason,
  505. "remark": remark,
  506. },
  507. })
  508. return nil
  509. })
  510. }
  511. if err = wg.Wait(); err != nil {
  512. return
  513. }
  514. s.addAdminLogs(c, rps, adminID, typ, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperDelete, fmt.Sprintf("已删除并封禁%s/扣除%d节操", forbidResult(ftime), moral), remark, now)
  515. s.cache.Do(c, func(ctx context.Context) {
  516. s.pubSearchReply(ctx, rps, model.StateDelAdmin)
  517. })
  518. return
  519. }
  520. // AdminRecoverReply recover reply by admin.
  521. func (s *Service) AdminRecoverReply(c context.Context, adminID int64, adName string, oid, rpID int64, typ int32, remark string) (err error) {
  522. rp, err := s.reply(c, oid, rpID)
  523. if err != nil {
  524. log.Error("s.reply(%d,%d) error(%v)", oid, rpID, err)
  525. return
  526. }
  527. now := time.Now()
  528. var sub *model.Subject
  529. if sub, rp, err = s.recReply(c, rp.Oid, rp.ID, model.StateNormal, now); err != nil {
  530. log.Error("s.recReply(%d,%d) error(%v)", rp.Oid, rp.ID, err)
  531. return
  532. }
  533. s.addAdminLog(c, rp.Oid, rp.ID, adminID, rp.Type, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperRecover, "已恢复评论", remark, now)
  534. s.pubEvent(c, "reply_recover", 0, sub, rp, nil)
  535. s.cache.Do(c, func(ctx context.Context) {
  536. s.pubSearchReply(ctx, map[int64]*model.Reply{rp.ID: rp}, model.StateNormal)
  537. })
  538. report.Manager(&report.ManagerInfo{
  539. UID: adminID,
  540. Uname: adName,
  541. Business: 41,
  542. Type: int(typ),
  543. Oid: rp.Oid,
  544. Ctime: now,
  545. Action: model.ReportActionReplyRecover,
  546. Index: []interface{}{
  547. rp.ID,
  548. rp.State,
  549. model.StateNormal,
  550. },
  551. Content: map[string]interface{}{"remark": remark},
  552. })
  553. return
  554. }
  555. // AdminPassReply recover reply by admin.
  556. func (s *Service) AdminPassReply(c context.Context, adid int64, adName string, oids, rpIDs []int64, typ int32, remark string) (err error) {
  557. s.adminPassReply(c, adid, adName, oids, rpIDs, typ, remark)
  558. return
  559. }
  560. func (s *Service) adminPassReply(c context.Context, adid int64, adName string, oids, rpIDs []int64, typ int32, remark string) (err error) {
  561. now := time.Now()
  562. rps, err := s.replies(c, oids, rpIDs)
  563. if err != nil {
  564. return
  565. }
  566. wg, ctx := errgroup.WithContext(c)
  567. for _, m := range rps {
  568. rp := m
  569. wg.Go(func() (err error) {
  570. if rp.State == model.StatePending {
  571. var sub *model.Subject
  572. if sub, rp, err = s.recReply(ctx, rp.Oid, rp.ID, model.StateNormal, now); err != nil {
  573. return
  574. }
  575. s.dao.DelAuditIndex(ctx, rp)
  576. s.pubEvent(c, "reply_recover", 0, sub, rp, nil)
  577. } else {
  578. var (
  579. tx *sql.Tx
  580. rows int64
  581. )
  582. if tx, err = s.dao.BeginTran(ctx); err != nil {
  583. return
  584. }
  585. if rows, err = s.dao.TxUpdateReplyState(tx, rp.Oid, rp.ID, model.StateNormal, now); err != nil || rows == 0 {
  586. log.Error("dao.Reply.TxUpdateReplyState(%v,%d) error(%v)", rp, model.StateNormal, err)
  587. tx.Rollback()
  588. return
  589. }
  590. if rp.State == model.StateMonitor {
  591. if _, err = s.dao.TxSubDecrMCount(tx, rp.Oid, rp.Type, now); err != nil {
  592. log.Error("dao.Reply.TxSubDecrMCount(%v) error(%v)", rp, err)
  593. tx.Rollback()
  594. return
  595. }
  596. }
  597. if err = tx.Commit(); err != nil {
  598. log.Error("tx.Commit error(%v)", err)
  599. return
  600. }
  601. if err = s.dao.DelReplyCache(ctx, rp.ID); err != nil {
  602. log.Error("s.dao.DelReplyCache(%d,%d) error(%v)", rp.Oid, rp.ID, err)
  603. }
  604. }
  605. report.Manager(&report.ManagerInfo{
  606. UID: adid,
  607. Uname: adName,
  608. Business: 41,
  609. Type: int(typ),
  610. Oid: rp.Oid,
  611. Ctime: now,
  612. Action: model.ReportActionReplyPass,
  613. Index: []interface{}{
  614. rp.ID,
  615. rp.State,
  616. model.StateNormal,
  617. },
  618. Content: map[string]interface{}{"remark": remark},
  619. })
  620. return
  621. })
  622. }
  623. if err = wg.Wait(); err != nil {
  624. return
  625. }
  626. s.addAdminLogs(c, rps, adid, typ, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperPass, "已通过评论", remark, now)
  627. s.cache.Do(c, func(ctx context.Context) {
  628. s.pubSearchReply(ctx, rps, model.StateNormal)
  629. })
  630. return
  631. }
  632. // addReplyIndex add reply index to redis.
  633. func (s *Service) addReplyIndex(c context.Context, rp *model.Reply) (err error) {
  634. var ok bool
  635. if rp.IsRoot() {
  636. if ok, err = s.dao.ExpireIndex(c, rp.Oid, rp.Type, model.SortByFloor); err == nil && ok {
  637. if err = s.dao.AddFloorIndex(c, rp); err != nil {
  638. log.Error("d.AddFloorIndex(%d,%d) error(%v)", rp.Oid, rp.Type, err)
  639. }
  640. }
  641. if ok, err = s.dao.ExpireIndex(c, rp.Oid, rp.Type, model.SortByCount); err == nil && ok {
  642. if err = s.dao.AddCountIndex(c, rp); err != nil {
  643. log.Error("s.AddCountIndex(%d,%d) error(%v)", rp.Oid, rp.Type, err)
  644. }
  645. }
  646. if ok, err = s.dao.ExpireIndex(c, rp.Oid, rp.Type, model.SortByLike); err == nil && ok {
  647. rpt, _ := s.dao.Report(c, rp.Oid, rp.ID)
  648. if err = s.dao.AddLikeIndex(c, rp, rpt); err != nil {
  649. log.Error("d.AddLikeIndex(%d,%d) error(%v)", rp.Oid, rp.Type, err)
  650. }
  651. }
  652. } else {
  653. if ok, err = s.dao.ExpireNewChildIndex(c, rp.Root); err == nil && ok {
  654. if err = s.dao.AddNewChildIndex(c, rp); err != nil {
  655. log.Error("d.AddFloorRootIndex(%d) error(%v)", rp.Root, err)
  656. }
  657. }
  658. }
  659. return
  660. }
  661. func (s *Service) recReply(c context.Context, oid, rpID int64, state int32, now time.Time) (sub *model.Subject, rp *model.Reply, err error) {
  662. if sub, rp, err = s.tranRecover(c, oid, rpID, state, now); err != nil {
  663. return
  664. }
  665. if rp.Content, err = s.dao.ReplyContent(c, oid, rpID); err != nil {
  666. return
  667. }
  668. if rp.Content == nil {
  669. err = ecode.ReplyNotExist
  670. return
  671. }
  672. if !rp.IsRoot() {
  673. if err = s.dao.DelReplyCache(c, rp.Root); err != nil {
  674. log.Error("s.dao.DelReplyCache(%d,%d) error(%v)", oid, rpID, err)
  675. }
  676. }
  677. if err = s.dao.DelReplyCache(c, rpID); err != nil {
  678. log.Error("s.dao.DelReplyCache(%d,%d) error(%v)", oid, rpID, err)
  679. }
  680. if err = s.addReplyIndex(c, rp); err != nil {
  681. log.Error("s.dao.DelReplyIndex(%d,%d) error(%v)", oid, rpID, err)
  682. }
  683. if err = s.dao.AddSubjectCache(c, sub); err != nil {
  684. log.Error("s.dao.DelSubjectCache(%+v) error(%v)", sub, err)
  685. }
  686. s.dao.SendStats(c, sub.Type, sub.Oid, sub.ACount)
  687. return
  688. }
  689. func (s *Service) delReply(c context.Context, oid, rpID int64, state int32, now time.Time) (sub *model.Subject, rp *model.Reply, err error) {
  690. if sub, rp, err = s.tranDel(c, oid, rpID, state, now); err != nil {
  691. if ecode.ReplyDeleted.Equal(err) && rp.IsDeleted() {
  692. if rp.Content, err = s.dao.ReplyContent(c, oid, rpID); err != nil {
  693. return
  694. } else if rp.Content == nil {
  695. err = ecode.ReplyNotExist
  696. return
  697. }
  698. err = ecode.ReplyDeleted
  699. }
  700. return
  701. }
  702. if rp.Content, err = s.dao.ReplyContent(c, oid, rpID); err != nil {
  703. return
  704. }
  705. if rp.Content == nil {
  706. err = ecode.ReplyNotExist
  707. return
  708. }
  709. return
  710. }
  711. func (s *Service) delCache(c context.Context, sub *model.Subject, rp *model.Reply) (err error) {
  712. if !rp.IsRoot() {
  713. if err = s.dao.DelReplyCache(c, rp.Root); err != nil {
  714. log.Error("s.dao.DelReplyCache(%d,%d) error(%v)", rp.Oid, rp.ID, err)
  715. }
  716. }
  717. if err = s.dao.DelReplyCache(c, rp.ID); err != nil {
  718. log.Error("s.dao.DelReplyCache(%d,%d) error(%v)", rp.Oid, rp.ID, err)
  719. }
  720. if err = s.dao.DelReplyIndex(c, rp); err != nil {
  721. log.Error("s.dao.DelReplyIndex(%d,%d) error(%v)", rp.Oid, rp.ID, err)
  722. }
  723. if err = s.dao.AddSubjectCache(c, sub); err != nil {
  724. log.Error("s.dao.DelSubjectCache(%+v) error(%v)", sub, err)
  725. }
  726. if rp.AttrVal(model.AttrTopAdmin) == model.AttrYes {
  727. s.dao.DelTopCache(c, rp.Oid, model.SubAttrTopAdmin)
  728. }
  729. if rp.AttrVal(model.AttrTopUpper) == model.AttrYes {
  730. s.dao.DelTopCache(c, rp.Oid, model.SubAttrTopUpper)
  731. }
  732. s.dao.SendStats(c, sub.Type, sub.Oid, sub.ACount)
  733. return
  734. }
  735. func (s *Service) tranRecover(c context.Context, oid, rpID int64, state int32, now time.Time) (sub *model.Subject, rp *model.Reply, err error) {
  736. var (
  737. rootRp *model.Reply
  738. count int32
  739. )
  740. tx, err := s.dao.BeginTran(c)
  741. if err != nil {
  742. return
  743. }
  744. if rp, err = s.dao.TxReplyForUpdate(tx, oid, rpID); err != nil {
  745. tx.Rollback()
  746. err = fmt.Errorf("s.dao.Reply(%d,%d) error(%v) ", oid, rpID, err)
  747. return
  748. }
  749. if rp == nil {
  750. tx.Rollback()
  751. err = ecode.ReplyNotExist
  752. return
  753. } else if rp.IsNormal() {
  754. tx.Rollback()
  755. err = ecode.ReplyActioned
  756. return
  757. }
  758. rows, err := s.dao.TxUpdateReplyState(tx, rp.Oid, rp.ID, state, now)
  759. if err != nil || rows == 0 {
  760. tx.Rollback()
  761. err = fmt.Errorf("error(%v) or rows(%d)", err, rows)
  762. return
  763. }
  764. rp.MTime = xtime.Time(now.Unix())
  765. if rp.IsRoot() {
  766. count = rp.RCount + 1
  767. } else {
  768. if rootRp, err = s.dao.TxReply(tx, rp.Oid, rp.Root); err != nil {
  769. tx.Rollback()
  770. return
  771. }
  772. count = 1
  773. }
  774. if rp.IsRoot() {
  775. rows, err = s.dao.TxIncrSubRCount(tx, rp.Oid, rp.Type, now)
  776. } else {
  777. rows, err = s.dao.TxIncrReplyRCount(tx, rp.Oid, rp.Root, now)
  778. }
  779. if err != nil || rows == 0 {
  780. tx.Rollback()
  781. err = fmt.Errorf("tranRecover increase count error(%v) or rows(%d)", err, rows)
  782. return
  783. }
  784. if rp.IsRoot() || (rootRp != nil && rootRp.IsNormal()) {
  785. if rows, err = s.dao.TxIncrSubACount(tx, rp.Oid, rp.Type, count, now); err != nil || rows == 0 {
  786. tx.Rollback()
  787. err = fmt.Errorf("TxIncrSubACount error(%v) or rows(%d)", err, rows)
  788. return
  789. }
  790. }
  791. if rp.State == model.StatePending {
  792. if rows, err = s.dao.TxSubDecrMCount(tx, rp.Oid, rp.Type, now); err != nil {
  793. tx.Rollback()
  794. err = fmt.Errorf("TxSubDecrMCount error(%v)", err)
  795. return
  796. }
  797. }
  798. if sub, err = s.dao.TxSubject(tx, rp.Oid, rp.Type); err != nil || sub == nil {
  799. tx.Rollback()
  800. err = fmt.Errorf(" s.dao.TxSubject(%d,%d) or rows(%d)", rp.Oid, rp.Type, rows)
  801. return
  802. }
  803. err = tx.Commit()
  804. return
  805. }
  806. func (s *Service) tranDel(c context.Context, oid, rpID int64, state int32, now time.Time) (sub *model.Subject, rp *model.Reply, err error) {
  807. var (
  808. count int32
  809. rootReply *model.Reply
  810. )
  811. tx, err := s.dao.BeginTran(c)
  812. if err != nil {
  813. return
  814. }
  815. rp, err = s.dao.TxReplyForUpdate(tx, oid, rpID)
  816. if err != nil {
  817. tx.Rollback()
  818. err = fmt.Errorf("s.dao.TxReplyForUpdate(%d,%d) error(%v) ", oid, rpID, err)
  819. return
  820. }
  821. if rp == nil {
  822. err = ecode.NothingFound
  823. return
  824. }
  825. if rp.AttrVal(model.AttrTopAdmin) == 1 || rp.IsDeleted() {
  826. if sub, err = s.dao.TxSubject(tx, rp.Oid, rp.Type); err != nil || sub == nil {
  827. tx.Rollback()
  828. err = fmt.Errorf("s.dao.TxSubject(%d,%d) error(%v)", rp.Oid, rp.Type, err)
  829. return
  830. }
  831. tx.Rollback()
  832. err = ecode.ReplyDeleted
  833. return
  834. }
  835. rows, err := s.dao.TxUpdateReplyState(tx, oid, rpID, state, now)
  836. if err != nil || rows == 0 {
  837. tx.Rollback()
  838. err = fmt.Errorf("s.dao.TxUpdateReplyState(%+v) rows:%d error(%v)", rp, rows, err)
  839. return
  840. }
  841. rp.MTime = xtime.Time(now.Unix())
  842. if rp.IsNormal() {
  843. if rp.IsRoot() {
  844. count = rp.RCount + 1
  845. if rows, err = s.dao.TxSubDecrACount(tx, rp.Oid, rp.Type, count, now); err != nil || rows == 0 {
  846. tx.Rollback()
  847. err = fmt.Errorf("s.dao.TxSubDecrACount(%+v) rows:%d error(%v)", rp, rows, err)
  848. return
  849. }
  850. rows, err = s.dao.TxDecrSubRCount(tx, rp.Oid, rp.Type, now)
  851. if err != nil || rows == 0 {
  852. tx.Rollback()
  853. err = fmt.Errorf("TxDecrReplyRCount(%+v) rows:%d error(%v)", rp, rows, err)
  854. return
  855. }
  856. } else {
  857. if rootReply, err = s.dao.TxReplyForUpdate(tx, rp.Oid, rp.Root); err != nil {
  858. tx.Rollback()
  859. return
  860. }
  861. if rootReply != nil {
  862. if rootReply.IsNormal() {
  863. if rows, err = s.dao.TxSubDecrACount(tx, rp.Oid, rp.Type, 1, now); err != nil || rows == 0 {
  864. tx.Rollback()
  865. err = fmt.Errorf("s.dao.TxSubDecrACount(%+v) rows:%d error(%v)", rp, rows, err)
  866. return
  867. }
  868. }
  869. _, err = s.dao.TxDecrReplyRCount(tx, rp.Oid, rp.Root, now)
  870. if err != nil {
  871. tx.Rollback()
  872. err = fmt.Errorf("TxDecrReplyRCount(%+v) error(%v)", rp, err)
  873. return
  874. }
  875. }
  876. }
  877. }
  878. if sub, err = s.dao.TxSubject(tx, rp.Oid, rp.Type); err != nil || sub == nil {
  879. tx.Rollback()
  880. err = fmt.Errorf("s.dao.TxSubject(%d,%d) rows:%d error(%v)", rp.Oid, rp.Type, rows, err)
  881. return
  882. }
  883. if rp.State == model.StatePending || rp.State == model.StateMonitor {
  884. if _, err = s.dao.TxSubDecrMCount(tx, rp.Oid, rp.Type, now); err != nil {
  885. log.Error("dao.Reply.TxSubDecrMCount(%v) error(%v)", rp, err)
  886. tx.Rollback()
  887. return
  888. }
  889. }
  890. if rp.AttrVal(model.AttrTopUpper) == model.AttrYes {
  891. rp.AttrSet(model.AttrNo, model.AttrTopUpper)
  892. sub.AttrSet(model.AttrNo, model.SubAttrTopUpper)
  893. err = sub.TopSet(0, 1, 0)
  894. if err != nil {
  895. tx.Rollback()
  896. log.Error("sub.TopSet(%d,%d,%d) failed!err:=%v ", rp.ID, rp.Oid, 0, err)
  897. return
  898. }
  899. if _, err = s.dao.TxUpSubMeta(tx, sub.Oid, sub.Type, sub.Meta, now); err != nil {
  900. tx.Rollback()
  901. log.Error("dao.TxUpMeta(oid:%d,tp:%d) err(%v) rows(%d)", sub.Oid, sub.Type, err)
  902. return
  903. }
  904. if rows, err = s.dao.TxUpSubAttr(tx, sub.Oid, sub.Type, sub.Attr, now); err != nil || rows == 0 {
  905. tx.Rollback()
  906. err = fmt.Errorf("s.dao.TxUpSubAttr(%+v) rows:%d error(%v)", sub, rows, err)
  907. return
  908. }
  909. if rows, err = s.dao.TxUpReplyAttr(tx, rp.Oid, rp.ID, rp.Attr, now); err != nil || rows == 0 {
  910. tx.Rollback()
  911. err = fmt.Errorf("s.dao.TxUpReplyAttr(%+v) rows:%d error(%v)", rp, rows, err)
  912. return
  913. }
  914. }
  915. err = tx.Commit()
  916. return
  917. }
  918. // ReplyTopLog ReplyTopLog
  919. func (s *Service) ReplyTopLog(c context.Context, sp model.LogSearchParam) (result *model.ReplyTopLogResult, err error) {
  920. result = &model.ReplyTopLogResult{
  921. Logs: []*model.ReplyTopLog{},
  922. }
  923. sp.Action = "top"
  924. reportData, err := s.dao.ReportLog(c, sp)
  925. if err != nil {
  926. return
  927. }
  928. result.Page = reportData.Page
  929. result.Sort = reportData.Sort
  930. result.Order = reportData.Order
  931. var mids []int64
  932. for _, data := range reportData.Result {
  933. mid := data.Index0
  934. action := data.Index1
  935. rpid := data.Index2
  936. title, link, _ := s.TitleLink(c, data.Oid, data.Type)
  937. var extra map[string]string
  938. if data.Content != "" {
  939. err = json.Unmarshal([]byte(data.Content), &extra)
  940. if err != nil {
  941. log.Error("MointorLog unmarshal failed!err:=%v", err)
  942. return
  943. }
  944. }
  945. result.Logs = append(result.Logs, &model.ReplyTopLog{
  946. Mid: mid,
  947. AdminID: data.AdminID,
  948. AdminName: data.AdminName,
  949. Oid: data.Oid,
  950. Type: data.Type,
  951. Remark: extra["remark"],
  952. CTime: data.Ctime,
  953. RpID: rpid,
  954. Action: action,
  955. Title: title,
  956. RedirectURL: link,
  957. })
  958. mids = append(mids, mid)
  959. }
  960. if len(mids) > 0 {
  961. var res *accmdl.InfosReply
  962. res, err = s.accSrv.Infos3(c, &accmdl.MidsReq{Mids: mids})
  963. if err != nil {
  964. log.Error(" s.accSrv.Infos3 (%v) error(%v)", mids, err)
  965. err = nil
  966. return
  967. }
  968. for _, log := range result.Logs {
  969. if user, ok := res.Infos[log.Mid]; ok {
  970. log.UserName = user.GetName()
  971. }
  972. }
  973. }
  974. return
  975. }
  976. // MarkAsSpam mark a reply(normal state) as spam.
  977. func (s *Service) MarkAsSpam(c context.Context, oids, rpIDs []int64, adminID int64, adminName, remark string) (err error) {
  978. rps, err := s.replies(c, oids, rpIDs)
  979. if err != nil {
  980. return
  981. }
  982. now := time.Now()
  983. for rpID, rp := range rps {
  984. if rp.State == model.StateNormal && rp.AttrVal(model.AttrGarbage) == model.AttrNo {
  985. var (
  986. rows int64
  987. tx *sql.Tx
  988. )
  989. tx, err = s.dao.BeginTran(c)
  990. if err != nil {
  991. return
  992. }
  993. _, err = s.dao.TxUpdateReplyState(tx, rp.Oid, rpID, model.StateGarbage, now)
  994. if err != nil {
  995. tx.Rollback()
  996. log.Error("s.dao.TxUpdateReplyState(%+v) error(%v)", rp, err)
  997. return
  998. }
  999. rp.AttrSet(model.AttrYes, model.AttrGarbage)
  1000. _, err = s.dao.TxUpReplyAttr(tx, rp.Oid, rpID, rp.Attr, now)
  1001. if err != nil {
  1002. tx.Rollback()
  1003. log.Error("s.dao.TxUpReplyAttr(%+v) rows:%d error(%v)", rp, rows, err)
  1004. return
  1005. }
  1006. if err = tx.Commit(); err != nil {
  1007. log.Error("tx.Commit(%+v) error(%v)", rp, err)
  1008. return
  1009. }
  1010. if err = s.dao.DelReplyCache(c, rpID); err != nil {
  1011. log.Error("s.dao.DelReplyCache(%+v) error(%v)", rp, err)
  1012. }
  1013. report.Manager(&report.ManagerInfo{
  1014. UID: adminID,
  1015. Uname: adminName,
  1016. Business: 41,
  1017. Type: int(rp.Type),
  1018. Oid: rp.Oid,
  1019. Ctime: now,
  1020. Action: model.ReportActionReplyGarbage,
  1021. Index: []interface{}{
  1022. rp.ID,
  1023. rp.State,
  1024. model.StateGarbage,
  1025. },
  1026. })
  1027. s.cache.Do(c, func(ctx context.Context) {
  1028. s.addAdminLog(ctx, rp.Oid, rp.ID, adminID, rp.Type, model.AdminIsNew, model.AdminIsNotReport, model.AdminOperMarkSpam, "标记为垃圾", remark, now)
  1029. })
  1030. }
  1031. }
  1032. return
  1033. }
  1034. // ExportReply get exported replies by query
  1035. func (s *Service) ExportReply(c context.Context, oid, mid int64, tp int8, state string, startTime, endTime time.Time) (data [][]string, err error) {
  1036. if data, err = s.dao.ExportReplies(c, oid, mid, tp, state, startTime, endTime); err != nil {
  1037. log.Error("s.dao.ExportReplies(%d,%d,%d,%s,%v,%v) error(%v)", oid, mid, tp, state, startTime, endTime)
  1038. return
  1039. }
  1040. return
  1041. }
  1042. // ReplyList ReplyList
  1043. func (s *Service) ReplyList(c context.Context, oids, rpids []int64) (res map[int64]*model.ReplyEx, err error) {
  1044. res = make(map[int64]*model.ReplyEx, 0)
  1045. replies, err := s.replies(c, oids, rpids)
  1046. if err != nil {
  1047. return
  1048. }
  1049. subjects := make(map[int32]map[int64]*model.Subject, 0)
  1050. var roots []int64
  1051. var rootoids []int64
  1052. for _, data := range replies {
  1053. sub := subjects[data.Type]
  1054. if sub == nil {
  1055. sub = make(map[int64]*model.Subject, 0)
  1056. subjects[data.Type] = sub
  1057. }
  1058. sub[data.Oid] = nil
  1059. if data.Root != 0 {
  1060. rootoids = append(rootoids, data.Oid)
  1061. roots = append(roots, data.Root)
  1062. }
  1063. }
  1064. rootreplies, err := s.replies(c, rootoids, roots)
  1065. if err != nil {
  1066. return
  1067. }
  1068. for typ, data := range subjects {
  1069. var ids []int64
  1070. for oid := range data {
  1071. ids = append(ids, oid)
  1072. }
  1073. sub, err := s.subjects(c, ids, typ)
  1074. if err != nil {
  1075. return res, err
  1076. }
  1077. subjects[typ] = sub
  1078. }
  1079. for _, data := range replies {
  1080. var isUp bool
  1081. var rootFloor int32
  1082. sub := (subjects[data.Type])[data.Oid]
  1083. if sub != nil && sub.Mid == data.Mid {
  1084. isUp = true
  1085. }
  1086. if data.Root != 0 && rootreplies[data.Root] != nil {
  1087. rootFloor = rootreplies[data.Root].Floor
  1088. }
  1089. res[data.ID] = &model.ReplyEx{*data, isUp, rootFloor}
  1090. }
  1091. return
  1092. }
  1093. // TopChildReply ...
  1094. func (s *Service) TopChildReply(c context.Context, rootID, childID, oid int64) (err error) {
  1095. var (
  1096. root *model.Reply
  1097. child *model.Reply
  1098. ok bool
  1099. )
  1100. rps, err := s.dao.Replies(c, []int64{oid, oid}, []int64{rootID, childID})
  1101. if err != nil {
  1102. return ecode.ReplyNotExist
  1103. }
  1104. if root, ok = rps[rootID]; !ok {
  1105. return ecode.ReplyNotExist
  1106. }
  1107. if child, ok = rps[childID]; !ok {
  1108. return ecode.ReplyNotExist
  1109. }
  1110. if root.Root != 0 || child.Root != root.ID {
  1111. return ecode.ReplyNotExist
  1112. }
  1113. if ok, err = s.dao.ExpireNewChildIndex(c, rootID); !ok || err != nil {
  1114. return ecode.ReplyNotExist
  1115. }
  1116. if err = s.dao.TopChildReply(c, rootID, childID); err != nil {
  1117. return ecode.ReplyNotExist
  1118. }
  1119. return
  1120. }