Skip to content

Commit

Permalink
feat: list feeds by category on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
cljoly committed Apr 2, 2023
1 parent b1cde00 commit ef83a10
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
38 changes: 34 additions & 4 deletions gemtext/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,51 @@ var (
)

type Home struct {
Categories *miniflux.Categories
Categories []*RichCategory
query *url.Values
}

func NewHome(categories *miniflux.Categories, query *url.Values) (*Home, error) {
// Categories enriched with their Feeds
type RichCategory struct {
*miniflux.Category
Feeds []*miniflux.Feed
}

func NewHome(categories *miniflux.Categories, feeds *miniflux.Feeds, query *url.Values) (*Home, error) {
if categories == nil || query == nil {
return nil, fmt.Errorf("error trying to render with nil categories")
return nil, fmt.Errorf("error trying to render with nil arguments")
}
nbrCategories := len(*categories)

feedsByCategory := feedsByCategoryID(feeds, nbrCategories)

richCategories := make([]*RichCategory, nbrCategories)
for i, category := range *categories {
richCategories[i] = &RichCategory{
Category: category,
Feeds: feedsByCategory[category.ID],
}
}

return &Home{
Categories: categories,
Categories: richCategories,
query: query,
}, nil
}

func feedsByCategoryID(feeds *miniflux.Feeds, categoryHint int) map[int64][]*miniflux.Feed {
feedsByCategory := make(map[int64][]*miniflux.Feed, categoryHint)
for _, feed := range *feeds {
if feeds, ok := feedsByCategory[feed.Category.ID]; ok {
feedsByCategory[feed.Category.ID] = append(feeds, feed)
} else {
feedsByCategory[feed.Category.ID] = make([]*miniflux.Feed, 0)
}
}

return feedsByCategory
}

func (home *Home) Params(key_values ...string) (string, error) {
return params(home.query, key_values...)
}
Expand Down
15 changes: 15 additions & 0 deletions gemtext/templates/home.gmi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
None
{{ end }}

## Feeds
{{ range .Categories }}

### {{ .Title }}

{{ range .Feeds -}}
=> /entry?feedID={{ .ID }} {{ .Title }}
{{ else }}
No feeds
{{ end -}}

{{ else }}
None
{{ end }}

## Help

TODO
9 changes: 8 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ func homeHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
return
}

gemtextHome, err := gemtext.NewHome(&categories, &query)
feeds, err := miniflux.Feeds()
if err != nil {
w.WriteHeader(gemini.StatusTemporaryFailure, "Error querying minflux")
log.Printf("error getting miniflux feeds: %v", err)
return
}

gemtextHome, err := gemtext.NewHome(&categories, &feeds, &query)
err = gemtextHome.Render(w)
if err != nil {
log.Printf("error rendering home template: %v", err)
Expand Down

0 comments on commit ef83a10

Please sign in to comment.