integration.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 storage
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "github.com/miniflux/miniflux/model"
  9. )
  10. // UserByFeverToken returns a user by using the Fever API token.
  11. func (s *Storage) UserByFeverToken(token string) (*model.User, error) {
  12. query := `
  13. SELECT
  14. users.id, users.is_admin, users.timezone
  15. FROM users
  16. LEFT JOIN integrations ON integrations.user_id=users.id
  17. WHERE integrations.fever_enabled='t' AND integrations.fever_token=$1
  18. `
  19. var user model.User
  20. err := s.db.QueryRow(query, token).Scan(&user.ID, &user.IsAdmin, &user.Timezone)
  21. switch {
  22. case err == sql.ErrNoRows:
  23. return nil, nil
  24. case err != nil:
  25. return nil, fmt.Errorf("unable to fetch user: %v", err)
  26. }
  27. return &user, nil
  28. }
  29. // Integration returns user integration settings.
  30. func (s *Storage) Integration(userID int64) (*model.Integration, error) {
  31. query := `SELECT
  32. user_id,
  33. pinboard_enabled,
  34. pinboard_token,
  35. pinboard_tags,
  36. pinboard_mark_as_unread,
  37. instapaper_enabled,
  38. instapaper_username,
  39. instapaper_password,
  40. fever_enabled,
  41. fever_username,
  42. fever_password,
  43. fever_token,
  44. wallabag_enabled,
  45. wallabag_url,
  46. wallabag_client_id,
  47. wallabag_client_secret,
  48. wallabag_username,
  49. wallabag_password
  50. FROM integrations
  51. WHERE user_id=$1
  52. `
  53. var integration model.Integration
  54. err := s.db.QueryRow(query, userID).Scan(
  55. &integration.UserID,
  56. &integration.PinboardEnabled,
  57. &integration.PinboardToken,
  58. &integration.PinboardTags,
  59. &integration.PinboardMarkAsUnread,
  60. &integration.InstapaperEnabled,
  61. &integration.InstapaperUsername,
  62. &integration.InstapaperPassword,
  63. &integration.FeverEnabled,
  64. &integration.FeverUsername,
  65. &integration.FeverPassword,
  66. &integration.FeverToken,
  67. &integration.WallabagEnabled,
  68. &integration.WallabagURL,
  69. &integration.WallabagClientID,
  70. &integration.WallabagClientSecret,
  71. &integration.WallabagUsername,
  72. &integration.WallabagPassword,
  73. )
  74. switch {
  75. case err == sql.ErrNoRows:
  76. return &integration, nil
  77. case err != nil:
  78. return &integration, fmt.Errorf("unable to fetch integration row: %v", err)
  79. }
  80. return &integration, nil
  81. }
  82. // UpdateIntegration saves user integration settings.
  83. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  84. query := `
  85. UPDATE integrations SET
  86. pinboard_enabled=$1,
  87. pinboard_token=$2,
  88. pinboard_tags=$3,
  89. pinboard_mark_as_unread=$4,
  90. instapaper_enabled=$5,
  91. instapaper_username=$6,
  92. instapaper_password=$7,
  93. fever_enabled=$8,
  94. fever_username=$9,
  95. fever_password=$10,
  96. fever_token=$11,
  97. wallabag_enabled=$12,
  98. wallabag_url=$13,
  99. wallabag_client_id=$14,
  100. wallabag_client_secret=$15,
  101. wallabag_username=$16,
  102. wallabag_password=$17
  103. WHERE user_id=$18
  104. `
  105. _, err := s.db.Exec(
  106. query,
  107. integration.PinboardEnabled,
  108. integration.PinboardToken,
  109. integration.PinboardTags,
  110. integration.PinboardMarkAsUnread,
  111. integration.InstapaperEnabled,
  112. integration.InstapaperUsername,
  113. integration.InstapaperPassword,
  114. integration.FeverEnabled,
  115. integration.FeverUsername,
  116. integration.FeverPassword,
  117. integration.FeverToken,
  118. integration.WallabagEnabled,
  119. integration.WallabagURL,
  120. integration.WallabagClientID,
  121. integration.WallabagClientSecret,
  122. integration.WallabagUsername,
  123. integration.WallabagPassword,
  124. integration.UserID,
  125. )
  126. if err != nil {
  127. return fmt.Errorf("unable to update integration row: %v", err)
  128. }
  129. return nil
  130. }
  131. // CreateIntegration creates initial user integration settings.
  132. func (s *Storage) CreateIntegration(userID int64) error {
  133. query := `INSERT INTO integrations (user_id) VALUES ($1)`
  134. _, err := s.db.Exec(query, userID)
  135. if err != nil {
  136. return fmt.Errorf("unable to create integration row: %v", err)
  137. }
  138. return nil
  139. }