aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/ui/controller/integrations.go
blob: 12a7964df319def75de5ee3e84db12c18aa4e75d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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 controller

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)
		return
	}

	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"})
}