make retrieval of apimodel themes more conventional

This commit is contained in:
tobi 2024-03-25 18:12:07 +01:00
parent 42ababa6fd
commit 54d53a10ef
4 changed files with 37 additions and 34 deletions

View file

@ -21,7 +21,6 @@ import (
"net/http"
"github.com/gin-gonic/gin"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
@ -73,17 +72,6 @@ func (m *Module) AccountThemesGETHandler(c *gin.Context) {
}
// Retrieve available themes.
gtsThemes := m.processor.Account().ThemesGet()
// Convert themes to apimodel.
themes := make([]apimodel.Theme, len(gtsThemes.SortedByTitle))
for i, gtsTheme := range gtsThemes.SortedByTitle {
themes[i] = apimodel.Theme{
Title: gtsTheme.Title,
Description: gtsTheme.Description,
FileName: gtsTheme.FileName,
}
}
themes := m.processor.Account().ThemesGet()
apiutil.JSON(c, http.StatusOK, themes)
}

View file

@ -220,3 +220,17 @@ type Relationship struct {
Endorsed bool // Are you featuring this user on your profile?
Note string // Your note on this account.
}
// Theme represents a user-selected
// CSS theme for an account.
type Theme struct {
// User-facing title of this theme.
Title string
// User-facing description of this theme.
Description string
// FileName of this theme in the themes
// directory (eg., `light-blurple.css`).
FileName string
}

View file

@ -26,7 +26,9 @@ import (
"strings"
"codeberg.org/gruf/go-bytesize"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
@ -36,22 +38,8 @@ var (
)
// GetThemes returns available account css themes.
func (p *Processor) ThemesGet() *Themes {
return p.themes
}
// Theme represents a user-selected
// CSS theme for an account.
type Theme struct {
// User-facing title of this theme.
Title string
// User-facing description of this theme.
Description string
// FileName of this theme in the themes
// directory (eg., `light-blurple.css`).
FileName string
func (p *Processor) ThemesGet() []apimodel.Theme {
return p.converter.ThemesToAPIThemes(p.themes.SortedByTitle)
}
// Themes represents an in-memory
@ -59,11 +47,11 @@ type Theme struct {
type Themes struct {
// Themes sorted alphabetically
// by title (case insensitive).
SortedByTitle []*Theme
SortedByTitle []*gtsmodel.Theme
// ByFileName contains themes retrievable
// by their filename eg., `light-blurple.css`.
ByFileName map[string]*Theme
ByFileName map[string]*gtsmodel.Theme
}
// PopulateThemes parses available account CSS
@ -82,7 +70,7 @@ func PopulateThemes() *Themes {
}
themes := &Themes{
ByFileName: make(map[string]*Theme),
ByFileName: make(map[string]*gtsmodel.Theme),
}
for _, f := range themesFiles {
@ -143,7 +131,7 @@ func PopulateThemes() *Themes {
themeDescription = strings.TrimSpace(string(descMatches[1]))
}
theme := &Theme{
theme := &gtsmodel.Theme{
Title: themeTitle,
Description: themeDescription,
FileName: fileName,
@ -155,7 +143,7 @@ func PopulateThemes() *Themes {
// Sort themes alphabetically
// by title (case insensitive).
slices.SortFunc(themes.SortedByTitle, func(a, b *Theme) int {
slices.SortFunc(themes.SortedByTitle, func(a, b *gtsmodel.Theme) int {
return cmp.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title))
})

View file

@ -1774,3 +1774,16 @@ func (c *Converter) convertTagsToAPITags(ctx context.Context, tags []*gtsmodel.T
return apiTags, errs.Combine()
}
// ThemesToAPIThemes converts a slice of gtsmodel Themes into apimodel Themes.
func (c *Converter) ThemesToAPIThemes(themes []*gtsmodel.Theme) []apimodel.Theme {
apiThemes := make([]apimodel.Theme, len(themes))
for i, theme := range themes {
apiThemes[i] = apimodel.Theme{
Title: theme.Title,
Description: theme.Description,
FileName: theme.FileName,
}
}
return apiThemes
}