integration.go 5.3 KB

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