integrations.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package controller
  5. import (
  6. "errors"
  7. "log"
  8. "github.com/miniflux/miniflux2/integration/pinboard"
  9. "github.com/miniflux/miniflux2/model"
  10. "github.com/miniflux/miniflux2/server/core"
  11. "github.com/miniflux/miniflux2/server/ui/form"
  12. )
  13. // ShowIntegrations renders the page with all external integrations.
  14. func (c *Controller) ShowIntegrations(ctx *core.Context, request *core.Request, response *core.Response) {
  15. user := ctx.LoggedUser()
  16. integration, err := c.store.Integration(user.ID)
  17. if err != nil {
  18. response.HTML().ServerError(err)
  19. return
  20. }
  21. args, err := c.getCommonTemplateArgs(ctx)
  22. if err != nil {
  23. response.HTML().ServerError(err)
  24. return
  25. }
  26. response.HTML().Render("integrations", args.Merge(tplParams{
  27. "menu": "settings",
  28. "form": form.IntegrationForm{
  29. PinboardEnabled: integration.PinboardEnabled,
  30. PinboardToken: integration.PinboardToken,
  31. PinboardTags: integration.PinboardTags,
  32. PinboardMarkAsUnread: integration.PinboardMarkAsUnread,
  33. },
  34. }))
  35. }
  36. // UpdateIntegration updates integration settings.
  37. func (c *Controller) UpdateIntegration(ctx *core.Context, request *core.Request, response *core.Response) {
  38. user := ctx.LoggedUser()
  39. integration, err := c.store.Integration(user.ID)
  40. if err != nil {
  41. response.HTML().ServerError(err)
  42. return
  43. }
  44. integrationForm := form.NewIntegrationForm(request.Request())
  45. integrationForm.Merge(integration)
  46. err = c.store.UpdateIntegration(integration)
  47. if err != nil {
  48. response.HTML().ServerError(err)
  49. return
  50. }
  51. response.Redirect(ctx.Route("integrations"))
  52. }
  53. // SaveEntry send the link to external services.
  54. func (c *Controller) SaveEntry(ctx *core.Context, request *core.Request, response *core.Response) {
  55. entryID, err := request.IntegerParam("entryID")
  56. if err != nil {
  57. response.HTML().BadRequest(err)
  58. return
  59. }
  60. user := ctx.LoggedUser()
  61. builder := c.store.GetEntryQueryBuilder(user.ID, user.Timezone)
  62. builder.WithEntryID(entryID)
  63. builder.WithoutStatus(model.EntryStatusRemoved)
  64. entry, err := builder.GetEntry()
  65. if err != nil {
  66. response.JSON().ServerError(err)
  67. return
  68. }
  69. if entry == nil {
  70. response.JSON().NotFound(errors.New("Entry not found"))
  71. return
  72. }
  73. integration, err := c.store.Integration(user.ID)
  74. if err != nil {
  75. response.JSON().ServerError(err)
  76. return
  77. }
  78. go func() {
  79. if integration.PinboardEnabled {
  80. client := pinboard.NewClient(integration.PinboardToken)
  81. err := client.AddBookmark(entry.URL, entry.Title, integration.PinboardTags, integration.PinboardMarkAsUnread)
  82. if err != nil {
  83. log.Println("[Pinboard]", err)
  84. }
  85. }
  86. }()
  87. response.JSON().Created(map[string]string{"message": "saved"})
  88. }