Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"data.go",
"getCheckInfo.go",
"getDMConf.go",
"gethistory.go",
"grpc.go",
"ipdb.go",
"liveRPC.go",
"ratelimit.go",
"send.go",
"white_list.go",
],
importpath = "go-common/app/service/live/live-dm/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/live/activity/api/liverpc:go_default_library",
"//app/service/live/activity/api/liverpc/v1:go_default_library",
"//app/service/live/av/api/liverpc:go_default_library",
"//app/service/live/banned_service/api/liverpc:go_default_library",
"//app/service/live/broadcast-proxy/api/v1:go_default_library",
"//app/service/live/fans_medal/api/liverpc:go_default_library",
"//app/service/live/fans_medal/api/liverpc/v2:go_default_library",
"//app/service/live/live-dm/conf:go_default_library",
"//app/service/live/live-dm/model:go_default_library",
"//app/service/live/live_user/api/liverpc:go_default_library",
"//app/service/live/live_user/api/liverpc/v1:go_default_library",
"//app/service/live/rankdb/api/liverpc:go_default_library",
"//app/service/live/rankdb/api/liverpc/v1:go_default_library",
"//app/service/live/rc/api/liverpc:go_default_library",
"//app/service/live/rc/api/liverpc/v1:go_default_library",
"//app/service/live/resource/sdk:go_default_library",
"//app/service/live/room/api/liverpc:go_default_library",
"//app/service/live/room/api/liverpc/v1:go_default_library",
"//app/service/live/room/api/liverpc/v2:go_default_library",
"//app/service/live/third_api/liveBroadcast:go_default_library",
"//app/service/live/user/api/liverpc:go_default_library",
"//app/service/live/user/api/liverpc/v3:go_default_library",
"//app/service/live/userext/api/liverpc:go_default_library",
"//app/service/live/userext/api/liverpc/v1:go_default_library",
"//app/service/live/xuser/api/grpc/v1:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/filter/api/grpc/v1:go_default_library",
"//app/service/main/location/api:go_default_library",
"//app/service/main/spy/api:go_default_library",
"//library/cache/redis:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/rpc/liverpc:go_default_library",
"//library/net/rpc/warden:go_default_library",
"//library/queue/databus:go_default_library",
"//library/sync/pipeline/fanout:go_default_library",
"//vendor/github.com/ipipdotnet/ipdb-go:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"getCheckInfo_test.go",
"getDMConf_test.go",
"ratelimit_test.go",
"send_test.go",
"white_list_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/service/live/activity/api/liverpc:go_default_library",
"//app/service/live/fans_medal/api/liverpc:go_default_library",
"//app/service/live/live-dm/conf:go_default_library",
"//app/service/live/live_user/api/liverpc:go_default_library",
"//app/service/live/rankdb/api/liverpc:go_default_library",
"//app/service/live/rc/api/liverpc:go_default_library",
"//app/service/live/room/api/liverpc:go_default_library",
"//app/service/live/userext/api/liverpc:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/filter/api/grpc/v1:go_default_library",
"//app/service/main/spy/api:go_default_library",
"//library/net/metadata:go_default_library",
],
)

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"go-common/app/service/live/live-dm/conf"
"go-common/library/cache/redis"
"go-common/library/sync/pipeline/fanout"
)
// Dao dao
type Dao struct {
c *conf.Config
redis *redis.Pool
whitelistredis *redis.Pool
Databus *fanout.Fanout
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
redis: redis.NewPool(c.Redis),
whitelistredis: redis.NewPool(c.WhiteListRedis),
Databus: fanout.New("dmDatabus", fanout.Worker(1), fanout.Buffer(c.CacheDatabus.Size)),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.redis.Close()
d.Databus.Close()
d.whitelistredis.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
// TODO: redis
return nil
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"encoding/json"
"go-common/app/service/live/live-dm/conf"
titansSdk "go-common/app/service/live/resource/sdk"
"go-common/library/log"
"go-common/library/log/infoc"
"go-common/library/queue/databus"
)
var (
//拜年祭投递databus
bndatabus *databus.Databus
//InfocDMSend 弹幕发送成功上报
InfocDMSend *infoc.Infoc
//InfocDMErr 弹幕发送失败上报
InfocDMErr *infoc.Infoc
)
//InitDatabus 初始化databus
func InitDatabus(c *conf.Config) {
bndatabus = databus.New(c.BNDatabus)
}
//InitLancer 初始化lancer上报
func InitLancer(c *conf.Config) {
InfocDMErr = infoc.New(c.Lancer.DMErr)
InfocDMSend = infoc.New(c.Lancer.DMSend)
}
//InitTitan 初始化kv配置
func InitTitan() {
conf := &titansSdk.Config{
TreeId: 72389,
Expire: 1,
}
titansSdk.Init(conf)
}
//LimitConf 弹幕限制配置
type LimitConf struct {
AreaLimit bool `json:"areaLimit"`
AllUserLimit bool `json:"AllUserLimit"`
LevelLimitStatus bool `json:"LevelLimitStatus"`
LevelLimit int64 `json:"LevelLimit"`
RealName bool `json:"RealName"`
PhoneLimit bool `json:"PhoneLimit"`
MsgLength int `json:"MsgLength"`
DmNum int64 `json:"DmNum"`
DMPercent int64 `json:"DMPercent"`
DMwhitelist bool `json:"DMwhitelist"`
DMwhitelistID string `json:"DMwhitelistID"`
}
//GetDMCheckConf 获取弹幕配置参数
func (l *LimitConf) GetDMCheckConf() {
cf, err := titansSdk.Get("dmLimit")
if err != nil {
log.Error("DM: get conf err:%+v", err)
return
}
conf := &LimitConf{}
err = json.Unmarshal([]byte(cf), conf)
if err != nil {
log.Error("DM: decode conf jsons err:%+v conf:%s", err, cf)
return
}
l.AreaLimit = conf.AreaLimit
l.AllUserLimit = conf.AllUserLimit
l.LevelLimitStatus = conf.LevelLimitStatus
l.LevelLimit = conf.LevelLimit
l.RealName = conf.RealName
l.PhoneLimit = conf.PhoneLimit
l.DmNum = conf.DmNum
l.DMwhitelist = conf.DMwhitelist
l.DMPercent = conf.DMPercent
l.MsgLength = conf.MsgLength
l.DMwhitelistID = conf.DMwhitelistID
}

View File

@@ -0,0 +1,391 @@
package dao
import (
"context"
"strconv"
"sync"
fansMedalService "go-common/app/service/live/fans_medal/api/liverpc/v2"
"go-common/app/service/live/live-dm/conf"
liveUserService "go-common/app/service/live/live_user/api/liverpc/v1"
roomService "go-common/app/service/live/room/api/liverpc/v2"
userService "go-common/app/service/live/user/api/liverpc/v3"
userextService "go-common/app/service/live/userext/api/liverpc/v1"
xuserService "go-common/app/service/live/xuser/api/grpc/v1"
acctountService "go-common/app/service/main/account/api"
filterService "go-common/app/service/main/filter/api/grpc/v1"
spyService "go-common/app/service/main/spy/api"
"go-common/library/log"
"github.com/pkg/errors"
)
//UserInfo 用户等级,经验等信息
type UserInfo struct {
Vip int
Svip int
UserLever int64
UserScore int64
ULevelRank int64
ULevelColor int64
UnameColor string
RoomAdmin bool
PrivilegeType int
Bubble int64
MedalInfo *FansMedalInfo
lock sync.Mutex
}
//UserScore 用户真实分已经弹幕ai分
type UserScore struct {
UserScore int64
MsgAI int64
MsgLevel int64
lock sync.Mutex
}
//DMConf 弹幕配置
type DMConf struct {
Mode int64
Color int64
Length int64
}
//FansMedalInfo 粉丝勋章信息
type FansMedalInfo struct {
MedalID int64
RUID int64
RUName string
MedalLevel int64
MedalName string
AnchorName string
RoomID int64
MColor int64
SpecialMedal string
lock sync.Mutex
}
//RoomConf 播主房间配置信息
type RoomConf struct {
UID int64
RoomShield int64
RoomID int64
Anchor string
}
//UserBindInfo 用户手机,实名等信息
type UserBindInfo struct {
Identification int32
MobileVerify int32
Uname string
URank int32
// MobileVirtual int64 无虚拟号段信息
}
//Get 获取用户的弹幕配置信息
func (d *DMConf) Get(ctx context.Context, uid int64, roomid int64, c *conf.Config) error {
req := &userextService.DanmuConfGetAllReq{
Uid: uid,
Roomid: roomid,
}
resp, err := UserExtServiceClient.V1DanmuConf.GetAll(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: userextService GetAll err: %v", err)
}
d.Color = 16777215
d.Mode = 1
d.Length = 20
return nil
}
if resp.Code != 0 {
log.Error("DM: userextService GetAll err code: %d", resp.Code)
d.Color = 16777215
d.Mode = 1
d.Length = 20
return nil
}
d.Color = resp.Data.Color
d.Mode = resp.Data.Mode
d.Length = resp.Data.Length
su := strconv.FormatInt(uid, 10)
if c.DmRules.Nixiang[su] {
d.Mode = 6
}
if c.DmRules.Color[su] != 0 {
d.Color = c.DmRules.Color[su]
}
return nil
}
// Get 获取用户等级,经验等信息
func (u *UserInfo) Get(ctx context.Context, uid int64) error {
req := &userService.UserGetUserLevelInfoReq{
Uid: uid,
}
resp, err := userClient.V3User.GetUserLevelInfo(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: user err: %v", err)
}
u.lock.Lock()
u.UserLever = 0
u.UserScore = 0
u.ULevelColor = 16752445
u.lock.Unlock()
return nil
}
if resp.Code != 0 {
log.Error("DM: user GetUserLevelInfo err code: %d", resp.Code)
u.lock.Lock()
u.UserLever = 0
u.UserScore = 0
u.ULevelColor = 16752445
u.lock.Unlock()
return nil
}
u.lock.Lock()
u.UserLever = resp.Data.Level
u.UserScore = resp.Data.Exp
u.ULevelColor = resp.Data.Color
u.lock.Unlock()
return nil
}
//GetVipInfo 获取用户的老爷等级
func (u *UserInfo) GetVipInfo(ctx context.Context, uid int64) error {
req := &xuserService.UidReq{
Uid: uid,
}
resp, err := vipCli.Info(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: xuserService get vip Info err: %v", err)
}
u.lock.Lock()
u.Vip = 0
u.Svip = 0
u.lock.Unlock()
return nil
}
u.lock.Lock()
u.Vip = resp.Info.Vip
u.Svip = resp.Info.Svip
u.lock.Unlock()
return nil
}
//GetPrivilegeType 获取大航海信息
func (u *UserInfo) GetPrivilegeType(ctx context.Context, uid int64, ruid int64) error {
req := &liveUserService.GuardGetByUidTargetIdReq{
Uid: uid,
TargetId: ruid,
IsLimitOne: 0,
}
resp, err := LiveUserServiceClient.V1Guard.GetByUidTargetId(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: liveUserService GetByUidTargetId err: %v", err)
}
u.lock.Lock()
u.PrivilegeType = 0
u.lock.Unlock()
return nil
}
if resp.Code != 0 {
log.Error("DM: liveUserService GetByUidTargetId err code: %d", resp.Code)
u.lock.Lock()
u.PrivilegeType = 0
u.lock.Unlock()
return nil
}
var PrivilegeType int64
for _, val := range resp.Data {
i := val.PrivilegeType
if PrivilegeType == 0 {
PrivilegeType = i
}
if PrivilegeType != 0 && PrivilegeType > i {
PrivilegeType = i
}
}
u.lock.Lock()
u.PrivilegeType = int(PrivilegeType)
u.lock.Unlock()
return nil
}
//IsRoomAdmin 判断用户是否是房管
func (u *UserInfo) IsRoomAdmin(ctx context.Context, uid int64, roomid int64) error {
defer u.lock.Unlock()
if roomid == 59125 || roomid == 5440 {
u.lock.Lock()
u.RoomAdmin = false
return nil
}
req := &xuserService.RoomAdminIsAdminShortReq{
Uid: uid,
Roomid: roomid,
}
resp, err := isAdmin.IsAdminShort(ctx, req)
if err != nil {
log.Error("DM: xuser IsAdminShort err: %+v", err)
u.lock.Lock()
u.RoomAdmin = false
return nil
}
if resp.Result == 1 {
u.lock.Lock()
u.RoomAdmin = true
return nil
}
u.lock.Lock()
u.RoomAdmin = false
return nil
}
//GetFansMedal 获取主播的粉丝勋章信息
func (f *FansMedalInfo) GetFansMedal(ctx context.Context, uid int64) error {
req := &fansMedalService.HighQpsLiveWearedReq{
Uid: uid,
}
resq, err := FansMedalServiceClient.V2HighQps.LiveWeared(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: fansMedalService LiveWeared err: %v", err)
}
f.RUID = 0
f.MedalLevel = 1
f.MedalName = ""
f.MColor = 0
return nil
}
if resq.Code != 0 {
log.Error("DM: fansMedalService LiveWeared err code: %d", resq.Code)
f.RUID = 0
f.MedalLevel = 1
f.MedalName = ""
f.MColor = 0
return nil
}
f.RUID = resq.Data.TargetId
f.MedalLevel = resq.Data.Level
f.MedalName = resq.Data.MedalName
f.MColor = resq.Data.MedalColor
return nil
}
//Get 获取主播房间配置信息s
func (r *RoomConf) Get(ctx context.Context, roomID int64) error {
req := &roomService.RoomGetByIdsReq{
Ids: []int64{roomID},
}
resp, err := RoomServiceClient.V2Room.GetByIds(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: roomService GetByIds err: %v", err)
}
r.RoomID = roomID
r.UID = 0
r.RoomShield = 0
r.Anchor = ""
return nil
}
if resp.Code != 0 {
log.Error("DM: roomService GetByIds err code: %d", resp.Code)
r.RoomID = roomID
r.UID = 0
r.RoomShield = 0
r.Anchor = ""
return nil
}
if _, ok := resp.Data[roomID]; !ok {
log.Error("DM: roomService GetByIds error roomid:%d", roomID)
r.RoomID = roomID
r.UID = 0
r.RoomShield = 0
r.Anchor = ""
return nil
}
r.RoomID = resp.Data[roomID].Roomid
r.UID = resp.Data[roomID].Uid
r.RoomShield = resp.Data[roomID].RoomShield
r.Anchor = resp.Data[roomID].Uname
return nil
}
//Get 获取用户绑定信息,实名认知,绑定手机等信息
func (u *UserBindInfo) Get(ctx context.Context, uid int64) error {
req := &acctountService.MidReq{
Mid: uid,
}
resp, err := ac.Profile3(ctx, req)
if err != nil {
log.Error("DM: acctountService Profile3 err: %v", err)
u.Identification = 0
u.MobileVerify = 0
u.Uname = ""
u.URank = 0
return nil
}
u.Identification = resp.Profile.Identification
u.MobileVerify = resp.Profile.TelStatus
u.Uname = resp.Profile.Name
u.URank = resp.Profile.Rank
return nil
}
//GetUserScore 用户真实分
func (u *UserScore) GetUserScore(ctx context.Context, uid int64) error {
req := &spyService.InfoReq{
Mid: uid,
}
resp, err := SpyClient.Info(ctx, req)
if err != nil {
log.Error("DM: 获取用户真实分错误 err: %v", err)
u.lock.Lock()
u.UserScore = 0
u.lock.Unlock()
return nil
}
u.lock.Lock()
u.UserScore = int64(resp.Ui.Score)
u.lock.Unlock()
return nil
}
//GetMsgScore 获取弹幕AI分
func (u *UserScore) GetMsgScore(ctx context.Context, msg string) error {
req := &filterService.FilterReq{
Area: "live_danmu",
Message: msg,
}
resp, err := FilterClient.Filter(ctx, req)
if err != nil {
log.Error("DM: main filter err: %v", err)
u.lock.Lock()
u.MsgLevel = 0
u.MsgAI = 0
u.lock.Unlock()
return nil
}
u.lock.Lock()
u.MsgLevel = int64(resp.Level)
if resp.Ai == nil {
log.Error("DM: main filter err: miss ai scores")
u.MsgAI = 0
u.lock.Unlock()
return nil
}
if len(resp.Ai.Scores) == 0 {
u.MsgAI = 0
u.lock.Unlock()
return nil
}
u.MsgAI = int64(resp.Ai.Scores[0] * 10)
u.lock.Unlock()
return nil
}

