mirror of https://github.com/synctv-org/synctv.git
fix: check parent
This commit is contained in:
parent
59e017c21e
commit
397a2cea19
|
@ -2,6 +2,7 @@ package op
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
|
@ -197,7 +198,38 @@ func (m *movies) GetMoviesWithPage(page, pageSize int, parentID string) ([]*mode
|
|||
}
|
||||
|
||||
// IsParentOf check if parentID is the parent of id
|
||||
func (m *movies) IsParentOf(parentID, id string) (bool, error) {
|
||||
func (m *movies) IsParentOf(id, parentID string) (bool, error) {
|
||||
if parentID == "" {
|
||||
return id != "", nil
|
||||
}
|
||||
mv, err := m.GetMovieByID(parentID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get parent movie failed: %v", err)
|
||||
}
|
||||
if !mv.IsFolder {
|
||||
return false, nil
|
||||
}
|
||||
return m.isParentOf(id, parentID, true)
|
||||
}
|
||||
|
||||
func (m *movies) IsParentFolder(id, parentID string) (bool, error) {
|
||||
if parentID == "" {
|
||||
return id != "", nil
|
||||
}
|
||||
mv, err := m.GetMovieByID(parentID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get parent movie failed: %v", err)
|
||||
}
|
||||
firstCheck := true
|
||||
if mv.IsFolder {
|
||||
firstCheck = false
|
||||
} else {
|
||||
parentID = mv.ParentID.String()
|
||||
}
|
||||
return m.isParentOf(id, parentID, firstCheck)
|
||||
}
|
||||
|
||||
func (m *movies) isParentOf(id, parentID string, firstCheck bool) (bool, error) {
|
||||
mv, err := m.GetMovieByID(id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
@ -206,7 +238,7 @@ func (m *movies) IsParentOf(parentID, id string) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
if mv.ParentID == model.EmptyNullString(parentID) {
|
||||
return true, nil
|
||||
return !firstCheck, nil
|
||||
}
|
||||
return m.IsParentOf(string(mv.ParentID), id)
|
||||
return m.isParentOf(string(mv.ParentID), parentID, false)
|
||||
}
|
||||
|
|
|
@ -82,18 +82,9 @@ func (r *Room) CheckVersion(version uint32) bool {
|
|||
}
|
||||
|
||||
func (r *Room) UpdateMovie(movieId string, movie *model.MovieBase) error {
|
||||
cid := r.current.current.Movie.ID
|
||||
if cid != "" {
|
||||
if cid == movieId {
|
||||
return errors.New("cannot update current movie")
|
||||
}
|
||||
ok, err := r.IsParentOf(cid, movieId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check parent failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return errors.New("cannot update current movie's parent")
|
||||
}
|
||||
err := r.checkCanModifyMovie(movieId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.movies.Update(movieId, movie)
|
||||
}
|
||||
|
@ -328,48 +319,68 @@ func (r *Room) SetPassword(password string) error {
|
|||
return db.SetRoomHashedPassword(r.ID, hashedPassword)
|
||||
}
|
||||
|
||||
func (r *Room) IsParentOf(movieID, parentID string) (bool, error) {
|
||||
if parentID == "" {
|
||||
return true, nil
|
||||
}
|
||||
return r.movies.IsParentOf(movieID, parentID)
|
||||
}
|
||||
|
||||
func (r *Room) DeleteMovieByID(id string) error {
|
||||
func (r *Room) checkCanModifyMovie(id string) error {
|
||||
if id == "" {
|
||||
return errors.New("movie id is nil")
|
||||
if r.current.current.Movie.ID != "" {
|
||||
return errors.New("cannot modify current movie")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
cid := r.current.current.Movie.ID
|
||||
if cid != "" {
|
||||
if cid == id {
|
||||
return errors.New("cannot delete current movie")
|
||||
return errors.New("cannot modify current movie")
|
||||
}
|
||||
ok, err := r.IsParentOf(cid, id)
|
||||
ok, err := r.movies.IsParentFolder(cid, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check parent failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return errors.New("cannot delete current movie's parent")
|
||||
return errors.New("cannot modify current movie's parent")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Room) checkCanModifyMovies(ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return errors.New("ids is nil")
|
||||
}
|
||||
cid := r.current.current.Movie.ID
|
||||
for _, id := range ids {
|
||||
if id == "" {
|
||||
if cid != "" {
|
||||
return errors.New("cannot modify current movie")
|
||||
}
|
||||
}
|
||||
if cid != "" {
|
||||
if id == cid {
|
||||
return errors.New("cannot modify current movie")
|
||||
}
|
||||
ok, err := r.movies.IsParentFolder(cid, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check parent failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return errors.New("cannot modify current movie's parent")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Room) DeleteMovieByID(id string) error {
|
||||
err := r.checkCanModifyMovie(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.movies.DeleteMovieByID(id)
|
||||
}
|
||||
|
||||
func (r *Room) DeleteMoviesByID(ids []string) error {
|
||||
cid := r.current.current.Movie.ID
|
||||
if cid != "" {
|
||||
for _, id := range ids {
|
||||
if id == cid {
|
||||
return errors.New("cannot delete current movie")
|
||||
}
|
||||
ok, err := r.IsParentOf(cid, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check parent failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return errors.New("cannot delete current movie's parent")
|
||||
}
|
||||
}
|
||||
err := r.checkCanModifyMovies(ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.movies.DeleteMoviesByID(ids)
|
||||
}
|
||||
|
@ -379,15 +390,9 @@ func (r *Room) ClearMovies() error {
|
|||
}
|
||||
|
||||
func (r *Room) ClearMoviesByParentID(parentID string) error {
|
||||
cid := r.current.current.Movie.ID
|
||||
if cid != "" {
|
||||
ok, err := r.IsParentOf(cid, parentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check parent failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return errors.New("cannot delete current movie's parent")
|
||||
}
|
||||
err := r.checkCanModifyMovie(parentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.movies.DeleteMovieByParentID(parentID)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue