group_mic.go 12.1 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8
package group_mic_s

import (
	"context"
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/redisCli"
hujiebin's avatar
hujiebin committed
9
	"git.hilo.cn/hilo-common/rpc"
hujiebin's avatar
hujiebin committed
10 11 12 13
	uuid "github.com/satori/go.uuid"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/redis_key"
	"hilo-group/domain/event/group_ev"
hujiebin's avatar
hujiebin committed
14
	"hilo-group/domain/model/groupPower_m"
hujiebin's avatar
hujiebin committed
15
	"hilo-group/domain/model/group_m"
hujiebin's avatar
hujiebin committed
16
	"hilo-group/domain/model/user_m"
hujiebin's avatar
hujiebin committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	"hilo-group/domain/service/signal_s"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

type GroupMicService struct {
	svc *domain.Service
}

func NewGroupPowerService(myContext *mycontext.MyContext) *GroupMicService {
	svc := domain.CreateService(myContext)
	return &GroupMicService{svc}
}

//修改群组中麦的数量
func (s *GroupMicService) GroupMicNumChange(groupId string, userId uint64, micNumType group_e.GroupMicNumType, micOn bool) error {
	model := domain.CreateModelContext(s.svc.MyContext)
	//数据库修改群组麦的数量
	//检查权限
	if err := group_m.CheckPermission(model, groupId, userId); err != nil {
		return err
	}
	//删除麦位数量类型缓存
	group_m.InitMicNumType(model, groupId, micNumType).ClearCache()

	//
	groupInfo, _ := group_m.GetGroupInfo(model, groupId)
	if groupInfo == nil {
		return bizerr.GroupNotFound
	}

	//判断数据是否发生了变化
	//关闭麦位
	if micOn == false {
		if groupInfo.MicOn == micOn {
			return nil
		}
	} else {
		if groupInfo.MicOn == micOn && groupInfo.MicNumType == micNumType {
			return nil
		}
	}
	//修改数据,然后数据持久化,
	g := group_m.GroupInfo{
		MicOn: micOn,
	}
	fields := []string{"mic_on"}
	//开启麦位才修改micNumType
	returnMicNumType := g.MicNumType
	if micOn == true {
		g.MicNumType = micNumType
		fields = append(fields, "mic_num_type")
		returnMicNumType = micNumType
	}
	db := g.Update(model, groupId, fields)
	if db.Error != nil {
		return myerr.WrapErr(db.Error)
	}

	//增加麦位数量类型缓存
	group_m.InitMicNumType(model, groupId, micNumType).AddCache()

	//麦位上的信息,清理
	group_m.ClearMic(groupId)

	type Content struct {
		MicOn      bool                    `json:"micOn"`
		MicNumType group_e.GroupMicNumType `json:"micNumType"`
		Timestamp  int64                   `json:"timestamp"`
	}

	r := Content{
		MicOn:      micOn,
		MicNumType: returnMicNumType,
		Timestamp:  time.Now().Unix(),
	}
	buf, err := json.Marshal(r)
	if err != nil {
		model.Log.Errorf("GroupMicNumChange Content json.Marshal err:%v", err)
	}
	// 发信令,让前端重新拉取,接受容错,
	signal_s.SendSignalMsg(model, groupId, group_m.GroupSystemMsg{
		MsgId:   group_e.GroupMicChangeSignal,
		Content: string(buf),
	}, false)

	group_m.MicNumChangeRPush(model, groupId, returnMicNumType, micOn)

	return nil
}

//加锁, sign作为密钥存在(预防被别人删除,比如:先者删除了后者的锁),
func redisLock(key string, sign string, expiration time.Duration, callBack func() error) error {
	flag, err := redisCli.GetRedis().SetNX(context.Background(), key, sign, expiration).Result()
	if err != nil {
		return myerr.WrapErr(err)
	}
	if flag {
		err = callBack()
		redisSign, _ := redisCli.GetRedis().Get(context.Background(), key).Result()
		if redisSign == sign {
			redisCli.GetRedis().Del(context.Background(), key)
		}
		return err
	} else {
		return bizerr.GroupConcurrencyLock
	}
}

//加入麦位,锁两秒
func (s *GroupMicService) GroupMicIn(groupUuid string, i int, userId uint64, externalId string) error {
hujiebin's avatar
hujiebin committed
129
	return redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
hujiebin's avatar
hujiebin committed
130 131 132 133 134
		model := domain.CreateModel(s.svc.CtxAndDb)
		mic, err := group_m.GetMic(model, groupUuid, i)
		if err != nil {
			return err
		}
hujiebin's avatar
hujiebin committed
135
		return mic.In(userId, externalId)
hujiebin's avatar
hujiebin committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	})
}

