[bugfix] Use ptr for instance stats entries to avoid skipping 0 values (#2666)

* [bugfix] Use ptr for instance stats entries to avoid skipping 0 values

* comment explaining why stats values are pointers
This commit is contained in:
tobi 2024-02-19 13:17:14 +01:00 committed by GitHub
parent 0554550acb
commit d10226e912
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 8 deletions

View File

@ -74,7 +74,9 @@ type InstanceV1 struct {
// URLs of interest for client applications.
URLs InstanceV1URLs `json:"urls,omitempty"`
// Statistics about the instance: number of posts, accounts, etc.
Stats map[string]int `json:"stats,omitempty"`
// Values are pointers because we don't want to skip 0 values when
// rendering stats via web templates.
Stats map[string]*int `json:"stats,omitempty"`
// URL of the instance avatar/banner image.
// example: https://example.org/files/instance/thumbnail.jpeg
Thumbnail string `json:"thumbnail"`

View File

@ -1024,24 +1024,24 @@ func (c *Converter) InstanceToAPIV1Instance(ctx context.Context, i *gtsmodel.Ins
instance.URLs.StreamingAPI = "wss://" + i.Domain
// statistics
stats := make(map[string]int, 3)
stats := make(map[string]*int, 3)
userCount, err := c.state.DB.CountInstanceUsers(ctx, i.Domain)
if err != nil {
return nil, fmt.Errorf("InstanceToAPIV1Instance: db error getting counting instance users: %w", err)
}
stats["user_count"] = userCount
stats["user_count"] = util.Ptr(userCount)
statusCount, err := c.state.DB.CountInstanceStatuses(ctx, i.Domain)
if err != nil {
return nil, fmt.Errorf("InstanceToAPIV1Instance: db error getting counting instance statuses: %w", err)
}
stats["status_count"] = statusCount
stats["status_count"] = util.Ptr(statusCount)
domainCount, err := c.state.DB.CountInstanceDomains(ctx, i.Domain)
if err != nil {
return nil, fmt.Errorf("InstanceToAPIV1Instance: db error getting counting instance domains: %w", err)
}
stats["domain_count"] = domainCount
stats["domain_count"] = util.Ptr(domainCount)
instance.Stats = stats
// thumbnail

View File

@ -26,7 +26,7 @@ Instance Logo
{{- end -}}
{{- define "strapUsers" -}}
{{- with .instance.Stats.user_count -}}
{{- with deref .instance.Stats.user_count -}}
{{- if eq . 1 -}}
<span class="count">{{- . -}}</span> user
{{- else -}}
@ -36,7 +36,7 @@ Instance Logo
{{- end -}}
{{- define "strapPosts" -}}
{{- with .instance.Stats.status_count -}}
{{- with deref .instance.Stats.status_count -}}
{{- if eq . 1 -}}
<span class="count">{{- . -}}</span> post
{{- else -}}
@ -46,7 +46,7 @@ Instance Logo
{{- end -}}
{{- define "strapInstances" -}}
{{- with .instance.Stats.domain_count -}}
{{- with deref .instance.Stats.domain_count -}}
{{- if eq . 1 -}}
<span class="count">{{- . -}}</span> other instance
{{- else -}}