ソースを参照

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

Frédéric Guillot 11 ヶ月 前
コミット
3de9629a49

+ 2 - 6
internal/googlereader/handler.go

@@ -672,6 +672,7 @@ func (h *handler) editSubscriptionHandler(w http.ResponseWriter, r *http.Request
 
 func (h *handler) streamItemContentsHandler(w http.ResponseWriter, r *http.Request) {
 	userID := request.UserID(r)
+	userName := request.UserName(r)
 	clientIP := request.ClientIP(r)
 
 	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)
 		return
 	}
-	var user *model.User
-	if user, err = h.store.UserByID(userID); err != nil {
-		json.ServerError(w, r, err)
-		return
-	}
 
 	requestModifiers, err := parseStreamFilterFromRequest(r)
 	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"),
 			},
 		},
-		Author: user.Username,
+		Author: userName,
 	}
 	contentItems := make([]contentItem, len(entries))
 	for i, entry := range entries {

+ 1 - 0
internal/googlereader/middleware.go

@@ -177,6 +177,7 @@ func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
 
 		ctx := r.Context()
 		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.IsAdminUserContextKey, user.IsAdmin)
 		ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)

+ 7 - 8
internal/googlereader/response.go

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

+ 10 - 0
internal/http/request/context.go

@@ -16,6 +16,7 @@ type ContextKey int
 // List of context keys.
 const (
 	UserIDContextKey ContextKey = iota
+	UserNameContextKey
 	UserTimezoneContextKey
 	IsAdminUserContextKey
 	IsAuthenticatedContextKey
@@ -64,6 +65,15 @@ func UserID(r *http.Request) int64 {
 	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.
 func UserTimezone(r *http.Request) string {
 	value := getContextStringValue(r, UserTimezoneContextKey)