room_black.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package room
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. pb "bet24.com/servers/micros/audioroom/proto"
  6. "bet24.com/servers/micros/audioroom/transaction/database"
  7. "bet24.com/servers/zego"
  8. "strconv"
  9. "time"
  10. )
  11. // 加载黑名单列表
  12. func (this *Room) loadBlackList() {
  13. blackList := database.GetBlackList(this.RoomId)
  14. if len(blackList) <= 0 {
  15. return
  16. }
  17. this.blackList = blackList
  18. }
  19. // 获取黑名单列表
  20. func (this *Room) GetBlackList() []pb.BlackInfo {
  21. return this.blackList
  22. }
  23. // 获取黑名单信息
  24. func (this *Room) GetBlackInfo(userId int) *pb.BlackInfo {
  25. // 黑名单列表
  26. for i := 0; i < len(this.blackList); i++ {
  27. if this.blackList[i].UserId == userId {
  28. return &this.blackList[i]
  29. }
  30. }
  31. return nil
  32. }
  33. // 是否黑名单
  34. func (this *Room) IsBlack(userId, blackType int) bool {
  35. if blackType <= pb.BlackType_Invalid || blackType >= pb.BlackType_Max {
  36. return false
  37. }
  38. blackInfo := this.GetBlackInfo(userId)
  39. if blackInfo == nil {
  40. return false
  41. }
  42. for _, t := range blackInfo.BlackTypes {
  43. if t.BlackType != blackType {
  44. continue
  45. }
  46. // 是否在有效期内
  47. if t.ExpireTimeStamp >= common.GetTimeStamp() {
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. // 是否有黑名单权限
  54. func (this *Room) IsBlackPermission(userId, toUserId, blackType int) bool {
  55. permissionType := 0
  56. switch blackType {
  57. case pb.BlackType_Room:
  58. permissionType = pb.PermissionType_Room
  59. case pb.BlackType_Mic:
  60. permissionType = pb.PermissionType_Mic
  61. }
  62. return this.IsSetPermission(userId, toUserId, permissionType)
  63. }
  64. // 添加黑名单
  65. func (this *Room) AddBlack(userId, toUserId, blackType, seconds int) int {
  66. //log.Debug("room_black.addBlack userId=%d toUserId=%d blackType=%d seconds=%d",
  67. // userId, toUserId, blackType, seconds)
  68. // 判断是否有权限
  69. if toUserId <= 0 {
  70. return 12
  71. }
  72. // 判断是否有权限
  73. if !this.IsBlackPermission(userId, toUserId, blackType) {
  74. return 12
  75. }
  76. // 默认值
  77. expireTimeStamp := common.GetStamp(time.Now().AddDate(10, 0, 0))
  78. // 计算过期时间戳
  79. if blackType == pb.BlackType_Room && seconds > 0 {
  80. expireTimeStamp = common.GetTimeStamp() + seconds
  81. }
  82. // 获取黑名单信息
  83. blackInfo := this.GetBlackInfo(toUserId)
  84. // 新增黑名单数据
  85. if blackInfo == nil {
  86. blackInfo := pb.BlackInfo{
  87. UserId: toUserId,
  88. }
  89. blackInfo.BlackTypes = append(blackInfo.BlackTypes, pb.BlackType{
  90. BlackType: blackType,
  91. ExpireTimeStamp: expireTimeStamp,
  92. })
  93. // 添加黑名单列表
  94. this.blackList = append(this.blackList, blackInfo)
  95. // 添加黑名单处理
  96. go this.AddBlackHandler(userId, toUserId, blackType, expireTimeStamp)
  97. return 1
  98. }
  99. // 已在黑名单列表
  100. for i := 0; i < len(blackInfo.BlackTypes); i++ {
  101. if blackInfo.BlackTypes[i].BlackType != blackType {
  102. continue
  103. }
  104. // 修改设置过期时间
  105. blackInfo.BlackTypes[i].ExpireTimeStamp = expireTimeStamp
  106. // 添加黑名单处理
  107. go this.AddBlackHandler(userId, toUserId, blackType, expireTimeStamp)
  108. return 1
  109. }
  110. // 新增该黑名单类型
  111. blackInfo.BlackTypes = append(blackInfo.BlackTypes, pb.BlackType{
  112. BlackType: blackType,
  113. ExpireTimeStamp: expireTimeStamp,
  114. })
  115. // 添加黑名单处理
  116. go this.AddBlackHandler(userId, toUserId, blackType, expireTimeStamp)
  117. return 1
  118. }
  119. // 添加黑名单处理
  120. func (this *Room) AddBlackHandler(userId, toUserId, blackType, expireTimeStamp int) {
  121. //log.Debug("room_black.addBlackHandler userId=%d toUserId=%d blackType=%d", userId, toUserId, blackType)
  122. // 判断是否在麦位
  123. for i := 0; i < len(this.mics); i++ {
  124. if this.mics[i].UserId != toUserId {
  125. continue
  126. }
  127. roomId := strconv.Itoa(this.RoomId)
  128. uid := strconv.Itoa(toUserId * -1)
  129. // sdk服务器 ==> 删除房间流
  130. code, message := zego.MuteUser(roomId, uid, this.mics[i].StreamId)
  131. log.Debug("room.addBlackHandler(zego.MuteUser) i=%d userId=%s roomId=%s streamId=%s code=%d message=%s",
  132. i, uid, roomId, this.mics[i].StreamId, code, message)
  133. // 清空麦位
  134. this.mics[i] = pb.MicInfo{
  135. UserId: 0,
  136. StreamId: "",
  137. }
  138. // 麦位拉黑,给所有人发
  139. go this.notify(pb.Notify_Action_Refresh_Mic, pb.ReasonData{
  140. Reason: pb.Notify_Reason_Mic_AddMicBlack,
  141. UserId: toUserId,
  142. })
  143. break
  144. }
  145. operateType := 0
  146. // 黑名单房间
  147. if blackType == pb.BlackType_Room {
  148. operateType = pb.OperateType_AddBlack
  149. // 给所有用户发送通知
  150. // this.userNotify(toUserId, pb.UserNotify_Black, pb.Role_Guest)
  151. this.notify(pb.Notify_Action_Refresh_User, pb.ReasonData{
  152. Reason: pb.Notify_Reason_User_AddRoomBlack,
  153. UserId: toUserId,
  154. })
  155. // 删除成员
  156. this.DelMember(userId, toUserId, true)
  157. } else if blackType == pb.BlackType_Mic {
  158. operateType = pb.OperateType_ForbidMic
  159. // 麦位拉黑,刷新用户
  160. this.notify(pb.Notify_Action_Refresh_User, pb.ReasonData{
  161. Reason: pb.Notify_Reason_User_AddMicBlack,
  162. UserId: toUserId,
  163. })
  164. }
  165. // 添加日志
  166. this.AddOperateLog(this.RoomId, userId, toUserId, operateType)
  167. // 数据库操作
  168. database.AddBlack(this.RoomId, toUserId, blackType, expireTimeStamp)
  169. // 删除麦位申请列表
  170. go this.delMicApply(toUserId)
  171. }
  172. // 移除黑名单
  173. func (this *Room) RemoveBlack(userId, toUserId, blackType int) int {
  174. //log.Debug("room_black.removeBlack userId=%d toUserId=%d blackType=%d",
  175. // userId, toUserId, blackType)
  176. // 判断是否有权限
  177. if !this.IsBlackPermission(userId, toUserId, blackType) {
  178. return 12
  179. }
  180. // 获取黑名单信息
  181. blackInfo := this.GetBlackInfo(toUserId)
  182. if blackInfo == nil {
  183. return 13
  184. }
  185. for i := 0; i < len(blackInfo.BlackTypes); i++ {
  186. if blackInfo.BlackTypes[i].BlackType != blackType {
  187. continue
  188. }
  189. // 移除黑名单
  190. blackInfo.BlackTypes = append(blackInfo.BlackTypes[:i], blackInfo.BlackTypes[i+1:]...)
  191. break
  192. }
  193. // 没有数据
  194. if len(blackInfo.BlackTypes) <= 0 {
  195. for i := 0; i < len(this.blackList); i++ {
  196. if this.blackList[i].UserId != toUserId {
  197. continue
  198. }
  199. // 删除
  200. this.blackList = append(this.blackList[:i], this.blackList[i+1:]...)
  201. break
  202. }
  203. }
  204. operateType := 0
  205. if blackType == pb.BlackType_Room {
  206. operateType = pb.OperateType_RemoveBlack
  207. } else if blackType == pb.BlackType_Mic {
  208. operateType = pb.OperateType_UnforbidMic
  209. // 麦位拉黑,给所有人发
  210. this.notify(pb.Notify_Action_Refresh_User, pb.ReasonData{
  211. Reason: pb.Notify_Reason_User_RemoveMicBlack,
  212. UserId: toUserId,
  213. })
  214. }
  215. go func() {
  216. // 添加日志
  217. this.AddOperateLog(this.RoomId, userId, toUserId, operateType)
  218. // 数据库操作
  219. database.RemoveBlack(this.RoomId, toUserId, blackType)
  220. }()
  221. return 1
  222. }