From e643effefae3068c119ae6a7f946178d6f3c1369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Fri, 28 Mar 2025 14:20:53 -0700 Subject: [PATCH] feat: combine feed icon handlers to use only `externalIconID` --- internal/googlereader/handler.go | 36 +------------------ internal/storage/entry_query_builder.go | 9 ++++- internal/storage/icon.go | 2 +- .../template/templates/common/feed_list.html | 2 +- .../templates/views/bookmark_entries.html | 2 +- .../templates/views/category_entries.html | 2 +- internal/template/templates/views/entry.html | 4 +-- .../templates/views/feed_entries.html | 2 +- .../templates/views/history_entries.html | 2 +- internal/template/templates/views/search.html | 2 +- .../templates/views/shared_entries.html | 2 +- .../template/templates/views/tag_entries.html | 2 +- .../templates/views/unread_entries.html | 2 +- internal/ui/feed_icon.go | 6 ++-- internal/ui/middleware.go | 8 +++++ internal/ui/ui.go | 2 +- 16 files changed, 33 insertions(+), 52 deletions(-) diff --git a/internal/googlereader/handler.go b/internal/googlereader/handler.go index 2f40f8f4..f4569380 100644 --- a/internal/googlereader/handler.go +++ b/internal/googlereader/handler.go @@ -210,7 +210,6 @@ func (r RequestModifiers) String() string { func Serve(router *mux.Router, store *storage.Storage) { handler := &handler{store, router} router.HandleFunc("/accounts/ClientLogin", handler.clientLoginHandler).Methods(http.MethodPost).Name("ClientLogin") - router.HandleFunc("/reader/api/0/icons/{externalIconID}", handler.iconHandler).Methods(http.MethodGet).Name("Icons") middleware := newMiddleware(store) sr := router.PathPrefix("/reader/api/0").Subrouter() @@ -728,39 +727,6 @@ func (h *handler) quickAddHandler(w http.ResponseWriter, r *http.Request) { }) } -func (h *handler) iconHandler(w http.ResponseWriter, r *http.Request) { - clientIP := request.ClientIP(r) - externalIconID := request.RouteStringParam(r, "externalIconID") - - slog.Debug("[GoogleReader] Handle /icons/{externalIconID}", - slog.String("handler", "iconHandler"), - slog.String("client_ip", clientIP), - slog.String("user_agent", r.UserAgent()), - slog.String("external_icon_id", externalIconID), - ) - - icon, err := h.store.IconByExternalID(externalIconID) - if err != nil { - json.ServerError(w, r, err) - return - } - - if icon == nil { - json.NotFound(w, r) - return - } - - response.New(w, r).WithCaching(icon.Hash, 72*time.Hour, func(b *response.Builder) { - b.WithHeader("Content-Security-Policy", `sandbox`) - b.WithHeader("Content-Type", icon.MimeType) - b.WithBody(icon.Content) - if icon.MimeType != "image/svg+xml" { - b.WithoutCompression() - } - b.Write() - }) -} - func getFeed(stream Stream, store *storage.Storage, userID int64) (*model.Feed, error) { feedID, err := strconv.ParseInt(stream.ID, 10, 64) if err != nil { @@ -863,7 +829,7 @@ func move(stream Stream, destination Stream, store *storage.Storage, userID int6 func (h *handler) feedIconURL(f *model.Feed) string { if f.Icon != nil && f.Icon.ExternalIconID != "" { - return config.Opts.RootURL() + route.Path(h.router, "Icons", "externalIconID", f.Icon.ExternalIconID) + return config.Opts.RootURL() + route.Path(h.router, "feedIcon", "externalIconID", f.Icon.ExternalIconID) } else { return "" } diff --git a/internal/storage/entry_query_builder.go b/internal/storage/entry_query_builder.go index e9f9692c..af9b6b1b 100644 --- a/internal/storage/entry_query_builder.go +++ b/internal/storage/entry_query_builder.go @@ -293,6 +293,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) { f.hide_globally, f.no_media_player, fi.icon_id, + i.external_id AS icon_external_id, u.timezone FROM entries e @@ -302,6 +303,8 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) { categories c ON c.id=f.category_id LEFT JOIN feed_icons fi ON fi.feed_id=f.id + LEFT JOIN + icons i ON i.id=fi.icon_id LEFT JOIN users u ON u.id=e.user_id WHERE %s %s @@ -323,6 +326,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) { for rows.Next() { var iconID sql.NullInt64 + var externalIconID sql.NullString var tz string entry := model.NewEntry() @@ -361,6 +365,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) { &entry.Feed.HideGlobally, &entry.Feed.NoMediaPlayer, &iconID, + &externalIconID, &tz, ) @@ -368,8 +373,10 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) { return nil, fmt.Errorf("store: unable to fetch entry row: %v", err) } - if iconID.Valid { + if iconID.Valid && externalIconID.Valid && externalIconID.String != "" { + entry.Feed.Icon.FeedID = entry.FeedID entry.Feed.Icon.IconID = iconID.Int64 + entry.Feed.Icon.ExternalIconID = externalIconID.String } else { entry.Feed.Icon.IconID = 0 } diff --git a/internal/storage/icon.go b/internal/storage/icon.go index 6cc98a25..072a5239 100644 --- a/internal/storage/icon.go +++ b/internal/storage/icon.go @@ -59,7 +59,7 @@ func (s *Storage) IconByExternalID(externalIconID string) (*model.Icon, error) { if err == sql.ErrNoRows { return nil, nil } else if err != nil { - return nil, fmt.Errorf("store: unable to fetch icon #%s: %w", externalIconID, err) + return nil, fmt.Errorf("store: unable to fetch icon %s: %w", externalIconID, err) } return &icon, nil diff --git a/internal/template/templates/common/feed_list.html b/internal/template/templates/common/feed_list.html index e76cf624..3da5da9c 100644 --- a/internal/template/templates/common/feed_list.html +++ b/internal/template/templates/common/feed_list.html @@ -10,7 +10,7 @@

{{ if and (.Icon) (gt .Icon.IconID 0) }} - + {{ end }} {{ if .Disabled }} 🚫 {{ end }} {{ .Title }} diff --git a/internal/template/templates/views/bookmark_entries.html b/internal/template/templates/views/bookmark_entries.html index a5657b43..2626515e 100644 --- a/internal/template/templates/views/bookmark_entries.html +++ b/internal/template/templates/views/bookmark_entries.html @@ -29,7 +29,7 @@

{{ if ne .Feed.Icon.IconID 0 }} - + {{ end }} {{ .Title }} diff --git a/internal/template/templates/views/category_entries.html b/internal/template/templates/views/category_entries.html index 19007f3e..9ce211f1 100644 --- a/internal/template/templates/views/category_entries.html +++ b/internal/template/templates/views/category_entries.html @@ -102,7 +102,7 @@ {{ end }} > {{ if ne .Feed.Icon.IconID 0 }} - + {{ end }} {{ .Title }} diff --git a/internal/template/templates/views/entry.html b/internal/template/templates/views/entry.html index 0d507a28..abb5d4ab 100644 --- a/internal/template/templates/views/entry.html +++ b/internal/template/templates/views/entry.html @@ -108,8 +108,8 @@ {{ end }}