reply.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. package http
  2. import (
  3. "net/url"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "go-common/app/interface/main/reply/conf"
  8. "go-common/app/interface/main/reply/model/adminlog"
  9. model "go-common/app/interface/main/reply/model/reply"
  10. xmodel "go-common/app/interface/main/reply/model/xreply"
  11. "go-common/app/interface/main/reply/service"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/xstr"
  16. )
  17. // regexp utf8 char 0x0e0d~0e4A
  18. var (
  19. _emptyUnicodeReg = []*regexp.Regexp{
  20. regexp.MustCompile(`[\x{202e}]+`), // right-to-left override
  21. regexp.MustCompile(`[\x{200b}]+`), // zeroWithChar
  22. regexp.MustCompile(`[\x{1f6ab}]+`), // no_entry_sign
  23. }
  24. re = regexp.MustCompile(`[\x{0E0D}\x{0E4A}]+`)
  25. _emojis = regexp.MustCompile(`\[[^\[+][^]]+]`)
  26. // trim
  27. returnReg = regexp.MustCompile(`[\n]{3,}`)
  28. returnReg2 = regexp.MustCompile(`(\r\n){3,}`)
  29. spaceReg = regexp.MustCompile(`[ ]{5,}`) // Chinese quanjiao space character
  30. )
  31. func isMobile(params url.Values) bool {
  32. //return (params.Get("appkey") == "c1b107428d337928" || params.Get("appkey") == "27eb53fc9058f8c3") && len(params.Get("access_key")) == 32
  33. return params.Get("mobi_app") != ""
  34. }
  35. // replyInfo get reply info by rpID.
  36. func replyInfo(c *bm.Context) {
  37. params := c.Request.Form
  38. oidStr := params.Get("oid")
  39. tpStr := params.Get("type")
  40. rpStr := params.Get("rpid")
  41. // check params
  42. oid, err := strconv.ParseInt(oidStr, 10, 64)
  43. if err != nil || oid <= 0 {
  44. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  45. err = ecode.RequestErr
  46. c.JSON(nil, err)
  47. return
  48. }
  49. tp, err := strconv.ParseInt(tpStr, 10, 8)
  50. if err != nil {
  51. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  52. err = ecode.RequestErr
  53. c.JSON(nil, err)
  54. return
  55. }
  56. rpID, err := strconv.ParseInt(rpStr, 10, 64)
  57. if err != nil {
  58. log.Warn("strconv.ParseInt(%s) error(%v)", rpStr, err)
  59. err = ecode.RequestErr
  60. c.JSON(nil, err)
  61. return
  62. }
  63. rp, err := rpSvr.Reply(c, oid, int8(tp), rpID)
  64. if err != nil {
  65. log.Warn("rpSvr.Reply(%d, %d, %d) error(%s)", oid, tp, rpID, err)
  66. c.JSON(nil, err)
  67. return
  68. }
  69. c.JSON(rp, nil)
  70. }
  71. func replyMultiInfo(c *bm.Context) {
  72. params := c.Request.Form
  73. oidStr := params.Get("oid")
  74. tpStr := params.Get("type")
  75. rpStr := params.Get("rpid")
  76. // check params
  77. oids, err := xstr.SplitInts(oidStr)
  78. if err != nil {
  79. log.Warn("xstr.SplitInts(%s) err(%v)", err)
  80. err = ecode.RequestErr
  81. c.JSON(nil, err)
  82. return
  83. }
  84. tp, err := strconv.ParseInt(tpStr, 10, 8)
  85. if err != nil {
  86. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  87. err = ecode.RequestErr
  88. c.JSON(nil, err)
  89. return
  90. }
  91. rpIDs, err := xstr.SplitInts(rpStr)
  92. if err != nil {
  93. log.Warn("xstr.SplintInt(%s) err(%v)", rpStr, err)
  94. err = ecode.RequestErr
  95. c.JSON(nil, err)
  96. return
  97. }
  98. if len(oids) != len(rpIDs) {
  99. log.Warn("miss oid:%v rpid:%v", oidStr, rpStr)
  100. err = ecode.RequestErr
  101. c.JSON(nil, err)
  102. return
  103. }
  104. data := map[string]*model.Reply{}
  105. for i := 0; i < len(oids); i++ {
  106. rp, err := rpSvr.Reply(c, oids[i], int8(tp), rpIDs[i])
  107. if err != nil {
  108. log.Warn("rpSvr.Reply(%d, %d, %d) error(%s)", oids[i], tp, rpIDs[i], err)
  109. continue
  110. }
  111. data[strconv.FormatInt(rpIDs[i], 10)] = rp
  112. }
  113. c.JSON(data, nil)
  114. }
  115. // reply range subject replies.
  116. func reply(c *bm.Context) {
  117. var (
  118. showEntry = int(1)
  119. showAdmin = int(1)
  120. showFloor = int(1)
  121. mid int64
  122. sort int64
  123. build int64
  124. plat int64
  125. oid int64
  126. curPage int
  127. err error
  128. nohot bool
  129. )
  130. params := c.Request.Form
  131. oidStr := params.Get("oid")
  132. otypStr := params.Get("type")
  133. sortStr := params.Get("sort")
  134. curPageStr := params.Get("pn")
  135. perPageStr := params.Get("ps")
  136. nohotStr := params.Get("nohot")
  137. platStr := params.Get("plat")
  138. buildStr := params.Get("build")
  139. appStr := params.Get("mobi_app")
  140. buvid := c.Request.Header.Get("buvid")
  141. if m, ok := c.Get("mid"); ok {
  142. mid = m.(int64)
  143. }
  144. if platStr == "" {
  145. plat = int64(model.PlatWeb)
  146. } else {
  147. plat, err = strconv.ParseInt(platStr, 10, 8)
  148. if err != nil {
  149. log.Warn("strconv.ParseInt(platStr %s) err(%v)", platStr, err)
  150. err = ecode.RequestErr
  151. c.JSON(nil, err)
  152. return
  153. }
  154. }
  155. if buildStr != "" {
  156. build, err = strconv.ParseInt(buildStr, 10, 64)
  157. if err != nil {
  158. log.Warn("strconv.ParseInt(build %s) err(%v)", buildStr, err)
  159. err = ecode.RequestErr
  160. c.JSON(nil, err)
  161. return
  162. }
  163. }
  164. oid, err = strconv.ParseInt(oidStr, 10, 64)
  165. if err != nil || oid <= 0 {
  166. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  167. err = ecode.RequestErr
  168. c.JSON(nil, err)
  169. return
  170. }
  171. otyp, err := strconv.ParseInt(otypStr, 10, 8)
  172. if err != nil {
  173. log.Warn("strconv.ParseInt(%s) error(%v)", otypStr, err)
  174. err = ecode.RequestErr
  175. c.JSON(nil, err)
  176. return
  177. }
  178. if sortStr != "" {
  179. sort, err = strconv.ParseInt(sortStr, 10, 8)
  180. if err != nil {
  181. log.Warn("strconv.ParseInt(%s) error(%v)", sortStr, err)
  182. err = ecode.RequestErr
  183. c.JSON(nil, err)
  184. return
  185. }
  186. }
  187. curPage, err = strconv.Atoi(curPageStr)
  188. if err != nil || curPage < 1 {
  189. curPage = 1
  190. }
  191. perPage, err := strconv.Atoi(perPageStr)
  192. if err != nil || perPage > conf.Conf.Reply.MaxPageSize || perPage <= 0 {
  193. perPage = conf.Conf.Reply.MaxPageSize
  194. }
  195. htmlEscape := true
  196. needSndReply := true
  197. // check android and ios appkey
  198. if appStr != "" {
  199. // if mobile, no html escape
  200. htmlEscape = false
  201. // if mobile, no return second replies of hot and top reply
  202. if int8(sort) == model.SortByFloor {
  203. needSndReply = false
  204. }
  205. }
  206. if nohotStr != "" {
  207. nohot, _ = strconv.ParseBool(nohotStr)
  208. }
  209. pageParams := &model.PageParams{
  210. Mid: mid,
  211. Oid: oid,
  212. Type: int8(otyp),
  213. Sort: int8(sort),
  214. PageNum: curPage,
  215. PageSize: perPage,
  216. NeedSecond: needSndReply,
  217. Escape: htmlEscape,
  218. NeedHot: !nohot,
  219. }
  220. pageRes, err := rpSvr.RootReplies(c, pageParams)
  221. if err != nil {
  222. log.Warn("rpSvr.RootReplies(%+v) error(%v)", pageParams, err)
  223. c.JSON(nil, err)
  224. return
  225. }
  226. if config, _ := rpSvr.GetReplyLogConfig(c, pageRes.Subject, 1); config != nil {
  227. showEntry = int(config.ShowEntry)
  228. showAdmin = int(config.ShowAdmin)
  229. }
  230. if !rpSvr.ShowFloor(pageParams.Oid, pageParams.Type) {
  231. showFloor = int(0)
  232. if !isMobile(params) {
  233. rpSvr.ResetFloor(pageRes.Roots...)
  234. rpSvr.ResetFloor(pageRes.TopAdmin)
  235. rpSvr.ResetFloor(pageRes.TopUpper)
  236. rpSvr.ResetFloor(pageRes.Hots...)
  237. }
  238. }
  239. rpSvr.EmojiReplace(int8(plat), build, pageRes.Roots...)
  240. rpSvr.EmojiReplace(int8(plat), build, pageRes.TopAdmin)
  241. rpSvr.EmojiReplace(int8(plat), build, pageRes.TopUpper)
  242. rpSvr.EmojiReplace(int8(plat), build, pageRes.Hots...)
  243. rpSvr.EmojiReplaceI(appStr, build, pageRes.Roots...)
  244. rpSvr.EmojiReplaceI(appStr, build, pageRes.TopAdmin)
  245. rpSvr.EmojiReplaceI(appStr, build, pageRes.TopUpper)
  246. rpSvr.EmojiReplaceI(appStr, build, pageRes.Hots...)
  247. pageRes.Roots = rpSvr.FilDelReply(pageRes.Roots)
  248. pageRes.Hots = rpSvr.FilDelReply(pageRes.Hots)
  249. data := map[string]interface{}{
  250. "page": map[string]int{
  251. "num": curPage,
  252. "size": perPage,
  253. "count": pageRes.Total,
  254. "acount": pageRes.AllCount,
  255. },
  256. "config": map[string]int{
  257. "showentry": showEntry,
  258. "showadmin": showAdmin,
  259. "showfloor": showFloor,
  260. },
  261. "replies": pageRes.Roots,
  262. "hots": pageRes.Hots,
  263. "upper": map[string]interface{}{
  264. "mid": pageRes.Subject.Mid,
  265. "top": pageRes.TopUpper,
  266. },
  267. "top": pageRes.TopAdmin,
  268. "notice": rpSvr.RplyNotice(c, int8(plat), build, appStr, buvid),
  269. }
  270. if mid > 0 {
  271. if !(rpSvr.IsWhiteAid(oid, int8(otyp))) {
  272. if rpSvr.RelationBlocked(c, pageRes.Subject.Mid, mid) {
  273. data["blacklist"] = 1
  274. }
  275. if ok, _ := rpSvr.CheckAssist(c, pageRes.Subject.Mid, mid); ok {
  276. data["assist"] = 1
  277. }
  278. }
  279. }
  280. c.JSON(data, nil)
  281. }
  282. // replyReply range replies from root reply.
  283. func replyReply(c *bm.Context) {
  284. var (
  285. err error
  286. mid int64
  287. root int64
  288. jumpID int64
  289. escape = true
  290. )
  291. params := c.Request.Form
  292. oidStr := params.Get("oid")
  293. tpStr := params.Get("type")
  294. rtStr := params.Get("root")
  295. jumpStr := params.Get("jump")
  296. pnStr := params.Get("pn")
  297. psStr := params.Get("ps")
  298. midS, ok := c.Get("mid")
  299. if !ok {
  300. log.Warn("user no login")
  301. mid = 0
  302. } else {
  303. mid = midS.(int64)
  304. }
  305. // check params
  306. oid, err := strconv.ParseInt(oidStr, 10, 64)
  307. if err != nil || oid <= 0 {
  308. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  309. err = ecode.RequestErr
  310. c.JSON(nil, err)
  311. return
  312. }
  313. tp, err := strconv.ParseInt(tpStr, 10, 8)
  314. if err != nil {
  315. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  316. err = ecode.RequestErr
  317. c.JSON(nil, err)
  318. return
  319. }
  320. pn, err := strconv.Atoi(pnStr)
  321. if err != nil || pn < 1 {
  322. pn = 1
  323. }
  324. ps, err := strconv.Atoi(psStr)
  325. if err != nil || ps > conf.Conf.Reply.MaxPageSize || ps <= 0 {
  326. ps = conf.Conf.Reply.MaxPageSize
  327. }
  328. if jumpStr != "" {
  329. if jumpID, err = strconv.ParseInt(jumpStr, 10, 64); err != nil {
  330. log.Warn("strconv.ParseInt(jump:%s) error(%v)", jumpStr, err)
  331. err = ecode.RequestErr
  332. c.JSON(nil, err)
  333. return
  334. }
  335. } else {
  336. if root, err = strconv.ParseInt(rtStr, 10, 64); err != nil {
  337. log.Warn("strconv.ParseInt(%s) error(%v)", rtStr, err)
  338. err = ecode.RequestErr
  339. c.JSON(nil, err)
  340. return
  341. }
  342. }
  343. // check android and ios appkey
  344. if isMobile(params) {
  345. // if mobile, no html escape
  346. escape = false
  347. }
  348. var config xmodel.ReplyConfig
  349. config.ShowFloor = 1
  350. if !rpSvr.ShowFloor(oid, int8(tp)) {
  351. config.ShowFloor = 0
  352. }
  353. rs, rtRp, umid, pn, err := rpSvr.SecondReplies(c, mid, oid, root, jumpID, int8(tp), pn, ps, escape)
  354. if err == nil {
  355. if config.ShowFloor == 0 && !isMobile(params) {
  356. rpSvr.ResetFloor(rs...)
  357. rpSvr.ResetFloor(rtRp)
  358. }
  359. data := make(map[string]interface{}, 2)
  360. data["page"] = map[string]int{
  361. "num": pn,
  362. "size": ps,
  363. "count": rtRp.RCount,
  364. }
  365. data["upper"] = map[string]int64{
  366. "mid": umid,
  367. }
  368. data["replies"] = rs
  369. data["root"] = rtRp
  370. data["config"] = config
  371. if mid > 0 {
  372. if !(rpSvr.IsWhiteAid(oid, int8(tp))) {
  373. if rpSvr.RelationBlocked(c, rtRp.Mid, mid) {
  374. data["blacklist"] = 1
  375. }
  376. if ok, _ := rpSvr.CheckAssist(c, umid, mid); ok {
  377. data["assist"] = 1
  378. }
  379. }
  380. }
  381. c.JSON(data, nil)
  382. } else {
  383. log.Warn("rpSvr.ReplyReplies(%d, %d, %d) error(%d)", oid, root, tp, err)
  384. c.JSON(nil, err)
  385. }
  386. }
  387. func getTopics(c *bm.Context) {
  388. var (
  389. err error
  390. mid int64
  391. )
  392. params := c.Request.Form
  393. midStr, ok := c.Get("mid")
  394. if ok {
  395. mid = midStr.(int64)
  396. } else {
  397. midStr := params.Get("mid")
  398. mid, err = strconv.ParseInt(midStr, 10, 64)
  399. if err != nil {
  400. log.Warn("strconv.ParseInt(%s) error(%v)", midStr, err)
  401. err = ecode.RequestErr
  402. c.JSON(nil, err)
  403. return
  404. }
  405. }
  406. oidStr := params.Get("oid")
  407. tpStr := params.Get("type")
  408. msg := params.Get("message")
  409. tp, err := strconv.ParseInt(tpStr, 10, 8)
  410. if err != nil {
  411. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  412. err = ecode.RequestErr
  413. c.JSON(nil, err)
  414. return
  415. }
  416. if !model.LegalSubjectType(int8(tp)) {
  417. err = ecode.ReplyIllegalSubType
  418. c.JSON(nil, err)
  419. return
  420. }
  421. oid, err := strconv.ParseInt(oidStr, 10, 64)
  422. if err != nil || oid <= 0 {
  423. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  424. err = ecode.RequestErr
  425. c.JSON(nil, err)
  426. return
  427. }
  428. if int8(tp) == model.SubTypeDynamic || int8(tp) == model.SubTypeLivePicture || int8(tp) == model.SubTypeLive {
  429. data := map[string]interface{}{
  430. "topics": []string{},
  431. }
  432. c.JSON(data, nil)
  433. return
  434. }
  435. topics, err := rpSvr.Topics(c, mid, oid, int8(tp), msg)
  436. if err != nil {
  437. log.Warn("rpSvr.Topics(%d,%d,%d,%s) error(%v)", mid, oid, tp, msg, err)
  438. topics = make([]string, 0)
  439. }
  440. data := map[string]interface{}{
  441. "topics": topics,
  442. }
  443. c.JSON(data, nil)
  444. }
  445. // addReply add a reply.
  446. func addReply(c *bm.Context) {
  447. var (
  448. err error
  449. rp *model.Reply
  450. captchaURL string
  451. ats []int64
  452. root, parent int64
  453. plat int64
  454. build int64
  455. )
  456. params := c.Request.Form
  457. midStr, _ := c.Get("mid")
  458. mid := midStr.(int64)
  459. oidStr := params.Get("oid")
  460. tpStr := params.Get("type")
  461. rtStr := params.Get("root")
  462. paStr := params.Get("parent")
  463. atStr := params.Get("at")
  464. msg := params.Get("message")
  465. platStr := params.Get("plat")
  466. device := params.Get("device")
  467. version := params.Get("version")
  468. captcha := params.Get("code")
  469. ak := params.Get("access_key")
  470. platform := params.Get("platform")
  471. buildStr := params.Get("build")
  472. buvid := c.Request.Header.Get("buvid")
  473. // check params
  474. msg = strings.TrimSpace(msg)
  475. msg = re.ReplaceAllString(msg, " ")
  476. msg = spaceReg.ReplaceAllString(msg, "   ")
  477. msg = returnReg.ReplaceAllString(msg, "\n\n\n")
  478. msg = returnReg2.ReplaceAllString(msg, "\n\n\n")
  479. // checkout empty
  480. for _, reg := range _emptyUnicodeReg {
  481. msg = reg.ReplaceAllString(msg, "")
  482. }
  483. // check len
  484. tmp := _emojis.ReplaceAllString(msg, "")
  485. tmp = strings.TrimSpace(tmp)
  486. ml := len([]rune(tmp))
  487. if conf.Conf.Reply.MaxConLen < ml || ml < conf.Conf.Reply.MinConLen {
  488. log.Warn("content(%s) length %d, max %d, min %d", msg, ml, conf.Conf.Reply.MaxConLen, conf.Conf.Reply.MinConLen)
  489. err = ecode.ReplyContentOver
  490. c.JSON(nil, err)
  491. return
  492. }
  493. oid, err := strconv.ParseInt(oidStr, 10, 64)
  494. if err != nil || oid <= 0 {
  495. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  496. err = ecode.RequestErr
  497. c.JSON(nil, err)
  498. return
  499. }
  500. if rtStr != "" {
  501. root, err = strconv.ParseInt(rtStr, 10, 64)
  502. if err != nil {
  503. log.Warn("strconv.ParseInt(%s) error(%v)", rtStr, err)
  504. err = ecode.RequestErr
  505. c.JSON(nil, err)
  506. return
  507. }
  508. }
  509. if paStr != "" {
  510. parent, err = strconv.ParseInt(paStr, 10, 64)
  511. if err != nil {
  512. log.Warn("strconv.ParseInt(%s) error(%v)", paStr, err)
  513. err = ecode.RequestErr
  514. c.JSON(nil, err)
  515. return
  516. }
  517. }
  518. if !((root == 0 && parent == 0) || (root > 0 && parent > 0)) {
  519. log.Warn("the wrong root(%d) and parent(%d)", root, parent)
  520. err = ecode.RequestErr
  521. c.JSON(nil, err)
  522. return
  523. }
  524. tp, err := strconv.ParseInt(tpStr, 10, 8)
  525. if err != nil {
  526. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  527. err = ecode.RequestErr
  528. c.JSON(nil, err)
  529. return
  530. }
  531. if !model.LegalSubjectType(int8(tp)) {
  532. err = ecode.ReplyIllegalSubType
  533. c.JSON(nil, err)
  534. return
  535. }
  536. if atStr != "" {
  537. ats, err = xstr.SplitInts(atStr)
  538. if err != nil {
  539. log.Warn("utils.SplitInts(%s) error(%v)", atStr, err)
  540. err = ecode.RequestErr
  541. c.JSON(nil, err)
  542. return
  543. }
  544. }
  545. if len(ats) > 10 {
  546. log.Warn("too many people to be at len(%d)", len(ats))
  547. err = ecode.ReplyTooManyAts
  548. c.JSON(nil, err)
  549. return
  550. }
  551. if platStr != "" {
  552. plat, err = strconv.ParseInt(platStr, 10, 8)
  553. if err != nil || !model.CheckPlat(int8(plat)) {
  554. log.Warn("the wrong plat(%s)", platStr)
  555. err = ecode.RequestErr
  556. c.JSON(nil, err)
  557. return
  558. }
  559. }
  560. if device != "" && len([]rune(device)) > 30 {
  561. log.Warn("device len(%d)", len([]rune(device)))
  562. err = ecode.RequestErr
  563. c.JSON(nil, err)
  564. return
  565. }
  566. if int8(tp) == model.SubTypeArchive {
  567. for _, aid := range cnf.Reply.ForbidList {
  568. if aid == oid {
  569. err = ecode.ReplyForbidList
  570. c.JSON(nil, err)
  571. return
  572. }
  573. }
  574. }
  575. if buildStr != "" {
  576. build, err = strconv.ParseInt(buildStr, 10, 64)
  577. if err != nil {
  578. log.Warn("strconv.ParseInt(%s) error(%v)", buildStr, err)
  579. err = ecode.RequestErr
  580. c.JSON(nil, err)
  581. return
  582. }
  583. }
  584. var status int
  585. if status, err = rpSvr.UserBlockStatus(c, mid); err != nil {
  586. err = ecode.ServerErr
  587. c.JSON(nil, err)
  588. return
  589. }
  590. if status == service.StatusForbidden {
  591. // "账号被封停"
  592. err = ecode.UserDisabled
  593. c.JSON(nil, err)
  594. return
  595. }
  596. if status == service.StatusNeedContest {
  597. // 该账号处于封禁中,点击申请答题
  598. err = ecode.ReplyContestNotPassed
  599. c.JSON(nil, err)
  600. return
  601. }
  602. if root == 0 && parent == 0 {
  603. rp, captchaURL, err = rpSvr.AddReply(c, mid, oid, int8(tp), int8(plat), ats, ak, c.Request.Header.Get("Cookie"), captcha, msg, device, version, platform, build, buvid)
  604. } else {
  605. rp, captchaURL, err = rpSvr.AddReplyReply(c, mid, oid, root, parent, int8(tp), int8(plat), ats, ak, c.Request.Header.Get("Cookie"), captcha, msg, device, version, platform, build, buvid)
  606. }
  607. if err != nil && err != ecode.ReplyMosaicByFilter {
  608. log.Warn("rpSvr.AddReply or ReplyReply failed mid(%d) oid(%d) error(%d)", mid, oid, err)
  609. data := map[string]interface{}{
  610. "need_captcha": (captchaURL != ""),
  611. "url": captchaURL,
  612. }
  613. c.JSON(data, err)
  614. return
  615. }
  616. data := map[string]interface{}{
  617. "rpid": rp.RpID,
  618. "rpid_str": strconv.FormatInt(rp.RpID, 10),
  619. "dialog": rp.Dialog,
  620. "dialog_str": strconv.FormatInt(rp.Dialog, 10),
  621. "root": rp.Root,
  622. "root_str": strconv.FormatInt(rp.Root, 10),
  623. "parent": rp.Parent,
  624. "parent_str": strconv.FormatInt(rp.Parent, 10),
  625. }
  626. c.JSON(data, nil)
  627. }
  628. // likeReply do like or cancel like for reply.
  629. func likeReply(c *bm.Context) {
  630. params := c.Request.Form
  631. mid, _ := c.Get("mid")
  632. oidStr := params.Get("oid")
  633. tpStr := params.Get("type")
  634. rpStr := params.Get("rpid")
  635. actStr := params.Get("action")
  636. ak := params.Get("access_key")
  637. platform := params.Get("platform")
  638. buildStr := params.Get("build")
  639. buvid := c.Request.Header.Get("buvid")
  640. var build int64
  641. var err error
  642. if buildStr != "" {
  643. build, err = strconv.ParseInt(buildStr, 10, 64)
  644. if err != nil {
  645. log.Warn("strconv.ParseInt(%s) error(%v)", buildStr, err)
  646. err = ecode.RequestErr
  647. c.JSON(nil, err)
  648. return
  649. }
  650. }
  651. // check parameters
  652. oid, err := strconv.ParseInt(oidStr, 10, 64)
  653. if err != nil || oid <= 0 {
  654. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  655. err = ecode.RequestErr
  656. c.JSON(nil, err)
  657. return
  658. }
  659. rpID, err := strconv.ParseInt(rpStr, 10, 64)
  660. if err != nil {
  661. log.Warn("strconv.ParseInt(%s) error(%v)", rpStr, err)
  662. err = ecode.RequestErr
  663. c.JSON(nil, err)
  664. return
  665. }
  666. tp, err := strconv.ParseInt(tpStr, 10, 8)
  667. if err != nil {
  668. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  669. err = ecode.RequestErr
  670. c.JSON(nil, err)
  671. return
  672. }
  673. act, err := strconv.ParseInt(actStr, 10, 8)
  674. if err != nil {
  675. log.Warn("strconv.ParseInt(%s) error(%v)", actStr, err)
  676. err = ecode.RequestErr
  677. c.JSON(nil, err)
  678. return
  679. }
  680. err = rpSvr.AddAction(c, mid.(int64), oid, rpID, int8(tp), int8(act), ak, c.Request.Header.Get("Cookie"), "like", platform, buvid, build)
  681. c.JSON(nil, err)
  682. }
  683. // hateReply do hate or cancel hate for reply.
  684. func hateReply(c *bm.Context) {
  685. params := c.Request.Form
  686. mid, _ := c.Get("mid")
  687. oidStr := params.Get("oid")
  688. tpStr := params.Get("type")
  689. rpStr := params.Get("rpid")
  690. actStr := params.Get("action")
  691. ak := params.Get("access_key")
  692. platform := params.Get("platform")
  693. buildStr := params.Get("build")
  694. buvid := c.Request.Header.Get("buvid")
  695. var build int64
  696. var err error
  697. if buildStr != "" {
  698. build, err = strconv.ParseInt(buildStr, 10, 64)
  699. if err != nil {
  700. log.Warn("strconv.ParseInt(%s) error(%v)", buildStr, err)
  701. err = ecode.RequestErr
  702. c.JSON(nil, err)
  703. return
  704. }
  705. }
  706. // check parameters
  707. oid, err := strconv.ParseInt(oidStr, 10, 64)
  708. if err != nil || oid <= 0 {
  709. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  710. err = ecode.RequestErr
  711. c.JSON(nil, err)
  712. return
  713. }
  714. rpID, err := strconv.ParseInt(rpStr, 10, 64)
  715. if err != nil {
  716. log.Warn("strconv.ParseInt(%s) error(%v)", rpStr, err)
  717. err = ecode.RequestErr
  718. c.JSON(nil, err)
  719. return
  720. }
  721. tp, err := strconv.ParseInt(tpStr, 10, 8)
  722. if err != nil {
  723. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  724. err = ecode.RequestErr
  725. c.JSON(nil, err)
  726. return
  727. }
  728. act, err := strconv.ParseInt(actStr, 10, 8)
  729. if err != nil {
  730. log.Warn("strconv.ParseInt(%s) error(%v)", actStr, err)
  731. err = ecode.RequestErr
  732. c.JSON(nil, err)
  733. return
  734. }
  735. err = rpSvr.AddAction(c, mid.(int64), oid, rpID, int8(tp), int8(act), ak, c.Request.Header.Get("Cookie"), "hate", platform, buvid, build)
  736. c.JSON(nil, err)
  737. }
  738. // jump to specified reply.
  739. func jumpReply(c *bm.Context) {
  740. var (
  741. showEntry = int(1)
  742. showAdmin = int(1)
  743. mid int64
  744. escape = true
  745. )
  746. params := c.Request.Form
  747. oidStr := params.Get("oid")
  748. tpStr := params.Get("type")
  749. rpIDStr := params.Get("rpid")
  750. psStr := params.Get("ps")
  751. rppsStr := params.Get("rpps")
  752. platStr := params.Get("plat")
  753. buildStr := params.Get("build")
  754. appStr := params.Get("mobi_app")
  755. midS, ok := c.Get("mid")
  756. buvid := c.Request.Header.Get("buvid")
  757. if !ok {
  758. log.Warn("user no login")
  759. mid = 0
  760. } else {
  761. mid = midS.(int64)
  762. }
  763. // check params
  764. plat, err := strconv.ParseInt(platStr, 10, 8)
  765. if err != nil {
  766. plat = 1 // default pc
  767. }
  768. var build int64
  769. if buildStr != "" {
  770. build, err = strconv.ParseInt(buildStr, 10, 64)
  771. if err != nil {
  772. log.Warn("strconv.ParseInt(build %s) err(%v)", buildStr, err)
  773. err = ecode.RequestErr
  774. c.JSON(nil, err)
  775. return
  776. }
  777. }
  778. oid, err := strconv.ParseInt(oidStr, 10, 64)
  779. if err != nil || oid <= 0 {
  780. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  781. err = ecode.RequestErr
  782. c.JSON(nil, err)
  783. return
  784. }
  785. tp, err := strconv.ParseInt(tpStr, 10, 8)
  786. if err != nil {
  787. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  788. err = ecode.RequestErr
  789. c.JSON(nil, err)
  790. return
  791. }
  792. rpID, err := strconv.ParseInt(rpIDStr, 10, 64)
  793. if err != nil {
  794. log.Warn("strconv.ParseInt(%s) error(%v)", rpIDStr, err)
  795. err = ecode.RequestErr
  796. c.JSON(nil, err)
  797. return
  798. }
  799. ps, _ := strconv.Atoi(psStr)
  800. if ps > conf.Conf.Reply.MaxPageSize || ps <= 0 {
  801. ps = conf.Conf.Reply.MaxPageSize
  802. }
  803. rpPs, _ := strconv.Atoi(rppsStr)
  804. if rpPs <= 0 || rpPs > conf.Conf.Reply.MaxPageSize {
  805. rpPs = 10
  806. }
  807. // check android and ios appkey
  808. if isMobile(params) {
  809. // if mobile, no html escape
  810. escape = false
  811. }
  812. rs, hots, topAdmin, topUpper, sub, pn, rtpn, total, err := rpSvr.JumpReplies(c, mid, oid, rpID, int8(tp), ps, rpPs, escape)
  813. if err != nil && err != ecode.ReplyNotExist {
  814. log.Warn("rpSvr.JumpReplies(%d,%d,%d,%d) error(%d)", oid, tp, rpID, ps, err)
  815. c.JSON(nil, err)
  816. return
  817. }
  818. if err == ecode.ReplyNotExist {
  819. var pageRes *model.PageResult
  820. pn = 1
  821. pageParams := &model.PageParams{
  822. Mid: mid, Oid: oid, Type: int8(tp), Sort: model.SortByFloor, PageNum: pn, PageSize: ps, NeedHot: false, NeedSecond: false, Escape: escape,
  823. }
  824. if pageRes, err = rpSvr.RootReplies(c, pageParams); err != nil {
  825. log.Warn("rpSvr.RootReplies(%d,%d,%d,%d) error(%d)", oid, tp, rpID, ps, err)
  826. c.JSON(nil, err)
  827. return
  828. }
  829. rs = pageRes.Roots
  830. sub = pageRes.Subject
  831. hots = pageRes.Hots
  832. topAdmin = pageRes.TopAdmin
  833. topUpper = pageRes.TopUpper
  834. total = pageRes.Total
  835. }
  836. if config, _ := rpSvr.GetReplyLogConfig(c, sub, 1); config != nil {
  837. showEntry = int(config.ShowEntry)
  838. showAdmin = int(config.ShowAdmin)
  839. }
  840. rpSvr.EmojiReplace(int8(plat), build, hots...)
  841. rpSvr.EmojiReplace(int8(plat), build, rs...)
  842. rpSvr.EmojiReplace(int8(plat), build, topAdmin)
  843. rpSvr.EmojiReplace(int8(plat), build, topUpper)
  844. rpSvr.EmojiReplaceI(appStr, build, hots...)
  845. rpSvr.EmojiReplaceI(appStr, build, rs...)
  846. rpSvr.EmojiReplaceI(appStr, build, topAdmin)
  847. rpSvr.EmojiReplaceI(appStr, build, topUpper)
  848. data := make(map[string]interface{}, 2)
  849. page := map[string]int{
  850. "num": pn,
  851. "size": ps,
  852. "count": total,
  853. "acount": sub.ACount,
  854. "rt_num": rtpn,
  855. }
  856. configValue := map[string]int{
  857. "showentry": showEntry,
  858. "showadmin": showAdmin,
  859. }
  860. upper := map[string]interface{}{
  861. "mid": sub.Mid,
  862. "top": topUpper,
  863. }
  864. data["config"] = configValue
  865. data["upper"] = upper
  866. data["page"] = page
  867. data["replies"] = rs
  868. data["hots"] = hots
  869. data["top"] = topAdmin
  870. if notice := rpSvr.RplyNotice(c, int8(plat), build, appStr, buvid); notice != nil {
  871. data["notice"] = notice
  872. }
  873. //NOTE donot need
  874. // data["need_captcha"] = rpSvr.NeedCaptcha(c, mid)
  875. c.JSON(data, nil)
  876. }
  877. // showReply show reply by upper.
  878. func showReply(c *bm.Context) {
  879. params := c.Request.Form
  880. mid, _ := c.Get("mid")
  881. oidsStr := params.Get("oid")
  882. rpsStr := params.Get("rpid")
  883. tpStr := params.Get("type")
  884. ak := params.Get("access_key")
  885. // check params
  886. oids, err := xstr.SplitInts(oidsStr)
  887. if err != nil {
  888. log.Warn("strconv.ParseInt(%s) error(%v)", oidsStr, err)
  889. err = ecode.RequestErr
  890. c.JSON(nil, err)
  891. return
  892. }
  893. rpIDs, err := xstr.SplitInts(rpsStr)
  894. if err != nil {
  895. log.Warn("strconv.ParseInt(%s) error(%v)", rpsStr, err)
  896. err = ecode.RequestErr
  897. c.JSON(nil, err)
  898. return
  899. }
  900. if len(oids) != len(rpIDs) {
  901. log.Warn("oids(%v) not equal roids(%v)", oids, rpIDs)
  902. err = ecode.RequestErr
  903. c.JSON(nil, err)
  904. return
  905. }
  906. tp, err := strconv.ParseInt(tpStr, 10, 8)
  907. if err != nil {
  908. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  909. err = ecode.RequestErr
  910. c.JSON(nil, err)
  911. return
  912. }
  913. if len(oids) != len(rpIDs) {
  914. log.Warn("miss oid:%s rpid:%s", oidsStr, rpsStr)
  915. err = ecode.RequestErr
  916. c.JSON(nil, err)
  917. return
  918. }
  919. for i := 0; i < len(oids); i++ {
  920. if oids[i] <= 0 {
  921. err = ecode.RequestErr
  922. c.JSON(nil, err)
  923. return
  924. }
  925. if err = rpSvr.Show(c, oids[i], mid.(int64), rpIDs[i], int8(tp), ak, c.Request.Header.Get("Cookie")); err != nil {
  926. c.JSON(nil, err)
  927. return
  928. }
  929. }
  930. c.JSON(nil, nil)
  931. }
  932. // hideReplys hide reply by upper.
  933. func hideReply(c *bm.Context) {
  934. params := c.Request.Form
  935. mid, _ := c.Get("mid")
  936. oidsStr := params.Get("oid")
  937. rpsStr := params.Get("rpid")
  938. tpStr := params.Get("type")
  939. ak := params.Get("access_key")
  940. // check params
  941. oids, err := xstr.SplitInts(oidsStr)
  942. if err != nil {
  943. log.Warn("strconv.ParseInt(%s) error(%v)", oidsStr, err)
  944. err = ecode.RequestErr
  945. c.JSON(nil, err)
  946. return
  947. }
  948. rpIDs, err := xstr.SplitInts(rpsStr)
  949. if err != nil {
  950. log.Warn("strconv.ParseInt(%s) error(%v)", rpsStr, err)
  951. err = ecode.RequestErr
  952. c.JSON(nil, err)
  953. return
  954. }
  955. if len(oids) != len(rpIDs) {
  956. log.Warn("oids(%v) not equal roids(%v)", oids, rpIDs)
  957. err = ecode.RequestErr
  958. c.JSON(nil, err)
  959. return
  960. }
  961. tp, err := strconv.ParseInt(tpStr, 10, 8)
  962. if err != nil {
  963. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  964. err = ecode.RequestErr
  965. c.JSON(nil, err)
  966. return
  967. }
  968. if len(oids) != len(rpIDs) {
  969. log.Warn("miss oid:%s rpid:%s", oidsStr, rpsStr)
  970. err = ecode.RequestErr
  971. c.JSON(nil, err)
  972. return
  973. }
  974. for i := 0; i < len(oids); i++ {
  975. if oids[i] <= 0 {
  976. err = ecode.RequestErr
  977. c.JSON(nil, err)
  978. return
  979. }
  980. if err = rpSvr.Hide(c, oids[i], mid.(int64), rpIDs[i], int8(tp), ak, c.Request.Header.Get("Cookie")); err != nil {
  981. c.JSON(nil, err)
  982. return
  983. }
  984. }
  985. c.JSON(nil, nil)
  986. }
  987. func replyCounts(c *bm.Context) {
  988. params := c.Request.Form
  989. oids, err := xstr.SplitInts(params.Get("oid"))
  990. if err != nil {
  991. log.Warn("sxtr.PlintInts(%v) err(%v)", params.Get("oid"), err)
  992. err = ecode.RequestErr
  993. c.JSON(nil, err)
  994. return
  995. }
  996. if len(oids) == 0 {
  997. err = ecode.RequestErr
  998. c.JSON(nil, err)
  999. return
  1000. }
  1001. for i := 0; i < len(oids); i++ {
  1002. if oids[i] <= 0 {
  1003. err = ecode.RequestErr
  1004. c.JSON(nil, err)
  1005. return
  1006. }
  1007. }
  1008. otyp, err := strconv.ParseInt(params.Get("type"), 10, 8)
  1009. if err != nil {
  1010. log.Warn("strconv.ParseInt(%v) err(%v)", params.Get("type"), err)
  1011. err = ecode.RequestErr
  1012. c.JSON(nil, err)
  1013. return
  1014. }
  1015. if !model.LegalSubjectType(int8(otyp)) {
  1016. err = ecode.ReplyIllegalSubType
  1017. c.JSON(nil, err)
  1018. return
  1019. }
  1020. counts, err := rpSvr.GetReplyCounts(c, oids, int8(otyp))
  1021. if err != nil {
  1022. log.Warn("rcSvr.GetReplyCount err(%v)", err)
  1023. c.JSON(nil, err)
  1024. return
  1025. }
  1026. c.JSON(counts, nil)
  1027. }
  1028. // replyCount get replies count.
  1029. func replyMultiCount(c *bm.Context) {
  1030. params := c.Request.Form
  1031. oidsStr := params.Get("oid")
  1032. tpStr := params.Get("type")
  1033. oids, err := xstr.SplitInts(oidsStr)
  1034. if err != nil {
  1035. log.Warn("sxtr.PlintInts(%v) err(%v)", oidsStr, err)
  1036. err = ecode.RequestErr
  1037. c.JSON(nil, err)
  1038. return
  1039. }
  1040. tp, err := strconv.ParseInt(tpStr, 10, 8)
  1041. if err != nil {
  1042. log.Warn("strconv.ParseInt(%v) err(%v)", tpStr, err)
  1043. err = ecode.RequestErr
  1044. c.JSON(nil, err)
  1045. return
  1046. }
  1047. if len(oids) == 0 {
  1048. err = ecode.RequestErr
  1049. c.JSON(nil, err)
  1050. return
  1051. }
  1052. for i := 0; i < len(oids); i++ {
  1053. if oids[i] <= 0 {
  1054. err = ecode.RequestErr
  1055. c.JSON(nil, err)
  1056. return
  1057. }
  1058. }
  1059. counts, err := rpSvr.ReplyCounts(c, oids, int8(tp))
  1060. if err != nil {
  1061. log.Warn("rcSvr.ReplyCount err(%v)", err)
  1062. c.JSON(nil, err)
  1063. return
  1064. }
  1065. c.JSON(counts, nil)
  1066. }
  1067. // replyAdminLog fetch pages of replies deleted by upper, reply user or administrators
  1068. func replyAdminLog(c *bm.Context) {
  1069. params := c.Request.Form
  1070. oidStr := params.Get("oid")
  1071. tpStr := params.Get("type")
  1072. curPageStr := params.Get("pn")
  1073. perPageStr := params.Get("ps")
  1074. // check params
  1075. oid, err := strconv.ParseInt(oidStr, 10, 64)
  1076. if err != nil || oid <= 0 {
  1077. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  1078. err = ecode.RequestErr
  1079. c.JSON(nil, err)
  1080. return
  1081. }
  1082. tp, err := strconv.ParseInt(tpStr, 10, 8)
  1083. if err != nil {
  1084. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  1085. err = ecode.RequestErr
  1086. c.JSON(nil, err)
  1087. return
  1088. }
  1089. curPage, err := strconv.Atoi(curPageStr)
  1090. if err != nil || curPage < 1 {
  1091. curPage = 1
  1092. }
  1093. perPage, err := strconv.Atoi(perPageStr)
  1094. if err != nil || perPage > conf.Conf.Reply.MaxPageSize || perPage <= 0 {
  1095. perPage = conf.Conf.Reply.MaxPageSize
  1096. }
  1097. if rpSvr.IsBnj(oid, int8(tp)) {
  1098. data := map[string]interface{}{
  1099. "page": map[string]int{
  1100. "num": curPage,
  1101. "size": perPage,
  1102. "pages": 0,
  1103. "total": 0,
  1104. },
  1105. "logs": make([]*adminlog.AdminLog, 0),
  1106. "reply_count": 0,
  1107. "report_count": 0,
  1108. }
  1109. c.JSON(data, nil)
  1110. return
  1111. }
  1112. logs, replyCount, reportCount, pageCount, total, err := rpSvr.PaginateUpperDeletedLogs(c, oid, int(tp), curPage, perPage)
  1113. if err != nil {
  1114. log.Warn("rpSvr.PaginateUpperDeletedLogs(%d, %d) error(%s)", oid, tp, err)
  1115. c.JSON(nil, err)
  1116. return
  1117. }
  1118. if replyCount > reportCount {
  1119. reportCount += replyCount
  1120. }
  1121. data := map[string]interface{}{
  1122. "page": map[string]int{
  1123. "num": curPage,
  1124. "size": perPage,
  1125. "pages": int(pageCount),
  1126. "total": int(total),
  1127. },
  1128. "logs": logs,
  1129. "reply_count": replyCount,
  1130. "report_count": reportCount,
  1131. }
  1132. c.JSON(data, nil)
  1133. }
  1134. // replyCount get replies count.
  1135. func replyCount(c *bm.Context) {
  1136. params := c.Request.Form
  1137. oidStr := params.Get("oid")
  1138. tpStr := params.Get("type")
  1139. // check params
  1140. oid, err := strconv.ParseInt(oidStr, 10, 64)
  1141. if err != nil || oid <= 0 {
  1142. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  1143. err = ecode.RequestErr
  1144. c.JSON(nil, err)
  1145. return
  1146. }
  1147. tp, err := strconv.ParseInt(tpStr, 10, 8)
  1148. if err != nil {
  1149. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  1150. err = ecode.RequestErr
  1151. c.JSON(nil, err)
  1152. return
  1153. }
  1154. count, err := rpSvr.ReplyCount(c, oid, int8(tp))
  1155. if err != nil {
  1156. c.JSON(nil, err)
  1157. return
  1158. }
  1159. data := map[string]int{"count": count}
  1160. c.JSON(data, nil)
  1161. }
  1162. // delReply delete reply by upper or self.
  1163. func delReply(c *bm.Context) {
  1164. params := c.Request.Form
  1165. mid, _ := c.Get("mid")
  1166. oidsStr := params.Get("oid")
  1167. tpStr := params.Get("type")
  1168. rpIDsStr := params.Get("rpid")
  1169. ak := params.Get("access_key")
  1170. platform := params.Get("platform")
  1171. buildStr := params.Get("build")
  1172. buvid := c.Request.Header.Get("buvid")
  1173. var build int64
  1174. var err error
  1175. // check params
  1176. if buildStr != "" {
  1177. build, err = strconv.ParseInt(buildStr, 10, 64)
  1178. if err != nil {
  1179. log.Warn("strconv.ParseInt(build %s) err(%v)", buildStr, err)
  1180. err = ecode.RequestErr
  1181. c.JSON(nil, err)
  1182. return
  1183. }
  1184. }
  1185. oids, err := xstr.SplitInts(oidsStr)
  1186. if err != nil {
  1187. log.Warn("strconv.ParseInt(%s) error(%v)", oidsStr, err)
  1188. err = ecode.RequestErr
  1189. c.JSON(nil, err)
  1190. return
  1191. }
  1192. rpIDs, err := xstr.SplitInts(rpIDsStr)
  1193. if err != nil {
  1194. log.Warn("strconv.ParseInt(%s) error(%v)", rpIDsStr, err)
  1195. err = ecode.RequestErr
  1196. c.JSON(nil, err)
  1197. return
  1198. }
  1199. tp, err := strconv.ParseInt(tpStr, 10, 8)
  1200. if err != nil {
  1201. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  1202. err = ecode.RequestErr
  1203. c.JSON(nil, err)
  1204. return
  1205. }
  1206. if len(oids) != len(rpIDs) {
  1207. log.Warn("miss oid:%s rpid:%s", oidsStr, rpIDsStr)
  1208. err = ecode.RequestErr
  1209. c.JSON(nil, err)
  1210. return
  1211. }
  1212. for i := 0; i < len(oids); i++ {
  1213. if oids[i] <= 0 {
  1214. err = ecode.RequestErr
  1215. c.JSON(nil, err)
  1216. return
  1217. }
  1218. if err = rpSvr.Delete(c, mid.(int64), oids[i], rpIDs[i], int8(tp), ak, c.Request.Header.Get("Cookie"), platform, build, buvid); err != nil {
  1219. c.JSON(nil, err)
  1220. return
  1221. }
  1222. }
  1223. c.JSON(nil, nil)
  1224. }
  1225. // AddTopReply add top reply by upper
  1226. func AddTopReply(c *bm.Context) {
  1227. params := c.Request.Form
  1228. oidStr := params.Get("oid")
  1229. tpStr := params.Get("type")
  1230. rpIDStr := params.Get("rpid")
  1231. actStr := params.Get("action")
  1232. platform := params.Get("platform")
  1233. buildStr := params.Get("build")
  1234. buvid := c.Request.Header.Get("buvid")
  1235. var build int64
  1236. var err error
  1237. if buildStr != "" {
  1238. build, err = strconv.ParseInt(buildStr, 10, 64)
  1239. if err != nil {
  1240. log.Warn("strconv.ParseInt(%s) error(%v)", buildStr, err)
  1241. err = ecode.RequestErr
  1242. c.JSON(nil, err)
  1243. return
  1244. }
  1245. }
  1246. mid, _ := c.Get("mid")
  1247. // check params
  1248. oid, err := strconv.ParseInt(oidStr, 10, 64)
  1249. if err != nil || oid <= 0 {
  1250. log.Warn("strconv.ParseInt(%s) error(%v)", oidStr, err)
  1251. err = ecode.RequestErr
  1252. c.JSON(nil, err)
  1253. return
  1254. }
  1255. rpID, err := strconv.ParseInt(rpIDStr, 10, 64)
  1256. if err != nil {
  1257. log.Warn("strconv.ParseInt(%s) error(%v)", rpIDStr, err)
  1258. err = ecode.RequestErr
  1259. c.JSON(nil, err)
  1260. return
  1261. }
  1262. tp, err := strconv.ParseInt(tpStr, 10, 8)
  1263. if err != nil {
  1264. log.Warn("strconv.ParseInt(%s) error(%v)", tpStr, err)
  1265. err = ecode.RequestErr
  1266. c.JSON(nil, err)
  1267. return
  1268. }
  1269. act, err := strconv.ParseInt(actStr, 10, 8)
  1270. if err != nil {
  1271. log.Warn("strconv.ParseInt(actStr :%s) err(%v)", actStr, err)
  1272. act = 1
  1273. }
  1274. err = rpSvr.UpperAddTop(c, mid.(int64), oid, rpID, int8(tp), int8(act), platform, build, buvid)
  1275. c.JSON(nil, err)
  1276. }
  1277. // emojis
  1278. func emojis(c *bm.Context) {
  1279. params := c.Request.Form
  1280. appStr := params.Get("mobi_app")
  1281. buildStr := params.Get("build")
  1282. if buildStr != "" {
  1283. build, err := strconv.ParseInt(buildStr, 10, 64)
  1284. if err != nil {
  1285. log.Warn("strconv.ParseInt(build %s) err(%v)", buildStr, err)
  1286. err = ecode.RequestErr
  1287. c.JSON(nil, err)
  1288. return
  1289. }
  1290. if appStr == "android_i" && build > 1125000 && build < 2005000 {
  1291. c.JSON(nil, nil)
  1292. return
  1293. }
  1294. }
  1295. c.JSON(rpSvr.Emojis(c), nil)
  1296. }
  1297. func replyHots(c *bm.Context) {
  1298. params := c.Request.Form
  1299. oidStr := params.Get("oid")
  1300. typStr := params.Get("type")
  1301. pnStr := params.Get("pn")
  1302. psStr := params.Get("ps")
  1303. oid, err := strconv.ParseInt(oidStr, 10, 64)
  1304. if err != nil || oid <= 0 {
  1305. err = ecode.RequestErr
  1306. c.JSON(nil, err)
  1307. return
  1308. }
  1309. typ, err := strconv.ParseInt(typStr, 10, 8)
  1310. if err != nil {
  1311. err = ecode.RequestErr
  1312. c.JSON(nil, err)
  1313. return
  1314. }
  1315. pn, _ := strconv.Atoi(pnStr)
  1316. if pn <= 0 {
  1317. pn = 1
  1318. }
  1319. ps, _ := strconv.Atoi(psStr)
  1320. if ps > conf.Conf.Reply.MaxPageSize || ps <= 0 {
  1321. ps = 3
  1322. }
  1323. sub, rps, err := rpSvr.ReplyHots(c, oid, int8(typ), pn, ps)
  1324. if err != nil {
  1325. c.JSON(nil, err)
  1326. return
  1327. }
  1328. rps = rpSvr.FilDelReply(rps)
  1329. data := map[string]interface{}{
  1330. "page": map[string]int{
  1331. "num": pn,
  1332. "size": ps,
  1333. "count": sub.RCount,
  1334. "acount": sub.ACount,
  1335. },
  1336. "replies": rps,
  1337. }
  1338. c.JSON(data, nil)
  1339. }
  1340. func dialog(c *bm.Context) {
  1341. var (
  1342. err error
  1343. mid int64
  1344. escape = true
  1345. )
  1346. v := new(struct {
  1347. Oid int64 `form:"oid" validate:"required"`
  1348. Type int8 `form:"type" validate:"required"`
  1349. Dialog int64 `form:"dialog" validate:"required"`
  1350. Root int64 `form:"root" validate:"required"`
  1351. Pn int `form:"pn" validate:"min=1"`
  1352. Ps int `form:"ps" validate:"min=1"`
  1353. Plat string `form:"plat"`
  1354. Build string `form:"build"`
  1355. MobiApp string `form:"mobi_app"`
  1356. })
  1357. if err = c.Bind(v); err != nil {
  1358. return
  1359. }
  1360. if v.Ps > conf.Conf.Reply.MaxPageSize {
  1361. v.Ps = conf.Conf.Reply.MaxPageSize
  1362. }
  1363. midS, ok := c.Get("mid")
  1364. if !ok {
  1365. log.Warn("user no login")
  1366. } else {
  1367. mid = midS.(int64)
  1368. }
  1369. rps, err := rpSvr.Dialog(c, mid, v.Oid, v.Type, v.Root, v.Dialog, v.Pn, v.Ps, escape)
  1370. if err != nil {
  1371. c.JSON(nil, err)
  1372. return
  1373. }
  1374. data := map[string]interface{}{
  1375. "page": map[string]int{
  1376. "num": v.Pn,
  1377. "size": v.Ps,
  1378. "total": len(rps),
  1379. },
  1380. // "dialog":,
  1381. "replies": rps,
  1382. }
  1383. c.JSON(data, nil)
  1384. }
  1385. func isHotReply(c *bm.Context) {
  1386. v := new(struct {
  1387. Type int8 `form:"type" validate:"required"`
  1388. Oid int64 `form:"oid" validate:"required"`
  1389. RpID int64 `form:"rpid" validate:"required"`
  1390. })
  1391. var (
  1392. isHot bool
  1393. err error
  1394. )
  1395. if err = c.Bind(v); err != nil {
  1396. return
  1397. }
  1398. if isHot, err = rpSvr.IsHotReply(c, v.Type, v.Oid, v.RpID); err != nil {
  1399. c.JSON(nil, err)
  1400. return
  1401. }
  1402. data := map[string]bool{
  1403. "isHot": isHot,
  1404. }
  1405. c.JSON(data, nil)
  1406. }
  1407. func hotsBatch(c *bm.Context) {
  1408. v := new(struct {
  1409. Type int8 `form:"type" validate:"required"`
  1410. Oids []int64 `form:"oids,split" validate:"required"`
  1411. Size int8 `form:"size" default:"1" validate:"min=1"`
  1412. Mid int64 `form:"mid"`
  1413. })
  1414. var (
  1415. err error
  1416. )
  1417. if err = c.Bind(v); err != nil {
  1418. return
  1419. }
  1420. c.JSON(rpSvr.HotsBatch(c, v.Type, v.Size, v.Oids, v.Mid))
  1421. }