integration.go 4.5 KB

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