integration.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. FROM integrations
  63. WHERE user_id=$1
  64. `
  65. var integration model.Integration
  66. err := s.db.QueryRow(query, userID).Scan(
  67. &integration.UserID,
  68. &integration.PinboardEnabled,
  69. &integration.PinboardToken,
  70. &integration.PinboardTags,
  71. &integration.PinboardMarkAsUnread,
  72. &integration.InstapaperEnabled,
  73. &integration.InstapaperUsername,
  74. &integration.InstapaperPassword,
  75. &integration.FeverEnabled,
  76. &integration.FeverUsername,
  77. &integration.FeverPassword,
  78. &integration.FeverToken,
  79. &integration.WallabagEnabled,
  80. &integration.WallabagURL,
  81. &integration.WallabagClientID,
  82. &integration.WallabagClientSecret,
  83. &integration.WallabagUsername,
  84. &integration.WallabagPassword,
  85. )
  86. switch {
  87. case err == sql.ErrNoRows:
  88. return &integration, nil
  89. case err != nil:
  90. return &integration, fmt.Errorf("unable to fetch integration row: %v", err)
  91. }
  92. return &integration, nil
  93. }
  94. // UpdateIntegration saves user integration settings.
  95. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  96. query := `
  97. UPDATE integrations SET
  98. pinboard_enabled=$1,
  99. pinboard_token=$2,
  100. pinboard_tags=$3,
  101. pinboard_mark_as_unread=$4,
  102. instapaper_enabled=$5,
  103. instapaper_username=$6,
  104. instapaper_password=$7,
  105. fever_enabled=$8,
  106. fever_username=$9,
  107. fever_password=$10,
  108. fever_token=$11,
  109. wallabag_enabled=$12,
  110. wallabag_url=$13,
  111. wallabag_client_id=$14,
  112. wallabag_client_secret=$15,
  113. wallabag_username=$16,
  114. wallabag_password=$17
  115. WHERE user_id=$18
  116. `
  117. _, err := s.db.Exec(
  118. query,
  119. integration.PinboardEnabled,
  120. integration.PinboardToken,
  121. integration.PinboardTags,
  122. integration.PinboardMarkAsUnread,
  123. integration.InstapaperEnabled,
  124. integration.InstapaperUsername,
  125. integration.InstapaperPassword,
  126. integration.FeverEnabled,
  127. integration.FeverUsername,
  128. integration.FeverPassword,
  129. integration.FeverToken,
  130. integration.WallabagEnabled,
  131. integration.WallabagURL,
  132. integration.WallabagClientID,
  133. integration.WallabagClientSecret,
  134. integration.WallabagUsername,
  135. integration.WallabagPassword,
  136. integration.UserID,
  137. )
  138. if err != nil {
  139. return fmt.Errorf("unable to update integration row: %v", err)
  140. }
  141. return nil
  142. }
  143. // CreateIntegration creates initial user integration settings.
  144. func (s *Storage) CreateIntegration(userID int64) error {
  145. query := `INSERT INTO integrations (user_id) VALUES ($1)`
  146. _, err := s.db.Exec(query, userID)
  147. if err != nil {
  148. return fmt.Errorf("unable to create integration row: %v", err)
  149. }
  150. return nil
  151. }