diff --git a/api/banned.go b/api/banned.go new file mode 100644 index 0000000..84f17d1 --- /dev/null +++ b/api/banned.go @@ -0,0 +1,49 @@ +package main + +import "github.com/zmb3/spotify/v2" + +func containsBannedWord(word string) bool { + var bannedWords []BannedWord + + result := db.Where("word LIKE ?", "%"+word+"%").Find(&bannedWords) + + if result.Error != nil { + return false + } + if len(bannedWords) == 0 { + return false + } + return true +} + +func addBannedWord(word string) (BannedWord, error) { + bannedWord := BannedWord{Word: word} + result := db.Create(&bannedWord) + if result.Error != nil { + return bannedWord, result.Error + } + return bannedWord, nil +} + +func isBannedTrack(trackUri spotify.URI) bool { + var bannedTracks []BannedTrack + + result := db.Where("track_uri LIKE ?", "%"+trackUri+"%").Find(&bannedTracks) + + if result.Error != nil { + return false + } + if len(bannedTracks) == 0 { + return false + } + return true +} + +func addBannedTrack(trackUri spotify.URI) (BannedTrack, error) { + bannedTrack := BannedTrack{TrackURI: trackUri} + result := db.Create(&bannedTrack) + if result.Error != nil { + return bannedTrack, result.Error + } + return bannedTrack, nil +} diff --git a/api/main.go b/api/main.go index 1f699f3..652eb9d 100644 --- a/api/main.go +++ b/api/main.go @@ -1,7 +1,10 @@ package main import ( + "bufio" "context" + "fmt" + "log" "os" "strconv" "strings" @@ -66,6 +69,50 @@ func init() { db.AutoMigrate(&TrackImage{}) db.AutoMigrate(&Device{}) db.AutoMigrate(&LoginToken{}) + db.AutoMigrate(&BannedWord{}) + db.AutoMigrate(&BannedTrack{}) + + // Load Banned Words + bannedWordsFile, err := os.Open("../resources/banned/words.txt") + if err != nil { + panic(err) + } + defer bannedWordsFile.Close() + + scannerBannedWords := bufio.NewScanner(bannedWordsFile) + for scannerBannedWords.Scan() { + if !containsBannedWord(scannerBannedWords.Text()) { + _, err := addBannedWord(fmt.Sprintf("%v", scannerBannedWords.Text())) + if err != nil { + logger.Fatal().Err(err).Msg("Cannot Set Banned Words") + } + } + } + + if err := scannerBannedWords.Err(); err != nil { + log.Fatal(err) + } + + // Load Banned Tracks + bannedTracksFile, err := os.Open("../resources/banned/tracks.txt") + if err != nil { + panic(err) + } + defer bannedTracksFile.Close() + + scannerBannedTracks := bufio.NewScanner(bannedTracksFile) + for scannerBannedTracks.Scan() { + if !isBannedTrack(spotify.URI(fmt.Sprintf("%v", scannerBannedTracks.Text()))) { + _, err := addBannedTrack(spotify.URI(fmt.Sprintf("%v", scannerBannedTracks.Text()))) + if err != nil { + logger.Fatal().Err(err).Msg("Cannot Set Banned Words") + } + } + } + + if err := scannerBannedTracks.Err(); err != nil { + log.Fatal(err) + } // Load Spotify API auth = spotifyauth.New( diff --git a/api/player.go b/api/player.go index 6fac716..92df525 100644 --- a/api/player.go +++ b/api/player.go @@ -77,6 +77,14 @@ func handlePlayer(c *gin.Context) { return } } + banQuery := c.Query("ban") + if banQuery == "true" { + _, err := addBannedTrack(currentTrackURI) + if err != nil { + c.JSON(http.StatusInternalServerError, err) + return + } + } } c.JSON(http.StatusAccepted, "Ok: "+action) diff --git a/api/tracks.go b/api/tracks.go index ff59893..cdd487e 100644 --- a/api/tracks.go +++ b/api/tracks.go @@ -10,10 +10,14 @@ import ( ) func handleTrack(c *gin.Context) { + // var bannedWords = [2]string{"ram ranch", "fart"} + var bannedWords []BannedWord var handleTrackInput HandleTrackInput // var playerState *spotify.PlayerState // var track Track + db.Find(&bannedWords) + ctx := c.Request.Context() action := c.Param("action") @@ -34,6 +38,20 @@ func handleTrack(c *gin.Context) { c.JSON(http.StatusBadRequest, err) return } + + // Check for Banned Filter + for _, artist := range track.Artists { + if containsBannedWord(artist.Name) { + c.JSON(http.StatusBadRequest, "Fuck off") + return + } + } + + if containsBannedWord(track.Name) || isBannedTrack(track.URI) || track.Duration > 600000 { + c.JSON(http.StatusBadRequest, "Fuck off") + return + } + // Get Track Images trackImages := []TrackImage{} for _, image := range track.Album.Images { diff --git a/api/types.go b/api/types.go index f82fcef..b35f54d 100644 --- a/api/types.go +++ b/api/types.go @@ -56,7 +56,8 @@ type HandlePlayerVolumeInput struct { } type HandleTrackInput struct { - URI spotify.URI `json:"uri"` + URI spotify.URI `json:"uri"` + Banned bool `json:"banned"` } type HandleVoteInput struct { @@ -93,6 +94,16 @@ func (t *Track) BeforeDelete(tx *gorm.DB) (err error) { return } +type BannedWord struct { + ID uint `gorm:"primarykey"` + Word string `json:"word"` +} + +type BannedTrack struct { + ID uint `gorm:"primarykey"` + TrackURI spotify.URI +} + type LoginToken struct { AccessToken string `gorm:"primaryKey" json:"access_token"` TokenType string `json:"token_type"` diff --git a/resources/banned/tracks.txt b/resources/banned/tracks.txt new file mode 100644 index 0000000..45d5da2 --- /dev/null +++ b/resources/banned/tracks.txt @@ -0,0 +1 @@ +spotify:track:6rsoBvxrlxdmqJyGPPciyq \ No newline at end of file diff --git a/resources/banned/words.txt b/resources/banned/words.txt new file mode 100644 index 0000000..fd45209 --- /dev/null +++ b/resources/banned/words.txt @@ -0,0 +1,5 @@ +fart +cum +ram ranch +orgasm +beats by talent \ No newline at end of file