View File

@@ -0,0 +1,162 @@
package dao
import (
"context"
"flag"
"fmt"
"path/filepath"
"testing"
fansMedalService "go-common/app/service/live/fans_medal/api/liverpc"
"go-common/app/service/live/live-dm/conf"
liveUserService "go-common/app/service/live/live_user/api/liverpc"
roomService "go-common/app/service/live/room/api/liverpc"
userextService "go-common/app/service/live/userext/api/liverpc"
acctountService "go-common/app/service/main/account/api"
filterService "go-common/app/service/main/filter/api/grpc/v1"
spyService "go-common/app/service/main/spy/api"
)
func init() {
dir, _ := filepath.Abs("../cmd/test.toml")
flag.Set("conf", dir)
var err error
if err = conf.Init(); err != nil {
panic(err)
}
// InitAPI()
// InitGrpc(conf.Conf)
UserExtServiceClient = userextService.New(getConf("userext"))
LiveUserServiceClient = liveUserService.New(getConf("liveUser"))
FansMedalServiceClient = fansMedalService.New(getConf("fansMedal"))
RoomServiceClient = roomService.New(getConf("room"))
ac, err = acctountService.NewClient(conf.Conf.AccClient)
if err != nil {
panic(err)
}
vipCli, err = newVipService(conf.Conf.XuserClent)
if err != nil {
panic(err)
}
SpyClient, err = spyService.NewClient(conf.Conf.SpyClient)
if err != nil {
panic(err)
}
FilterClient, err = filterService.NewClient(conf.Conf.FilterClient)
if err != nil {
panic(err)
}
}
//group=qa01 DEPLOY_ENV=uat go test -run TestDMConf_Get
func TestDMConf_Get(t *testing.T) {
dc := &DMConf{}
if err := dc.Get(context.TODO(), 111, 222, conf.Conf); err != nil {
t.Error("获取弹幕配置失败", err)
}
if dc.Color == 0 && dc.Length == 0 && dc.Mode == 0 {
t.Error("获取弹幕配置失败, 返回值错误")
}
fmt.Println("##### Mode: ", dc.Mode)
fmt.Println("##### Color: ", dc.Color)
fmt.Println("##### Length: ", dc.Length)
}
//TODO 未测试
//group=fat1 DEPLOY_ENV=uat go test -run TestUserInfo_Get
func TestUserInfo_Get(t *testing.T) {
u := &UserInfo{}
if err := u.Get(context.TODO(), 110000232); err != nil {
t.Error(err)
}
if u.UserLever == 0 && u.UserScore == 0 {
t.Error("返回值错误")
}
fmt.Println("#### UserLever: ", u.UserLever)
fmt.Println("#### UserScore: ", u.UserScore)
fmt.Println("### Usercolor: ", u.ULevelColor)
}
//DEPLOY_ENV=uat go test -run TestUserInfo_GetVipInfo
func TestUserInfo_GetVipInfo(t *testing.T) {
u := &UserInfo{}
if err := u.GetVipInfo(context.TODO(), 2); err != nil {
t.Error("获取老爷失败: ", err)
}
fmt.Println("#### VIP: ", u.Vip)
fmt.Println("### SVIP: ", u.Svip)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetPrivilegeType
func TestUserInfo_GetPrivilegeType(t *testing.T) {
u := &UserInfo{}
if err := u.GetPrivilegeType(context.TODO(), 10799340, 6810576); err != nil {
t.Error("返回值错误: ", err)
}
fmt.Println("PrivilegeType", u.PrivilegeType)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_IsRoomAdmin
func TestUserInfo_IsRoomAdmin(t *testing.T) {
u := &UserInfo{}
if err := u.IsRoomAdmin(context.TODO(), 1877309, 5392); err != nil {
t.Error("返回值错误: ", err)
}
fmt.Println("IsRoomAdmin->", u.RoomAdmin)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetFansMedal
func TestUserInfo_GetFansMedal(t *testing.T) {
m := &FansMedalInfo{}
if err := m.GetFansMedal(context.TODO(), 83940); err != nil {
t.Error("获取粉丝勋章失败: ", err)
}
fmt.Println("#####RUID: ", m.RUID)
fmt.Println("#####MedalLevel: ", m.MedalLevel)
fmt.Println("#####MedalName: ", m.MedalName)
fmt.Println("#####MColor: ", m.MColor)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestRoomConf_Get
func TestRoomConf_Get(t *testing.T) {
r := &RoomConf{}
if err := r.Get(context.TODO(), 1016); err != nil {
t.Error("获取房间配置失败: ", err)
}
fmt.Println("RoomID->", r.RoomID)
fmt.Println("UID->", r.UID)
fmt.Println("RoomShield->", r.RoomShield)
fmt.Println("Anchor->", r.Anchor)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserBindInfo_Get
func TestUserBindInfo_Get(t *testing.T) {
u := &UserBindInfo{}
if err := u.Get(context.TODO(), 222); err != nil {
t.Error("获取用户绑定信息失败: ", err)
}
fmt.Println("Identification->", u.Identification)
fmt.Println("MobileVerify->", u.MobileVerify)
fmt.Println("Uname->", u.Uname)
fmt.Println("URank->", u.URank)
}
//DEPLOY_ENV=uat go test -run TestGerUserScore
func TestGerUserScore(t *testing.T) {
u := &UserScore{}
if err := u.GetUserScore(context.TODO(), 111); err != nil {
t.Error("获取用户真实分失败:", err)
}
fmt.Println("###### UserScore:", u.UserScore)
}
//缺少souce值
//DEPLOY_ENV=uat go test -run TestGetMsgScore
func TestGetMsgScore(t *testing.T) {
u := &UserScore{}
if err := u.GetMsgScore(context.TODO(), "fuck"); err != nil {
t.Error("获取真实分失败:", err)
}
fmt.Println("MsgLeve->", u.MsgLevel)
fmt.Println("MsgAI=>", u.MsgAI)
}

View File

@@ -0,0 +1,234 @@
package dao
import (
"context"
activityService "go-common/app/service/live/activity/api/liverpc/v1"
rankdbService "go-common/app/service/live/rankdb/api/liverpc/v1"
rcService "go-common/app/service/live/rc/api/liverpc/v1"
roomService "go-common/app/service/live/room/api/liverpc/v2"
userextService "go-common/app/service/live/userext/api/liverpc/v1"
acctountService "go-common/app/service/main/account/api"
"go-common/library/log"
"github.com/pkg/errors"
)
//CommentTitle 头衔信息
type CommentTitle struct {
OldTitle string `json:"oldtitle"`
Title string `json:"title"`
}
//GetUnameColor 获取用户的昵称颜色
func (u *UserInfo) GetUnameColor(ctx context.Context, uid int64, rid int64) error {
req := &userextService.ColorGetUnameColorReq{
Uid: uid,
RoomId: rid,
}
resq, err := UserExtServiceClient.V1Color.GetUnameColor(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: UserExt GetUnameColor err: %v", err)
}
u.lock.Lock()
u.UnameColor = ""
u.lock.Unlock()
return nil
}
if resq.Code != 0 {
log.Error("DM: UserExt GetUnameColor errror code: %d", resq.Code)
u.lock.Lock()
u.UnameColor = ""
u.lock.Unlock()
return nil
}
u.lock.Lock()
u.UnameColor = resq.Data.UnameColor
u.lock.Unlock()
return nil
}
//GetSpeicalMedal 获取特殊勋章
func (f *FansMedalInfo) GetSpeicalMedal(ctx context.Context, uid int64, rid int64) error {
//更新special
req := &activityService.UnionFansGetSpecialMedalReq{
Uid: uid,
Ruid: rid,
}
resq, err := ActivityServiceClient.V1UnionFans.GetSpecialMedal(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: GetSpecialMedal err: %v", err)
}
f.lock.Lock()
f.SpecialMedal = ""
f.lock.Unlock()
return nil
}
if resq.Code != 0 {
log.Error("DM: GetSpecialMedal err code: %d", resq.Code)
f.lock.Lock()
f.SpecialMedal = ""
f.lock.Unlock()
return nil
}
f.lock.Lock()
f.SpecialMedal = resq.Data.SpecialMedal
f.lock.Unlock()
return nil
}
//GetUserLevelRank 获取用户等级RANK
func (u *UserInfo) GetUserLevelRank(ctx context.Context, uid int64) error {
defer u.lock.Unlock()
req := &rankdbService.UserRankGetUserRankReq{
Uid: uid,
Type: "user_level",
}
resq, err := RankdbServiceClient.V1UserRank.GetUserRank(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: GetUserRank err: %v", err)
}
u.lock.Lock()
u.ULevelRank = 1000000
return nil
}
if resq.Code != 0 {
log.Error("DM: GetUserRank error code: %d", resq.Code)
u.lock.Lock()
u.ULevelRank = 1000000
return nil
}
u.lock.Lock()
u.ULevelRank = resq.Data.Rank
return nil
}
//GetCommentTitle 获取头衔
func (c *CommentTitle) GetCommentTitle(ctx context.Context) error {
req := &rcService.UserTitleGetCommentTitleReq{}
resq, err := RcServiceClient.V1UserTitle.GetCommentTitle(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: GetCommentTitle err: %v", err)
}
c.OldTitle = ""
c.Title = ""
return nil
}
if resq.Code != 0 {
log.Error("DM: GetCommentTitle error code: %d", resq.Code)
c.OldTitle = ""
c.Title = ""
return nil
}
if len(resq.Data) == 0 {
c.OldTitle = ""
c.Title = ""
return nil
}
c.OldTitle = resq.Data[0]
c.Title = resq.Data[1]
return nil
}
//GetMedalanchorName 获取勋章对应主播的昵称
func (f *FansMedalInfo) GetMedalanchorName(ctx context.Context, uid int64) error {
if uid == 0 {
f.RUName = ""
return nil
}
req := &acctountService.MidReq{
Mid: uid,
}
resp, err := ac.Profile3(ctx, req)
if err != nil {
log.Error("DM: acctountService Profile3 err: %v", err)
f.lock.Lock()
f.RUName = ""
f.lock.Unlock()
return nil
}
f.lock.Lock()
f.RUName = resp.Profile.Name
f.lock.Unlock()
return nil
}
//GetMedalRoomid 获取勋章对应主播的房间
func (f *FansMedalInfo) GetMedalRoomid(ctx context.Context, uid int64) error {
if uid == 0 {
f.lock.Lock()
f.RoomID = 0
f.lock.Unlock()
return nil
}
req := &roomService.RoomRoomIdByUidReq{
Uid: uid,
}
resp, err := RoomServiceClient.V2Room.RoomIdByUid(ctx, req)
if err != nil {
log.Error("DM: room RoomIdByUid err: %v", err)
f.lock.Lock()
f.RoomID = 0
f.lock.Unlock()
return nil
}
if resp.Code == 404 {
f.lock.Lock()
f.RoomID = 0
f.lock.Unlock()
return nil
}
if resp.Code != 0 {
log.Error("DM: room RoomIdByUid err code: %d", resp.Code)
f.lock.Lock()
f.RoomID = 0
f.lock.Unlock()
return nil
}
f.lock.Lock()
f.RoomID = resp.Data.RoomId
f.lock.Unlock()
return nil
}
//GetUserBubble 判断用户是否有气泡
func (u *UserInfo) GetUserBubble(ctx context.Context, uid int64, roomid int64, bubble int64, guardLevel int) error {
req := &userextService.BubbleGetBubbleReq{
Uid: uid,
RoomId: roomid,
BubbleId: bubble,
GuardLevel: int64(guardLevel),
}
defer u.lock.Unlock()
resp, err := UserExtServiceClient.V1Bubble.GetBubble(ctx, req)
if err != nil {
if errors.Cause(err) != context.Canceled {
log.Error("DM: userext Bubble check err: %v", err)
}
u.lock.Lock()
u.Bubble = bubble
return nil
}
if resp.Code != 0 {
log.Error("DM: userext Bubble check err code: %d", resp.Code)
u.lock.Lock()
u.Bubble = bubble
return nil
}
if resp.Data == nil {
log.Error("DM: userext Bubble check err not data")
u.lock.Lock()
u.Bubble = bubble
return nil
}
u.lock.Lock()
u.Bubble = resp.Data.Bubble
return nil
}

View File

@@ -0,0 +1,153 @@
package dao
import (
"context"
"flag"
"fmt"
"path/filepath"
"testing"
activityService "go-common/app/service/live/activity/api/liverpc"
"go-common/app/service/live/live-dm/conf"
rankdbService "go-common/app/service/live/rankdb/api/liverpc"
rcService "go-common/app/service/live/rc/api/liverpc"
userextService "go-common/app/service/live/userext/api/liverpc"
acctountService "go-common/app/service/main/account/api"
"go-common/library/net/metadata"
)
func init() {
dir, _ := filepath.Abs("../cmd/test.toml")
flag.Set("conf", dir)
var err error
if err = conf.Init(); err != nil {
panic(err)
}
UserExtServiceClient = userextService.New(getConf("userext"))
ActivityServiceClient = activityService.New(getConf("activity"))
RankdbServiceClient = rankdbService.New(getConf("rankdbService"))
RcServiceClient = rcService.New(getConf("rc"))
ac, err = acctountService.NewClient(conf.Conf.AccClient)
if err != nil {
panic(err)
}
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetUnameColor
func TestUserInfo_GetUnameColor(t *testing.T) {
u := &UserInfo{}
if err := u.GetUnameColor(context.TODO(), 28272030, 10004); err != nil {
t.Error("获取用户昵称颜色失败: ", err)
}
fmt.Println("UnameColor->", u.UnameColor)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetSpeicalMedal
func TestUserInfo_GetSpeicalMedal(t *testing.T) {
m := &FansMedalInfo{}
if err := m.GetSpeicalMedal(context.TODO(), 111, 222); err != nil {
t.Error("获取特殊勋章信息失败:", err)
}
fmt.Println("SpecialMedal->", m.SpecialMedal)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetUserLevelRank
func TestUserInfo_GetUserLevelRank(t *testing.T) {
u := &UserInfo{}
if err := u.GetUserLevelRank(context.TODO(), 111); err != nil {
t.Error("获取用户等级RANK失败:", err)
}
fmt.Println("ULevelRank->", u.ULevelRank)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestCommentTitle_GetCommentTitle
func TestCommentTitle_GetCommentTitle(t *testing.T) {
c := &CommentTitle{}
ctx1 := metadata.NewContext(context.TODO(), metadata.MD{})
if md, ok := metadata.FromContext(ctx1); ok {
md[metadata.Mid] = 5200
}
if err := c.GetCommentTitle(ctx1); err != nil {
t.Error("获取用户头衔失败:", err)
}
fmt.Println("OldTitle->", c.OldTitle)
fmt.Println("Title->", c.Title)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestFansMedalInfo_GetMedalanchorName
func TestFansMedalInfo_GetMedalanchorName(t *testing.T) {
f := &FansMedalInfo{}
if err := f.GetMedalanchorName(context.TODO(), 222); err != nil {
t.Error("获取勋章对应主播昵称错误:", err)
}
fmt.Println("RUName->", f.RUName)
}
//group=fat1 DEPLOY_ENV=uat go test -run TestUserInof_GetUserBubble
func TestUserInof_GetUserBubble(t *testing.T) {
u := &UserInfo{}
if err := u.GetUserBubble(context.TODO(), 1, 1, 1, 1); err != nil {
t.Error("GetUserBubble调用失败")
}
if u.Bubble != 1 {
t.Error("判断气泡失败 uid 1 roomid 1 bubble 1: bubble: ", u.Bubble)
}
fmt.Println("Bubble1->", u.Bubble)
if err := u.GetUserBubble(context.TODO(), 1, 2, 1, 1); err != nil {
t.Error("GetUserBubble调用失败")
}
if u.Bubble != 0 {
t.Error("判断气泡失败 uid 1 roomid 2 bubble 1: bubble: ", u.Bubble)
}
fmt.Println("Bubble2->", u.Bubble)
}
// //group=qa01 DEPLOY_ENV=uat go test -run TestUserInfo_GetUserLevelColor
// func TestUserInfo_GetUserLevelColor(t *testing.T) {
// u := &UserInfo{}
// if err := u.GetUserLevelColor(52); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 16752445 {
// t.Error("51级以上颜色错误 16752445 ->", u.ULevelColor)
// }
// if err := u.GetUserLevelColor(42); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 16746162 {
// t.Error("51-41级颜色错误 16752445 ->", u.ULevelColor)
// }
// if err := u.GetUserLevelColor(32); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 10512625 {
// t.Error("41-31级颜色错误 10512625 ->", u.ULevelColor)
// }
// if err := u.GetUserLevelColor(22); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 5805790 {
// t.Error("31-21级颜色错误 16752445 ->", u.ULevelColor)
// }
// if err := u.GetUserLevelColor(12); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 6406234 {
// t.Error("21-11级颜色错误 16752445 ->", u.ULevelColor)
// }
// if err := u.GetUserLevelColor(2); err != nil {
// t.Error("返回值错误: ", err)
// }
// if u.ULevelColor != 9868950 {
// t.Error("0-11级颜色错误 16752445 ->", u.ULevelColor)
// }
// }

View File

@@ -0,0 +1,51 @@
package dao
import (
"context"
"fmt"
"go-common/library/cache/redis"
"go-common/library/log"
)
//GetHistoryData 获取历史数据
func (d *Dao) GetHistoryData(ctx context.Context, roomid int64) (result map[string][]string, err error) {
var conn = d.redis.Get(ctx)
defer conn.Close()
var admkey = fmt.Sprintf("%s%d", _adminMsgHistoryCache, roomid)
var userkey = fmt.Sprintf("%s%d", _msgHistoryCache, roomid)
if err = conn.Send("LRANGE", admkey, 0, 9); err != nil {
log.Error("DM: LRANGE ADMIN KEY ROOMID %d ERR:%v", roomid, err)
}
if err = conn.Send("LRANGE", userkey, 0, 9); err != nil {
log.Error("DM: LRANGE USER KEY ROOMID %d ERR:%v", roomid, err)
}
if err = conn.Flush(); err != nil {
log.Error("DM: Flush KEY ROOMID %d ERR:%v", roomid, err)
return nil, err
}
var admin, user [][]byte
admin, err = redis.ByteSlices(conn.Receive())
if err != nil {
log.Error("DM: ByteSlices ADMIN KEY ROOMID %d ERR:%v", roomid, err)
return nil, err
}
user, err = redis.ByteSlices(conn.Receive())
if err != nil {
log.Error("DM: ByteSlices USER KEY ROOMID %d ERR:%v", roomid, err)
return nil, err
}
result = make(map[string][]string)
result["admin"] = make([]string, 0, 10)
result["room"] = make([]string, 0, 10)
for i := len(admin) - 1; i >= 0; i-- {
result["admin"] = append(result["admin"], string(admin[i]))
}
for i := len(user) - 1; i >= 0; i-- {
result["room"] = append(result["room"], string(user[i]))
}
return result, nil
}

View File

@@ -0,0 +1,93 @@
package dao
import (
"context"
broadcasrtService "go-common/app/service/live/broadcast-proxy/api/v1"
"go-common/app/service/live/live-dm/conf"
xuserService "go-common/app/service/live/xuser/api/grpc/v1"
acctountService "go-common/app/service/main/account/api"
filterService "go-common/app/service/main/filter/api/grpc/v1"
locationService "go-common/app/service/main/location/api"
spyService "go-common/app/service/main/spy/api"
"go-common/library/net/rpc/warden"
"google.golang.org/grpc"
)
//LocationAppID location服务注册ID
const locationAppID = "location.service"
const vipAppID = "live.xuser"
var (
ac acctountService.AccountClient
vipCli xuserService.VipClient
//FilterClient 屏蔽词过滤
FilterClient filterService.FilterClient
//LcClient 地理区域信息
LcClient locationService.LocationClient
//SpyClient 用户真实分
SpyClient spyService.SpyClient
//BcastClient 弹幕推送
BcastClient *broadcasrtService.Client
//UserExp 用户等级
userExp *xuserService.Client
//isAdmin 房管
isAdmin xuserService.RoomAdminClient
)
//InitGrpc 初始化grpcclient
func InitGrpc(c *conf.Config) {
var err error
ac, err = acctountService.NewClient(c.AccClient)
if err != nil {
panic(err)
}
FilterClient, err = filterService.NewClient(c.FilterClient)
if err != nil {
panic(err)
}
LcClient, err = newLocationClient(c.LocationClient)
if err != nil {
panic(err)
}
vipCli, err = newVipService(c.XuserClent)
if err != nil {
panic(err)
}
SpyClient, err = spyService.NewClient(c.SpyClient)
if err != nil {
panic(err)
}
BcastClient, err = broadcasrtService.NewClient(c.BcastClient)
if err != nil {
panic(err)
}
userExp, err = xuserService.NewClient(c.UExpClient)
if err != nil {
panic(err)
}
isAdmin, err = xuserService.NewXuserRoomAdminClient(c.IsAdminClient)
if err != nil {
panic(err)
}
}
//创建Location服务client
func newLocationClient(cfg *warden.ClientConfig, opts ...grpc.DialOption) (locationService.LocationClient, error) {
client := warden.NewClient(cfg, opts...)
conn, err := client.Dial(context.Background(), "discovery://default/"+locationAppID)
if err != nil {
return nil, err
}
return locationService.NewLocationClient(conn), nil
}
func newVipService(cfg *warden.ClientConfig, opts ...grpc.DialOption) (xuserService.VipClient, error) {
client := warden.NewClient(cfg, opts...)
conn, err := client.Dial(context.Background(), "discovery://default/"+vipAppID)
if err != nil {
return nil, err
}
return xuserService.NewVipClient(conn), nil
}

View File

@@ -0,0 +1,30 @@
package dao
import (
"github.com/ipipdotnet/ipdb-go"
)
const (
ip4dbaddr = "/data/conf/v4.ipdb"
ip6dbaddr = "/data/conf/v6.ipdb"
)
var (
//IP4db ip4地址库
IP4db *ipdb.City
//IP6db ip6地址库
IP6db *ipdb.City
)
//InitIPdb 初始化IPdb
func InitIPdb() {
var err error
IP4db, err = ipdb.NewCity(ip4dbaddr)
if err != nil {
panic(err)
}
IP6db, err = ipdb.NewCity(ip6dbaddr)
if err != nil {
panic(err)
}
}

View File

@@ -0,0 +1,65 @@
package dao
import (
activityService "go-common/app/service/live/activity/api/liverpc"
avService "go-common/app/service/live/av/api/liverpc"
bannedService "go-common/app/service/live/banned_service/api/liverpc"
fansMedalService "go-common/app/service/live/fans_medal/api/liverpc"
"go-common/app/service/live/live-dm/conf"
liveUserService "go-common/app/service/live/live_user/api/liverpc"
rankdbService "go-common/app/service/live/rankdb/api/liverpc"
rcService "go-common/app/service/live/rc/api/liverpc"
roomService "go-common/app/service/live/room/api/liverpc"
liveBroadCast "go-common/app/service/live/third_api/liveBroadcast"
userService "go-common/app/service/live/user/api/liverpc"
userextService "go-common/app/service/live/userext/api/liverpc"
"go-common/library/net/rpc/liverpc"
)
var (
// BannedServiceClient liveRpc banner_service api
BannedServiceClient *bannedService.Client
// RoomServiceClient liveRpc room service api
RoomServiceClient *roomService.Client
// LiveUserServiceClient liveRpc liveUser service api
LiveUserServiceClient *liveUserService.Client
// AvServiceClient liveRpc av service api
AvServiceClient *avService.Client
//FansMedalServiceClient liverpc fansmedal service api
FansMedalServiceClient *fansMedalService.Client
//ActivityServiceClient liverpc activity service api
ActivityServiceClient *activityService.Client
//RcServiceClient liverpc rc service api
RcServiceClient *rcService.Client
//RankdbServiceClient liverpc rankdb service api
RankdbServiceClient *rankdbService.Client
//UserExtServiceClient liverpc userext service api
UserExtServiceClient *userextService.Client
//LiveBroadCastClient liverpc thirdApi
LiveBroadCastClient *liveBroadCast.Client
//UserClient liveRpc user api
userClient *userService.Client
)
//InitAPI init all service APIS
func InitAPI() {
BannedServiceClient = bannedService.New(getConf("banneDService"))
RoomServiceClient = roomService.New(getConf("room"))
LiveUserServiceClient = liveUserService.New(getConf("liveUser"))
AvServiceClient = avService.New(getConf("avService"))
FansMedalServiceClient = fansMedalService.New(getConf("fansMedal"))
ActivityServiceClient = activityService.New(getConf("activity"))
RcServiceClient = rcService.New(getConf("rc"))
RankdbServiceClient = rankdbService.New(getConf("rankdbService"))
UserExtServiceClient = userextService.New(getConf("userext"))
LiveBroadCastClient = liveBroadCast.New(conf.Conf.HTTPClient)
userClient = userService.New(getConf("user"))
}
func getConf(appName string) *liverpc.ClientConfig {
c := conf.Conf.LiveRPC
if c != nil {
return c[appName]
}
return nil
}

View File

@@ -0,0 +1,111 @@
package dao
import (
"context"
"crypto/md5"
"fmt"
"strconv"
"time"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
)
// LimitCheckInfo 频率限制检查参数
type LimitCheckInfo struct {
UID int64
RoomID int64
Msg string
MsgType int64
Dao *Dao
Conf *LimitConf
}
const (
_MaxGiftMsgNum = "r:m:stormmsgcount:"
_MAXMsgNum = "r:m:rmx:"
)
// LimitPerSec 每秒发言限制
func (l *LimitCheckInfo) LimitPerSec(ctx context.Context) error {
key := fmt.Sprintf("%d.%d", l.UID, time.Now().Unix())
var conn = l.Dao.redis.Get(ctx)
defer conn.Close()
ret, err := conn.Do("SET", key, 1, "EX", 1, "NX")
if err != nil {
log.Error("limitPerSec:conn.Do(SET EX NX) %s %d %v", key, 1, err)
return nil
}
if ret != nil {
return nil
}
return ecode.Error(0, "msg in 1s")
}
//LimitSameMsg 同一个用户同一房间5s 只能发送一条相同弹幕
func (l *LimitCheckInfo) LimitSameMsg(ctx context.Context) error {
key := fmt.Sprintf("%d.%s", l.RoomID, md5.Sum([]byte(strconv.FormatInt(l.UID, 10)+l.Msg)))
var conn = l.Dao.redis.Get(ctx)
defer conn.Close()
ret, err := conn.Do("SET", key, 1, "EX", 5, "NX")
if err != nil {
log.Error("DM LimitSameMsg conn.Do(SET, %s, 1, EX, 5, NX) error(%v)", key, err)
return nil
}
if ret != nil {
return nil
}
return ecode.Error(0, "msg repeat")
}
//LimitRoomPerSecond 单房间每秒只能发送制定条数弹幕
func (l *LimitCheckInfo) LimitRoomPerSecond(ctx context.Context) error {
maxNum := l.Conf.DmNum
percent := l.Conf.DMPercent
danNum := maxNum * percent / 100.0
giftNum := maxNum - danNum
msgKey := fmt.Sprintf("%s.%d.%d", _MAXMsgNum, l.RoomID, time.Now().Unix())
giftKey := fmt.Sprintf("%s.%d.%d", _MaxGiftMsgNum, l.RoomID, time.Now().Unix())
var conn = l.Dao.redis.Get(ctx)
defer conn.Close()
if l.MsgType != 0 {
//礼物弹幕
if count, err := redis.Int64(conn.Do("INCRBY", giftKey, 1)); err != nil {
log.Error("DMRateLimit: LimitRoomPerSecond INCRBY err: %v", err)
} else {
if count > giftNum {
return ecode.Error(0, "max limit")
}
}
if _, err := conn.Do("EXPIRE", giftKey, 2); err != nil {
log.Error("DMRateLimit: LimitRoomPerSecond EXPIRE err: %v", err)
}
return nil
}
//普通弹幕
var max = maxNum
if exit, err := redis.Bool(conn.Do("EXISTS", giftKey)); err != nil {
log.Error("DMRateLimit: LimitRoomPerSecond EXISTS gift err: %v", err)
} else {
if exit {
max = danNum
} else {
max = maxNum
}
}
if count, err := redis.Int64(conn.Do("INCRBY", msgKey, 1)); err != nil {
log.Error("DMRateLimit: LimitRoomPerSecond INCR err: %v", err)
} else {
if count > max {
return ecode.Error(0, "max limit")
}
}
if _, err := conn.Do("EXPIRE", msgKey, 2); err != nil {
log.Error("DMRateLimit: LimitRoomPerSecond EXPIRE err: %v", err)
}
return nil
}

View File

@@ -0,0 +1,74 @@
package dao
import (
"context"
"flag"
"go-common/app/service/live/live-dm/conf"
"path/filepath"
"testing"
)
func init() {
dir, _ := filepath.Abs("../cmd/test.toml")
flag.Set("conf", dir)
var err error
if err = conf.Init(); err != nil {
panic(err)
}
InitAPI()
InitGrpc(conf.Conf)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestLimitPerSec
func TestLimitPerSec(t *testing.T) {
l := LimitCheckInfo{
UID: 111,
RoomID: 222,
Msg: "6666",
Dao: New(conf.Conf),
MsgType: 0,
Conf: &LimitConf{
DmNum: 20,
DMPercent: 25,
},
}
if err := l.LimitPerSec(context.TODO()); err != nil {
t.Error("每秒限制错误:", err)
}
}
//group=qa01 DEPLOY_ENV=uat go test -run TestLimitSameMsg
func TestLimitSameMsg(t *testing.T) {
l := LimitCheckInfo{
UID: 111,
RoomID: 222,
Msg: "6666",
Dao: New(conf.Conf),
MsgType: 0,
Conf: &LimitConf{
DmNum: 20,
DMPercent: 25,
},
}
if err := l.LimitSameMsg(context.TODO()); err != nil {
t.Error("5秒相同发言错误:", err)
}
}
//group=qa01 DEPLOY_ENV=uat go test -run TestLimitRoomPerSecond
func TestLimitRoomPerSecond(t *testing.T) {
l := LimitCheckInfo{
UID: 111,
RoomID: 222,
Msg: "6666",
Dao: New(conf.Conf),
MsgType: 0,
Conf: &LimitConf{
DmNum: 20,
DMPercent: 25,
},
}
if err := l.LimitRoomPerSecond(context.TODO()); err != nil {
t.Error("每秒20弹幕错误:", err)
}
}

View File

@@ -0,0 +1,144 @@
package dao
import (
"context"
"fmt"
"strconv"
broadcasrtService "go-common/app/service/live/broadcast-proxy/api/v1"
"go-common/app/service/live/live-dm/model"
roomService "go-common/app/service/live/room/api/liverpc/v1"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_adminMsgHistoryCache = "cache:last10_roomadminmsg:"
_msgHistoryCache = "cache:last10_roommsg:"
)
//SaveHistory 弹幕历史存入redis
func (d *Dao) SaveHistory(ctx context.Context, hm string, adm bool, rid int64) {
var conn = d.redis.Get(ctx)
defer conn.Close()
if adm {
admKey := fmt.Sprintf("%s%d", _adminMsgHistoryCache, rid)
if err := conn.Send("LPUSH", admKey, hm); err != nil {
log.Error("DM: SaveHistory LPUSH err: %v", err)
}
if err := conn.Send("LLEN", admKey); err != nil {
log.Error("DM: SaveHistory LLEN err: %v", err)
return
}
if err := conn.Flush(); err != nil {
log.Error("DM: SaveHistory Flush err: %v", err)
return
}
if _, err := conn.Receive(); err != nil {
log.Error("DM: SaveHistory LPUSH err: %v", err)
}
count, err := redis.Int64(conn.Receive())
if err != nil {
log.Error("DM: SaveHistory LPUSH LLEN err: %v", err)
return
}
if count > 15 {
err := conn.Send("LTRIM", admKey, 0, 9)
if err != nil {
log.Error("DM: SaveHistory LTRIM err: %v", err)
}
}
if err := conn.Send("EXPIRE", admKey, 86400); err != nil {
log.Error("DM: SaveHistory EXPIRE err: %v", err)
}
if err := conn.Flush(); err != nil {
log.Error("DM: SaveHistory Flush err: %v", err)
}
}
userKey := fmt.Sprintf("%s%d", _msgHistoryCache, rid)
if err := conn.Send("LPUSH", userKey, hm); err != nil {
log.Error("DM: SaveHistory LPUSH err: %v", err)
}
if err := conn.Send("LLEN", userKey); err != nil {
log.Error("DM: SaveHistory LLEN err: %v", err)
return
}
if err := conn.Flush(); err != nil {
log.Error("DM: SaveHistory Flush err: %v", err)
return
}
if _, err := conn.Receive(); err != nil {
log.Error("DM: SaveHistory Receive LPUSH err: %v", err)
}
count, err := redis.Int64(conn.Receive())
if err != nil {
log.Error("DM: SaveHistory Int64 err: %v", err)
return
}
if count > 15 {
if err := conn.Send("LTRIM", userKey, 0, 9); err != nil {
log.Error("DM: SaveHistory LTRIM err: %v", err)
}
}
if err := conn.Send("EXPIRE", userKey, 86400); err != nil {
log.Error("DM: SaveHistory EXPIRE err: %v", err)
}
if err := conn.Flush(); err != nil {
log.Error("DM: SaveHistory Flush err: %v", err)
}
}
//IncrDMNum 弹幕条数
func IncrDMNum(ctx context.Context, rid int64, mode int64) {
req := &roomService.RoomIncrDanmuSendNumReq{
RoomId: rid,
Mode: mode,
}
resp, err := RoomServiceClient.V1Room.IncrDanmuSendNum(ctx, req)
if err != nil {
log.Error("DM: IncrDMNum err: %v", err)
return
}
if resp.Code != 0 {
log.Error("DM: IncrDMNum err code: %d", resp.Code)
return
}
}
//SendBroadCast 发送弹幕(http)
func SendBroadCast(ctx context.Context, sm string, rid int64) error {
err := LiveBroadCastClient.PushBroadcast(ctx, rid, 0, sm)
if err != nil {
log.Error("DM: SendBroadCast err: %v", err)
return err
}
return nil
}
//SendBroadCastGrpc 调用GRPC发送弹幕
func SendBroadCastGrpc(ctx context.Context, sm string, rid int64) error {
req := &broadcasrtService.RoomMessageRequest{
RoomId: int32(rid),
Message: sm,
}
_, err := BcastClient.DanmakuClient.RoomMessage(ctx, req)
if err != nil {
log.Error("DM: SendBroadCastGrpc err: %v", err)
return err
}
return nil
}
//SendBNDatabus 拜年祭制定房间投递到databus
func SendBNDatabus(ctx context.Context, uid int64, info *model.BNDatabus) {
uids := strconv.FormatInt(uid, 10)
if err := bndatabus.Send(ctx, uids, info); err != nil {
log.Error("[service.live-dm.v1.bndatabus] send error(%v), record(%v)", err, info)
}
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"flag"
"go-common/app/service/live/live-dm/conf"
"path/filepath"
"testing"
)
func init() {
dir, _ := filepath.Abs("../cmd/test.toml")
flag.Set("conf", dir)
var err error
if err = conf.Init(); err != nil {
panic(err)
}
InitAPI()
InitGrpc(conf.Conf)
}
//group=qa01 DEPLOY_ENV=uat go test -run TestIncrDMNum
func TestIncrDMNum(t *testing.T) {
IncrDMNum(context.TODO(), 5392, 2)
}

View File

@@ -0,0 +1,22 @@
package dao
import (
"context"
"go-common/library/cache/redis"
"go-common/library/log"
)
//IsWhietListUID 通过UID判读是否是白名单用户
func (d *Dao) IsWhietListUID(ctx context.Context, key string) (isWhite bool) {
conn := d.whitelistredis.Get(ctx)
defer conn.Close()
var err error
isWhite, err = redis.Bool(conn.Do("EXISTS", key))
if err != nil {
log.Error("[DM] GetWhietListByUID redis err:%+v", err)
return true
}
return
}

View File

@@ -0,0 +1,29 @@
package dao
import (
"context"
"flag"
"go-common/app/service/live/live-dm/conf"
"path/filepath"
"testing"
)
func init() {
dir, _ := filepath.Abs("../cmd/test.toml")
flag.Set("conf", dir)
var err error
if err = conf.Init(); err != nil {
panic(err)
}
}
// DEPLOY_ENV=uat go test -run TestIsWhietListUID
func TestIsWhietListUID(t *testing.T) {
dao := New(conf.Conf)
if isWhite := dao.IsWhietListUID(context.TODO(), "1111"); isWhite {
t.Error("白名单判断失败")
}
if isWhite := dao.IsWhietListUID(context.TODO(), "13269933"); !isWhite {
t.Error("白名单判断失败")
}
}