integration_update.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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"
  11. "miniflux.app/v2/internal/locale"
  12. "miniflux.app/v2/internal/ui/form"
  13. )
  14. func (h *handler) updateIntegration(w http.ResponseWriter, r *http.Request) {
  15. sess := request.WebSession(r)
  16. printer := locale.NewPrinter(sess.Language())
  17. userID := request.UserID(r)
  18. integration, err := h.store.Integration(userID)
  19. if err != nil {
  20. response.HTMLServerError(w, r, err)
  21. return
  22. }
  23. integrationForm := form.NewIntegrationForm(r)
  24. integrationForm.Merge(integration)
  25. if integration.FeverUsername != "" && h.store.HasDuplicateFeverUsername(userID, integration.FeverUsername) {
  26. sess.SetErrorMessage(printer.Print("error.duplicate_fever_username"))
  27. response.HTMLRedirect(w, r, h.routePath("/integrations"))
  28. return
  29. }
  30. if integration.FeverEnabled {
  31. if integrationForm.FeverPassword != "" {
  32. integration.FeverToken = fmt.Sprintf("%x", md5.Sum([]byte(integration.FeverUsername+":"+integrationForm.FeverPassword)))
  33. }
  34. } else {
  35. integration.FeverToken = ""
  36. }
  37. if integration.GoogleReaderUsername != "" && h.store.HasDuplicateGoogleReaderUsername(userID, integration.GoogleReaderUsername) {
  38. sess.SetErrorMessage(printer.Print("error.duplicate_googlereader_username"))
  39. response.HTMLRedirect(w, r, h.routePath("/integrations"))
  40. return
  41. }
  42. if integration.GoogleReaderEnabled {
  43. if integrationForm.GoogleReaderPassword != "" {
  44. integration.GoogleReaderPassword, err = crypto.HashPassword(integrationForm.GoogleReaderPassword)
  45. if err != nil {
  46. response.HTMLServerError(w, r, err)
  47. return
  48. }
  49. }
  50. } else {
  51. integration.GoogleReaderPassword = ""
  52. }
  53. if integrationForm.WebhookEnabled {
  54. if integrationForm.WebhookURL == "" {
  55. integration.WebhookEnabled = false
  56. integration.WebhookSecret = ""
  57. } else if integration.WebhookSecret == "" {
  58. integration.WebhookSecret = crypto.GenerateRandomStringHex(32)
  59. }
  60. } else {
  61. integration.WebhookURL = ""
  62. integration.WebhookSecret = ""
  63. }
  64. if integrationForm.LinktacoEnabled {
  65. if integrationForm.LinktacoAPIToken == "" || integrationForm.LinktacoOrgSlug == "" {
  66. sess.SetErrorMessage(printer.Print("error.linktaco_missing_required_fields"))
  67. response.HTMLRedirect(w, r, h.routePath("/integrations"))
  68. return
  69. }
  70. if integration.LinktacoVisibility == "" {
  71. integration.LinktacoVisibility = "PUBLIC"
  72. }
  73. }
  74. err = h.store.UpdateIntegration(integration)
  75. if err != nil {
  76. response.HTMLServerError(w, r, err)
  77. return
  78. }
  79. sess.SetSuccessMessage(printer.Print("alert.prefs_saved"))
  80. response.HTMLRedirect(w, r, h.routePath("/integrations"))
  81. }