comment.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/bbq/app-bbq/model"
  9. user "go-common/app/service/bbq/user/api"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "github.com/json-iterator/go"
  13. )
  14. // ReplySubCursor http请求子评论游标获取评论
  15. func (d *Dao) ReplySubCursor(c context.Context, req map[string]interface{}) (res *model.SubCursorRes, err error) {
  16. var r []byte
  17. ip := metadata.String(c, metadata.RemoteIP)
  18. res = new(model.SubCursorRes)
  19. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_subcursor"], "GET", req, ip)
  20. if err != nil {
  21. log.Infov(c,
  22. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  23. )
  24. return
  25. }
  26. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  27. decoder := json.NewDecoder(bytes.NewBuffer(r))
  28. decoder.UseNumber()
  29. err = decoder.Decode(&res)
  30. // err = json.Unmarshal(r, &res)
  31. if err != nil {
  32. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  33. return
  34. }
  35. //获取所有mid
  36. var mid []int64
  37. if res.Root != nil {
  38. mid = append(mid, res.Root.Mid)
  39. }
  40. if res.Root.Replies != nil {
  41. mid = append(mid, d.getMidInReplys(res.Root.Replies)...)
  42. }
  43. //批量获取userinfo
  44. var userinfo map[int64]*user.UserBase
  45. userinfo, err = d.JustGetUserBase(c, mid)
  46. if err != nil {
  47. log.Errorv(c, log.KV("log", fmt.Sprintf("d.UserBase err[%v]", err)))
  48. return
  49. }
  50. if res.Root != nil {
  51. d.replaceMemberInReply(res.Root, userinfo)
  52. }
  53. return
  54. }
  55. // ReplyCursor http请求游标获取评论
  56. func (d *Dao) ReplyCursor(c context.Context, req map[string]interface{}) (res *model.CursorRes, err error) {
  57. var r []byte
  58. ip := metadata.String(c, metadata.RemoteIP)
  59. res = new(model.CursorRes)
  60. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_cursor"], "GET", req, ip)
  61. if err != nil {
  62. log.Infov(c,
  63. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  64. )
  65. return
  66. }
  67. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  68. decoder := json.NewDecoder(bytes.NewBuffer(r))
  69. decoder.UseNumber()
  70. err = decoder.Decode(&res)
  71. if err != nil {
  72. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  73. return
  74. }
  75. //获取所有mid
  76. var mid []int64
  77. if res.Replies != nil {
  78. mid = append(mid, d.getMidInReplys(res.Replies)...)
  79. }
  80. if res.Hots != nil {
  81. mid = append(mid, d.getMidInReplys(res.Hots)...)
  82. }
  83. if res.Top != nil {
  84. if res.Top.Admin != nil {
  85. mid = append(mid, res.Top.Admin.Mid)
  86. }
  87. if res.Top.Upper != nil {
  88. mid = append(mid, res.Top.Upper.Mid)
  89. }
  90. }
  91. //批量获取userinfo
  92. var userinfo map[int64]*user.UserBase
  93. userinfo, err = d.JustGetUserBase(c, mid)
  94. if err != nil {
  95. log.Errorv(c, log.KV("log", fmt.Sprintf("d.UserBase err[%v]", err)))
  96. return
  97. }
  98. if res.Replies != nil {
  99. d.replaceMemberInReplys(res.Replies, userinfo)
  100. }
  101. if res.Hots != nil {
  102. d.replaceMemberInReplys(res.Hots, userinfo)
  103. }
  104. if res.Top != nil {
  105. if res.Top.Admin != nil {
  106. d.replaceMemberInReplys(res.Top.Admin.Replies, userinfo)
  107. }
  108. if res.Top.Upper != nil {
  109. d.replaceMemberInReplys(res.Top.Upper.Replies, userinfo)
  110. }
  111. }
  112. return
  113. }
  114. // getMidInReplys 批量获取评论列表中mid
  115. func (d *Dao) getMidInReplys(r []*model.Reply) (l []int64) {
  116. for _, v := range r {
  117. l = append(l, v.Mid)
  118. for _, c := range v.Content.Members {
  119. cmid, err := strconv.ParseInt(c.Mid, 10, 64)
  120. if err != nil {
  121. log.Errorv(
  122. context.Background(),
  123. log.KV("log", fmt.Sprintf("strconv err [%v] data[%s]", err, c.Mid)),
  124. )
  125. continue
  126. }
  127. l = append(l, cmid)
  128. }
  129. if len(v.Replies) > 0 {
  130. sl := d.getMidInReplys(v.Replies)
  131. l = append(l, sl...)
  132. }
  133. }
  134. return
  135. }
  136. // replaceMemberInReplys 批量替换评论列表中用户信息
  137. func (d *Dao) replaceMemberInReplys(r []*model.Reply, umap map[int64]*user.UserBase) {
  138. for _, v := range r {
  139. if u, ok := umap[v.Mid]; ok {
  140. v.Member.BInfo = u
  141. } else {
  142. v.Member.BInfo = new(user.UserBase)
  143. v.Member.BInfo.Mid = v.Mid
  144. v.Member.BInfo.Uname = v.Member.Name
  145. v.Member.BInfo.Face = v.Member.Avatar
  146. }
  147. for _, c := range v.Content.Members {
  148. cmid, err := strconv.ParseInt(c.Mid, 10, 64)
  149. if err != nil {
  150. log.Errorv(
  151. context.Background(),
  152. log.KV("log", fmt.Sprintf("strconv err [%v] data[%s]", err, c.Mid)),
  153. )
  154. continue
  155. }
  156. if u, ok := umap[cmid]; ok {
  157. c.BInfo = u
  158. } else {
  159. c.BInfo = new(user.UserBase)
  160. c.BInfo.Mid = cmid
  161. c.BInfo.Uname = c.Name
  162. c.BInfo.Face = c.Avatar
  163. }
  164. }
  165. if len(v.Replies) > 0 {
  166. d.replaceMemberInReplys(v.Replies, umap)
  167. }
  168. }
  169. }
  170. // replaceMemberInReply 替换评论中用户信息
  171. func (d *Dao) replaceMemberInReply(r *model.Reply, umap map[int64]*user.UserBase) {
  172. if u, ok := umap[r.Mid]; ok {
  173. r.Member.BInfo = u
  174. } else {
  175. r.Member.BInfo = new(user.UserBase)
  176. r.Member.BInfo.Mid = r.Mid
  177. r.Member.BInfo.Uname = r.Member.Name
  178. r.Member.BInfo.Face = r.Member.Avatar
  179. }
  180. for _, c := range r.Content.Members {
  181. cmid, err := strconv.ParseInt(c.Mid, 10, 64)
  182. if err != nil {
  183. log.Errorv(
  184. context.Background(),
  185. log.KV("log", fmt.Sprintf("strconv err [%v] data[%s]", err, c.Mid)),
  186. )
  187. continue
  188. }
  189. if u, ok := umap[cmid]; ok {
  190. c.BInfo = u
  191. } else {
  192. c.BInfo = new(user.UserBase)
  193. c.BInfo.Mid = cmid
  194. c.BInfo.Uname = c.Name
  195. c.BInfo.Face = c.Avatar
  196. }
  197. }
  198. if len(r.Replies) > 0 {
  199. d.replaceMemberInReplys(r.Replies, umap)
  200. }
  201. }
  202. // ReplyAdd http请求添加评论
  203. func (d *Dao) ReplyAdd(c context.Context, req map[string]interface{}) (res *model.AddRes, err error) {
  204. ip := metadata.String(c, metadata.RemoteIP)
  205. res = new(model.AddRes)
  206. var r []byte
  207. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_add"], "POST", req, ip)
  208. if err != nil {
  209. log.Infov(c,
  210. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  211. )
  212. }
  213. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  214. json.Unmarshal(r, &res)
  215. return
  216. }
  217. //ReplyLike http请求评论点赞
  218. func (d *Dao) ReplyLike(c context.Context, req map[string]interface{}) (err error) {
  219. ip := metadata.String(c, metadata.RemoteIP)
  220. _, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_like"], "POST", req, ip)
  221. return
  222. }
  223. // ReplyList 评论列表
  224. func (d *Dao) ReplyList(c context.Context, req map[string]interface{}) (res *model.ReplyList, err error) {
  225. ip := metadata.String(c, metadata.RemoteIP)
  226. res = new(model.ReplyList)
  227. var r []byte
  228. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_list"], "GET", req, ip)
  229. if err != nil {
  230. log.Infov(c,
  231. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  232. )
  233. return
  234. }
  235. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  236. decoder := json.NewDecoder(bytes.NewBuffer(r))
  237. decoder.UseNumber()
  238. err = decoder.Decode(&res)
  239. if err != nil {
  240. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  241. return
  242. }
  243. //获取所有mid
  244. var mid []int64
  245. if res.Replies != nil {
  246. mid = append(mid, d.getMidInReplys(res.Replies)...)
  247. }
  248. if res.Hots != nil {
  249. mid = append(mid, d.getMidInReplys(res.Hots)...)
  250. }
  251. if res.Top != nil {
  252. mid = append(mid, res.Top.Mid)
  253. }
  254. //批量获取userinfo
  255. var userinfo map[int64]*user.UserBase
  256. userinfo, err = d.JustGetUserBase(c, mid)
  257. if err != nil {
  258. log.Errorv(c, log.KV("log", fmt.Sprintf("d.UserBase err[%v]", err)))
  259. return
  260. }
  261. if res.Replies != nil {
  262. d.replaceMemberInReplys(res.Replies, userinfo)
  263. }
  264. if res.Hots != nil {
  265. d.replaceMemberInReplys(res.Hots, userinfo)
  266. }
  267. if res.Top != nil {
  268. d.replaceMemberInReplys(res.Top.Replies, userinfo)
  269. }
  270. return
  271. }
  272. // ReplyReport 评论举报
  273. func (d *Dao) ReplyReport(c context.Context, req map[string]interface{}) (err error) {
  274. ip := metadata.String(c, metadata.RemoteIP)
  275. _, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_report"], "POST", req, ip)
  276. return
  277. }
  278. // ReplyCounts 批量评论数
  279. func (d *Dao) ReplyCounts(c context.Context, ids []int64, t int64) (res map[int64]*model.ReplyCount, err error) {
  280. ip := metadata.String(c, metadata.RemoteIP)
  281. oidStr := strings.Replace(strings.Trim(fmt.Sprint(ids), "[]"), " ", ",", -1)
  282. req := map[string]interface{}{
  283. "type": t,
  284. "oid": oidStr,
  285. }
  286. res = make(map[int64]*model.ReplyCount)
  287. var r []byte
  288. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_counts"], "GET", req, ip)
  289. if err != nil {
  290. log.Infov(c,
  291. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  292. )
  293. return
  294. }
  295. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  296. decoder := json.NewDecoder(bytes.NewBuffer(r))
  297. decoder.UseNumber()
  298. err = decoder.Decode(&res)
  299. if err != nil {
  300. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  301. }
  302. return
  303. }
  304. // ReplyMinfo 批量请求评论
  305. func (d *Dao) ReplyMinfo(c context.Context, svID int64, rpids []int64) (res map[int64]*model.Reply, err error) {
  306. res = make(map[int64]*model.Reply)
  307. if len(rpids) == 0 {
  308. return
  309. }
  310. ip := metadata.String(c, metadata.RemoteIP)
  311. var rpidStr string
  312. for _, rpid := range rpids {
  313. if len(rpidStr) != 0 {
  314. rpidStr += fmt.Sprintf(",%d", rpid)
  315. } else {
  316. rpidStr += fmt.Sprintf("%d", rpid)
  317. }
  318. }
  319. log.V(1).Infov(c, log.KV("log", "reply minfo: rpids="+rpidStr))
  320. req := map[string]interface{}{
  321. "type": model.DefaultCmType,
  322. "oid": svID,
  323. "rpid": rpidStr,
  324. }
  325. var r []byte
  326. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_minfo"], "GET", req, ip)
  327. if err != nil {
  328. log.Errorv(c,
  329. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  330. )
  331. return
  332. }
  333. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  334. decoder := json.NewDecoder(bytes.NewBuffer(r))
  335. decoder.UseNumber()
  336. err = decoder.Decode(&res)
  337. if err != nil {
  338. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  339. }
  340. log.V(1).Infov(c, log.KV("log", "get reply minfo"), log.KV("req_size", len(rpids)), log.KV("rsp_size", len(res)))
  341. return
  342. }
  343. // ReplyHot 获取批量热评
  344. func (d *Dao) ReplyHot(c context.Context, mid int64, oids []int64) (res map[int64][]*model.Reply, err error) {
  345. res = make(map[int64][]*model.Reply)
  346. if len(oids) == 0 {
  347. return
  348. }
  349. ip := metadata.String(c, metadata.RemoteIP)
  350. var oidStr string
  351. for _, oid := range oids {
  352. if len(oidStr) != 0 {
  353. oidStr += fmt.Sprintf(",%d", oid)
  354. } else {
  355. oidStr += fmt.Sprintf("%d", oid)
  356. }
  357. }
  358. log.V(1).Infov(c, log.KV("log", "reply minfo: oids="+oidStr))
  359. req := map[string]interface{}{
  360. "type": model.DefaultCmType,
  361. "oids": oidStr,
  362. "mid": mid,
  363. }
  364. var r []byte
  365. r, err = replyHTTPCommon(c, d.httpClient, d.c.URLs["reply_hots"], "GET", req, ip)
  366. if err != nil {
  367. log.Errorv(c,
  368. log.KV("log", fmt.Sprintf("replyHTTPCommon err [%v]", err)),
  369. )
  370. return
  371. }
  372. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  373. decoder := json.NewDecoder(bytes.NewBuffer(r))
  374. decoder.UseNumber()
  375. err = decoder.Decode(&res)
  376. if err != nil {
  377. log.Errorv(c, log.KV("log", fmt.Sprintf("json unmarlshal err data[%s]", string(r))))
  378. }
  379. log.V(1).Infov(c, log.KV("log", "get reply hots"), log.KV("req_size", len(oids)), log.KV("rsp_size", len(res)))
  380. // 替换bbq的用户信息
  381. var mIDs []int64
  382. for _, replies := range res {
  383. for _, reply := range replies {
  384. mIDs = append(mIDs, reply.Mid)
  385. }
  386. }
  387. var userBases map[int64]*user.UserBase
  388. userBases, err = d.JustGetUserBase(c, mIDs)
  389. if err != nil {
  390. log.Errorv(c, log.KV("log", fmt.Sprintf("d.UserBase err[%v]", err)))
  391. return
  392. }
  393. for _, replies := range res {
  394. if len(replies) > 0 {
  395. d.replaceMemberInReplys(replies, userBases)
  396. }
  397. }
  398. return
  399. }