//离开麦位
func (s *GroupMicService) GroupMicLeave(groupUuid string, i int, userId uint64, externalId string) error {
	return redisLock(redis_key.GetPrefixGroupMicUserDelLock(groupUuid, i), uuid.NewV4().String(), time.Second*2, func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		micUser, err := group_m.GetMicUser(model, groupUuid, i)
		if err != nil {
			return err
		}
		if micUser != nil {
			return micUser.LeaveByUser(userId, externalId)
		}
		return nil
	})
}

//邀请上麦,锁两秒
func (s *GroupMicService) GroupMicInvite(groupUuid string, operateUserId uint64, beInvitedExternalId string) error {
	model := domain.CreateModelContext(s.svc.MyContext)
	if err := group_m.InviteMicIn(model, groupUuid, operateUserId, beInvitedExternalId); err != nil {
		return err
	}
	return nil
}

//麦位加锁,
func (s *GroupMicService) GroupMicLock(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrLock(userId, externalId)
}

//麦位解锁
func (s *GroupMicService) GroupMicUnLock(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrUnLock(userId, externalId)
}

// 麦位静音
func (s *GroupMicService) GroupMicMute(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrMute(userId, externalId)
}

// 麦位解除静音
func (s *GroupMicService) GroupMicUnMute(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrUnMute(userId, externalId)
}

//开启麦
func (s *GroupMicService) GroupMicSpeechOpen(userId uint64, externalId string, groupUuid string, i int) error {
	//自己/管理人开启禁麦,并发率不高,后果等级不高
	model := domain.CreateModelContext(s.svc.MyContext)
	micUser, err := group_m.GetMicUser(model, groupUuid, i)
	if err != nil {
		return err
	}
	return micUser.SpeechOpen(userId, externalId)
}

//关闭麦
chenweijian's avatar
chenweijian committed
219
func (s *GroupMicService) GroupMicSpeechClose(userId uint64, externalId string, groupUuid, lang string, i int) error {
hujiebin's avatar
hujiebin committed
220 221 222 223 224
	model := domain.CreateModelContext(s.svc.MyContext)
	micUser, err := group_m.GetMicUser(model, groupUuid, i)
	if err != nil {
		return err
	}
chenweijian's avatar
chenweijian committed
225
	return micUser.SpeechClose(userId, externalId, lang)
hujiebin's avatar
hujiebin committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

//麦上的人群发消息
func (s *GroupMicService) GroupIMMassByInMic(groupId string, userId uint64, externalId string, content string) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		micUser, err := group_m.GetMicUserByExternalId(model, externalId)
		if err != nil {
			return err
		}
		if err := micUser.ImMass(externalId); err != nil {
			return err
		}
		//校验群组ID
		if micUser.GroupUuid != groupId {
			return myerr.NewSysError("groupId 不一致, http:groupId <> micUser.groupId")
		}

		//获取群成员
		gm, err := group_m.GetMembers(model.Db, groupId)
		if err != nil {
			return err
		}
		uids := make([]uint64, 0)
		for _, i := range gm {
			//排除自己
			if userId != i.UserId {
				uids = append(uids, i.UserId)
			}
		}
		return group_ev.PublishGroupImMass(model, &group_ev.GroupImMassEvent{
			GroupId: groupId,
			UserId:  userId,
			Members: uids,
			Content: content,
		})
	})
}

// 群管理人群发消息
func (s *GroupMicService) GroupIMMassByMgr(groupId string, userId uint64, externalId string, content string) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		//检查权限
		if err := group_m.CheckPermission(model, groupId, userId); err != nil {
			return err
		}
		//获取群成员
		gm, err := group_m.GetMembers(model.Db, groupId)
		if err != nil {
			return err
		}
		uids := make([]uint64, 0)
		for _, i := range gm {
			//排除自己
			if userId != i.UserId {
				uids = append(uids, i.UserId)
			}
		}
		return group_ev.PublishGroupImMass(model, &group_ev.GroupImMassEvent{
			GroupId: groupId,
			UserId:  userId,
			Members: uids,
			Content: content,
		})
	})
}
hujiebin's avatar
hujiebin committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

