From d10226e91219d96622b3ff60a172ba7c022c5f9d Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:17:14 +0100 Subject: [PATCH] [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 --- internal/api/model/instancev1.go | 4 +++- internal/typeutils/internaltofrontend.go | 8 ++++---- web/template/page_header.tmpl | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/api/model/instancev1.go b/internal/api/model/instancev1.go index bec719941..b402cdefe 100644 --- a/internal/api/model/instancev1.go +++ b/internal/api/model/instancev1.go @@ -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"` diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index f92f0bb65..592f36010 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -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 diff --git a/web/template/page_header.tmpl b/web/template/page_header.tmpl index dc727d144..5a2956883 100644 --- a/web/template/page_header.tmpl +++ b/web/template/page_header.tmpl @@ -26,7 +26,7 @@ Instance Logo {{- end -}} {{- define "strapUsers" -}} -{{- with .instance.Stats.user_count -}} +{{- with deref .instance.Stats.user_count -}} {{- if eq . 1 -}} {{- . -}} 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 -}} {{- . -}} 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 -}} {{- . -}} other instance {{- else -}}