diff options
author | 2017-12-02 19:32:14 -0800 | |
---|---|---|
committer | 2017-12-02 19:32:14 -0800 | |
commit | 2356ddad28d50aacc8bbb3e020792d3b92f2fb4d (patch) | |
tree | a881d13d2fcc2e0d0fef2f7d26cfd2d6b044209f /server/ui | |
parent | 2f1367a8d4c33e7c6ba459cfc6756e079c7a1af4 (diff) |
Add Pinboard integration
Diffstat (limited to 'server/ui')
-rw-r--r-- | server/ui/controller/integrations.go | 87 | ||||
-rw-r--r-- | server/ui/form/integration.go | 37 |
2 files changed, 123 insertions, 1 deletions
diff --git a/server/ui/controller/integrations.go b/server/ui/controller/integrations.go index 887f619..12a7964 100644 --- a/server/ui/controller/integrations.go +++ b/server/ui/controller/integrations.go @@ -4,10 +4,25 @@ package controller -import "github.com/miniflux/miniflux2/server/core" +import ( + "errors" + "log" + + "github.com/miniflux/miniflux2/integration/pinboard" + "github.com/miniflux/miniflux2/model" + "github.com/miniflux/miniflux2/server/core" + "github.com/miniflux/miniflux2/server/ui/form" +) // ShowIntegrations renders the page with all external integrations. func (c *Controller) ShowIntegrations(ctx *core.Context, request *core.Request, response *core.Response) { + user := ctx.LoggedUser() + integration, err := c.store.Integration(user.ID) + if err != nil { + response.HTML().ServerError(err) + return + } + args, err := c.getCommonTemplateArgs(ctx) if err != nil { response.HTML().ServerError(err) @@ -16,5 +31,75 @@ func (c *Controller) ShowIntegrations(ctx *core.Context, request *core.Request, response.HTML().Render("integrations", args.Merge(tplParams{ "menu": "settings", + "form": form.IntegrationForm{ + PinboardEnabled: integration.PinboardEnabled, + PinboardToken: integration.PinboardToken, + PinboardTags: integration.PinboardTags, + PinboardMarkAsUnread: integration.PinboardMarkAsUnread, + }, })) } + +// UpdateIntegration updates integration settings. +func (c *Controller) UpdateIntegration(ctx *core.Context, request *core.Request, response *core.Response) { + user := ctx.LoggedUser() + integration, err := c.store.Integration(user.ID) + if err != nil { + response.HTML().ServerError(err) + return + } + + integrationForm := form.NewIntegrationForm(request.Request()) + integrationForm.Merge(integration) + + err = c.store.UpdateIntegration(integration) + if err != nil { + response.HTML().ServerError(err) + return + } + + response.Redirect(ctx.Route("integrations")) +} + +// SaveEntry send the link to external services. +func (c *Controller) SaveEntry(ctx *core.Context, request *core.Request, response *core.Response) { + entryID, err := request.IntegerParam("entryID") + if err != nil { + response.HTML().BadRequest(err) + return + } + + user := ctx.LoggedUser() + builder := c.store.GetEntryQueryBuilder(user.ID, user.Timezone) + builder.WithEntryID(entryID) + builder.WithoutStatus(model.EntryStatusRemoved) + + entry, err := builder.GetEntry() + if err != nil { + response.JSON().ServerError(err) + return + } + + if entry == nil { + response.JSON().NotFound(errors.New("Entry not found")) + return + } + + integration, err := c.store.Integration(user.ID) + if err != nil { + response.JSON().ServerError(err) + return + } + + go func() { + if integration.PinboardEnabled { + client := pinboard.NewClient(integration.PinboardToken) + err := client.AddBookmark(entry.URL, entry.Title, integration.PinboardTags, integration.PinboardMarkAsUnread) + if err != nil { + log.Println("[Pinboard]", err) + } + } + }() + + response.JSON().Created(map[string]string{"message": "saved"}) +} diff --git a/server/ui/form/integration.go b/server/ui/form/integration.go new file mode 100644 index 0000000..566adae --- /dev/null +++ b/server/ui/form/integration.go @@ -0,0 +1,37 @@ +// Copyright 2017 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 form + +import ( + "net/http" + + "github.com/miniflux/miniflux2/model" +) + +// IntegrationForm represents user integration settings form. +type IntegrationForm struct { + PinboardEnabled bool + PinboardToken string + PinboardTags string + PinboardMarkAsUnread bool +} + +// Merge copy form values to the model. +func (i IntegrationForm) Merge(integration *model.Integration) { + integration.PinboardEnabled = i.PinboardEnabled + integration.PinboardToken = i.PinboardToken + integration.PinboardTags = i.PinboardTags + integration.PinboardMarkAsUnread = i.PinboardMarkAsUnread +} + +// NewIntegrationForm returns a new AuthForm. +func NewIntegrationForm(r *http.Request) *IntegrationForm { + return &IntegrationForm{ + PinboardEnabled: r.FormValue("pinboard_enabled") == "1", + PinboardToken: r.FormValue("pinboard_token"), + PinboardTags: r.FormValue("pinboard_tags"), + PinboardMarkAsUnread: r.FormValue("pinboard_mark_as_unread") == "1", + } +} |