Skip to content

Commit

Permalink
(backend): refactor short roomid
Browse files Browse the repository at this point in the history
  • Loading branch information
Grvzard committed Sep 3, 2024
1 parent 3e63df5 commit 18c3fc8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 49 deletions.
1 change: 0 additions & 1 deletion backend/crud/bl-roomid-short.json

This file was deleted.

12 changes: 0 additions & 12 deletions backend/crud/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package crud
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"os"

"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -17,12 +15,6 @@ var Client *mongo.Client
var Coll *mongo.Collection
var Sqldb *gorm.DB

//go:embed bl-roomid-short.json
var shortRoomidJson []byte

// []uint32 actually
var ShortRoomid []int64

func InitDatabase() {
uri := os.Getenv("API_MONGO_URI")
if uri == "" {
Expand All @@ -40,10 +32,6 @@ func InitDatabase() {
if err != nil {
panic(err)
}

if err := json.Unmarshal(shortRoomidJson, &ShortRoomid); err != nil {
panic(fmt.Sprintf("Failed to parse embedded JSON file: %v", err))
}
}

func DestoryDatabase() {
Expand Down
57 changes: 21 additions & 36 deletions backend/crud/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
)

type StreamerInfoDb struct {
Uid int64
AreaName string `bson:"area_name"`
ParentName string `bson:"parent_name"`
Roomid int64
Uname string
Id primitive.ObjectID `bson:"_id"`
Uid int64
AreaName string `bson:"area_name"`
ParentName string `bson:"parent_name"`
Roomid int64
ShortRoomid int64 `bson:"shortid"`
Uname string
Id primitive.ObjectID `bson:"_id"`
}

type StreamerInfo struct {
Expand Down Expand Up @@ -51,44 +52,28 @@ func streamerByFilter(filter interface{}) []StreamerInfo {
AreaName: e.AreaName,
ParentName: e.ParentName,
Roomid: e.Roomid,
ShortRoomid: 0,
ShortRoomid: e.ShortRoomid,
Uname: e.Uname,
FirstLogTs: uint32(e.Id.Timestamp().Unix()),
})
}
return results
}

func StreamerByUid(uid int64) []StreamerInfo {
return streamerByFilter(bson.M{"uid": uid})
}
// func StreamerByUid(uid int64) []StreamerInfo {
// return streamerByFilter(bson.M{"uid": uid})
// }

func StreamerByRoomid(roomid int64) []StreamerInfo {
return streamerByFilter(bson.M{"roomid": roomid})
}
// func StreamerByRoomid(roomid int64) []StreamerInfo {
// return streamerByFilter(bson.M{"roomid": roomid})
// }

func StreamerBy(id int64) []StreamerInfo {
var results []StreamerInfo
if id <= 1000 {
results = StreamerByUid(id)
origin_roomid := ShortRoomid[id]
// 0 means none, 1 ~ 1000 are reserved
if origin_roomid > 1000 {
if tmp := StreamerByRoomid(origin_roomid); tmp != nil {
// should be only 1 element
for _, e := range tmp {
e.ShortRoomid = id
results = append(results, e)
}
}
}
} else {
results = streamerByFilter(bson.M{
"$or": []bson.M{
{"uid": id},
{"roomid": id},
},
})
}
return results
return streamerByFilter(bson.M{
"$or": []bson.M{
{"uid": id},
{"roomid": id},
{"shortid": id},
},
})
}

0 comments on commit 18c3fc8

Please sign in to comment.