diff --git a/api/category.go b/api/category.go index 5a578652..e74aa3b5 100644 --- a/api/category.go +++ b/api/category.go @@ -8,7 +8,6 @@ import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" ) @@ -21,8 +20,7 @@ func (c *Controller) CreateCategory(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) category.UserID = userID if err := category.ValidateCategoryCreation(); err != nil { json.BadRequest(w, err) @@ -57,8 +55,7 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - category.UserID = ctx.UserID() + category.UserID = request.UserID(r) category.ID = categoryID if err := category.ValidateCategoryModification(); err != nil { json.BadRequest(w, err) @@ -76,8 +73,7 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) { // GetCategories is the API handler to get a list of categories for a given user. func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - categories, err := c.store.Categories(ctx.UserID()) + categories, err := c.store.Categories(request.UserID(r)) if err != nil { json.ServerError(w, err) return @@ -88,8 +84,7 @@ func (c *Controller) GetCategories(w http.ResponseWriter, r *http.Request) { // RemoveCategory is the API handler to remove a category. func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) categoryID, err := request.IntParam(r, "categoryID") if err != nil { json.BadRequest(w, err) diff --git a/api/entry.go b/api/entry.go index 37917c78..7f87888f 100644 --- a/api/entry.go +++ b/api/entry.go @@ -9,7 +9,6 @@ import ( "net/http" "time" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/model" @@ -30,10 +29,7 @@ func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() - - builder := c.store.NewEntryQueryBuilder(userID) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithFeedID(feedID) builder.WithEntryID(entryID) @@ -59,7 +55,7 @@ func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) { return } - builder := c.store.NewEntryQueryBuilder(context.New(r).UserID()) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithEntryID(entryID) entry, err := builder.GetEntry() @@ -111,7 +107,7 @@ func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) { return } - builder := c.store.NewEntryQueryBuilder(context.New(r).UserID()) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithFeedID(feedID) builder.WithStatus(status) builder.WithOrder(order) @@ -164,7 +160,7 @@ func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) { return } - builder := c.store.NewEntryQueryBuilder(context.New(r).UserID()) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithStatus(status) builder.WithOrder(order) builder.WithDirection(direction) @@ -200,7 +196,7 @@ func (c *Controller) SetEntryStatus(w http.ResponseWriter, r *http.Request) { return } - if err := c.store.SetEntriesStatus(context.New(r).UserID(), entryIDs, status); err != nil { + if err := c.store.SetEntriesStatus(request.UserID(r), entryIDs, status); err != nil { json.ServerError(w, err) return } @@ -216,7 +212,7 @@ func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) { return } - if err := c.store.ToggleBookmark(context.New(r).UserID(), entryID); err != nil { + if err := c.store.ToggleBookmark(request.UserID(r), entryID); err != nil { json.ServerError(w, err) return } diff --git a/api/feed.go b/api/feed.go index a295451b..e303f580 100644 --- a/api/feed.go +++ b/api/feed.go @@ -8,7 +8,6 @@ import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" ) @@ -31,8 +30,7 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) if c.store.FeedURLExists(userID, feedInfo.FeedURL) { json.BadRequest(w, errors.New("This feed_url already exists")) @@ -72,8 +70,7 @@ func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) if !c.store.FeedExists(userID, feedID) { json.NotFound(w, errors.New("Unable to find this feed")) @@ -103,8 +100,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) originalFeed, err := c.store.FeedByID(userID, feedID) if err != nil { @@ -140,7 +136,7 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { // GetFeeds is the API handler that get all feeds that belongs to the given user. func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) { - feeds, err := c.store.Feeds(context.New(r).UserID()) + feeds, err := c.store.Feeds(request.UserID(r)) if err != nil { json.ServerError(w, err) return @@ -157,7 +153,7 @@ func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) { return } - feed, err := c.store.FeedByID(context.New(r).UserID(), feedID) + feed, err := c.store.FeedByID(request.UserID(r), feedID) if err != nil { json.ServerError(w, err) return @@ -179,8 +175,7 @@ func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) if !c.store.FeedExists(userID, feedID) { json.NotFound(w, errors.New("Feed not found")) diff --git a/api/icon.go b/api/icon.go index 0ad1081c..f9c29641 100644 --- a/api/icon.go +++ b/api/icon.go @@ -8,7 +8,6 @@ import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" ) @@ -26,7 +25,7 @@ func (c *Controller) FeedIcon(w http.ResponseWriter, r *http.Request) { return } - icon, err := c.store.IconByFeedID(context.New(r).UserID(), feedID) + icon, err := c.store.IconByFeedID(request.UserID(r), feedID) if err != nil { json.ServerError(w, err) return diff --git a/api/opml.go b/api/opml.go index 8554dc1d..eb214f32 100644 --- a/api/opml.go +++ b/api/opml.go @@ -7,7 +7,7 @@ package api // import "miniflux.app/api" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/http/response/xml" "miniflux.app/reader/opml" @@ -16,7 +16,7 @@ import ( // Export is the API handler that export feeds to OPML. func (c *Controller) Export(w http.ResponseWriter, r *http.Request) { opmlHandler := opml.NewHandler(c.store) - opml, err := opmlHandler.Export(context.New(r).UserID()) + opml, err := opmlHandler.Export(request.UserID(r)) if err != nil { json.ServerError(w, err) return @@ -28,7 +28,7 @@ func (c *Controller) Export(w http.ResponseWriter, r *http.Request) { // Import is the API handler that import an OPML file. func (c *Controller) Import(w http.ResponseWriter, r *http.Request) { opmlHandler := opml.NewHandler(c.store) - err := opmlHandler.Import(context.New(r).UserID(), r.Body) + err := opmlHandler.Import(request.UserID(r), r.Body) defer r.Body.Close() if err != nil { json.ServerError(w, err) diff --git a/api/user.go b/api/user.go index 5ad056ea..167fd72f 100644 --- a/api/user.go +++ b/api/user.go @@ -8,15 +8,13 @@ import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" ) // CurrentUser is the API handler to retrieve the authenticated user. func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { json.ServerError(w, err) return @@ -27,8 +25,7 @@ func (c *Controller) CurrentUser(w http.ResponseWriter, r *http.Request) { // CreateUser is the API handler to create a new user. func (c *Controller) CreateUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } @@ -61,8 +58,7 @@ func (c *Controller) CreateUser(w http.ResponseWriter, r *http.Request) { // UpdateUser is the API handler to update the given user. func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } @@ -106,8 +102,7 @@ func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) { // Users is the API handler to get the list of users. func (c *Controller) Users(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } @@ -118,14 +113,13 @@ func (c *Controller) Users(w http.ResponseWriter, r *http.Request) { return } - users.UseTimezone(ctx.UserTimezone()) + users.UseTimezone(request.UserTimezone(r)) json.OK(w, r, users) } // UserByID is the API handler to fetch the given user by the ID. func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } @@ -147,14 +141,13 @@ func (c *Controller) UserByID(w http.ResponseWriter, r *http.Request) { return } - user.UseTimezone(ctx.UserTimezone()) + user.UseTimezone(request.UserTimezone(r)) json.OK(w, r, user) } // UserByUsername is the API handler to fetch the given user by the username. func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } @@ -176,8 +169,7 @@ func (c *Controller) UserByUsername(w http.ResponseWriter, r *http.Request) { // RemoveUser is the API handler to remove an existing user. func (c *Controller) RemoveUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if !ctx.IsAdminUser() { + if !request.IsAdminUser(r) { json.Forbidden(w) return } diff --git a/fever/fever.go b/fever/fever.go index 2f17fd2e..e1090b7f 100644 --- a/fever/fever.go +++ b/fever/fever.go @@ -11,7 +11,6 @@ import ( "time" "miniflux.app/config" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/integration" @@ -180,8 +179,7 @@ is_spark equal to 1. */ func (c *Controller) handleGroups(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching groups for userID=%d", userID) categories, err := c.store.Categories(userID) @@ -231,8 +229,7 @@ should be limited to feeds with an is_spark equal to 0. For the “Sparks” super group the items should be limited to feeds with an is_spark equal to 1. */ func (c *Controller) handleFeeds(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching feeds for userID=%d", userID) feeds, err := c.store.Feeds(userID) @@ -285,8 +282,7 @@ A PHP/HTML example: echo ''; */ func (c *Controller) handleFavicons(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching favicons for userID=%d", userID) icons, err := c.store.Icons(userID) @@ -341,8 +337,7 @@ Three optional arguments control determine the items included in the response. func (c *Controller) handleItems(w http.ResponseWriter, r *http.Request) { var result itemsResponse - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching items for userID=%d", userID) builder := c.store.NewEntryQueryBuilder(userID) @@ -425,8 +420,7 @@ A request with the unread_item_ids argument will return one additional member: unread_item_ids (string/comma-separated list of positive integers) */ func (c *Controller) handleUnreadItems(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching unread items for userID=%d", userID) builder := c.store.NewEntryQueryBuilder(userID) @@ -457,8 +451,7 @@ with the remote Fever installation. saved_item_ids (string/comma-separated list of positive integers) */ func (c *Controller) handleSavedItems(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Fetching saved items for userID=%d", userID) builder := c.store.NewEntryQueryBuilder(userID) @@ -486,8 +479,7 @@ func (c *Controller) handleSavedItems(w http.ResponseWriter, r *http.Request) { id=? where ? is replaced with the id of the item to modify */ func (c *Controller) handleWriteItems(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Receiving mark=item call for userID=%d", userID) entryID := request.FormInt64Value(r, "id") @@ -544,8 +536,7 @@ func (c *Controller) handleWriteItems(w http.ResponseWriter, r *http.Request) { before=? where ? is replaced with the Unix timestamp of the the local client’s most recent items API request */ func (c *Controller) handleWriteFeeds(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Receiving mark=feed call for userID=%d", userID) feedID := request.FormInt64Value(r, "id") @@ -585,8 +576,7 @@ func (c *Controller) handleWriteFeeds(w http.ResponseWriter, r *http.Request) { before=? where ? is replaced with the Unix timestamp of the the local client’s most recent items API request */ func (c *Controller) handleWriteGroups(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - userID := ctx.UserID() + userID := request.UserID(r) logger.Debug("[Fever] Receiving mark=group call for userID=%d", userID) groupID := request.FormInt64Value(r, "id") diff --git a/http/context/context.go b/http/context/context.go deleted file mode 100644 index 7a434b5f..00000000 --- a/http/context/context.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2018 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package context // import "miniflux.app/http/context" - -import ( - "net/http" - - "miniflux.app/middleware" -) - -// Context contains helper functions related to the current request. -type Context struct { - request *http.Request -} - -// IsAdminUser checks if the logged user is administrator. -func (c *Context) IsAdminUser() bool { - return c.getContextBoolValue(middleware.IsAdminUserContextKey) -} - -// IsAuthenticated returns a boolean if the user is authenticated. -func (c *Context) IsAuthenticated() bool { - return c.getContextBoolValue(middleware.IsAuthenticatedContextKey) -} - -// UserID returns the UserID of the logged user. -func (c *Context) UserID() int64 { - return c.getContextIntValue(middleware.UserIDContextKey) -} - -// UserTimezone returns the timezone used by the logged user. -func (c *Context) UserTimezone() string { - value := c.getContextStringValue(middleware.UserTimezoneContextKey) - if value == "" { - value = "UTC" - } - return value -} - -// UserLanguage get the locale used by the current logged user. -func (c *Context) UserLanguage() string { - language := c.getContextStringValue(middleware.UserLanguageContextKey) - if language == "" { - language = "en_US" - } - return language -} - -// UserTheme get the theme used by the current logged user. -func (c *Context) UserTheme() string { - theme := c.getContextStringValue(middleware.UserThemeContextKey) - if theme == "" { - theme = "default" - } - return theme -} - -// CSRF returns the current CSRF token. -func (c *Context) CSRF() string { - return c.getContextStringValue(middleware.CSRFContextKey) -} - -// SessionID returns the current session ID. -func (c *Context) SessionID() string { - return c.getContextStringValue(middleware.SessionIDContextKey) -} - -// UserSessionToken returns the current user session token. -func (c *Context) UserSessionToken() string { - return c.getContextStringValue(middleware.UserSessionTokenContextKey) -} - -// OAuth2State returns the current OAuth2 state. -func (c *Context) OAuth2State() string { - return c.getContextStringValue(middleware.OAuth2StateContextKey) -} - -// FlashMessage returns the message message if any. -func (c *Context) FlashMessage() string { - return c.getContextStringValue(middleware.FlashMessageContextKey) -} - -// FlashErrorMessage returns the message error message if any. -func (c *Context) FlashErrorMessage() string { - return c.getContextStringValue(middleware.FlashErrorMessageContextKey) -} - -// PocketRequestToken returns the Pocket Request Token if any. -func (c *Context) PocketRequestToken() string { - return c.getContextStringValue(middleware.PocketRequestTokenContextKey) -} - -func (c *Context) getContextStringValue(key middleware.ContextKey) string { - if v := c.request.Context().Value(key); v != nil { - return v.(string) - } - - return "" -} - -func (c *Context) getContextBoolValue(key middleware.ContextKey) bool { - if v := c.request.Context().Value(key); v != nil { - return v.(bool) - } - - return false -} - -func (c *Context) getContextIntValue(key middleware.ContextKey) int64 { - if v := c.request.Context().Value(key); v != nil { - return v.(int64) - } - - return 0 -} - -// New creates a new Context. -func New(r *http.Request) *Context { - return &Context{r} -} diff --git a/http/request/context.go b/http/request/context.go new file mode 100644 index 00000000..78014e41 --- /dev/null +++ b/http/request/context.go @@ -0,0 +1,128 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package request // import "miniflux.app/http/request" + +import "net/http" + +// ContextKey represents a context key. +type ContextKey int + +// List of context keys. +const ( + UserIDContextKey ContextKey = iota + UserTimezoneContextKey + IsAdminUserContextKey + IsAuthenticatedContextKey + UserSessionTokenContextKey + UserLanguageContextKey + UserThemeContextKey + SessionIDContextKey + CSRFContextKey + OAuth2StateContextKey + FlashMessageContextKey + FlashErrorMessageContextKey + PocketRequestTokenContextKey +) + +// IsAdminUser checks if the logged user is administrator. +func IsAdminUser(r *http.Request) bool { + return getContextBoolValue(r, IsAdminUserContextKey) +} + +// IsAuthenticated returns a boolean if the user is authenticated. +func IsAuthenticated(r *http.Request) bool { + return getContextBoolValue(r, IsAuthenticatedContextKey) +} + +// UserID returns the UserID of the logged user. +func UserID(r *http.Request) int64 { + return getContextInt64Value(r, UserIDContextKey) +} + +// UserTimezone returns the timezone used by the logged user. +func UserTimezone(r *http.Request) string { + value := getContextStringValue(r, UserTimezoneContextKey) + if value == "" { + value = "UTC" + } + return value +} + +// UserLanguage get the locale used by the current logged user. +func UserLanguage(r *http.Request) string { + language := getContextStringValue(r, UserLanguageContextKey) + if language == "" { + language = "en_US" + } + return language +} + +// UserTheme get the theme used by the current logged user. +func UserTheme(r *http.Request) string { + theme := getContextStringValue(r, UserThemeContextKey) + if theme == "" { + theme = "default" + } + return theme +} + +// CSRF returns the current CSRF token. +func CSRF(r *http.Request) string { + return getContextStringValue(r, CSRFContextKey) +} + +// SessionID returns the current session ID. +func SessionID(r *http.Request) string { + return getContextStringValue(r, SessionIDContextKey) +} + +// UserSessionToken returns the current user session token. +func UserSessionToken(r *http.Request) string { + return getContextStringValue(r, UserSessionTokenContextKey) +} + +// OAuth2State returns the current OAuth2 state. +func OAuth2State(r *http.Request) string { + return getContextStringValue(r, OAuth2StateContextKey) +} + +// FlashMessage returns the message message if any. +func FlashMessage(r *http.Request) string { + return getContextStringValue(r, FlashMessageContextKey) +} + +// FlashErrorMessage returns the message error message if any. +func FlashErrorMessage(r *http.Request) string { + return getContextStringValue(r, FlashErrorMessageContextKey) +} + +// PocketRequestToken returns the Pocket Request Token if any. +func PocketRequestToken(r *http.Request) string { + return getContextStringValue(r, PocketRequestTokenContextKey) +} + +func getContextStringValue(r *http.Request, key ContextKey) string { + if v := r.Context().Value(key); v != nil { + return v.(string) + } + + return "" +} + +func getContextBoolValue(r *http.Request, key ContextKey) bool { + if v := r.Context().Value(key); v != nil { + return v.(bool) + } + + return false +} + +func getContextInt64Value(r *http.Request, key ContextKey) int64 { + if v := r.Context().Value(key); v != nil { + return v.(int64) + } + + return 0 +} diff --git a/http/request/doc.go b/http/request/doc.go new file mode 100644 index 00000000..1fcaaa63 --- /dev/null +++ b/http/request/doc.go @@ -0,0 +1,10 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the MIT license +// that can be found in the LICENSE file. + +/* + +Package request holds helper functions regarding the HTTP request. + +*/ +package request // import "miniflux.app/http/request" diff --git a/middleware/app_session.go b/middleware/app_session.go index 8b3284b5..7f2b2144 100644 --- a/middleware/app_session.go +++ b/middleware/app_session.go @@ -49,14 +49,14 @@ func (m *Middleware) AppSession(next http.Handler) http.Handler { } ctx := r.Context() - ctx = context.WithValue(ctx, SessionIDContextKey, session.ID) - ctx = context.WithValue(ctx, CSRFContextKey, session.Data.CSRF) - ctx = context.WithValue(ctx, OAuth2StateContextKey, session.Data.OAuth2State) - ctx = context.WithValue(ctx, FlashMessageContextKey, session.Data.FlashMessage) - ctx = context.WithValue(ctx, FlashErrorMessageContextKey, session.Data.FlashErrorMessage) - ctx = context.WithValue(ctx, UserLanguageContextKey, session.Data.Language) - ctx = context.WithValue(ctx, UserThemeContextKey, session.Data.Theme) - ctx = context.WithValue(ctx, PocketRequestTokenContextKey, session.Data.PocketRequestToken) + ctx = context.WithValue(ctx, request.SessionIDContextKey, session.ID) + ctx = context.WithValue(ctx, request.CSRFContextKey, session.Data.CSRF) + ctx = context.WithValue(ctx, request.OAuth2StateContextKey, session.Data.OAuth2State) + ctx = context.WithValue(ctx, request.FlashMessageContextKey, session.Data.FlashMessage) + ctx = context.WithValue(ctx, request.FlashErrorMessageContextKey, session.Data.FlashErrorMessage) + ctx = context.WithValue(ctx, request.UserLanguageContextKey, session.Data.Language) + ctx = context.WithValue(ctx, request.UserThemeContextKey, session.Data.Theme) + ctx = context.WithValue(ctx, request.PocketRequestTokenContextKey, session.Data.PocketRequestToken) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/middleware/basic_auth.go b/middleware/basic_auth.go index 9c3d8daa..bd8402ad 100644 --- a/middleware/basic_auth.go +++ b/middleware/basic_auth.go @@ -50,10 +50,10 @@ func (m *Middleware) BasicAuth(next http.Handler) http.Handler { m.store.SetLastLogin(user.ID) ctx := r.Context() - ctx = context.WithValue(ctx, UserIDContextKey, user.ID) - ctx = context.WithValue(ctx, UserTimezoneContextKey, user.Timezone) - ctx = context.WithValue(ctx, IsAdminUserContextKey, user.IsAdmin) - ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true) + ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID) + ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone) + ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin) + ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true) next.ServeHTTP(w, r.WithContext(ctx)) }) diff --git a/middleware/context_keys.go b/middleware/context_keys.go deleted file mode 100644 index 77a16551..00000000 --- a/middleware/context_keys.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package middleware // import "miniflux.app/middleware" - -// ContextKey represents a context key. -type ContextKey int - -// List of context keys. -const ( - UserIDContextKey ContextKey = iota - UserTimezoneContextKey - IsAdminUserContextKey - IsAuthenticatedContextKey - UserSessionTokenContextKey - UserLanguageContextKey - UserThemeContextKey - SessionIDContextKey - CSRFContextKey - OAuth2StateContextKey - FlashMessageContextKey - FlashErrorMessageContextKey - PocketRequestTokenContextKey -) diff --git a/middleware/doc.go b/middleware/doc.go new file mode 100644 index 00000000..8e768e54 --- /dev/null +++ b/middleware/doc.go @@ -0,0 +1,10 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the MIT license +// that can be found in the LICENSE file. + +/* + +Package middleware contains application HTTP middlewares. + +*/ +package middleware // import "miniflux.app/middleware" diff --git a/middleware/fever.go b/middleware/fever.go index 3fdbcfdd..9addf7ee 100644 --- a/middleware/fever.go +++ b/middleware/fever.go @@ -8,6 +8,7 @@ import ( "context" "net/http" + "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/logger" ) @@ -34,10 +35,10 @@ func (m *Middleware) FeverAuth(next http.Handler) http.Handler { m.store.SetLastLogin(user.ID) ctx := r.Context() - ctx = context.WithValue(ctx, UserIDContextKey, user.ID) - ctx = context.WithValue(ctx, UserTimezoneContextKey, user.Timezone) - ctx = context.WithValue(ctx, IsAdminUserContextKey, user.IsAdmin) - ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true) + ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID) + ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone) + ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin) + ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true) next.ServeHTTP(w, r.WithContext(ctx)) }) diff --git a/middleware/user_session.go b/middleware/user_session.go index 764cbdbc..66f0ad07 100644 --- a/middleware/user_session.go +++ b/middleware/user_session.go @@ -34,9 +34,9 @@ func (m *Middleware) UserSession(next http.Handler) http.Handler { logger.Debug("[Middleware:UserSession] %s", session) ctx := r.Context() - ctx = context.WithValue(ctx, UserIDContextKey, session.UserID) - ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true) - ctx = context.WithValue(ctx, UserSessionTokenContextKey, session.Token) + ctx = context.WithValue(ctx, request.UserIDContextKey, session.UserID) + ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true) + ctx = context.WithValue(ctx, request.UserSessionTokenContextKey, session.Token) next.ServeHTTP(w, r.WithContext(ctx)) } diff --git a/ui/about.go b/ui/about.go index 18155323..e11ab521 100644 --- a/ui/about.go +++ b/ui/about.go @@ -7,8 +7,8 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/ui/session" "miniflux.app/ui/view" "miniflux.app/version" @@ -16,16 +16,14 @@ import ( // About shows the about page. func (c *Controller) About(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("version", version.Version) view.Set("build_date", version.BuildDate) view.Set("menu", "settings") diff --git a/ui/bookmark_entries.go b/ui/bookmark_entries.go index 09b9bf6a..08385f68 100644 --- a/ui/bookmark_entries.go +++ b/ui/bookmark_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,9 +17,7 @@ import ( // ShowStarredPage renders the page with all starred entries. func (c *Controller) ShowStarredPage(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -47,8 +44,8 @@ func (c *Controller) ShowStarredPage(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("total", count) view.Set("entries", entries) diff --git a/ui/category_create.go b/ui/category_create.go index 8f7019ff..20d280a4 100644 --- a/ui/category_create.go +++ b/ui/category_create.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/session" "miniflux.app/ui/view" @@ -15,16 +15,14 @@ import ( // CreateCategory shows the form to create a new category. func (c *Controller) CreateCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("menu", "categories") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) diff --git a/ui/category_edit.go b/ui/category_edit.go index 6651a0a8..0ab4b1bd 100644 --- a/ui/category_edit.go +++ b/ui/category_edit.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" @@ -17,11 +16,10 @@ import ( // EditCategory shows the form to modify a category. func (c *Controller) EditCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -33,7 +31,7 @@ func (c *Controller) EditCategory(w http.ResponseWriter, r *http.Request) { return } - category, err := c.store.Category(ctx.UserID(), categoryID) + category, err := c.store.Category(request.UserID(r), categoryID) if err != nil { html.ServerError(w, err) return diff --git a/ui/category_entries.go b/ui/category_entries.go index cee4fc92..ac39fa18 100644 --- a/ui/category_entries.go +++ b/ui/category_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,9 +17,7 @@ import ( // CategoryEntries shows all entries for the given category. func (c *Controller) CategoryEntries(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -32,7 +29,7 @@ func (c *Controller) CategoryEntries(w http.ResponseWriter, r *http.Request) { return } - category, err := c.store.Category(ctx.UserID(), categoryID) + category, err := c.store.Category(request.UserID(r), categoryID) if err != nil { html.ServerError(w, err) return @@ -64,8 +61,8 @@ func (c *Controller) CategoryEntries(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("category", category) view.Set("total", count) view.Set("entries", entries) diff --git a/ui/category_list.go b/ui/category_list.go index 4ec97952..3dc17cad 100644 --- a/ui/category_list.go +++ b/ui/category_list.go @@ -7,17 +7,15 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response/html" "miniflux.app/ui/session" + "miniflux.app/http/request" "miniflux.app/ui/view" ) // CategoryList shows the page with all categories. func (c *Controller) CategoryList(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -29,8 +27,8 @@ func (c *Controller) CategoryList(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("categories", categories) view.Set("total", len(categories)) view.Set("menu", "categories") diff --git a/ui/category_remove.go b/ui/category_remove.go index 6cdb0e45..033d9e37 100644 --- a/ui/category_remove.go +++ b/ui/category_remove.go @@ -2,12 +2,11 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -16,9 +15,7 @@ import ( // RemoveCategory deletes a category from the database. func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -30,7 +27,7 @@ func (c *Controller) RemoveCategory(w http.ResponseWriter, r *http.Request) { return } - category, err := c.store.Category(ctx.UserID(), categoryID) + category, err := c.store.Category(request.UserID(r), categoryID) if err != nil { html.ServerError(w, err) return diff --git a/ui/category_save.go b/ui/category_save.go index 9a1dcf54..2ae43838 100644 --- a/ui/category_save.go +++ b/ui/category_save.go @@ -7,10 +7,10 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" "miniflux.app/http/response/html" "miniflux.app/http/route" + "miniflux.app/http/request" "miniflux.app/logger" "miniflux.app/model" "miniflux.app/ui/form" @@ -20,9 +20,7 @@ import ( // SaveCategory validate and save the new category into the database. func (c *Controller) SaveCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -30,8 +28,8 @@ func (c *Controller) SaveCategory(w http.ResponseWriter, r *http.Request) { categoryForm := form.NewCategoryForm(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("form", categoryForm) view.Set("menu", "categories") view.Set("user", user) diff --git a/ui/category_update.go b/ui/category_update.go index 892ad138..0553d979 100644 --- a/ui/category_update.go +++ b/ui/category_update.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -20,9 +19,7 @@ import ( // UpdateCategory validates and updates a category. func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -34,7 +31,7 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) { return } - category, err := c.store.Category(ctx.UserID(), categoryID) + category, err := c.store.Category(request.UserID(r), categoryID) if err != nil { html.ServerError(w, err) return @@ -47,8 +44,8 @@ func (c *Controller) UpdateCategory(w http.ResponseWriter, r *http.Request) { categoryForm := form.NewCategoryForm(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("form", categoryForm) view.Set("category", category) view.Set("menu", "categories") diff --git a/ui/controller.go b/ui/controller.go index 5b1bbafe..005b8634 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -5,13 +5,14 @@ package ui // import "miniflux.app/ui" import ( - "github.com/gorilla/mux" "miniflux.app/config" "miniflux.app/locale" "miniflux.app/reader/feed" "miniflux.app/scheduler" "miniflux.app/storage" "miniflux.app/template" + + "github.com/gorilla/mux" ) // Controller contains all HTTP handlers for the user interface. diff --git a/ui/entry_bookmark.go b/ui/entry_bookmark.go index 9a469e1e..e6d2f0ec 100644 --- a/ui/entry_bookmark.go +++ b/ui/entry_bookmark.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -20,9 +19,7 @@ import ( // ShowStarredEntry shows a single feed entry in "starred" mode. func (c *Controller) ShowStarredEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -78,8 +75,8 @@ func (c *Controller) ShowStarredEntry(w http.ResponseWriter, r *http.Request) { prevEntryRoute = route.Path(c.router, "starredEntry", "entryID", prevEntry.ID) } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entry", entry) view.Set("prevEntry", prevEntry) view.Set("nextEntry", nextEntry) diff --git a/ui/entry_category.go b/ui/entry_category.go index 41b729f1..795aaf11 100644 --- a/ui/entry_category.go +++ b/ui/entry_category.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -20,9 +19,7 @@ import ( // ShowCategoryEntry shows a single feed entry in "category" mode. func (c *Controller) ShowCategoryEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -85,8 +82,8 @@ func (c *Controller) ShowCategoryEntry(w http.ResponseWriter, r *http.Request) { prevEntryRoute = route.Path(c.router, "categoryEntry", "categoryID", categoryID, "entryID", prevEntry.ID) } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entry", entry) view.Set("prevEntry", prevEntry) view.Set("nextEntry", nextEntry) diff --git a/ui/entry_feed.go b/ui/entry_feed.go index be04e149..968c8f4c 100644 --- a/ui/entry_feed.go +++ b/ui/entry_feed.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -20,9 +19,7 @@ import ( // ShowFeedEntry shows a single feed entry in "feed" mode. func (c *Controller) ShowFeedEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -85,8 +82,8 @@ func (c *Controller) ShowFeedEntry(w http.ResponseWriter, r *http.Request) { prevEntryRoute = route.Path(c.router, "feedEntry", "feedID", feedID, "entryID", prevEntry.ID) } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entry", entry) view.Set("prevEntry", prevEntry) view.Set("nextEntry", nextEntry) diff --git a/ui/entry_read.go b/ui/entry_read.go index 3309c814..61c71144 100644 --- a/ui/entry_read.go +++ b/ui/entry_read.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -19,9 +18,7 @@ import ( // ShowReadEntry shows a single feed entry in "history" mode. func (c *Controller) ShowReadEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -66,8 +63,8 @@ func (c *Controller) ShowReadEntry(w http.ResponseWriter, r *http.Request) { prevEntryRoute = route.Path(c.router, "readEntry", "entryID", prevEntry.ID) } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entry", entry) view.Set("prevEntry", prevEntry) view.Set("nextEntry", nextEntry) diff --git a/ui/entry_save.go b/ui/entry_save.go index 292b5268..488022ab 100644 --- a/ui/entry_save.go +++ b/ui/entry_save.go @@ -2,13 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/integration" @@ -23,9 +22,7 @@ func (c *Controller) SaveEntry(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - - builder := c.store.NewEntryQueryBuilder(ctx.UserID()) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithEntryID(entryID) builder.WithoutStatus(model.EntryStatusRemoved) @@ -40,7 +37,7 @@ func (c *Controller) SaveEntry(w http.ResponseWriter, r *http.Request) { return } - settings, err := c.store.Integration(ctx.UserID()) + settings, err := c.store.Integration(request.UserID(r)) if err != nil { json.ServerError(w, err) return diff --git a/ui/entry_scraper.go b/ui/entry_scraper.go index 52e08467..00823078 100644 --- a/ui/entry_scraper.go +++ b/ui/entry_scraper.go @@ -8,7 +8,6 @@ import ( "errors" "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/model" @@ -24,8 +23,7 @@ func (c *Controller) FetchContent(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - builder := c.store.NewEntryQueryBuilder(ctx.UserID()) + builder := c.store.NewEntryQueryBuilder(request.UserID(r)) builder.WithEntryID(entryID) builder.WithoutStatus(model.EntryStatusRemoved) diff --git a/ui/entry_search.go b/ui/entry_search.go index 7e09bbf9..15babc16 100644 --- a/ui/entry_search.go +++ b/ui/entry_search.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -20,9 +19,7 @@ import ( // ShowSearchEntry shows a single entry in "search" mode. func (c *Controller) ShowSearchEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -80,8 +77,8 @@ func (c *Controller) ShowSearchEntry(w http.ResponseWriter, r *http.Request) { prevEntryRoute = route.Path(c.router, "searchEntry", "entryID", prevEntry.ID) } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("searchQuery", searchQuery) view.Set("entry", entry) view.Set("prevEntry", prevEntry) diff --git a/ui/entry_toggle_bookmark.go b/ui/entry_toggle_bookmark.go index 13de9719..e8e87ca6 100644 --- a/ui/entry_toggle_bookmark.go +++ b/ui/entry_toggle_bookmark.go @@ -2,12 +2,11 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/logger" @@ -21,8 +20,7 @@ func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - if err := c.store.ToggleBookmark(ctx.UserID(), entryID); err != nil { + if err := c.store.ToggleBookmark(request.UserID(r), entryID); err != nil { logger.Error("[Controller:ToggleBookmark] %v", err) json.ServerError(w, nil) return diff --git a/ui/entry_unread.go b/ui/entry_unread.go index 0a8c076a..30a6d34b 100644 --- a/ui/entry_unread.go +++ b/ui/entry_unread.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -20,9 +19,7 @@ import ( // ShowUnreadEntry shows a single feed entry in "unread" mode. func (c *Controller) ShowUnreadEntry(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -86,8 +83,8 @@ func (c *Controller) ShowUnreadEntry(w http.ResponseWriter, r *http.Request) { } entry.Status = model.EntryStatusRead - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entry", entry) view.Set("prevEntry", prevEntry) view.Set("nextEntry", nextEntry) diff --git a/ui/entry_update_status.go b/ui/entry_update_status.go index 4f5c114e..8e9de4de 100644 --- a/ui/entry_update_status.go +++ b/ui/entry_update_status.go @@ -2,13 +2,13 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "errors" "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/logger" ) @@ -27,8 +27,7 @@ func (c *Controller) UpdateEntriesStatus(w http.ResponseWriter, r *http.Request) return } - ctx := context.New(r) - err = c.store.SetEntriesStatus(ctx.UserID(), entryIDs, status) + err = c.store.SetEntriesStatus(request.UserID(r), entryIDs, status) if err != nil { logger.Error("[Controller:UpdateEntryStatus] %v", err) json.ServerError(w, nil) diff --git a/ui/feed_edit.go b/ui/feed_edit.go index 213bfea2..c78ca5e8 100644 --- a/ui/feed_edit.go +++ b/ui/feed_edit.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" @@ -17,9 +16,7 @@ import ( // EditFeed shows the form to modify a subscription. func (c *Controller) EditFeed(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -60,8 +57,8 @@ func (c *Controller) EditFeed(w http.ResponseWriter, r *http.Request) { Password: feed.Password, } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("form", feedForm) view.Set("categories", categories) view.Set("feed", feed) diff --git a/ui/feed_entries.go b/ui/feed_entries.go index fa18b263..dc0f05d0 100644 --- a/ui/feed_entries.go +++ b/ui/feed_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,9 +17,7 @@ import ( // ShowFeedEntries shows all entries for the given feed. func (c *Controller) ShowFeedEntries(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -64,8 +61,8 @@ func (c *Controller) ShowFeedEntries(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("feed", feed) view.Set("entries", entries) view.Set("total", count) diff --git a/ui/feed_list.go b/ui/feed_list.go index 553ee55e..9fe4aac6 100644 --- a/ui/feed_list.go +++ b/ui/feed_list.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/session" "miniflux.app/ui/view" @@ -15,9 +15,7 @@ import ( // ShowFeedsPage shows the page with all subscriptions. func (c *Controller) ShowFeedsPage(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -29,8 +27,8 @@ func (c *Controller) ShowFeedsPage(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("feeds", feeds) view.Set("total", len(feeds)) view.Set("menu", "feeds") diff --git a/ui/feed_refresh.go b/ui/feed_refresh.go index 634edfad..d9da2383 100644 --- a/ui/feed_refresh.go +++ b/ui/feed_refresh.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -23,8 +22,7 @@ func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - if err := c.feedHandler.RefreshFeed(ctx.UserID(), feedID); err != nil { + if err := c.feedHandler.RefreshFeed(request.UserID(r), feedID); err != nil { logger.Error("[Controller:RefreshFeed] %v", err) } @@ -33,7 +31,7 @@ func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) { // RefreshAllFeeds refresh all feeds in the background for the current user. func (c *Controller) RefreshAllFeeds(w http.ResponseWriter, r *http.Request) { - userID := context.New(r).UserID() + userID := request.UserID(r) jobs, err := c.store.NewUserBatch(userID, c.store.CountFeeds(userID)) if err != nil { html.ServerError(w, err) diff --git a/ui/feed_remove.go b/ui/feed_remove.go index 4a7294e2..9071a331 100644 --- a/ui/feed_remove.go +++ b/ui/feed_remove.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -22,8 +21,7 @@ func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - if err := c.store.RemoveFeed(ctx.UserID(), feedID); err != nil { + if err := c.store.RemoveFeed(request.UserID(r), feedID); err != nil { html.ServerError(w, err) return } diff --git a/ui/feed_update.go b/ui/feed_update.go index 474f7e94..fc3a9a28 100644 --- a/ui/feed_update.go +++ b/ui/feed_update.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -20,9 +19,7 @@ import ( // UpdateFeed update a subscription and redirect to the feed entries page. func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -53,8 +50,8 @@ func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) { feedForm := form.NewFeedForm(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("form", feedForm) view.Set("categories", categories) view.Set("feed", feed) diff --git a/ui/history_entries.go b/ui/history_entries.go index de12539d..25310a9d 100644 --- a/ui/history_entries.go +++ b/ui/history_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,9 +17,7 @@ import ( // ShowHistoryPage renders the page with all read entries. func (c *Controller) ShowHistoryPage(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -46,8 +43,8 @@ func (c *Controller) ShowHistoryPage(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("entries", entries) view.Set("total", count) view.Set("pagination", c.getPagination(route.Path(c.router, "history"), count, offset)) diff --git a/ui/history_flush.go b/ui/history_flush.go index 0776f9c8..5bb8910e 100644 --- a/ui/history_flush.go +++ b/ui/history_flush.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -15,7 +15,7 @@ import ( // FlushHistory changes all "read" items to "removed". func (c *Controller) FlushHistory(w http.ResponseWriter, r *http.Request) { - err := c.store.FlushHistory(context.New(r).UserID()) + err := c.store.FlushHistory(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/integration_pocket.go b/ui/integration_pocket.go index 62d3ddc8..cce83b39 100644 --- a/ui/integration_pocket.go +++ b/ui/integration_pocket.go @@ -7,8 +7,8 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" "miniflux.app/integration/pocket" @@ -18,9 +18,7 @@ import ( // PocketAuthorize redirects the end-user to Pocket website to authorize the application. func (c *Controller) PocketAuthorize(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -32,13 +30,13 @@ func (c *Controller) PocketAuthorize(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) + sess := session.New(c.store, request.SessionID(r)) connector := pocket.NewConnector(c.cfg.PocketConsumerKey(integration.PocketConsumerKey)) redirectURL := c.cfg.BaseURL() + route.Path(c.router, "pocketCallback") requestToken, err := connector.RequestToken(redirectURL) if err != nil { logger.Error("[Pocket:Authorize] %v", err) - sess.NewFlashErrorMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Unable to fetch request token from Pocket!")) + sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Unable to fetch request token from Pocket!")) response.Redirect(w, r, route.Path(c.router, "integrations")) return } @@ -49,10 +47,9 @@ func (c *Controller) PocketAuthorize(w http.ResponseWriter, r *http.Request) { // PocketCallback saves the personal access token after the authorization step. func (c *Controller) PocketCallback(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) + sess := session.New(c.store, request.SessionID(r)) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -65,10 +62,10 @@ func (c *Controller) PocketCallback(w http.ResponseWriter, r *http.Request) { } connector := pocket.NewConnector(c.cfg.PocketConsumerKey(integration.PocketConsumerKey)) - accessToken, err := connector.AccessToken(ctx.PocketRequestToken()) + accessToken, err := connector.AccessToken(request.PocketRequestToken(r)) if err != nil { logger.Error("[Pocket:Callback] %v", err) - sess.NewFlashErrorMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Unable to fetch access token from Pocket!")) + sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Unable to fetch access token from Pocket!")) response.Redirect(w, r, route.Path(c.router, "integrations")) return } @@ -82,6 +79,6 @@ func (c *Controller) PocketCallback(w http.ResponseWriter, r *http.Request) { return } - sess.NewFlashMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Your Pocket account is now linked!")) + sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Your Pocket account is now linked!")) response.Redirect(w, r, route.Path(c.router, "integrations")) } diff --git a/ui/integration_show.go b/ui/integration_show.go index 0ae3eee9..1c8adce1 100644 --- a/ui/integration_show.go +++ b/ui/integration_show.go @@ -7,7 +7,7 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" "miniflux.app/ui/session" @@ -16,9 +16,7 @@ import ( // ShowIntegrations renders the page with all external integrations. func (c *Controller) ShowIntegrations(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -55,8 +53,8 @@ func (c *Controller) ShowIntegrations(w http.ResponseWriter, r *http.Request) { PocketConsumerKey: integration.PocketConsumerKey, } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("form", integrationForm) view.Set("menu", "settings") view.Set("user", user) diff --git a/ui/integration_update.go b/ui/integration_update.go index 22c90001..e6d4a678 100644 --- a/ui/integration_update.go +++ b/ui/integration_update.go @@ -9,8 +9,8 @@ import ( "fmt" "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" "miniflux.app/ui/form" @@ -19,10 +19,8 @@ import ( // UpdateIntegration updates integration settings. func (c *Controller) UpdateIntegration(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - - user, err := c.store.UserByID(ctx.UserID()) + sess := session.New(c.store, request.SessionID(r)) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -38,7 +36,7 @@ func (c *Controller) UpdateIntegration(w http.ResponseWriter, r *http.Request) { integrationForm.Merge(integration) if integration.FeverUsername != "" && c.store.HasDuplicateFeverUsername(user.ID, integration.FeverUsername) { - sess.NewFlashErrorMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("There is already someone else with the same Fever username!")) + sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("There is already someone else with the same Fever username!")) response.Redirect(w, r, route.Path(c.router, "integrations")) return } @@ -55,6 +53,6 @@ func (c *Controller) UpdateIntegration(w http.ResponseWriter, r *http.Request) { return } - sess.NewFlashMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Preferences saved!")) + sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Preferences saved!")) response.Redirect(w, r, route.Path(c.router, "integrations")) } diff --git a/ui/login_check.go b/ui/login_check.go index a2f28268..95c5908d 100644 --- a/ui/login_check.go +++ b/ui/login_check.go @@ -3,7 +3,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/cookie" "miniflux.app/http/request" "miniflux.app/http/response" @@ -18,13 +17,10 @@ import ( // CheckLogin validates the username/password and redirects the user to the unread page. func (c *Controller) CheckLogin(w http.ResponseWriter, r *http.Request) { remoteAddr := request.RealIP(r) - - ctx := context.New(r) - sess := session.New(c.store, ctx) - + sess := session.New(c.store, request.SessionID(r)) authForm := form.NewAuthForm(r) - view := view.New(c.tpl, ctx, sess) + view := view.New(c.tpl, r, sess) view.Set("errorMessage", "Invalid username or password.") view.Set("form", authForm) diff --git a/ui/login_show.go b/ui/login_show.go index d963df50..82c19ec9 100644 --- a/ui/login_show.go +++ b/ui/login_show.go @@ -7,8 +7,8 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" "miniflux.app/ui/session" @@ -17,13 +17,12 @@ import ( // ShowLoginPage shows the login form. func (c *Controller) ShowLoginPage(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - if ctx.IsAuthenticated() { + if request.IsAuthenticated(r) { response.Redirect(w, r, route.Path(c.router, "unread")) return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) html.OK(w, r, view.Render("login")) } diff --git a/ui/logout.go b/ui/logout.go index e610282f..0c8e9e7b 100644 --- a/ui/logout.go +++ b/ui/logout.go @@ -7,8 +7,8 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/cookie" + "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,10 +18,8 @@ import ( // Logout destroy the session and redirects the user to the login page. func (c *Controller) Logout(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - - user, err := c.store.UserByID(ctx.UserID()) + sess := session.New(c.store, request.SessionID(r)) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -30,7 +28,7 @@ func (c *Controller) Logout(w http.ResponseWriter, r *http.Request) { sess.SetLanguage(user.Language) sess.SetTheme(user.Theme) - if err := c.store.RemoveUserSessionByToken(user.ID, ctx.UserSessionToken()); err != nil { + if err := c.store.RemoveUserSessionByToken(user.ID, request.UserSessionToken(r)); err != nil { logger.Error("[Controller:Logout] %v", err) } diff --git a/ui/oauth2_callback.go b/ui/oauth2_callback.go index 4e0cd748..b1bb9332 100644 --- a/ui/oauth2_callback.go +++ b/ui/oauth2_callback.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/cookie" "miniflux.app/http/request" "miniflux.app/http/response" @@ -20,8 +19,7 @@ import ( // OAuth2Callback receives the authorization code and create a new session. func (c *Controller) OAuth2Callback(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) + sess := session.New(c.store, request.SessionID(r)) provider := request.Param(r, "provider", "") if provider == "" { @@ -38,8 +36,8 @@ func (c *Controller) OAuth2Callback(w http.ResponseWriter, r *http.Request) { } state := request.QueryParam(r, "state", "") - if state == "" || state != ctx.OAuth2State() { - logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, ctx.OAuth2State()) + if state == "" || state != request.OAuth2State(r) { + logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, request.OAuth2State(r)) response.Redirect(w, r, route.Path(c.router, "login")) return } @@ -58,7 +56,7 @@ func (c *Controller) OAuth2Callback(w http.ResponseWriter, r *http.Request) { return } - if ctx.IsAuthenticated() { + if request.IsAuthenticated(r) { user, err := c.store.UserByExtraField(profile.Key, profile.ID) if err != nil { html.ServerError(w, err) @@ -66,18 +64,18 @@ func (c *Controller) OAuth2Callback(w http.ResponseWriter, r *http.Request) { } if user != nil { - logger.Error("[OAuth2] User #%d cannot be associated because %s is already associated", ctx.UserID(), user.Username) - sess.NewFlashErrorMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("There is already someone associated with this provider!")) + logger.Error("[OAuth2] User #%d cannot be associated because %s is already associated", request.UserID(r), user.Username) + sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("There is already someone associated with this provider!")) response.Redirect(w, r, route.Path(c.router, "settings")) return } - if err := c.store.UpdateExtraField(ctx.UserID(), profile.Key, profile.ID); err != nil { + if err := c.store.UpdateExtraField(request.UserID(r), profile.Key, profile.ID); err != nil { html.ServerError(w, err) return } - sess.NewFlashMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Your external account is now linked!")) + sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Your external account is now linked!")) response.Redirect(w, r, route.Path(c.router, "settings")) return } diff --git a/ui/oauth2_redirect.go b/ui/oauth2_redirect.go index 08f065d7..3b7c88aa 100644 --- a/ui/oauth2_redirect.go +++ b/ui/oauth2_redirect.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/route" @@ -17,8 +16,7 @@ import ( // OAuth2Redirect redirects the user to the consent page to ask for permission. func (c *Controller) OAuth2Redirect(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) + sess := session.New(c.store, request.SessionID(r)) provider := request.Param(r, "provider", "") if provider == "" { diff --git a/ui/oauth2_unlink.go b/ui/oauth2_unlink.go index e0dbd16e..bf76f050 100644 --- a/ui/oauth2_unlink.go +++ b/ui/oauth2_unlink.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -32,26 +31,25 @@ func (c *Controller) OAuth2Unlink(w http.ResponseWriter, r *http.Request) { return } - ctx := context.New(r) - sess := session.New(c.store, ctx) + sess := session.New(c.store, request.SessionID(r)) - hasPassword, err := c.store.HasPassword(ctx.UserID()) + hasPassword, err := c.store.HasPassword(request.UserID(r)) if err != nil { html.ServerError(w, err) return } if !hasPassword { - sess.NewFlashErrorMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("You must define a password otherwise you won't be able to login again.")) + sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("You must define a password otherwise you won't be able to login again.")) response.Redirect(w, r, route.Path(c.router, "settings")) return } - if err := c.store.RemoveExtraField(ctx.UserID(), authProvider.GetUserExtraKey()); err != nil { + if err := c.store.RemoveExtraField(request.UserID(r), authProvider.GetUserExtraKey()); err != nil { html.ServerError(w, err) return } - sess.NewFlashMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Your external account is now dissociated!")) + sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Your external account is now dissociated!")) response.Redirect(w, r, route.Path(c.router, "settings")) } diff --git a/ui/opml_export.go b/ui/opml_export.go index 561c6c3b..20194b0c 100644 --- a/ui/opml_export.go +++ b/ui/opml_export.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/response/xml" "miniflux.app/reader/opml" @@ -15,8 +15,7 @@ import ( // Export generates the OPML file. func (c *Controller) Export(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - opml, err := opml.NewHandler(c.store).Export(ctx.UserID()) + opml, err := opml.NewHandler(c.store).Export(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/opml_import.go b/ui/opml_import.go index 6e0f7469..a8e732ed 100644 --- a/ui/opml_import.go +++ b/ui/opml_import.go @@ -7,7 +7,7 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/session" "miniflux.app/ui/view" @@ -15,16 +15,14 @@ import ( // Import shows the import form. func (c *Controller) Import(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("menu", "feeds") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) diff --git a/ui/opml_upload.go b/ui/opml_upload.go index 414c7e38..86150fd7 100644 --- a/ui/opml_upload.go +++ b/ui/opml_upload.go @@ -7,7 +7,7 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -19,9 +19,7 @@ import ( // UploadOPML handles OPML file importation. func (c *Controller) UploadOPML(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -42,8 +40,8 @@ func (c *Controller) UploadOPML(w http.ResponseWriter, r *http.Request) { fileHeader.Size, ) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("menu", "feeds") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) diff --git a/ui/search_entries.go b/ui/search_entries.go index e6b533ed..3f17ede1 100644 --- a/ui/search_entries.go +++ b/ui/search_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,9 +17,7 @@ import ( // ShowSearchEntries shows all entries for the given feed. func (c *Controller) ShowSearchEntries(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -48,8 +45,8 @@ func (c *Controller) ShowSearchEntries(w http.ResponseWriter, r *http.Request) { return } - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) pagination := c.getPagination(route.Path(c.router, "searchEntries"), count, offset) pagination.SearchQuery = searchQuery diff --git a/ui/session/session.go b/ui/session/session.go index f6a35c28..23518664 100644 --- a/ui/session/session.go +++ b/ui/session/session.go @@ -6,67 +6,64 @@ package session // import "miniflux.app/ui/session" import ( "miniflux.app/crypto" - "miniflux.app/http/context" "miniflux.app/storage" ) // Session handles session data. type Session struct { - store *storage.Storage - ctx *context.Context + store *storage.Storage + sessionID string } // NewOAuth2State generates a new OAuth2 state and stores the value into the database. func (s *Session) NewOAuth2State() string { state := crypto.GenerateRandomString(32) - s.store.UpdateSessionField(s.ctx.SessionID(), "oauth2_state", state) + s.store.UpdateSessionField(s.sessionID, "oauth2_state", state) return state } // NewFlashMessage creates a new flash message. func (s *Session) NewFlashMessage(message string) { - s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", message) + s.store.UpdateSessionField(s.sessionID, "flash_message", message) } // FlashMessage returns the current flash message if any. -func (s *Session) FlashMessage() string { - message := s.ctx.FlashMessage() +func (s *Session) FlashMessage(message string) string { if message != "" { - s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", "") + s.store.UpdateSessionField(s.sessionID, "flash_message", "") } return message } // NewFlashErrorMessage creates a new flash error message. func (s *Session) NewFlashErrorMessage(message string) { - s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", message) + s.store.UpdateSessionField(s.sessionID, "flash_error_message", message) } // FlashErrorMessage returns the last flash error message if any. -func (s *Session) FlashErrorMessage() string { - message := s.ctx.FlashErrorMessage() +func (s *Session) FlashErrorMessage(message string) string { if message != "" { - s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", "") + s.store.UpdateSessionField(s.sessionID, "flash_error_message", "") } return message } // SetLanguage updates the language field in session. func (s *Session) SetLanguage(language string) { - s.store.UpdateSessionField(s.ctx.SessionID(), "language", language) + s.store.UpdateSessionField(s.sessionID, "language", language) } // SetTheme updates the theme field in session. func (s *Session) SetTheme(theme string) { - s.store.UpdateSessionField(s.ctx.SessionID(), "theme", theme) + s.store.UpdateSessionField(s.sessionID, "theme", theme) } // SetPocketRequestToken updates Pocket Request Token. func (s *Session) SetPocketRequestToken(requestToken string) { - s.store.UpdateSessionField(s.ctx.SessionID(), "pocket_request_token", requestToken) + s.store.UpdateSessionField(s.sessionID, "pocket_request_token", requestToken) } // New returns a new session handler. -func New(store *storage.Storage, ctx *context.Context) *Session { - return &Session{store, ctx} +func New(store *storage.Storage, sessionID string) *Session { + return &Session{store, sessionID} } diff --git a/ui/session_list.go b/ui/session_list.go index 3cb8d944..3f5bf9e0 100644 --- a/ui/session_list.go +++ b/ui/session_list.go @@ -7,20 +7,18 @@ package ui // import "miniflux.app/ui" import ( "net/http" + "miniflux.app/http/request" "miniflux.app/ui/session" "miniflux.app/ui/view" - - "miniflux.app/http/context" "miniflux.app/http/response/html" ) // ShowSessions shows the list of active user sessions. func (c *Controller) ShowSessions(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -34,7 +32,7 @@ func (c *Controller) ShowSessions(w http.ResponseWriter, r *http.Request) { sessions.UseTimezone(user.Timezone) - view.Set("currentSessionToken", ctx.UserSessionToken()) + view.Set("currentSessionToken", request.UserSessionToken(r)) view.Set("sessions", sessions) view.Set("menu", "settings") view.Set("user", user) diff --git a/ui/session_remove.go b/ui/session_remove.go index 6bec368b..f08cd3d6 100644 --- a/ui/session_remove.go +++ b/ui/session_remove.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -17,15 +16,13 @@ import ( // RemoveSession remove a user session. func (c *Controller) RemoveSession(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sessionID, err := request.IntParam(r, "sessionID") if err != nil { html.BadRequest(w, err) return } - err = c.store.RemoveUserSessionByID(ctx.UserID(), sessionID) + err = c.store.RemoveUserSessionByID(request.UserID(r), sessionID) if err != nil { logger.Error("[Controller:RemoveSession] %v", err) } diff --git a/ui/settings_show.go b/ui/settings_show.go index e079c650..07703eb1 100644 --- a/ui/settings_show.go +++ b/ui/settings_show.go @@ -7,7 +7,7 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/locale" "miniflux.app/model" @@ -18,11 +18,10 @@ import ( // ShowSettings shows the settings page. func (c *Controller) ShowSettings(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/settings_update.go b/ui/settings_update.go index 564afe25..e57f3b05 100644 --- a/ui/settings_update.go +++ b/ui/settings_update.go @@ -7,9 +7,9 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/http/route" "miniflux.app/locale" "miniflux.app/logger" @@ -21,11 +21,10 @@ import ( // UpdateSettings update the settings. func (c *Controller) UpdateSettings(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -70,6 +69,6 @@ func (c *Controller) UpdateSettings(w http.ResponseWriter, r *http.Request) { sess.SetLanguage(user.Language) sess.SetTheme(user.Theme) - sess.NewFlashMessage(c.translator.GetLanguage(ctx.UserLanguage()).Get("Preferences saved!")) + sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("Preferences saved!")) response.Redirect(w, r, route.Path(c.router, "settings")) } diff --git a/ui/static_manifest.go b/ui/static_manifest.go index f8f6ac01..51369a2e 100644 --- a/ui/static_manifest.go +++ b/ui/static_manifest.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/json" "miniflux.app/http/route" "miniflux.app/model" @@ -32,9 +32,7 @@ func (c *Controller) WebManifest(w http.ResponseWriter, r *http.Request) { BackgroundColor string `json:"background_color"` } - ctx := context.New(r) - themeColor := model.ThemeColor(ctx.UserTheme()) - + themeColor := model.ThemeColor(request.UserTheme(r)) manifest := &webManifest{ Name: "Miniflux", ShortName: "Miniflux", diff --git a/ui/subscription_add.go b/ui/subscription_add.go index f78c7c69..1639d0d2 100644 --- a/ui/subscription_add.go +++ b/ui/subscription_add.go @@ -7,19 +7,18 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/ui/session" "miniflux.app/ui/view" ) // AddSubscription shows the form to add a new feed. func (c *Controller) AddSubscription(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/subscription_bookmarklet.go b/ui/subscription_bookmarklet.go index 889ae42a..dd0d11f3 100644 --- a/ui/subscription_bookmarklet.go +++ b/ui/subscription_bookmarklet.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" @@ -17,11 +16,10 @@ import ( // Bookmarklet prefill the form to add a subscription from the URL provided by the bookmarklet. func (c *Controller) Bookmarklet(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/subscription_choose.go b/ui/subscription_choose.go index 0deec546..f1cdfcbb 100644 --- a/ui/subscription_choose.go +++ b/ui/subscription_choose.go @@ -7,9 +7,9 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/http/route" "miniflux.app/ui/form" "miniflux.app/ui/session" @@ -18,11 +18,10 @@ import ( // ChooseSubscription shows a page to choose a subscription. func (c *Controller) ChooseSubscription(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/subscription_submit.go b/ui/subscription_submit.go index fe615131..c6e8a950 100644 --- a/ui/subscription_submit.go +++ b/ui/subscription_submit.go @@ -7,9 +7,9 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/http/route" "miniflux.app/logger" "miniflux.app/reader/subscription" @@ -20,11 +20,10 @@ import ( // SubmitSubscription try to find a feed from the URL provided by the user. func (c *Controller) SubmitSubscription(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - v := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + v := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -89,7 +88,7 @@ func (c *Controller) SubmitSubscription(w http.ResponseWriter, r *http.Request) response.Redirect(w, r, route.Path(c.router, "feedEntries", "feedID", feed.ID)) case n > 1: - v := view.New(c.tpl, ctx, sess) + v := view.New(c.tpl, r, sess) v.Set("subscriptions", subscriptions) v.Set("form", subscriptionForm) v.Set("menu", "feeds") diff --git a/ui/unread_entries.go b/ui/unread_entries.go index 77f153d3..5992162d 100644 --- a/ui/unread_entries.go +++ b/ui/unread_entries.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/http/route" @@ -18,11 +17,10 @@ import ( // ShowUnreadPage render the page with all unread entries. func (c *Controller) ShowUnreadPage(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/unread_mark_all_read.go b/ui/unread_mark_all_read.go index 7d475239..c4679235 100644 --- a/ui/unread_mark_all_read.go +++ b/ui/unread_mark_all_read.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/route" "miniflux.app/logger" @@ -15,7 +15,7 @@ import ( // MarkAllAsRead marks all unread entries as read. func (c *Controller) MarkAllAsRead(w http.ResponseWriter, r *http.Request) { - if err := c.store.MarkAllAsRead(context.New(r).UserID()); err != nil { + if err := c.store.MarkAllAsRead(request.UserID(r)); err != nil { logger.Error("[MarkAllAsRead] %v", err) } diff --git a/ui/user_create.go b/ui/user_create.go index 07cd8195..b31b0de2 100644 --- a/ui/user_create.go +++ b/ui/user_create.go @@ -2,12 +2,12 @@ // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package ui // import "miniflux.app/ui" +package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" + "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" "miniflux.app/ui/session" @@ -16,11 +16,10 @@ import ( // CreateUser shows the user creation form. func (c *Controller) CreateUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/user_edit.go b/ui/user_edit.go index 00f1acbd..79e6d4a5 100644 --- a/ui/user_edit.go +++ b/ui/user_edit.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response/html" "miniflux.app/ui/form" @@ -17,11 +16,10 @@ import ( // EditUser shows the form to edit a user. func (c *Controller) EditUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/user_list.go b/ui/user_list.go index 040ac7c1..dce30e0d 100644 --- a/ui/user_list.go +++ b/ui/user_list.go @@ -7,19 +7,18 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/ui/session" "miniflux.app/ui/view" ) // ShowUsers renders the list of users. func (c *Controller) ShowUsers(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/user_remove.go b/ui/user_remove.go index 63d2c46a..4b5440d4 100644 --- a/ui/user_remove.go +++ b/ui/user_remove.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -16,9 +15,7 @@ import ( // RemoveUser deletes a user from the database. func (c *Controller) RemoveUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return diff --git a/ui/user_save.go b/ui/user_save.go index a9167d2f..a871dab3 100644 --- a/ui/user_save.go +++ b/ui/user_save.go @@ -7,9 +7,9 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/response" "miniflux.app/http/response/html" + "miniflux.app/http/request" "miniflux.app/http/route" "miniflux.app/logger" "miniflux.app/ui/form" @@ -19,9 +19,7 @@ import ( // SaveUser validate and save the new user into the database. func (c *Controller) SaveUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -34,8 +32,8 @@ func (c *Controller) SaveUser(w http.ResponseWriter, r *http.Request) { userForm := form.NewUserForm(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("menu", "settings") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) diff --git a/ui/user_update.go b/ui/user_update.go index 68811679..4882b47c 100644 --- a/ui/user_update.go +++ b/ui/user_update.go @@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui" import ( "net/http" - "miniflux.app/http/context" "miniflux.app/http/request" "miniflux.app/http/response" "miniflux.app/http/response/html" @@ -20,9 +19,7 @@ import ( // UpdateUser validate and update a user. func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) { - ctx := context.New(r) - - user, err := c.store.UserByID(ctx.UserID()) + user, err := c.store.UserByID(request.UserID(r)) if err != nil { html.ServerError(w, err) return @@ -52,8 +49,8 @@ func (c *Controller) UpdateUser(w http.ResponseWriter, r *http.Request) { userForm := form.NewUserForm(r) - sess := session.New(c.store, ctx) - view := view.New(c.tpl, ctx, sess) + sess := session.New(c.store, request.SessionID(r)) + view := view.New(c.tpl, r, sess) view.Set("menu", "settings") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) diff --git a/ui/view/view.go b/ui/view/view.go index 03517562..a54961aa 100644 --- a/ui/view/view.go +++ b/ui/view/view.go @@ -5,7 +5,9 @@ package view // import "miniflux.app/ui/view" import ( - "miniflux.app/http/context" + "net/http" + + "miniflux.app/http/request" "miniflux.app/template" "miniflux.app/ui/session" "miniflux.app/ui/static" @@ -14,7 +16,7 @@ import ( // View wraps template argument building. type View struct { tpl *template.Engine - ctx *context.Context + r *http.Request params map[string]interface{} } @@ -26,17 +28,17 @@ func (v *View) Set(param string, value interface{}) *View { // Render executes the template with arguments. func (v *View) Render(template string) []byte { - return v.tpl.Render(template, v.ctx.UserLanguage(), v.params) + return v.tpl.Render(template, request.UserLanguage(v.r), v.params) } // New returns a new view with default parameters. -func New(tpl *template.Engine, ctx *context.Context, sess *session.Session) *View { - b := &View{tpl, ctx, make(map[string]interface{})} - theme := ctx.UserTheme() +func New(tpl *template.Engine, r *http.Request, sess *session.Session) *View { + b := &View{tpl, r, make(map[string]interface{})} + theme := request.UserTheme(r) b.params["menu"] = "" - b.params["csrf"] = ctx.CSRF() - b.params["flashMessage"] = sess.FlashMessage() - b.params["flashErrorMessage"] = sess.FlashErrorMessage() + b.params["csrf"] = request.CSRF(r) + b.params["flashMessage"] = sess.FlashMessage(request.FlashMessage(r)) + b.params["flashErrorMessage"] = sess.FlashErrorMessage(request.FlashErrorMessage(r)) b.params["theme"] = theme b.params["theme_checksum"] = static.StylesheetsChecksums[theme] b.params["app_js_checksum"] = static.JavascriptsChecksums["app"]