1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

feat(googlereader): avoid SQL query to fetch username in streamItemContentsHandler

This commit is contained in:
Frédéric Guillot 2025-05-04 20:31:12 -07:00
parent 8d821dfc3b
commit 56adf22e32
4 changed files with 20 additions and 14 deletions

View file

@ -672,6 +672,7 @@ func (h *handler) editSubscriptionHandler(w http.ResponseWriter, r *http.Request
func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Request) { func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Request) {
userID := request.UserID(r) userID := request.UserID(r)
userName := request.UserName(r)
clientIP := request.ClientIP(r) clientIP := request.ClientIP(r)
slog.Debug("[GoogleReader] Handle /stream/items/contents", slog.Debug("[GoogleReader] Handle /stream/items/contents",
@ -691,11 +692,6 @@ func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Reque
json.ServerError(w, r, err) json.ServerError(w, r, err)
return return
} }
var user *model.User
if user, err = h.store.UserByID(userID); err != nil {
json.ServerError(w, r, err)
return
}
requestModifiers, err := parseStreamFilterFromRequest(r) requestModifiers, err := parseStreamFilterFromRequest(r)
if err != nil { if err != nil {
@ -743,7 +739,7 @@ func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Reque
HREF: config.Opts.RootURL() + route.Path(h.router, "StreamItemsContents"), HREF: config.Opts.RootURL() + route.Path(h.router, "StreamItemsContents"),
}, },
}, },
Author: user.Username, Author: userName,
} }
contentItems := make([]contentItem, len(entries)) contentItems := make([]contentItem, len(entries))
for i, entry := range entries { for i, entry := range entries {

View file

@ -177,6 +177,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
ctx := r.Context() ctx := r.Context()
ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID) ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
ctx = context.WithValue(ctx, request.UserNameContextKey, user.Username)
ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone) ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin) ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true) ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)

View file

@ -68,14 +68,13 @@ type tagsResponse struct {
} }
type streamContentItems struct { type streamContentItems struct {
Direction string `json:"direction"` Direction string `json:"direction"`
ID string `json:"id"` ID string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Self []contentHREF `json:"self"` Self []contentHREF `json:"self"`
Alternate []contentHREFType `json:"alternate"` Updated int64 `json:"updated"`
Updated int64 `json:"updated"` Items []contentItem `json:"items"`
Items []contentItem `json:"items"` Author string `json:"author"`
Author string `json:"author"`
} }
type contentItem struct { type contentItem struct {

View file

@ -16,6 +16,7 @@ type ContextKey int
// List of context keys. // List of context keys.
const ( const (
UserIDContextKey ContextKey = iota UserIDContextKey ContextKey = iota
UserNameContextKey
UserTimezoneContextKey UserTimezoneContextKey
IsAdminUserContextKey IsAdminUserContextKey
IsAuthenticatedContextKey IsAuthenticatedContextKey
@ -64,6 +65,15 @@ func UserID(r *http.Request) int64 {
return getContextInt64Value(r, UserIDContextKey) return getContextInt64Value(r, UserIDContextKey)
} }
// UserName returns the username of the logged user.
func UserName(r *http.Request) string {
value := getContextStringValue(r, UserNameContextKey)
if value == "" {
value = "unknown"
}
return value
}
// UserTimezone returns the timezone used by the logged user. // UserTimezone returns the timezone used by the logged user.
func UserTimezone(r *http.Request) string { func UserTimezone(r *http.Request) string {
value := getContextStringValue(r, UserTimezoneContextKey) value := getContextStringValue(r, UserTimezoneContextKey)