reply_admin.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/reply/conf"
  7. "go-common/app/interface/main/reply/model/reply"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/queue/databus/report"
  12. xtime "go-common/library/time"
  13. )
  14. // AdminGetSubject get subject state
  15. func (s *Service) AdminGetSubject(c context.Context, oid int64, tp int8) (sub *reply.Subject, err error) {
  16. if !reply.LegalSubjectType(tp) {
  17. err = ecode.ReplyIllegalSubType
  18. return
  19. }
  20. if sub, err = s.dao.Subject.Get(c, oid, tp); err != nil {
  21. log.Error("s.dao.Subject.Get(%d,%d) error(%v)", oid, tp, err)
  22. return
  23. }
  24. if sub == nil {
  25. err = ecode.NothingFound
  26. }
  27. return
  28. }
  29. // AdminSubRegist register subject of new archive
  30. func (s *Service) AdminSubRegist(c context.Context, oid, mid int64, tp, state int8, appkey string) (err error) {
  31. var has bool
  32. tps, ok := conf.Conf.AppkeyType[appkey]
  33. if !ok || len(tps) == 0 {
  34. err = ecode.ReplyIllegalSubType
  35. return
  36. }
  37. if tp != 0 {
  38. for _, t := range tps {
  39. if t == tp {
  40. has = true
  41. break
  42. }
  43. }
  44. } else {
  45. has = true
  46. tp = tps[0]
  47. }
  48. if !has {
  49. err = ecode.ReplyIllegalSubType
  50. return
  51. }
  52. now := time.Now()
  53. sub := &reply.Subject{
  54. Oid: oid,
  55. Type: tp,
  56. Mid: mid,
  57. State: state,
  58. CTime: xtime.Time(now.Unix()),
  59. MTime: xtime.Time(now.Unix()),
  60. }
  61. if sub.ID, err = s.dao.Subject.Insert(c, sub); err != nil {
  62. log.Error("s.dao.Subject.Insert(%v) error(%v)", sub, err)
  63. }
  64. s.cache.Do(c, func(ctx context.Context) {
  65. if err = s.dao.Mc.DeleteSubject(ctx, oid, tp); err != nil {
  66. log.Error("s.dao.Mc.DeleteSubject(%d, %d) state:%d error(%v)", oid, tp, state, err)
  67. }
  68. })
  69. return
  70. }
  71. // AdminSubjectState change subject state by admin.
  72. func (s *Service) AdminSubjectState(c context.Context, adid, oid, mid int64, tp, state int8, remark string) (err error) {
  73. if !reply.LegalSubjectType(tp) {
  74. err = ecode.ReplyIllegalSubType
  75. return
  76. }
  77. now := time.Now()
  78. if err = reply.CheckSubState(state); err != nil {
  79. log.Error("checkstate err(%v)", err)
  80. return
  81. }
  82. if mid <= 0 {
  83. var sub *reply.Subject
  84. sub, err = s.dao.Subject.Get(c, oid, tp)
  85. if err != nil {
  86. log.Error("s.dao.subject (%d,%d)error(%v)", oid, tp, err)
  87. return
  88. }
  89. if sub == nil {
  90. err = ecode.NothingFound
  91. return
  92. }
  93. mid = sub.Mid
  94. }
  95. if _, err = s.setSubject(c, oid, tp, state, mid); err != nil {
  96. log.Error("s.addSubject(%d, %d, %d, %d) error(%v)", oid, tp, state, mid, err)
  97. return
  98. }
  99. s.cache.Do(c, func(ctx context.Context) {
  100. if err = s.dao.Mc.DeleteSubject(ctx, oid, tp); err != nil {
  101. log.Error("s.dao.Mc.DeleteSubject(%d, %d) state:%d error(%v)", oid, tp, state, err)
  102. }
  103. })
  104. s.dao.Admin.Insert(c, adid, oid, 0, tp, fmt.Sprintf("修改主题状态为: %d", state), remark, reply.AdminIsNotNew, reply.AdminIsNotReport, reply.AdminOperSubState, now)
  105. return
  106. }
  107. // AdminSubjectMid set the subject mid info.
  108. func (s Service) AdminSubjectMid(c context.Context, adid, mid, oid int64, tp int8, remark string) (err error) {
  109. // check subject
  110. if !reply.LegalSubjectType(tp) {
  111. err = ecode.ReplyIllegalSubType
  112. return
  113. }
  114. now := time.Now()
  115. if _, err = s.dao.Subject.UpMid(c, mid, oid, tp, now); err != nil {
  116. log.Error("replySubDao.UpMid(%d, %d, %d) error(%v)", mid, oid, tp, err)
  117. return
  118. }
  119. s.cache.Do(c, func(ctx context.Context) {
  120. if err = s.dao.Mc.DeleteSubject(ctx, oid, tp); err != nil {
  121. log.Error("s.dao.Mc.DeleteSubject(%d, %d) mid:%d error(%v)", oid, tp, mid, err)
  122. }
  123. })
  124. s.dao.Admin.Insert(c, adid, oid, 0, tp, fmt.Sprintf("修改主题mid为: %d", mid), remark, reply.AdminIsNotNew, reply.AdminIsNotReport, reply.AdminOperSubMid, now)
  125. return
  126. }
  127. // Delete delete reply by upper or self.
  128. func (s *Service) Delete(c context.Context, mid, oid, rpID int64, tp int8, ak, ck, platform string, build int64, buvid string) (err error) {
  129. // check subject
  130. if !reply.LegalSubjectType(tp) {
  131. err = ecode.ReplyIllegalSubType
  132. return
  133. }
  134. rp, _ := s.reply(c, mid, oid, rpID, tp)
  135. if rp == nil {
  136. return
  137. }
  138. subject, err := s.getSubject(c, oid, tp)
  139. if err != nil {
  140. return
  141. }
  142. var (
  143. assist, operation bool
  144. state = reply.ReplyStateUserDel
  145. )
  146. // check permission,only upper and self can del
  147. if !s.IsWhiteAid(subject.Oid, subject.Type) {
  148. assist, operation = s.CheckAssist(c, subject.Mid, mid)
  149. if !operation {
  150. assist = false
  151. }
  152. if assist {
  153. state = reply.ReplyStateAssistDel
  154. }
  155. }
  156. if subject.Mid == mid {
  157. state = reply.ReplyStateUpDel
  158. }
  159. if subject.Mid == mid || mid == rp.Mid || assist {
  160. if rp.IsDeleted() {
  161. s.dao.Redis.DelIndex(c, rp)
  162. err = ecode.ReplyDeleted
  163. return
  164. } else if rp.AttrVal(reply.ReplyAttrAdminTop) == 1 {
  165. err = ecode.ReplyDelTopForbidden
  166. return
  167. }
  168. } else {
  169. err = ecode.AccessDenied
  170. return
  171. }
  172. s.dao.Databus.Delete(c, mid, oid, rpID, time.Now().Unix(), tp, assist)
  173. remoteIP := metadata.String(c, metadata.RemoteIP)
  174. report.User(&report.UserInfo{
  175. Mid: rp.Mid,
  176. Platform: platform,
  177. Build: build,
  178. Buvid: buvid,
  179. Business: 41,
  180. Type: int(rp.Type),
  181. Oid: rp.Oid,
  182. Action: reply.ReportReplyDel,
  183. Ctime: time.Now(),
  184. IP: remoteIP,
  185. Index: []interface{}{
  186. rp.RpID,
  187. rp.State,
  188. state,
  189. },
  190. })
  191. return
  192. }
  193. // AdminEdit edit reply content by admin.
  194. func (s *Service) AdminEdit(c context.Context, adid, oid, rpID int64, tp int8, msg, remark string) (err error) {
  195. now := time.Now()
  196. var rp *reply.Reply
  197. // check subject
  198. if !reply.LegalSubjectType(tp) {
  199. err = ecode.ReplyIllegalSubType
  200. return
  201. }
  202. if rp, err = s.reply(c, 0, oid, rpID, tp); err != nil {
  203. return
  204. }
  205. if rp.IsDeleted() {
  206. err = ecode.ReplyDeleted
  207. return
  208. }
  209. if _, err = s.dao.Content.UpMessage(c, oid, rpID, msg, now); err != nil {
  210. log.Error("s.content.UpMessage(%d, %d, %s, %v), err is (%v)", oid, rpID, msg, now, err)
  211. return
  212. }
  213. if err = s.dao.Mc.DeleteReply(c, rpID); err != nil {
  214. log.Error("s.dao.Mc.DeleteReply(%d, %d, %s, %v), err is (%v)", oid, rpID, msg, now, err)
  215. }
  216. // admin log
  217. if _, err = s.dao.Admin.UpIsNotNew(c, rpID, now); err != nil {
  218. log.Error("s.admin.UpIsNotNew(%d, %d, %s, %v), err is (%v)", oid, rpID, msg, now, err)
  219. }
  220. if _, err = s.dao.Admin.Insert(c, adid, oid, rpID, tp, "已修改评论内容", remark, reply.AdminIsNew, reply.AdminIsNotReport, reply.AdminOperEdit, now); err != nil {
  221. log.Error("s.admin.Insert(%d, %d, %s, %v), err is (%v)", oid, rpID, msg, now, err)
  222. }
  223. // dao.Kafka
  224. s.dao.Databus.AdminEdit(c, oid, rpID, tp)
  225. return
  226. }
  227. // AdminDelete delete reply by admin.
  228. func (s *Service) AdminDelete(c context.Context, adid, oid, rpID, ftime int64, tp int8, moral int, notify bool, adname, remark string, reason, freason int8) (err error) {
  229. // check subject
  230. now := time.Now()
  231. if !reply.LegalSubjectType(tp) {
  232. err = ecode.ReplyIllegalSubType
  233. return
  234. }
  235. rp, _ := s.reply(c, 0, oid, rpID, tp)
  236. if rp == nil {
  237. log.Error("s.Reply(oid:%v,tp:%v,:rpID:%v)", oid, tp, rpID, err)
  238. return
  239. } else if rp.AttrVal(reply.ReplyAttrAdminTop) == 1 {
  240. err = ecode.ReplyDelTopForbidden
  241. return
  242. }
  243. s.dao.Databus.AdminDelete(c, adid, oid, rpID, ftime, moral, notify, adname, remark, now.Unix(), tp, reason, freason)
  244. return
  245. }
  246. // AdminPass recover reply by admin.
  247. func (s *Service) AdminPass(c context.Context, adid, oid, rpID int64, tp int8, remark string) (err error) {
  248. // check subject
  249. now := time.Now()
  250. if !reply.LegalSubjectType(tp) {
  251. err = ecode.ReplyIllegalSubType
  252. return
  253. }
  254. s.dao.Databus.AdminPass(c, adid, oid, rpID, remark, now.Unix(), tp)
  255. return
  256. }
  257. // AdminRecover recover reply by admin.
  258. func (s *Service) AdminRecover(c context.Context, adid, oid, rpID int64, tp int8, remark string) (err error) {
  259. // check subject
  260. now := time.Now()
  261. if !reply.LegalSubjectType(tp) {
  262. err = ecode.ReplyIllegalSubType
  263. return
  264. }
  265. s.dao.Databus.AdminRecover(c, adid, oid, rpID, remark, now.Unix(), tp)
  266. return
  267. }
  268. // AdminDeleteByReport delete report reply by admin.
  269. func (s *Service) AdminDeleteByReport(c context.Context, adid, oid, rpID, ftime int64, tp int8, moral int, notify bool, adname, remark string, audit int8, reason int8, content string, freason int8) (err error) {
  270. var (
  271. rp *reply.Reply
  272. now = time.Now()
  273. )
  274. // check subject
  275. if !reply.LegalSubjectType(tp) {
  276. err = ecode.ReplyIllegalSubType
  277. return
  278. }
  279. rpt, err := s.dao.Report.Get(c, oid, rpID)
  280. if err != nil {
  281. log.Error("s.report.GetReport(%d, %d) error(%v)", oid, rpID, err)
  282. return
  283. }
  284. if rpt == nil {
  285. err = ecode.ReplyReportNotExist
  286. return
  287. }
  288. if rp, err = s.dao.Reply.Get(c, oid, rpID); err != nil {
  289. log.Error("s.reply.GetReply(%d, %d) error(%v)", oid, rpID, err)
  290. return
  291. } else if rp == nil {
  292. err = ecode.ReplyNotExist
  293. return
  294. } else if rp.AttrVal(reply.ReplyAttrAdminTop) == 1 {
  295. err = ecode.ReplyDelTopForbidden
  296. return
  297. }
  298. s.dao.Databus.AdminDeleteByReport(c, adid, oid, rpID, rpt.Mid, ftime, moral, notify, adname, remark, now.Unix(), tp, audit, reason, content, freason)
  299. return
  300. }
  301. // AdminReportStateSet set report state by admin.
  302. func (s *Service) AdminReportStateSet(c context.Context, adid, oid, rpID int64, tp, state int8) (err error) {
  303. now := time.Now()
  304. // check subject
  305. if !reply.LegalSubjectType(tp) {
  306. err = ecode.ReplyIllegalSubType
  307. return
  308. }
  309. rpt, err := s.dao.Report.Get(c, oid, rpID)
  310. if err != nil {
  311. log.Error("s.report.GetReport(%d, %d) met error (%v)", oid, rpID, err)
  312. return
  313. }
  314. if rpt == nil {
  315. err = ecode.ReplyReportNotExist
  316. return
  317. }
  318. // dao.Kafka
  319. s.dao.Databus.AdminStateSet(c, adid, oid, rpID, now.Unix(), tp, state)
  320. return
  321. }
  322. // AdminReportTransfer transfer report by admin.
  323. func (s *Service) AdminReportTransfer(c context.Context, adid, oid, rpID int64, tp, audit int8) (err error) {
  324. now := time.Now()
  325. // check subject
  326. if !reply.LegalSubjectType(tp) {
  327. err = ecode.ReplyIllegalSubType
  328. return
  329. }
  330. rpt, err := s.dao.Report.Get(c, oid, rpID)
  331. if err != nil {
  332. log.Error("s.report.GetReport(%d, %d) met error (%v)", oid, rpID, err)
  333. return
  334. }
  335. if rpt == nil {
  336. err = ecode.ReplyReportNotExist
  337. return
  338. }
  339. // dao.Kafka
  340. s.dao.Databus.AdminTransfer(c, adid, oid, rpID, now.Unix(), tp, audit)
  341. return
  342. }
  343. // AdminReportIgnore ignore report by admin.
  344. func (s *Service) AdminReportIgnore(c context.Context, adid, oid, rpID int64, tp, audit int8, remark string) (err error) {
  345. now := time.Now()
  346. // check subject
  347. if !reply.LegalSubjectType(tp) {
  348. err = ecode.ReplyIllegalSubType
  349. return
  350. }
  351. rpt, err := s.dao.Report.Get(c, oid, rpID)
  352. if err != nil {
  353. log.Error("s.report.GetReport(%d, %d) met error (%v)", oid, rpID, err)
  354. return
  355. }
  356. if rpt == nil {
  357. err = ecode.ReplyReportNotExist
  358. return
  359. }
  360. // dao.Kafka
  361. s.dao.Databus.AdminIgnore(c, adid, oid, rpID, now.Unix(), tp, audit)
  362. return
  363. }
  364. // AdminAddTop add top reply by admin
  365. func (s *Service) AdminAddTop(c context.Context, adid, oid, rpID int64, tp, act int8) (err error) {
  366. var (
  367. ts = time.Now().Unix()
  368. r *reply.Reply
  369. )
  370. if !reply.LegalSubjectType(tp) {
  371. err = ecode.ReplyIllegalSubType
  372. return
  373. }
  374. sub, err := s.Subject(c, oid, tp)
  375. if err != nil {
  376. log.Error("s.Subject(oid %v) err(%v)", oid, err)
  377. return
  378. }
  379. if r, err = s.GetTop(c, sub, oid, tp, reply.ReplyAttrAdminTop); err != nil {
  380. log.Error("s.GetTop(%d,%d) err(%v)", oid, tp, err)
  381. return
  382. }
  383. if r != nil && act == 1 {
  384. log.Warn("oid(%d) type(%d) already have top ", oid, tp)
  385. err = ecode.ReplyHaveTop
  386. return
  387. }
  388. if r == nil && act == 0 {
  389. log.Warn("oid(%d) type(%d) do not have top ", oid, tp)
  390. err = ecode.ReplyNotExist
  391. return
  392. }
  393. // TODO: only need reply,no not need content and user info
  394. if r, err = s.reply(c, 0, oid, rpID, tp); err != nil {
  395. log.Error("s.GetReply err (%v)", err)
  396. return
  397. }
  398. if r == nil {
  399. log.Warn("oid(%d) type(%d) rpID(%d) do not exist ", oid, tp, rpID)
  400. err = ecode.ReplyNotExist
  401. return
  402. }
  403. if r.AttrVal(reply.ReplyAttrUpperTop) == 1 {
  404. err = ecode.ReplyHaveTop
  405. return
  406. }
  407. if r.Root != 0 {
  408. log.Warn("oir(%d) type(%d) rpID(%d) not root reply", oid, tp, rpID)
  409. err = ecode.ReplyNotRootReply
  410. return
  411. }
  412. s.dao.Databus.AdminAddTop(c, adid, oid, rpID, ts, act, tp)
  413. return
  414. }
  415. // AdminReportRecover recover report by admin.
  416. func (s *Service) AdminReportRecover(c context.Context, adid, oid, rpID int64, tp, audit int8, remark string) (err error) {
  417. // check subject
  418. now := time.Now()
  419. if !reply.LegalSubjectType(tp) {
  420. err = ecode.ReplyIllegalSubType
  421. return
  422. }
  423. s.dao.Databus.AdminReportRecover(c, adid, oid, rpID, remark, now.Unix(), tp, audit)
  424. return
  425. }