From 8d80e9103f836284b73bc8d176c5ca63c31b0dfc Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 4 Mar 2024 00:32:16 +0100 Subject: [PATCH] Delay call of `view.New` after logging the user in There is no need to do extra work like creating a session and its associated view until the user has been properly identified and as many possibly-failing sql request have been successfully run. --- internal/ui/api_key_create.go | 5 ++--- internal/ui/api_key_list.go | 5 ++--- internal/ui/bookmark_entries.go | 1 - internal/ui/category_edit.go | 5 ++--- internal/ui/session_list.go | 5 ++--- internal/ui/settings_show.go | 5 ++--- internal/ui/settings_update.go | 5 ++--- internal/ui/subscription_add.go | 5 ++--- internal/ui/subscription_bookmarklet.go | 5 ++--- internal/ui/subscription_choose.go | 5 ++--- internal/ui/subscription_submit.go | 23 +++++++++++------------ internal/ui/unread_entries.go | 6 ++---- internal/ui/user_create.go | 5 ++--- internal/ui/user_edit.go | 5 ++--- internal/ui/user_list.go | 5 ++--- 15 files changed, 37 insertions(+), 53 deletions(-) diff --git a/internal/ui/api_key_create.go b/internal/ui/api_key_create.go index 05665037..9b1376ac 100644 --- a/internal/ui/api_key_create.go +++ b/internal/ui/api_key_create.go @@ -14,15 +14,14 @@ import ( ) func (h *handler) showCreateAPIKeyPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", &form.APIKeyForm{}) view.Set("menu", "settings") view.Set("user", user) diff --git a/internal/ui/api_key_list.go b/internal/ui/api_key_list.go index 14879196..fafb8628 100644 --- a/internal/ui/api_key_list.go +++ b/internal/ui/api_key_list.go @@ -13,9 +13,6 @@ import ( ) func (h *handler) showAPIKeysPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -28,6 +25,8 @@ func (h *handler) showAPIKeysPage(w http.ResponseWriter, r *http.Request) { return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("apiKeys", apiKeys) view.Set("menu", "settings") view.Set("user", user) diff --git a/internal/ui/bookmark_entries.go b/internal/ui/bookmark_entries.go index 049ab1ef..8e31b4fd 100644 --- a/internal/ui/bookmark_entries.go +++ b/internal/ui/bookmark_entries.go @@ -43,7 +43,6 @@ func (h *handler) showStarredPage(w http.ResponseWriter, r *http.Request) { sess := session.New(h.store, request.SessionID(r)) view := view.New(h.tpl, r, sess) - view.Set("total", count) view.Set("entries", entries) view.Set("pagination", getPagination(route.Path(h.router, "starred"), count, offset, user.EntriesPerPage)) diff --git a/internal/ui/category_edit.go b/internal/ui/category_edit.go index fdc98b59..2edc8e13 100644 --- a/internal/ui/category_edit.go +++ b/internal/ui/category_edit.go @@ -14,9 +14,6 @@ import ( ) func (h *handler) showEditCategoryPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -43,6 +40,8 @@ func (h *handler) showEditCategoryPage(w http.ResponseWriter, r *http.Request) { categoryForm.HideGlobally = "checked" } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", categoryForm) view.Set("category", category) view.Set("menu", "categories") diff --git a/internal/ui/session_list.go b/internal/ui/session_list.go index 2dfcf7cc..a945cbbe 100644 --- a/internal/ui/session_list.go +++ b/internal/ui/session_list.go @@ -13,9 +13,6 @@ import ( ) func (h *handler) showSessionsPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -30,6 +27,8 @@ func (h *handler) showSessionsPage(w http.ResponseWriter, r *http.Request) { sessions.UseTimezone(user.Timezone) + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("currentSessionToken", request.UserSessionToken(r)) view.Set("sessions", sessions) view.Set("menu", "settings") diff --git a/internal/ui/settings_show.go b/internal/ui/settings_show.go index 96714271..23e6d401 100644 --- a/internal/ui/settings_show.go +++ b/internal/ui/settings_show.go @@ -16,9 +16,6 @@ import ( ) func (h *handler) showSettingsPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -58,6 +55,8 @@ func (h *handler) showSettingsPage(w http.ResponseWriter, r *http.Request) { return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", settingsForm) view.Set("themes", model.Themes()) view.Set("languages", locale.AvailableLanguages()) diff --git a/internal/ui/settings_update.go b/internal/ui/settings_update.go index d580e8d7..122ad441 100644 --- a/internal/ui/settings_update.go +++ b/internal/ui/settings_update.go @@ -18,9 +18,6 @@ import ( ) func (h *handler) updateSettings(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - loggedUser, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -35,6 +32,8 @@ func (h *handler) updateSettings(w http.ResponseWriter, r *http.Request) { settingsForm := form.NewSettingsForm(r) + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", settingsForm) view.Set("themes", model.Themes()) view.Set("languages", locale.AvailableLanguages()) diff --git a/internal/ui/subscription_add.go b/internal/ui/subscription_add.go index 6e115aa8..ca56a973 100644 --- a/internal/ui/subscription_add.go +++ b/internal/ui/subscription_add.go @@ -15,9 +15,6 @@ import ( ) func (h *handler) showAddSubscriptionPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -30,6 +27,8 @@ func (h *handler) showAddSubscriptionPage(w http.ResponseWriter, r *http.Request return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("categories", categories) view.Set("menu", "feeds") view.Set("user", user) diff --git a/internal/ui/subscription_bookmarklet.go b/internal/ui/subscription_bookmarklet.go index 120ea9e2..990e6428 100644 --- a/internal/ui/subscription_bookmarklet.go +++ b/internal/ui/subscription_bookmarklet.go @@ -17,9 +17,6 @@ import ( ) func (h *handler) bookmarklet(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -45,6 +42,8 @@ func (h *handler) bookmarklet(w http.ResponseWriter, r *http.Request) { bookmarkletURL = xurls.Relaxed().FindString(text) } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", form.SubscriptionForm{URL: bookmarkletURL}) view.Set("categories", categories) view.Set("menu", "feeds") diff --git a/internal/ui/subscription_choose.go b/internal/ui/subscription_choose.go index 6a6ae8a6..af355d34 100644 --- a/internal/ui/subscription_choose.go +++ b/internal/ui/subscription_choose.go @@ -18,9 +18,6 @@ import ( ) func (h *handler) showChooseSubscriptionPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -33,6 +30,8 @@ func (h *handler) showChooseSubscriptionPage(w http.ResponseWriter, r *http.Requ return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("categories", categories) view.Set("menu", "feeds") view.Set("user", user) diff --git a/internal/ui/subscription_submit.go b/internal/ui/subscription_submit.go index 6d5600e8..267e9d7c 100644 --- a/internal/ui/subscription_submit.go +++ b/internal/ui/subscription_submit.go @@ -21,9 +21,6 @@ import ( ) func (h *handler) submitSubscription(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - v := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -36,6 +33,8 @@ func (h *handler) submitSubscription(w http.ResponseWriter, r *http.Request) { return } + sess := session.New(h.store, request.SessionID(r)) + v := view.New(h.tpl, r, sess) v.Set("categories", categories) v.Set("menu", "feeds") v.Set("user", user) @@ -141,15 +140,15 @@ func (h *handler) submitSubscription(w http.ResponseWriter, r *http.Request) { html.Redirect(w, r, route.Path(h.router, "feedEntries", "feedID", feed.ID)) case n > 1: - v := view.New(h.tpl, r, sess) - v.Set("subscriptions", subscriptions) - v.Set("form", subscriptionForm) - v.Set("menu", "feeds") - v.Set("user", user) - v.Set("countUnread", h.store.CountUnreadEntries(user.ID)) - v.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID)) - v.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyConfigured()) + view := view.New(h.tpl, r, sess) + view.Set("subscriptions", subscriptions) + view.Set("form", subscriptionForm) + view.Set("menu", "feeds") + view.Set("user", user) + view.Set("countUnread", h.store.CountUnreadEntries(user.ID)) + view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID)) + view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyConfigured()) - html.OK(w, r, v.Render("choose_subscription")) + html.OK(w, r, view.Render("choose_subscription")) } } diff --git a/internal/ui/unread_entries.go b/internal/ui/unread_entries.go index 9a361ae1..c962f70e 100644 --- a/internal/ui/unread_entries.go +++ b/internal/ui/unread_entries.go @@ -19,10 +19,6 @@ import ( func (h *handler) showUnreadPage(w http.ResponseWriter, r *http.Request) { beginPreProcessing := time.Now() - - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -59,6 +55,8 @@ func (h *handler) showUnreadPage(w http.ResponseWriter, r *http.Request) { } finishSqlFetchUnreadEntries := time.Now() + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("entries", entries) view.Set("pagination", getPagination(route.Path(h.router, "unread"), countUnread, offset, user.EntriesPerPage)) view.Set("menu", "unread") diff --git a/internal/ui/user_create.go b/internal/ui/user_create.go index d35596a5..8e6bd6ef 100644 --- a/internal/ui/user_create.go +++ b/internal/ui/user_create.go @@ -14,9 +14,6 @@ import ( ) func (h *handler) showCreateUserPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -28,6 +25,8 @@ func (h *handler) showCreateUserPage(w http.ResponseWriter, r *http.Request) { return } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", &form.UserForm{}) view.Set("menu", "settings") view.Set("user", user) diff --git a/internal/ui/user_edit.go b/internal/ui/user_edit.go index 199feaa8..8631bbc2 100644 --- a/internal/ui/user_edit.go +++ b/internal/ui/user_edit.go @@ -15,9 +15,6 @@ import ( // EditUser shows the form to edit a user. func (h *handler) showEditUserPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -46,6 +43,8 @@ func (h *handler) showEditUserPage(w http.ResponseWriter, r *http.Request) { IsAdmin: selectedUser.IsAdmin, } + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("form", userForm) view.Set("selected_user", selectedUser) view.Set("menu", "settings") diff --git a/internal/ui/user_list.go b/internal/ui/user_list.go index d514708f..a698fae4 100644 --- a/internal/ui/user_list.go +++ b/internal/ui/user_list.go @@ -13,9 +13,6 @@ import ( ) func (h *handler) showUsersPage(w http.ResponseWriter, r *http.Request) { - sess := session.New(h.store, request.SessionID(r)) - view := view.New(h.tpl, r, sess) - user, err := h.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, r, err) @@ -35,6 +32,8 @@ func (h *handler) showUsersPage(w http.ResponseWriter, r *http.Request) { users.UseTimezone(user.Timezone) + sess := session.New(h.store, request.SessionID(r)) + view := view.New(h.tpl, r, sess) view.Set("users", users) view.Set("menu", "settings") view.Set("user", user)