integration_update.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "crypto/md5"
  6. "fmt"
  7. "net/http"
  8. "miniflux.app/v2/internal/crypto"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response/html"
  11. "miniflux.app/v2/internal/http/route"
  12. "miniflux.app/v2/internal/locale"
  13. "miniflux.app/v2/internal/ui/form"
  14. "miniflux.app/v2/internal/ui/session"
  15. )
  16. func (h *handler) updateIntegration(w http.ResponseWriter, r *http.Request) {
  17. printer := locale.NewPrinter(request.UserLanguage(r))
  18. sess := session.New(h.store, request.SessionID(r))
  19. userID := request.UserID(r)
  20. integration, err := h.store.Integration(userID)
  21. if err != nil {
  22. html.ServerError(w, r, err)
  23. return
  24. }
  25. integrationForm := form.NewIntegrationForm(r)
  26. integrationForm.Merge(integration)
  27. if integration.FeverUsername != "" && h.store.HasDuplicateFeverUsername(userID, integration.FeverUsername) {
  28. sess.NewFlashErrorMessage(printer.Print("error.duplicate_fever_username"))
  29. html.Redirect(w, r, route.Path(h.router, "integrations"))
  30. return
  31. }
  32. if integration.FeverEnabled {
  33. if integrationForm.FeverPassword != "" {
  34. integration.FeverToken = fmt.Sprintf("%x", md5.Sum([]byte(integration.FeverUsername+":"+integrationForm.FeverPassword)))
  35. }
  36. } else {
  37. integration.FeverToken = ""
  38. }
  39. if integration.GoogleReaderUsername != "" && h.store.HasDuplicateGoogleReaderUsername(userID, integration.GoogleReaderUsername) {
  40. sess.NewFlashErrorMessage(printer.Print("error.duplicate_googlereader_username"))
  41. html.Redirect(w, r, route.Path(h.router, "integrations"))
  42. return
  43. }
  44. if integration.GoogleReaderEnabled {
  45. if integrationForm.GoogleReaderPassword != "" {
  46. integration.GoogleReaderPassword, err = crypto.HashPassword(integrationForm.GoogleReaderPassword)
  47. if err != nil {
  48. html.ServerError(w, r, err)
  49. return
  50. }
  51. }
  52. } else {
  53. integration.GoogleReaderPassword = ""
  54. }
  55. if integrationForm.WebhookEnabled {
  56. if integrationForm.WebhookURL == "" {
  57. integration.WebhookEnabled = false
  58. integration.WebhookSecret = ""
  59. } else if integration.WebhookSecret == "" {
  60. integration.WebhookSecret = crypto.GenerateRandomStringHex(32)
  61. }
  62. } else {
  63. integration.WebhookURL = ""
  64. integration.WebhookSecret = ""
  65. }
  66. if integrationForm.LinktacoEnabled {
  67. if integrationForm.LinktacoAPIToken == "" || integrationForm.LinktacoOrgSlug == "" {
  68. sess.NewFlashErrorMessage(printer.Print("error.linktaco_missing_required_fields"))
  69. html.Redirect(w, r, route.Path(h.router, "integrations"))
  70. return
  71. }
  72. if integration.LinktacoVisibility == "" {
  73. integration.LinktacoVisibility = "PUBLIC"
  74. }
  75. }
  76. err = h.store.UpdateIntegration(integration)
  77. if err != nil {
  78. html.ServerError(w, r, err)
  79. return
  80. }
  81. sess.NewFlashMessage(printer.Print("alert.prefs_saved"))
  82. html.Redirect(w, r, route.Path(h.router, "integrations"))
  83. }