From b270159aae97359afe02f3c4843fa0f4e9a1a1b6 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Mon, 21 May 2018 12:19:56 -0700 Subject: Offer the possibility to set Pocket Consumer Key as environment variable --- config/config.go | 5 +++++ daemon/routes.go | 2 +- fever/fever.go | 8 +++++--- integration/integration.go | 6 +++--- integration/pocket/pocket.go | 6 +++--- template/html/integrations.html | 6 ++++-- template/views.go | 10 ++++++---- ui/entry_save.go | 2 +- ui/integration_pocket.go | 4 ++-- ui/integration_show.go | 1 + 10 files changed, 31 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index bc4fc60..2e08434 100644 --- a/config/config.go +++ b/config/config.go @@ -197,6 +197,11 @@ func (c *Config) CreateAdmin() bool { return c.get("CREATE_ADMIN", "") != "" } +// PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable. +func (c *Config) PocketConsumerKey(defaultValue string) string { + return c.get("POCKET_CONSUMER_KEY", defaultValue) +} + // NewConfig returns a new Config. func NewConfig() *Config { return &Config{IsHTTPS: os.Getenv("HTTPS") != ""} diff --git a/daemon/routes.go b/daemon/routes.go index 49e8467..6f52437 100644 --- a/daemon/routes.go +++ b/daemon/routes.go @@ -25,7 +25,7 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle router := mux.NewRouter() templateEngine := template.NewEngine(cfg, router, translator) apiController := api.NewController(store, feedHandler) - feverController := fever.NewController(store) + feverController := fever.NewController(cfg, store) uiController := ui.NewController(cfg, store, pool, feedHandler, templateEngine, translator, router) middleware := middleware.New(cfg, store, router) diff --git a/fever/fever.go b/fever/fever.go index 5d0109d..7aa4885 100644 --- a/fever/fever.go +++ b/fever/fever.go @@ -10,6 +10,7 @@ import ( "strings" "time" + "github.com/miniflux/miniflux/config" "github.com/miniflux/miniflux/http/context" "github.com/miniflux/miniflux/http/request" "github.com/miniflux/miniflux/http/response/json" @@ -128,6 +129,7 @@ type favicon struct { // Controller implements the Fever API. type Controller struct { + cfg *config.Config store *storage.Storage } @@ -528,7 +530,7 @@ func (c *Controller) handleWriteItems(w http.ResponseWriter, r *http.Request) { } go func() { - integration.SendEntry(entry, settings) + integration.SendEntry(c.cfg, entry, settings) }() } @@ -642,6 +644,6 @@ func (c *Controller) buildFeedGroups(feeds model.Feeds) []feedsGroups { } // NewController returns a new Fever API. -func NewController(store *storage.Storage) *Controller { - return &Controller{store: store} +func NewController(cfg *config.Config, store *storage.Storage) *Controller { + return &Controller{cfg, store} } diff --git a/integration/integration.go b/integration/integration.go index d853773..af19939 100644 --- a/integration/integration.go +++ b/integration/integration.go @@ -5,6 +5,7 @@ package integration import ( + "github.com/miniflux/miniflux/config" "github.com/miniflux/miniflux/integration/instapaper" "github.com/miniflux/miniflux/integration/nunuxkeeper" "github.com/miniflux/miniflux/integration/pinboard" @@ -15,7 +16,7 @@ import ( ) // SendEntry send the entry to the activated providers. -func SendEntry(entry *model.Entry, integration *model.Integration) { +func SendEntry(cfg *config.Config, entry *model.Entry, integration *model.Integration) { if integration.PinboardEnabled { client := pinboard.NewClient(integration.PinboardToken) err := client.AddBookmark( @@ -63,10 +64,9 @@ func SendEntry(entry *model.Entry, integration *model.Integration) { } if integration.PocketEnabled { - client := pocket.NewClient(integration.PocketAccessToken, integration.PocketConsumerKey) + client := pocket.NewClient(cfg.PocketConsumerKey(integration.PocketConsumerKey), integration.PocketAccessToken) if err := client.AddURL(entry.URL, entry.Title); err != nil { logger.Error("[Integration] UserID #%d: %v", integration.UserID, err) } } - } diff --git a/integration/pocket/pocket.go b/integration/pocket/pocket.go index 16a826c..77f77d4 100644 --- a/integration/pocket/pocket.go +++ b/integration/pocket/pocket.go @@ -12,8 +12,8 @@ import ( // Client represents a Pocket client. type Client struct { - accessToken string consumerKey string + accessToken string } // AddURL sends a single link to Pocket. @@ -50,6 +50,6 @@ func (c *Client) AddURL(link, title string) error { } // NewClient returns a new Pocket client. -func NewClient(accessToken, consumerKey string) *Client { - return &Client{accessToken: accessToken, consumerKey: consumerKey} +func NewClient(consumerKey, accessToken string) *Client { + return &Client{consumerKey, accessToken} } diff --git a/template/html/integrations.html b/template/html/integrations.html index 4316656..5eef264 100644 --- a/template/html/integrations.html +++ b/template/html/integrations.html @@ -79,8 +79,10 @@ {{ t "Save articles to Pocket" }} - - + {{ if not .hasPocketConsumerKeyConfigured }} + + + {{ end }} diff --git a/template/views.go b/template/views.go index a74fb75..27297bb 100644 --- a/template/views.go +++ b/template/views.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2018-05-20 15:22:33.785042033 -0700 PDT m=+0.016512546 +// 2018-05-21 12:14:59.39668234 -0700 PDT m=+0.007850936 package template @@ -892,8 +892,10 @@ var templateViewsMap = map[string]string{ {{ t "Save articles to Pocket" }} - - + {{ if not .hasPocketConsumerKeyConfigured }} + + + {{ end }} @@ -1263,7 +1265,7 @@ var templateViewsMapChecksums = map[string]string{ "feeds": "2a5abe37968ea34a0576dbef52341645cb1fc9562e351382fbf721491da6f4fa", "history_entries": "451f0b202f47c9db5344d3e73862f5b7afbd4323fbdba21b6087866c40f045d3", "import": "73b5112e20bfd232bf73334544186ea419505936bc237d481517a8622901878f", - "integrations": "9b31a245d7d10e28a5682ec993b173972b3d69a86e731e8b09cf13a9ecbf6a09", + "integrations": "4f93dab2630e6b3bf8bf51fc844e48fc763b8e7d6bb98c3c1692ea43f0e93930", "login": "7d83c3067c02f1f6aafdd8816c7f97a4eb5a5a4bdaaaa4cc1e2fbb9c17ea65e8", "sessions": "3fa79031dd883847eba92fbafe5f535fa3a4e1614bb610f20588b6f8fc8b3624", "settings": "ea2505b9d0a6d6bb594dba87a92079de19baa6d494f0651693a7685489fb7de9", diff --git a/ui/entry_save.go b/ui/entry_save.go index 9b24078..12ebf2a 100644 --- a/ui/entry_save.go +++ b/ui/entry_save.go @@ -47,7 +47,7 @@ func (c *Controller) SaveEntry(w http.ResponseWriter, r *http.Request) { } go func() { - integration.SendEntry(entry, settings) + integration.SendEntry(c.cfg, entry, settings) }() json.Created(w, map[string]string{"message": "saved"}) diff --git a/ui/integration_pocket.go b/ui/integration_pocket.go index 4f65d61..02497c7 100644 --- a/ui/integration_pocket.go +++ b/ui/integration_pocket.go @@ -33,7 +33,7 @@ func (c *Controller) PocketAuthorize(w http.ResponseWriter, r *http.Request) { } sess := session.New(c.store, ctx) - connector := pocket.NewConnector(integration.PocketConsumerKey) + 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 { @@ -64,7 +64,7 @@ func (c *Controller) PocketCallback(w http.ResponseWriter, r *http.Request) { return } - connector := pocket.NewConnector(integration.PocketConsumerKey) + connector := pocket.NewConnector(c.cfg.PocketConsumerKey(integration.PocketConsumerKey)) accessToken, err := connector.AccessToken(ctx.PocketRequestToken()) if err != nil { logger.Error("[Pocket:Callback] %v", err) diff --git a/ui/integration_show.go b/ui/integration_show.go index d368d85..31d374f 100644 --- a/ui/integration_show.go +++ b/ui/integration_show.go @@ -61,6 +61,7 @@ func (c *Controller) ShowIntegrations(w http.ResponseWriter, r *http.Request) { view.Set("menu", "settings") view.Set("user", user) view.Set("countUnread", c.store.CountUnreadEntries(user.ID)) + view.Set("hasPocketConsumerKeyConfigured", c.cfg.PocketConsumerKey("") != "") html.OK(w, view.Render("integrations")) } -- cgit v1.2.3