// 增加势力上麦经验/时长
func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId uint64, joinMicTimestamp int64) error {
	var model = domain.CreateModelContext(s.svc.MyContext)
	exists, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, userId)
	if err != nil {
		return err
	}
	if !exists {
		return nil
	}
	// 获取势力下所有群组
	groups, err := groupPower_m.GetGroupPowerGroups(model, groupPowerId)
	if err != nil {
		return err
	}
	inGroup := false
	for _, group := range groups {
		if group.ImGroupId == groupId {
			inGroup = true
			break
		}
	}
	if !inGroup {
		return nil
	}
	// 增加势力上麦经验
	if err := groupPower_m.IncrGroupPowerExpOnMic(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerExpOnMic fail:%v", err)
	}
323 324 325 326 327 328 329
	// 增加势力上麦时长-月
	if err := groupPower_m.IncrGroupPowerStarOnMicMonth(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerStarOnMicMonth fail:%v", err)
	}
	// 增加势力上麦时长-天
	if err := groupPower_m.IncrGroupPowerStarOnMicDay(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerStarOnMicDay fail:%v", err)
hujiebin's avatar
hujiebin committed
330 331 332
	}
	return nil
}
hujiebin's avatar
hujiebin committed
333

hujiebin's avatar
hujiebin committed
334
// 检查cp上麦
hujiebin's avatar
hujiebin committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
func (s *GroupMicService) CheckCpOnMic(groupUuid string) {
	model := domain.CreateModelContext(s.svc.MyContext)
	// 填充cp麦位
	micNumType, err := group_m.GetMicNumType(model, groupUuid)
	if err != nil {
		return
	}
	_, micUsers, err := group_m.GetAllMic(groupUuid, micNumType)
	if err != nil {
		return
	}
	// userId->micIndex
	var userMicIndex = make(map[uint64]int)
	var userIds []uint64
	for _, u := range micUsers {
		userMicIndex[u.UserId] = u.I
		userIds = append(userIds, u.UserId)
	}
	cpPairs, err := rpc.MGetUserCpPairs(model, userIds)
	// 更新麦上cp的信息
	if err != nil {
		model.Log.Errorf("MGetUserCpPairs fail:%v", err)
		return
	}
	users, err := user_m.GetUserMapByIds(model, userIds)
	if err != nil {
		model.Log.Errorf("GetUserMapByIds fail:%v", err)
		return
	}
	for _, pairs := range cpPairs {
		userId := pairs[0]
		cpUserId := pairs[1]
		redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
			model := domain.CreateModel(s.svc.CtxAndDb)
			mic, err := group_m.GetMic(model, groupUuid, userMicIndex[userId])
			if err != nil {
				return err
			}
			return mic.Update(userId, users[userId].ExternalId, cpUserId)
		})
		redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error {
			model := domain.CreateModel(s.svc.CtxAndDb)
			mic, err := group_m.GetMic(model, groupUuid, userMicIndex[cpUserId])
			if err != nil {
				return err
			}
			return mic.Update(cpUserId, users[cpUserId].ExternalId, userId)
		})
	}
	return
}
hujiebin's avatar
hujiebin committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

// 检查cp下麦
func (s *GroupMicService) CheckCpLeaveMic(groupUuid string, leaveUid uint64) {
	model := domain.CreateModelContext(s.svc.MyContext)
	// 填充cp麦位
	micNumType, err := group_m.GetMicNumType(model, groupUuid)
	if err != nil {
		return
	}
	_, micUsers, err := group_m.GetAllMic(groupUuid, micNumType)
	if err != nil {
		return
	}
	// userId->micIndex
	var userMicIndex = make(map[uint64]int)
	var cpUserId uint64
	for _, u := range micUsers {
		if u.UserId == leaveUid {
			cpUserId = u.CpUserId
		}
		userMicIndex[u.UserId] = u.I
	}
	if cpUserId <= 0 {
		return
	}
	user, err := user_m.GetUser(model, cpUserId)
	if err != nil {
		model.Log.Errorf("GetUser fail:%v", err)
		return
	}
	redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		mic, err := group_m.GetMic(model, groupUuid, userMicIndex[cpUserId])
		if err != nil {
			return err
		}
		return mic.Update(cpUserId, user.ExternalId, 0)
	})
	return
}