Opt: update db model

pull/21/head
zijiren233 2 years ago
parent 12ef9a97c0
commit a749263f62

@ -4,24 +4,24 @@ import "gorm.io/gorm"
type Movie struct {
gorm.Model
Position uint `gorm:"not null"`
RoomID uint `gorm:"not null;index"`
Room Room `gorm:"foreignKey:RoomID"`
MovieInfo `gorm:"embedded"`
Position uint `gorm:"not null"`
RoomID uint `gorm:"not null;index"`
Room Room `gorm:"foreignKey:RoomID"`
MovieInfo
}
type MovieInfo struct {
BaseMovieInfo `gorm:"embedded"`
PullKey string `gorm:"varchar(128)" json:"pullKey"`
CreatorID uint `gorm:"not null;index" json:"creatorId"`
BaseMovieInfo
PullKey string `json:"pullKey"`
CreatorID uint `gorm:"not null;index" json:"creatorId"`
}
type BaseMovieInfo struct {
Url string `gorm:"varchar(4096)" json:"url"`
Name string `gorm:"not null;varchar(256)" json:"name"`
Url string `json:"url"`
Name string `gorm:"not null" json:"name"`
Live bool `json:"live"`
Proxy bool `json:"proxy"`
RtmpSource bool `json:"rtmpSource"`
Type string `gorm:"varchar(32)" json:"type"`
Type string `json:"type"`
Headers map[string]string `gorm:"serializer:fastjson" json:"headers"`
}

@ -8,7 +8,7 @@ import (
type Room struct {
gorm.Model
Name string `gorm:"not null;uniqueIndex;varchar(32)"`
Name string `gorm:"not null;uniqueIndex"`
Setting
CreatorID uint `gorm:"index"`
Creator User `gorm:"foreignKey:CreatorID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

@ -8,7 +8,7 @@ import (
type User struct {
gorm.Model
Username string `gorm:"not null;uniqueIndex;varchar(32)"`
Username string `gorm:"not null;uniqueIndex"`
HashedPassword []byte
GroupUserRelations []RoomUserRelation `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Movies []Movie `gorm:"foreignKey:CreatorID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

@ -27,6 +27,7 @@ import (
type Room struct {
model.Room
uuid uuid.UUID
version uint32
current *current
rtmpa *rtmps.App
@ -52,8 +53,8 @@ func (r *Room) LazyInit() (err error) {
}
for _, m := range ms {
if err = r.initMovie(m); err != nil {
log.Errorf("failed to init movie: %s", err.Error())
return
log.Errorf("lazy init room %d movie %d failed: %s", r.ID, m.ID, err.Error())
DeleteMovieByID(r.ID, m.ID)
}
}
})
@ -147,7 +148,7 @@ func (r *Room) initMovie(movie *model.Movie) error {
return errors.New("rtmp is not enabled")
}
if movie.PullKey == "" {
movie.PullKey = uuid.New().String()
movie.PullKey = uuid.NewString()
}
_, err := r.rtmpa.NewChannel(movie.PullKey)
if err != nil {
@ -163,7 +164,7 @@ func (r *Room) initMovie(movie *model.Movie) error {
}
switch u.Scheme {
case "rtmp":
movie.PullKey = uuid.NewMD5(uuid.NameSpaceURL, []byte(movie.Url)).String()
movie.PullKey = uuid.NewMD5(r.uuid, []byte(movie.Url)).String()
c, err := r.rtmpa.NewChannel(movie.PullKey)
if err != nil {
return err
@ -186,7 +187,10 @@ func (r *Room) initMovie(movie *model.Movie) error {
}
}()
case "http", "https":
movie.PullKey = uuid.NewMD5(uuid.NameSpaceURL, []byte(movie.Url)).String()
if movie.Type != "flv" {
return errors.New("only flv is supported")
}
movie.PullKey = uuid.NewMD5(r.uuid, []byte(movie.Url)).String()
c, err := r.rtmpa.NewChannel(movie.PullKey)
if err != nil {
return err
@ -228,7 +232,7 @@ func (r *Room) initMovie(movie *model.Movie) error {
if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("unsupported scheme")
}
movie.PullKey = uuid.NewMD5(uuid.NameSpaceURL, []byte(movie.Url)).String()
movie.PullKey = uuid.NewMD5(r.uuid, []byte(movie.Url)).String()
case !movie.Live && !movie.Proxy, movie.Live && !movie.Proxy && !movie.RtmpSource:
u, err := url.Parse(movie.Url)
if err != nil {

@ -3,12 +3,15 @@ package op
import (
"errors"
"math/rand"
"strconv"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/synctv-org/synctv/internal/db"
"github.com/synctv-org/synctv/internal/model"
"github.com/zijiren233/gencontainer/rwmap"
"github.com/zijiren233/stream"
)
var roomCache rwmap.RWMap[uint, *Room]
@ -32,6 +35,7 @@ func WithVersion(version uint32) RoomConf {
func initRoom(room *model.Room, conf ...RoomConf) (*Room, error) {
r := &Room{
Room: *room,
uuid: uuid.NewMD5(uuid.NameSpaceURL, stream.StringToBytes(strconv.Itoa(int(room.ID)))),
lastActive: time.Now().UnixMilli(),
version: rand.Uint32(),
current: newCurrent(),

Loading…
Cancel
Save