Skip to content

Commit

Permalink
Merge pull request #342 from sbondCo/games
Browse files Browse the repository at this point in the history
Games
  • Loading branch information
IRHM authored Feb 11, 2024
2 parents 0f7bdaf + 3d5cf70 commit 1c44b18
Show file tree
Hide file tree
Showing 31 changed files with 2,325 additions and 133 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FROM golang:1.21-alpine AS server
WORKDIR /server
COPY server/*.go server/go.* ./
COPY server/arr/*.go ./arr/
COPY server/game/*.go ./game/

# Required so we can build with cgo
RUN apk update && apk add --no-cache musl-dev gcc build-base
Expand Down
41 changes: 39 additions & 2 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"log"
"log/slog"
"os"
"time"

"github.com/sbondCo/Watcharr/game"
"gorm.io/gorm"
)

Expand All @@ -31,6 +33,7 @@ type ServerConfig struct {

SONARR []SonarrSettings `json:",omitempty"`
RADARR []RadarrSettings `json:",omitempty"`
TWITCH game.IGDB `json:",omitempty"`

// Enable/disable debug logging. Useful for when trying
// to figure out exactly what the server is doing at a point
Expand All @@ -53,6 +56,10 @@ func (c *ServerConfig) GetSafe() ServerConfig {
DEBUG: c.DEBUG,
SONARR: c.SONARR, // Dont act safe, this contains sonarr api key, needed for config
RADARR: c.RADARR, // Dont act safe, this contains radarr api key, needed for config
TWITCH: game.IGDB{
ClientID: c.TWITCH.ClientID,
ClientSecret: c.TWITCH.ClientSecret,
}, // Dont act safe, this contains twitch secrets, needed for config
}
}

Expand Down Expand Up @@ -144,13 +151,17 @@ func writeConfig() error {
type ServerFeatures struct {
Sonarr bool `json:"sonarr"`
Radarr bool `json:"radarr"`
Games bool `json:"games"`
}

// Get enabled server functionality from Config.
// Mainly so the frontend can store this once and know
// which btns should be shown, etc.
func getEnabledFeatures(userPerms int) ServerFeatures {
var f ServerFeatures
if Config.TWITCH.ClientID != nil && Config.TWITCH.ClientSecret != nil {
f.Games = true
}
// https://github.com/sbondCo/Watcharr/issues/211
// Currently requesting permissions have not been setup, only admins for now.
if !hasPermission(userPerms, PERM_ADMIN) {
Expand All @@ -165,6 +176,32 @@ func getEnabledFeatures(userPerms int) ServerFeatures {
return f
}

func saveTwitchConfig(c game.IGDB) error {
// If existing client id and secret are same.. just return here
if (Config.TWITCH.ClientID != nil && c.ClientID != nil && Config.TWITCH.ClientSecret != nil && c.ClientSecret != nil) &&
*Config.TWITCH.ClientID == *c.ClientID && *Config.TWITCH.ClientSecret == *c.ClientSecret {
slog.Info("saveTwitchConfig: New ClientID and ClientSecret match old ClientID and ClientSecret.. ignoring request to update.")
return nil
}
// Update our config
Config.TWITCH.ClientID = c.ClientID
Config.TWITCH.ClientSecret = c.ClientSecret
Config.TWITCH.AccessToken = ""
Config.TWITCH.AccessTokenExpires = time.Time{}
// Try to init again
err := Config.TWITCH.Init()
if err != nil {
slog.Error("saveTwitchConfig failed to initialize TWITCH", "error", err)
return errors.New("initialization with credentials failed")
}
err = writeConfig()
if err != nil {
slog.Error("saveTwitchConfig failed to write config", "error", err)
return errors.New("failed to save config")
}
return nil
}

type ServerStats struct {
Users int64 `json:"users"`
PrivateUsers int64 `json:"privateUsers"`
Expand Down Expand Up @@ -206,13 +243,13 @@ func getServerStats(db *gorm.DB) ServerStats {
if resp.Error != nil {
slog.Error("getServerStats - MostWatchedShow query failed", "error", resp.Error)
} else {
stats.MostWatchedShow = w.Content
stats.MostWatchedShow = *w.Content
}
resp = db.Model(&Watched{}).Select("content_id, COUNT(*) AS mag").Joins("JOIN contents ON contents.type = ? AND contents.id = watcheds.content_id", "movie").Group("content_id").Order("mag DESC").Preload("Content").First(&w)
if resp.Error != nil {
slog.Error("getServerStats - MostWatchedMovie query failed", "error", resp.Error)
} else {
stats.MostWatchedMovie = w.Content
stats.MostWatchedMovie = *w.Content
}
return stats
}
36 changes: 28 additions & 8 deletions server/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getFollows(db *gorm.DB, userId uint) ([]FollowPublic, error) {
}

// Get followed profile thoughts, rating, etc on specific content.
func getFollowsThoughts(db *gorm.DB, userId uint, mediaType ContentType, tmdbId string) ([]FollowThoughts, error) {
func getFollowsThoughts(db *gorm.DB, userId uint, mediaType string, mediaId string) ([]FollowThoughts, error) {
var follows []Follow
res := db.Where("user_id = ?", userId).Preload("FollowedUser", "private = ? AND private_thoughts = ?", 0, 0).Find(&follows)
if res.Error != nil {
Expand All @@ -104,16 +104,36 @@ func getFollowsThoughts(db *gorm.DB, userId uint, mediaType ContentType, tmdbId
}
followIds = append(followIds, v.FollowedUser.ID)
}
// Get our content id from type and tmdbId
var content Content
res = db.Where("type = ? AND tmdb_id = ?", mediaType, tmdbId).Select("id").Find(&content)
if res.Error != nil {
slog.Error("getFollows: Error finding content from db.", "error", res.Error)
return []FollowThoughts{}, errors.New("failed to find content")
var contentOrGameId int
if mediaType == "game" {
// Get our content id from type and tmdbId
var content Game
res = db.Where("igdb_id = ?", mediaId).Select("id").Find(&content)
if res.Error != nil {
slog.Error("getFollows: Error finding content from db.", "error", res.Error)
return []FollowThoughts{}, errors.New("failed to find content")
}
contentOrGameId = content.ID
} else if mediaType == "movie" || mediaType == "tv" {
// Get our content id from type and tmdbId
var content Content
res = db.Where("type = ? AND tmdb_id = ?", mediaType, mediaId).Select("id").Find(&content)
if res.Error != nil {
slog.Error("getFollows: Error finding content from db.", "error", res.Error)
return []FollowThoughts{}, errors.New("failed to find content")
}
contentOrGameId = content.ID
} else {
slog.Error("getFollows: Unrecognized media type (movie, tv or game supported).", "media_type", mediaType)
return []FollowThoughts{}, errors.New("unrecognized media type")
}
// Get list of followeds watcheds for this content
var fw []Watched
res = db.Where("content_id = ? AND user_id IN ?", content.ID, followIds).Find(&fw)
if mediaType == "game" {
res = db.Where("game_id = ? AND user_id IN ?", contentOrGameId, followIds).Find(&fw)
} else {
res = db.Where("content_id = ? AND user_id IN ?", contentOrGameId, followIds).Find(&fw)
}
if res.Error != nil {
slog.Error("getFollows: Error finding followed watcheds from db.", "error", res.Error)
return []FollowThoughts{}, errors.New("failed to find followed watcheds")
Expand Down
Loading

0 comments on commit 1c44b18

Please sign in to comment.