integrations.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "github.com/miniflux/miniflux2/integration"
  8. "github.com/miniflux/miniflux2/model"
  9. "github.com/miniflux/miniflux2/server/core"
  10. "github.com/miniflux/miniflux2/server/ui/form"
  11. )
  12. // ShowIntegrations renders the page with all external integrations.
  13. func (c *Controller) ShowIntegrations(ctx *core.Context, request *core.Request, response *core.Response) {
  14. user := ctx.LoggedUser()
  15. integration, err := c.store.Integration(user.ID)
  16. if err != nil {
  17. response.HTML().ServerError(err)
  18. return
  19. }
  20. args, err := c.getCommonTemplateArgs(ctx)
  21. if err != nil {
  22. response.HTML().ServerError(err)
  23. return
  24. }
  25. response.HTML().Render("integrations", args.Merge(tplParams{
  26. "menu": "settings",
  27. "form": form.IntegrationForm{
  28. PinboardEnabled: integration.PinboardEnabled,
  29. PinboardToken: integration.PinboardToken,
  30. PinboardTags: integration.PinboardTags,
  31. PinboardMarkAsUnread: integration.PinboardMarkAsUnread,
  32. InstapaperEnabled: integration.InstapaperEnabled,
  33. InstapaperUsername: integration.InstapaperUsername,
  34. InstapaperPassword: integration.InstapaperPassword,
  35. },
  36. }))
  37. }
  38. // UpdateIntegration updates integration settings.
  39. func (c *Controller) UpdateIntegration(ctx *core.Context, request *core.Request, response *core.Response) {
  40. user := ctx.LoggedUser()
  41. integration, err := c.store.Integration(user.ID)
  42. if err != nil {
  43. response.HTML().ServerError(err)
  44. return
  45. }
  46. integrationForm := form.NewIntegrationForm(request.Request())
  47. integrationForm.Merge(integration)
  48. err = c.store.UpdateIntegration(integration)
  49. if err != nil {
  50. response.HTML().ServerError(err)
  51. return
  52. }
  53. response.Redirect(ctx.Route("integrations"))
  54. }
  55. // SaveEntry send the link to external services.
  56. func (c *Controller) SaveEntry(ctx *core.Context, request *core.Request, response *core.Response) {
  57. entryID, err := request.IntegerParam("entryID")
  58. if err != nil {
  59. response.HTML().BadRequest(err)
  60. return
  61. }
  62. user := ctx.LoggedUser()
  63. builder := c.store.GetEntryQueryBuilder(user.ID, user.Timezone)
  64. builder.WithEntryID(entryID)
  65. builder.WithoutStatus(model.EntryStatusRemoved)
  66. entry, err := builder.GetEntry()
  67. if err != nil {
  68. response.JSON().ServerError(err)
  69. return
  70. }
  71. if entry == nil {
  72. response.JSON().NotFound(errors.New("Entry not found"))
  73. return
  74. }
  75. settings, err := c.store.Integration(user.ID)
  76. if err != nil {
  77. response.JSON().ServerError(err)
  78. return
  79. }
  80. go func() {
  81. integration.SendEntry(entry, settings)
  82. }()
  83. response.JSON().Created(map[string]string{"message": "saved"})
  84. }