fix admin support for openblive protocol

This commit is contained in:
aynakeya
2024-08-07 20:58:56 +08:00
parent dc1612e5c8
commit b707853ba8
4 changed files with 60 additions and 2 deletions

6
go.mod
View File

@@ -7,16 +7,20 @@ require (
github.com/aynakeya/open-bilibili-live v0.0.7
github.com/go-resty/resty/v2 v2.7.0
github.com/spf13/cast v1.5.1
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.16.0
)
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
golang.org/x/sys v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@@ -0,0 +1,29 @@
package openblive
import (
"fmt"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
)
const getAdminApi = "https://api.live.bilibili.com/xlive/web-room/v1/roomAdmin/get_by_room?roomid=%d&page_size=99"
func getAdmins(roomId int) (adminNames []string) {
uri := fmt.Sprintf(getAdminApi, roomId)
val, err := resty.New().R().Get(uri)
// test only, add me to admin
adminNames = append(adminNames, "Aynakeya")
if err != nil {
return adminNames
}
valStr := val.String()
if !gjson.Valid(valStr) {
return adminNames
}
result := gjson.Parse(valStr)
result.Get("data.data").ForEach(func(key, value gjson.Result) bool {
adminNames = append(adminNames, value.Get("uname").String())
return true
})
return adminNames
}

View File

@@ -0,0 +1,16 @@
package openblive
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestGetAdminNames(t *testing.T) {
result := getAdmins(3819533)
require.Equal(t, 4, len(result))
}
func TestGetAdminNamesFails(t *testing.T) {
result := getAdmins(3819531231231233)
require.Equal(t, 1, len(result))
}

View File

@@ -13,6 +13,7 @@ const ProviderName = "openblive"
type OpenBLiveClient struct {
cfg liveroom.LiveRoom
admins map[string]int
openbliveClient *openblive.BLiveClient
conn openblive.BLiveLongConnection
onMessage func(msg *liveroom.Message)
@@ -45,11 +46,13 @@ func (o *OpenBLiveClient) danmuHandler(data openblive.DanmakuData) {
if data.FansMedalName == "" {
roomId = ""
}
_, isAdmin := o.admins[data.UName]
isAdmin = false
msg(&liveroom.Message{
User: liveroom.User{
Uid: data.OpenID,
Username: data.UName,
Admin: false, // not supported by open bilibili live
Admin: isAdmin, // not supported by open bilibili live
Privilege: utils.BilibiliGuardLevelToPrivilege(data.GuardLevel),
Medal: liveroom.UserMedal{
Name: data.FansMedalName,
@@ -87,6 +90,12 @@ func (o *OpenBLiveClient) Connect() error {
if err != nil {
return err
}
// get admin list
adminNames := getAdmins(o.openbliveClient.AppInfo.AnchorInfo.RoomID)
o.admins = make(map[string]int)
for _, name := range adminNames {
o.admins[name] = 1
}
o.conn = o.openbliveClient.GetLongConn()
o.conn.OnDanmu(o.danmuHandler)
o.conn.OnDisconnect(o.disconnectHandler)