reply_report.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package http
  2. import (
  3. "strconv"
  4. "strings"
  5. "text/template"
  6. model "go-common/app/interface/main/reply/model/reply"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/xstr"
  11. )
  12. // reportReply report a reply.
  13. func reportReply(c *bm.Context) {
  14. params := c.Request.Form
  15. mid, _ := c.Get("mid")
  16. oidsStr := params.Get("oid")
  17. rpsStr := params.Get("rpid")
  18. tpStr := params.Get("type")
  19. reaStr := params.Get("reason")
  20. cont := params.Get("content")
  21. platform := params.Get("platform")
  22. buildStr := params.Get("build")
  23. buvid := c.Request.Header.Get("buvid")
  24. var build int64
  25. var err error
  26. if buildStr != "" {
  27. build, err = strconv.ParseInt(buildStr, 10, 64)
  28. if err != nil {
  29. log.Warn("strconv.ParseInt(%s) error(%v)", buildStr, err)
  30. err = ecode.RequestErr
  31. c.JSON(nil, err)
  32. return
  33. }
  34. }
  35. // check params
  36. oids, err := xstr.SplitInts(oidsStr)
  37. if err != nil {
  38. log.Warn("strconv.ParseInt(%s) error(%v)", oidsStr, err)
  39. err = ecode.RequestErr
  40. c.JSON(nil, err)
  41. return
  42. }
  43. rpIds, err := xstr.SplitInts(rpsStr)
  44. if err != nil {
  45. log.Warn("strconv.ParseInt(%s) error(%v)", rpsStr, err)
  46. err = ecode.RequestErr
  47. c.JSON(nil, err)
  48. return
  49. }
  50. if len(oids) != len(rpIds) {
  51. log.Warn("oids(%s) not equal rpids(%s)", oidsStr, rpsStr)
  52. err = ecode.RequestErr
  53. c.JSON(nil, err)
  54. return
  55. }
  56. tp, err := strconv.ParseInt(tpStr, 10, 8)
  57. if err != nil {
  58. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  59. err = ecode.RequestErr
  60. c.JSON(nil, err)
  61. return
  62. }
  63. reaTmp, err := strconv.ParseInt(reaStr, 10, 8)
  64. if err != nil {
  65. log.Warn("strconv.ParseInt(%s) error(%v)", reaStr, err)
  66. err = ecode.RequestErr
  67. c.JSON(nil, err)
  68. return
  69. }
  70. rea := int8(reaTmp)
  71. if rea == model.ReportReasonOther {
  72. cont = strings.TrimSpace(cont)
  73. cl := len([]rune(cont))
  74. if 200 < cl || cl < 2 {
  75. log.Warn("content(%s) length %d, max 200, min 2", cont, cl)
  76. err = ecode.RequestErr
  77. c.JSON(nil, err)
  78. return
  79. }
  80. cont = template.HTMLEscapeString(cont)
  81. } else {
  82. cont = ""
  83. }
  84. for i := 0; i < len(oids); i++ {
  85. cd, err := rpSvr.AddReport(c, mid.(int64), oids[i], rpIds[i], int8(tp), rea, cont, platform, build, buvid)
  86. if err != nil {
  87. var data map[string]int
  88. if err == ecode.ReplyReportDeniedAsCD {
  89. data = map[string]int{"ttl": cd}
  90. } else {
  91. log.Warn("rpSvr.AddReport(%d, %d, %d, %d, %d, %s, %s, %d, %s) error(%v)", mid, oids[i], rpIds[i], tp, rea, cont, err, platform, build, buvid)
  92. }
  93. c.JSON(data, err)
  94. return
  95. }
  96. }
  97. c.JSON(nil, nil)
  98. }
  99. func reportRelated(c *bm.Context) {
  100. var (
  101. mid int64
  102. escape = true
  103. )
  104. params := c.Request.Form
  105. oidStr := params.Get("oid")
  106. rpidStr := params.Get("root")
  107. tpStr := params.Get("type")
  108. midIf, ok := c.Get("mid")
  109. if ok {
  110. mid = midIf.(int64)
  111. }
  112. oid, err := strconv.ParseInt(oidStr, 10, 64)
  113. if err != nil {
  114. log.Warn("strconv.ParseInt(oid:%d) error(%v)", oidStr, err)
  115. err = ecode.RequestErr
  116. c.JSON(nil, err)
  117. return
  118. }
  119. rpid, err := strconv.ParseInt(rpidStr, 10, 64)
  120. if err != nil {
  121. log.Warn("strconv.ParseInt(rpid:%d) error(%v)", rpidStr, err)
  122. err = ecode.RequestErr
  123. c.JSON(nil, err)
  124. return
  125. }
  126. tp, err := strconv.ParseInt(tpStr, 10, 64)
  127. if err != nil {
  128. log.Warn("strconv.ParseInt(type:%d) error(%v)", tpStr, err)
  129. err = ecode.RequestErr
  130. c.JSON(nil, err)
  131. return
  132. }
  133. // check android and ios appkey
  134. // if mobile, no html escape
  135. if isMobile(params) {
  136. escape = false
  137. }
  138. sub, root, rels, err := rpSvr.ReportRelated(c, mid, oid, rpid, int8(tp), escape)
  139. if err != nil {
  140. log.Warn("rpSvr.ReportRelated(%d,%d,%d) error(%v)", oid, rpid, tp, err)
  141. c.JSON(nil, err)
  142. return
  143. }
  144. data := map[string]interface{}{
  145. "root": root,
  146. "related": rels,
  147. "upper": map[string]interface{}{
  148. "mid": sub.Mid,
  149. },
  150. }
  151. c.JSON(data, err)
  152. }
  153. func reportSndReply(c *bm.Context) {
  154. var (
  155. mid int64
  156. escape = true
  157. )
  158. params := c.Request.Form
  159. oidStr := params.Get("oid")
  160. rpidStr := params.Get("root")
  161. tpStr := params.Get("type")
  162. pnStr := params.Get("pn")
  163. psStr := params.Get("ps")
  164. midIf, ok := c.Get("mid")
  165. if ok {
  166. mid = midIf.(int64)
  167. }
  168. oid, err := strconv.ParseInt(oidStr, 10, 64)
  169. if err != nil {
  170. log.Warn("strconv.ParseInt(oid:%d) error(%v)", oidStr, err)
  171. err = ecode.RequestErr
  172. c.JSON(nil, err)
  173. return
  174. }
  175. rpid, err := strconv.ParseInt(rpidStr, 10, 64)
  176. if err != nil {
  177. log.Warn("strconv.ParseInt(rpid:%d) error(%v)", rpidStr, err)
  178. err = ecode.RequestErr
  179. c.JSON(nil, err)
  180. return
  181. }
  182. tp, err := strconv.ParseInt(tpStr, 10, 64)
  183. if err != nil {
  184. log.Warn("strconv.ParseInt(type:%d) error(%v)", tpStr, err)
  185. err = ecode.RequestErr
  186. c.JSON(nil, err)
  187. return
  188. }
  189. pn, err := strconv.ParseInt(pnStr, 10, 64)
  190. if err != nil {
  191. log.Warn("strconv.ParseInt(pn:%d) error(%v)", pnStr, err)
  192. err = ecode.RequestErr
  193. c.JSON(nil, err)
  194. return
  195. }
  196. ps, err := strconv.ParseInt(psStr, 10, 64)
  197. if err != nil {
  198. log.Warn("strconv.ParseInt(ps:%d) error(%v)", psStr, err)
  199. err = ecode.RequestErr
  200. c.JSON(nil, err)
  201. return
  202. }
  203. // check android and ios appkey
  204. // if mobile, no html escape
  205. if isMobile(params) {
  206. escape = false
  207. }
  208. sub, root, rs, err := rpSvr.ReportReply(c, mid, oid, rpid, int8(tp), int(pn), int(ps), escape)
  209. if err != nil {
  210. c.JSON(nil, err)
  211. return
  212. }
  213. data := make(map[string]interface{}, 2)
  214. data["page"] = map[string]int{
  215. "num": int(pn),
  216. "size": int(ps),
  217. "count": root.Count,
  218. }
  219. data["upper"] = map[string]interface{}{
  220. "mid": sub.Mid,
  221. }
  222. data["root"] = root
  223. data["replies"] = rs
  224. c.JSON(data, err)
  225. }