Add Steam integration showing online status and recent games
Some checks failed
Deploy with Docker Compose / deploy (push) Has been cancelled
Some checks failed
Deploy with Docker Compose / deploy (push) Has been cancelled
Fetches player summary and recently played games from Steam API with 5-minute server-side caching. Displays in the home sidebar with online indicator and game artwork. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -117,6 +117,7 @@ type ComplexityRoot struct {
|
||||
RowingSessions func(childComplexity int) int
|
||||
SpotifyListening func(childComplexity int) int
|
||||
SpotifyRecent func(childComplexity int) int
|
||||
SteamStatus func(childComplexity int) int
|
||||
User func(childComplexity int, id int) int
|
||||
Users func(childComplexity int) int
|
||||
}
|
||||
@@ -160,6 +161,19 @@ type ComplexityRoot struct {
|
||||
Name func(childComplexity int) int
|
||||
}
|
||||
|
||||
SteamGame struct {
|
||||
AppID func(childComplexity int) int
|
||||
HeaderImageURL func(childComplexity int) int
|
||||
Name func(childComplexity int) int
|
||||
Playtime2Weeks func(childComplexity int) int
|
||||
PlaytimeForever func(childComplexity int) int
|
||||
}
|
||||
|
||||
SteamStatus struct {
|
||||
Online func(childComplexity int) int
|
||||
RecentGames func(childComplexity int) int
|
||||
}
|
||||
|
||||
User struct {
|
||||
Admin func(childComplexity int) int
|
||||
CreatedAt func(childComplexity int) int
|
||||
@@ -208,6 +222,7 @@ type QueryResolver interface {
|
||||
SpotifyListening(ctx context.Context) (*model.SpotifyPlaying, error)
|
||||
SpotifyRecent(ctx context.Context) ([]*model.SpotifyRecentItem, error)
|
||||
GiteaFeed(ctx context.Context) (*model.GiteaFeedItem, error)
|
||||
SteamStatus(ctx context.Context) (*model.SteamStatus, error)
|
||||
Me(ctx context.Context) (*models.User, error)
|
||||
}
|
||||
type RowingResolver interface {
|
||||
@@ -598,6 +613,12 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.Query.SpotifyRecent(childComplexity), true
|
||||
case "Query.steamStatus":
|
||||
if e.ComplexityRoot.Query.SteamStatus == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.Query.SteamStatus(childComplexity), true
|
||||
case "Query.user":
|
||||
if e.ComplexityRoot.Query.User == nil {
|
||||
break
|
||||
@@ -731,6 +752,50 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
|
||||
|
||||
return e.ComplexityRoot.SpotifyTrack.Name(childComplexity), true
|
||||
|
||||
case "SteamGame.appId":
|
||||
if e.ComplexityRoot.SteamGame.AppID == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamGame.AppID(childComplexity), true
|
||||
case "SteamGame.headerImageUrl":
|
||||
if e.ComplexityRoot.SteamGame.HeaderImageURL == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamGame.HeaderImageURL(childComplexity), true
|
||||
case "SteamGame.name":
|
||||
if e.ComplexityRoot.SteamGame.Name == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamGame.Name(childComplexity), true
|
||||
case "SteamGame.playtime2Weeks":
|
||||
if e.ComplexityRoot.SteamGame.Playtime2Weeks == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamGame.Playtime2Weeks(childComplexity), true
|
||||
case "SteamGame.playtimeForever":
|
||||
if e.ComplexityRoot.SteamGame.PlaytimeForever == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamGame.PlaytimeForever(childComplexity), true
|
||||
|
||||
case "SteamStatus.online":
|
||||
if e.ComplexityRoot.SteamStatus.Online == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamStatus.Online(childComplexity), true
|
||||
case "SteamStatus.recentGames":
|
||||
if e.ComplexityRoot.SteamStatus.RecentGames == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.ComplexityRoot.SteamStatus.RecentGames(childComplexity), true
|
||||
|
||||
case "User.admin":
|
||||
if e.ComplexityRoot.User.Admin == nil {
|
||||
break
|
||||
@@ -850,7 +915,7 @@ func newExecutionContext(
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed "schema/activity.graphql" "schema/auth.graphql" "schema/favorite.graphql" "schema/gitea.graphql" "schema/message.graphql" "schema/post.graphql" "schema/rowing.graphql" "schema/schema.graphql" "schema/spotify.graphql" "schema/user.graphql"
|
||||
//go:embed "schema/activity.graphql" "schema/auth.graphql" "schema/favorite.graphql" "schema/gitea.graphql" "schema/message.graphql" "schema/post.graphql" "schema/rowing.graphql" "schema/schema.graphql" "schema/spotify.graphql" "schema/steam.graphql" "schema/user.graphql"
|
||||
var sourcesFS embed.FS
|
||||
|
||||
func sourceData(filename string) string {
|
||||
@@ -871,6 +936,7 @@ var sources = []*ast.Source{
|
||||
{Name: "schema/rowing.graphql", Input: sourceData("schema/rowing.graphql"), BuiltIn: false},
|
||||
{Name: "schema/schema.graphql", Input: sourceData("schema/schema.graphql"), BuiltIn: false},
|
||||
{Name: "schema/spotify.graphql", Input: sourceData("schema/spotify.graphql"), BuiltIn: false},
|
||||
{Name: "schema/steam.graphql", Input: sourceData("schema/steam.graphql"), BuiltIn: false},
|
||||
{Name: "schema/user.graphql", Input: sourceData("schema/user.graphql"), BuiltIn: false},
|
||||
}
|
||||
var parsedSchema = gqlparser.MustLoadSchema(sources...)
|
||||
@@ -2985,6 +3051,41 @@ func (ec *executionContext) fieldContext_Query_giteaFeed(_ context.Context, fiel
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Query_steamStatus(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_Query_steamStatus,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return ec.Resolvers.Query().SteamStatus(ctx)
|
||||
},
|
||||
nil,
|
||||
ec.marshalOSteamStatus2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamStatus,
|
||||
true,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_Query_steamStatus(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "Query",
|
||||
Field: field,
|
||||
IsMethod: true,
|
||||
IsResolver: true,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
switch field.Name {
|
||||
case "online":
|
||||
return ec.fieldContext_SteamStatus_online(ctx, field)
|
||||
case "recentGames":
|
||||
return ec.fieldContext_SteamStatus_recentGames(ctx, field)
|
||||
}
|
||||
return nil, fmt.Errorf("no field named %q was found under type SteamStatus", field.Name)
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Query_me(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
@@ -3686,6 +3787,221 @@ func (ec *executionContext) fieldContext_SpotifyTrack_album(_ context.Context, f
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamGame_appId(ctx context.Context, field graphql.CollectedField, obj *model.SteamGame) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamGame_appId,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.AppID, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNInt2int,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamGame_appId(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamGame",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type Int does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamGame_name(ctx context.Context, field graphql.CollectedField, obj *model.SteamGame) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamGame_name,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.Name, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNString2string,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamGame_name(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamGame",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type String does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamGame_playtime2Weeks(ctx context.Context, field graphql.CollectedField, obj *model.SteamGame) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamGame_playtime2Weeks,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.Playtime2Weeks, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNInt2int,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamGame_playtime2Weeks(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamGame",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type Int does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamGame_playtimeForever(ctx context.Context, field graphql.CollectedField, obj *model.SteamGame) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamGame_playtimeForever,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.PlaytimeForever, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNInt2int,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamGame_playtimeForever(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamGame",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type Int does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamGame_headerImageUrl(ctx context.Context, field graphql.CollectedField, obj *model.SteamGame) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamGame_headerImageUrl,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.HeaderImageURL, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNString2string,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamGame_headerImageUrl(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamGame",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type String does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamStatus_online(ctx context.Context, field graphql.CollectedField, obj *model.SteamStatus) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamStatus_online,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.Online, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNBoolean2bool,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamStatus_online(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamStatus",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
return nil, errors.New("field of type Boolean does not have child fields")
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _SteamStatus_recentGames(ctx context.Context, field graphql.CollectedField, obj *model.SteamStatus) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
ec.OperationContext,
|
||||
field,
|
||||
ec.fieldContext_SteamStatus_recentGames,
|
||||
func(ctx context.Context) (any, error) {
|
||||
return obj.RecentGames, nil
|
||||
},
|
||||
nil,
|
||||
ec.marshalNSteamGame2ᚕᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamGameᚄ,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
func (ec *executionContext) fieldContext_SteamStatus_recentGames(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||
fc = &graphql.FieldContext{
|
||||
Object: "SteamStatus",
|
||||
Field: field,
|
||||
IsMethod: false,
|
||||
IsResolver: false,
|
||||
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||
switch field.Name {
|
||||
case "appId":
|
||||
return ec.fieldContext_SteamGame_appId(ctx, field)
|
||||
case "name":
|
||||
return ec.fieldContext_SteamGame_name(ctx, field)
|
||||
case "playtime2Weeks":
|
||||
return ec.fieldContext_SteamGame_playtime2Weeks(ctx, field)
|
||||
case "playtimeForever":
|
||||
return ec.fieldContext_SteamGame_playtimeForever(ctx, field)
|
||||
case "headerImageUrl":
|
||||
return ec.fieldContext_SteamGame_headerImageUrl(ctx, field)
|
||||
}
|
||||
return nil, fmt.Errorf("no field named %q was found under type SteamGame", field.Name)
|
||||
},
|
||||
}
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) _User_id(ctx context.Context, field graphql.CollectedField, obj *models.User) (ret graphql.Marshaler) {
|
||||
return graphql.ResolveField(
|
||||
ctx,
|
||||
@@ -6382,6 +6698,25 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr
|
||||
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
|
||||
}
|
||||
|
||||
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
|
||||
case "steamStatus":
|
||||
field := field
|
||||
|
||||
innerFunc := func(ctx context.Context, _ *graphql.FieldSet) (res graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
}
|
||||
}()
|
||||
res = ec._Query_steamStatus(ctx, field)
|
||||
return res
|
||||
}
|
||||
|
||||
rrm := func(ctx context.Context) graphql.Marshaler {
|
||||
return ec.OperationContext.RootResolverMiddleware(ctx,
|
||||
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
|
||||
}
|
||||
|
||||
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
|
||||
case "me":
|
||||
field := field
|
||||
@@ -6851,6 +7186,109 @@ func (ec *executionContext) _SpotifyTrack(ctx context.Context, sel ast.Selection
|
||||
return out
|
||||
}
|
||||
|
||||
var steamGameImplementors = []string{"SteamGame"}
|
||||
|
||||
func (ec *executionContext) _SteamGame(ctx context.Context, sel ast.SelectionSet, obj *model.SteamGame) graphql.Marshaler {
|
||||
fields := graphql.CollectFields(ec.OperationContext, sel, steamGameImplementors)
|
||||
|
||||
out := graphql.NewFieldSet(fields)
|
||||
deferred := make(map[string]*graphql.FieldSet)
|
||||
for i, field := range fields {
|
||||
switch field.Name {
|
||||
case "__typename":
|
||||
out.Values[i] = graphql.MarshalString("SteamGame")
|
||||
case "appId":
|
||||
out.Values[i] = ec._SteamGame_appId(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
case "name":
|
||||
out.Values[i] = ec._SteamGame_name(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
case "playtime2Weeks":
|
||||
out.Values[i] = ec._SteamGame_playtime2Weeks(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
case "playtimeForever":
|
||||
out.Values[i] = ec._SteamGame_playtimeForever(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
case "headerImageUrl":
|
||||
out.Values[i] = ec._SteamGame_headerImageUrl(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
default:
|
||||
panic("unknown field " + strconv.Quote(field.Name))
|
||||
}
|
||||
}
|
||||
out.Dispatch(ctx)
|
||||
if out.Invalids > 0 {
|
||||
return graphql.Null
|
||||
}
|
||||
|
||||
atomic.AddInt32(&ec.Deferred, int32(len(deferred)))
|
||||
|
||||
for label, dfs := range deferred {
|
||||
ec.ProcessDeferredGroup(graphql.DeferredGroup{
|
||||
Label: label,
|
||||
Path: graphql.GetPath(ctx),
|
||||
FieldSet: dfs,
|
||||
Context: ctx,
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
var steamStatusImplementors = []string{"SteamStatus"}
|
||||
|
||||
func (ec *executionContext) _SteamStatus(ctx context.Context, sel ast.SelectionSet, obj *model.SteamStatus) graphql.Marshaler {
|
||||
fields := graphql.CollectFields(ec.OperationContext, sel, steamStatusImplementors)
|
||||
|
||||
out := graphql.NewFieldSet(fields)
|
||||
deferred := make(map[string]*graphql.FieldSet)
|
||||
for i, field := range fields {
|
||||
switch field.Name {
|
||||
case "__typename":
|
||||
out.Values[i] = graphql.MarshalString("SteamStatus")
|
||||
case "online":
|
||||
out.Values[i] = ec._SteamStatus_online(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
case "recentGames":
|
||||
out.Values[i] = ec._SteamStatus_recentGames(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
out.Invalids++
|
||||
}
|
||||
default:
|
||||
panic("unknown field " + strconv.Quote(field.Name))
|
||||
}
|
||||
}
|
||||
out.Dispatch(ctx)
|
||||
if out.Invalids > 0 {
|
||||
return graphql.Null
|
||||
}
|
||||
|
||||
atomic.AddInt32(&ec.Deferred, int32(len(deferred)))
|
||||
|
||||
for label, dfs := range deferred {
|
||||
ec.ProcessDeferredGroup(graphql.DeferredGroup{
|
||||
Label: label,
|
||||
Path: graphql.GetPath(ctx),
|
||||
FieldSet: dfs,
|
||||
Context: ctx,
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
var userImplementors = []string{"User"}
|
||||
|
||||
func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj *models.User) graphql.Marshaler {
|
||||
@@ -7603,6 +8041,32 @@ func (ec *executionContext) marshalNSpotifyTrack2ᚖadamᚑfrenchᚗcoᚗukᚋba
|
||||
return ec._SpotifyTrack(ctx, sel, v)
|
||||
}
|
||||
|
||||
func (ec *executionContext) marshalNSteamGame2ᚕᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamGameᚄ(ctx context.Context, sel ast.SelectionSet, v []*model.SteamGame) graphql.Marshaler {
|
||||
ret := graphql.MarshalSliceConcurrently(ctx, len(v), 0, false, func(ctx context.Context, i int) graphql.Marshaler {
|
||||
fc := graphql.GetFieldContext(ctx)
|
||||
fc.Result = &v[i]
|
||||
return ec.marshalNSteamGame2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamGame(ctx, sel, v[i])
|
||||
})
|
||||
|
||||
for _, e := range ret {
|
||||
if e == graphql.Null {
|
||||
return graphql.Null
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (ec *executionContext) marshalNSteamGame2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamGame(ctx context.Context, sel ast.SelectionSet, v *model.SteamGame) graphql.Marshaler {
|
||||
if v == nil {
|
||||
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
|
||||
graphql.AddErrorf(ctx, "the requested element is null which the schema does not allow")
|
||||
}
|
||||
return graphql.Null
|
||||
}
|
||||
return ec._SteamGame(ctx, sel, v)
|
||||
}
|
||||
|
||||
func (ec *executionContext) unmarshalNString2string(ctx context.Context, v any) (string, error) {
|
||||
res, err := graphql.UnmarshalString(v)
|
||||
return res, graphql.ErrorOnPath(ctx, err)
|
||||
@@ -7888,6 +8352,13 @@ func (ec *executionContext) marshalOSpotifyTrack2ᚖadamᚑfrenchᚗcoᚗukᚋba
|
||||
return ec._SpotifyTrack(ctx, sel, v)
|
||||
}
|
||||
|
||||
func (ec *executionContext) marshalOSteamStatus2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐSteamStatus(ctx context.Context, sel ast.SelectionSet, v *model.SteamStatus) graphql.Marshaler {
|
||||
if v == nil {
|
||||
return graphql.Null
|
||||
}
|
||||
return ec._SteamStatus(ctx, sel, v)
|
||||
}
|
||||
|
||||
func (ec *executionContext) unmarshalOString2string(ctx context.Context, v any) (string, error) {
|
||||
res, err := graphql.UnmarshalString(v)
|
||||
return res, graphql.ErrorOnPath(ctx, err)
|
||||
|
||||
@@ -83,6 +83,19 @@ type SpotifyTrack struct {
|
||||
Album *SpotifyAlbum `json:"album"`
|
||||
}
|
||||
|
||||
type SteamGame struct {
|
||||
AppID int `json:"appId"`
|
||||
Name string `json:"name"`
|
||||
Playtime2Weeks int `json:"playtime2Weeks"`
|
||||
PlaytimeForever int `json:"playtimeForever"`
|
||||
HeaderImageURL string `json:"headerImageUrl"`
|
||||
}
|
||||
|
||||
type SteamStatus struct {
|
||||
Online bool `json:"online"`
|
||||
RecentGames []*SteamGame `json:"recentGames"`
|
||||
}
|
||||
|
||||
type UpdatePostInput struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
|
||||
@@ -450,6 +450,44 @@ func (r *queryResolver) GiteaFeed(ctx context.Context) (*model.GiteaFeedItem, er
|
||||
return mapGiteaFeed(feed), nil
|
||||
}
|
||||
|
||||
// SteamStatus is the resolver for the steamStatus field.
|
||||
func (r *queryResolver) SteamStatus(ctx context.Context) (*model.SteamStatus, error) {
|
||||
if r.Store.SteamAPIKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if r.Store.SteamFresh() {
|
||||
return &model.SteamStatus{
|
||||
Online: r.Store.SteamOnline,
|
||||
RecentGames: mapSteamGames(r.Store.SteamRecentGames),
|
||||
}, nil
|
||||
}
|
||||
|
||||
games, err := services.FetchRecentlyPlayedGames(r.Store.SteamAPIKey, r.Store.SteamID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
summary, err := services.FetchPlayerSummary(r.Store.SteamAPIKey, r.Store.SteamID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
online := false
|
||||
if summary != nil {
|
||||
online = summary.PersonaState > 0
|
||||
}
|
||||
|
||||
r.Store.SteamRecentGames = games
|
||||
r.Store.SteamOnline = online
|
||||
r.Store.SteamFetchedAt = time.Now()
|
||||
|
||||
return &model.SteamStatus{
|
||||
Online: online,
|
||||
RecentGames: mapSteamGames(games),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Me is the resolver for the me field.
|
||||
func (r *queryResolver) Me(ctx context.Context) (*models.User, error) {
|
||||
userID, ok := UserIDFromCtx(ctx)
|
||||
|
||||
@@ -12,6 +12,7 @@ type Query {
|
||||
spotifyListening: SpotifyPlaying
|
||||
spotifyRecent: [SpotifyRecentItem!]
|
||||
giteaFeed: GiteaFeedItem
|
||||
steamStatus: SteamStatus
|
||||
me: User
|
||||
}
|
||||
|
||||
|
||||
12
backend/graph/schema/steam.graphql
Normal file
12
backend/graph/schema/steam.graphql
Normal file
@@ -0,0 +1,12 @@
|
||||
type SteamGame {
|
||||
appId: Int!
|
||||
name: String!
|
||||
playtime2Weeks: Int!
|
||||
playtimeForever: Int!
|
||||
headerImageUrl: String!
|
||||
}
|
||||
|
||||
type SteamStatus {
|
||||
online: Boolean!
|
||||
recentGames: [SteamGame!]!
|
||||
}
|
||||
22
backend/graph/steam_helpers.go
Normal file
22
backend/graph/steam_helpers.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"adam-french.co.uk/backend/graph/model"
|
||||
"adam-french.co.uk/backend/services"
|
||||
)
|
||||
|
||||
func mapSteamGames(games []services.SteamRecentGame) []*model.SteamGame {
|
||||
result := make([]*model.SteamGame, len(games))
|
||||
for i, g := range games {
|
||||
result[i] = &model.SteamGame{
|
||||
AppID: g.AppID,
|
||||
Name: g.Name,
|
||||
Playtime2Weeks: g.Playtime2Weeks,
|
||||
PlaytimeForever: g.PlaytimeForever,
|
||||
HeaderImageURL: fmt.Sprintf("https://cdn.akamai.steamstatic.com/steam/apps/%d/header.jpg", g.AppID),
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -25,6 +25,12 @@ type Store struct {
|
||||
GiteaPort string
|
||||
GiteaFeed *services.GiteaFeedResponse
|
||||
GiteaFeedFetchedAt time.Time
|
||||
|
||||
SteamAPIKey string
|
||||
SteamID string
|
||||
SteamRecentGames []services.SteamRecentGame
|
||||
SteamOnline bool
|
||||
SteamFetchedAt time.Time
|
||||
}
|
||||
|
||||
func (s *Store) GiteaFeedFresh() bool {
|
||||
@@ -33,3 +39,10 @@ func (s *Store) GiteaFeedFresh() bool {
|
||||
}
|
||||
return time.Since(s.GiteaFeedFetchedAt) < time.Minute
|
||||
}
|
||||
|
||||
func (s *Store) SteamFresh() bool {
|
||||
if s.SteamRecentGames == nil {
|
||||
return false
|
||||
}
|
||||
return time.Since(s.SteamFetchedAt) < 5*time.Minute
|
||||
}
|
||||
|
||||
@@ -74,7 +74,10 @@ func main() {
|
||||
giteaHost := os.Getenv("GITEA_HOST")
|
||||
giteaPort := os.Getenv("GITEA_PORT")
|
||||
|
||||
store := handlers.Store{DB: db, SpotifyAuth: spotifyAuth, SpotifyClient: spotifyClient, ClaudeClient: claudeClient, Auth: auth, Notes: notes, GiteaHost: giteaHost, GiteaPort: giteaPort}
|
||||
steamAPIKey := os.Getenv("STEAM_API_KEY")
|
||||
steamID := os.Getenv("STEAM_ID")
|
||||
|
||||
store := handlers.Store{DB: db, SpotifyAuth: spotifyAuth, SpotifyClient: spotifyClient, ClaudeClient: claudeClient, Auth: auth, Notes: notes, GiteaHost: giteaHost, GiteaPort: giteaPort, SteamAPIKey: steamAPIKey, SteamID: steamID}
|
||||
|
||||
protected := r.Group("/", store.AuthMiddlewear)
|
||||
admin := r.Group("/", store.AuthMiddlewear, store.AdminMiddleware)
|
||||
|
||||
83
backend/services/steam.go
Normal file
83
backend/services/steam.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SteamRecentGame struct {
|
||||
AppID int `json:"appid"`
|
||||
Name string `json:"name"`
|
||||
Playtime2Weeks int `json:"playtime_2weeks"`
|
||||
PlaytimeForever int `json:"playtime_forever"`
|
||||
}
|
||||
|
||||
type SteamRecentGamesResponse struct {
|
||||
Response struct {
|
||||
TotalCount int `json:"total_count"`
|
||||
Games []SteamRecentGame `json:"games"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
type SteamPlayerSummary struct {
|
||||
PersonaState int `json:"personastate"`
|
||||
}
|
||||
|
||||
type SteamPlayerSummariesResponse struct {
|
||||
Response struct {
|
||||
Players []SteamPlayerSummary `json:"players"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
func FetchRecentlyPlayedGames(apiKey, steamID string) ([]SteamRecentGame, error) {
|
||||
url := fmt.Sprintf("https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v1/?key=%s&steamid=%s&count=3", apiKey, steamID)
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result SteamRecentGamesResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Response.Games, nil
|
||||
}
|
||||
|
||||
func FetchPlayerSummary(apiKey, steamID string) (*SteamPlayerSummary, error) {
|
||||
url := fmt.Sprintf("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", apiKey, steamID)
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result SteamPlayerSummariesResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(result.Response.Players) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &result.Response.Players[0], nil
|
||||
}
|
||||
Reference in New Issue
Block a user