integration.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/storage"
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "golang.org/x/crypto/bcrypt"
  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. // HasDuplicateGoogleReaderUsername checks if another user have the same Google Reader username.
  18. func (s *Storage) HasDuplicateGoogleReaderUsername(userID int64, googleReaderUsername string) bool {
  19. query := `SELECT true FROM integrations WHERE user_id != $1 AND googlereader_username=$2`
  20. var result bool
  21. s.db.QueryRow(query, userID, googleReaderUsername).Scan(&result)
  22. return result
  23. }
  24. // UserByFeverToken returns a user by using the Fever API token.
  25. func (s *Storage) UserByFeverToken(token string) (*model.User, error) {
  26. query := `
  27. SELECT
  28. users.id, users.is_admin, users.timezone
  29. FROM
  30. users
  31. LEFT JOIN
  32. integrations ON integrations.user_id=users.id
  33. WHERE
  34. integrations.fever_enabled='t' AND lower(integrations.fever_token)=lower($1)
  35. `
  36. var user model.User
  37. err := s.db.QueryRow(query, token).Scan(&user.ID, &user.IsAdmin, &user.Timezone)
  38. switch {
  39. case err == sql.ErrNoRows:
  40. return nil, nil
  41. case err != nil:
  42. return nil, fmt.Errorf("store: unable to fetch user: %v", err)
  43. default:
  44. return &user, nil
  45. }
  46. }
  47. // GoogleReaderUserCheckPassword validates the Google Reader hashed password.
  48. func (s *Storage) GoogleReaderUserCheckPassword(username, password string) error {
  49. var hash string
  50. query := `
  51. SELECT
  52. googlereader_password
  53. FROM
  54. integrations
  55. WHERE
  56. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  57. `
  58. err := s.db.QueryRow(query, username).Scan(&hash)
  59. if err == sql.ErrNoRows {
  60. return fmt.Errorf(`store: unable to find this user: %s`, username)
  61. } else if err != nil {
  62. return fmt.Errorf(`store: unable to fetch user: %v`, err)
  63. }
  64. if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
  65. return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
  66. }
  67. return nil
  68. }
  69. // GoogleReaderUserGetIntegration returns part of the Google Reader parts of the integration struct.
  70. func (s *Storage) GoogleReaderUserGetIntegration(username string) (*model.Integration, error) {
  71. var integration model.Integration
  72. query := `
  73. SELECT
  74. user_id,
  75. googlereader_enabled,
  76. googlereader_username,
  77. googlereader_password
  78. FROM
  79. integrations
  80. WHERE
  81. integrations.googlereader_enabled='t' AND integrations.googlereader_username=$1
  82. `
  83. err := s.db.QueryRow(query, username).Scan(&integration.UserID, &integration.GoogleReaderEnabled, &integration.GoogleReaderUsername, &integration.GoogleReaderPassword)
  84. if err == sql.ErrNoRows {
  85. return &integration, fmt.Errorf(`store: unable to find this user: %s`, username)
  86. } else if err != nil {
  87. return &integration, fmt.Errorf(`store: unable to fetch user: %v`, err)
  88. }
  89. return &integration, nil
  90. }
  91. // Integration returns user integration settings.
  92. func (s *Storage) Integration(userID int64) (*model.Integration, error) {
  93. query := `
  94. SELECT
  95. user_id,
  96. pinboard_enabled,
  97. pinboard_token,
  98. pinboard_tags,
  99. pinboard_mark_as_unread,
  100. instapaper_enabled,
  101. instapaper_username,
  102. instapaper_password,
  103. fever_enabled,
  104. fever_username,
  105. fever_token,
  106. googlereader_enabled,
  107. googlereader_username,
  108. googlereader_password,
  109. wallabag_enabled,
  110. wallabag_only_url,
  111. wallabag_url,
  112. wallabag_client_id,
  113. wallabag_client_secret,
  114. wallabag_username,
  115. wallabag_password,
  116. notion_enabled,
  117. notion_token,
  118. notion_page_id,
  119. nunux_keeper_enabled,
  120. nunux_keeper_url,
  121. nunux_keeper_api_key,
  122. espial_enabled,
  123. espial_url,
  124. espial_api_key,
  125. espial_tags,
  126. readwise_enabled,
  127. readwise_api_key,
  128. pocket_enabled,
  129. pocket_access_token,
  130. pocket_consumer_key,
  131. telegram_bot_enabled,
  132. telegram_bot_token,
  133. telegram_bot_chat_id,
  134. linkding_enabled,
  135. linkding_url,
  136. linkding_api_key,
  137. linkding_tags,
  138. linkding_mark_as_unread,
  139. matrix_bot_enabled,
  140. matrix_bot_user,
  141. matrix_bot_password,
  142. matrix_bot_url,
  143. matrix_bot_chat_id
  144. FROM
  145. integrations
  146. WHERE
  147. user_id=$1
  148. `
  149. var integration model.Integration
  150. err := s.db.QueryRow(query, userID).Scan(
  151. &integration.UserID,
  152. &integration.PinboardEnabled,
  153. &integration.PinboardToken,
  154. &integration.PinboardTags,
  155. &integration.PinboardMarkAsUnread,
  156. &integration.InstapaperEnabled,
  157. &integration.InstapaperUsername,
  158. &integration.InstapaperPassword,
  159. &integration.FeverEnabled,
  160. &integration.FeverUsername,
  161. &integration.FeverToken,
  162. &integration.GoogleReaderEnabled,
  163. &integration.GoogleReaderUsername,
  164. &integration.GoogleReaderPassword,
  165. &integration.WallabagEnabled,
  166. &integration.WallabagOnlyURL,
  167. &integration.WallabagURL,
  168. &integration.WallabagClientID,
  169. &integration.WallabagClientSecret,
  170. &integration.WallabagUsername,
  171. &integration.WallabagPassword,
  172. &integration.NotionEnabled,
  173. &integration.NotionToken,
  174. &integration.NotionPageID,
  175. &integration.NunuxKeeperEnabled,
  176. &integration.NunuxKeeperURL,
  177. &integration.NunuxKeeperAPIKey,
  178. &integration.EspialEnabled,
  179. &integration.EspialURL,
  180. &integration.EspialAPIKey,
  181. &integration.EspialTags,
  182. &integration.ReadwiseEnabled,
  183. &integration.ReadwiseAPIKey,
  184. &integration.PocketEnabled,
  185. &integration.PocketAccessToken,
  186. &integration.PocketConsumerKey,
  187. &integration.TelegramBotEnabled,
  188. &integration.TelegramBotToken,
  189. &integration.TelegramBotChatID,
  190. &integration.LinkdingEnabled,
  191. &integration.LinkdingURL,
  192. &integration.LinkdingAPIKey,
  193. &integration.LinkdingTags,
  194. &integration.LinkdingMarkAsUnread,
  195. &integration.MatrixBotEnabled,
  196. &integration.MatrixBotUser,
  197. &integration.MatrixBotPassword,
  198. &integration.MatrixBotURL,
  199. &integration.MatrixBotChatID,
  200. )
  201. switch {
  202. case err == sql.ErrNoRows:
  203. return &integration, nil
  204. case err != nil:
  205. return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
  206. default:
  207. return &integration, nil
  208. }
  209. }
  210. // UpdateIntegration saves user integration settings.
  211. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  212. query := `
  213. UPDATE
  214. integrations
  215. SET
  216. pinboard_enabled=$1,
  217. pinboard_token=$2,
  218. pinboard_tags=$3,
  219. pinboard_mark_as_unread=$4,
  220. instapaper_enabled=$5,
  221. instapaper_username=$6,
  222. instapaper_password=$7,
  223. fever_enabled=$8,
  224. fever_username=$9,
  225. fever_token=$10,
  226. wallabag_enabled=$11,
  227. wallabag_only_url=$12,
  228. wallabag_url=$13,
  229. wallabag_client_id=$14,
  230. wallabag_client_secret=$15,
  231. wallabag_username=$16,
  232. wallabag_password=$17,
  233. nunux_keeper_enabled=$18,
  234. nunux_keeper_url=$19,
  235. nunux_keeper_api_key=$20,
  236. pocket_enabled=$21,
  237. pocket_access_token=$22,
  238. pocket_consumer_key=$23,
  239. googlereader_enabled=$24,
  240. googlereader_username=$25,
  241. googlereader_password=$26,
  242. telegram_bot_enabled=$27,
  243. telegram_bot_token=$28,
  244. telegram_bot_chat_id=$29,
  245. espial_enabled=$30,
  246. espial_url=$31,
  247. espial_api_key=$32,
  248. espial_tags=$33,
  249. linkding_enabled=$34,
  250. linkding_url=$35,
  251. linkding_api_key=$36,
  252. linkding_tags=$37,
  253. linkding_mark_as_unread=$38,
  254. matrix_bot_enabled=$39,
  255. matrix_bot_user=$40,
  256. matrix_bot_password=$41,
  257. matrix_bot_url=$42,
  258. matrix_bot_chat_id=$43,
  259. notion_enabled=$44,
  260. notion_token=$45,
  261. notion_page_id=$46,
  262. readwise_enabled=$47,
  263. readwise_api_key=$48
  264. WHERE
  265. user_id=$49
  266. `
  267. _, err := s.db.Exec(
  268. query,
  269. integration.PinboardEnabled,
  270. integration.PinboardToken,
  271. integration.PinboardTags,
  272. integration.PinboardMarkAsUnread,
  273. integration.InstapaperEnabled,
  274. integration.InstapaperUsername,
  275. integration.InstapaperPassword,
  276. integration.FeverEnabled,
  277. integration.FeverUsername,
  278. integration.FeverToken,
  279. integration.WallabagEnabled,
  280. integration.WallabagOnlyURL,
  281. integration.WallabagURL,
  282. integration.WallabagClientID,
  283. integration.WallabagClientSecret,
  284. integration.WallabagUsername,
  285. integration.WallabagPassword,
  286. integration.NunuxKeeperEnabled,
  287. integration.NunuxKeeperURL,
  288. integration.NunuxKeeperAPIKey,
  289. integration.PocketEnabled,
  290. integration.PocketAccessToken,
  291. integration.PocketConsumerKey,
  292. integration.GoogleReaderEnabled,
  293. integration.GoogleReaderUsername,
  294. integration.GoogleReaderPassword,
  295. integration.TelegramBotEnabled,
  296. integration.TelegramBotToken,
  297. integration.TelegramBotChatID,
  298. integration.EspialEnabled,
  299. integration.EspialURL,
  300. integration.EspialAPIKey,
  301. integration.EspialTags,
  302. integration.LinkdingEnabled,
  303. integration.LinkdingURL,
  304. integration.LinkdingAPIKey,
  305. integration.LinkdingTags,
  306. integration.LinkdingMarkAsUnread,
  307. integration.MatrixBotEnabled,
  308. integration.MatrixBotUser,
  309. integration.MatrixBotPassword,
  310. integration.MatrixBotURL,
  311. integration.MatrixBotChatID,
  312. integration.NotionEnabled,
  313. integration.NotionToken,
  314. integration.NotionPageID,
  315. integration.ReadwiseEnabled,
  316. integration.ReadwiseAPIKey,
  317. integration.UserID,
  318. )
  319. if err != nil {
  320. return fmt.Errorf(`store: unable to update integration row: %v`, err)
  321. }
  322. return nil
  323. }
  324. // HasSaveEntry returns true if the given user can save articles to third-parties.
  325. func (s *Storage) HasSaveEntry(userID int64) (result bool) {
  326. query := `
  327. SELECT
  328. true
  329. FROM
  330. integrations
  331. WHERE
  332. user_id=$1
  333. AND
  334. (pinboard_enabled='t' OR instapaper_enabled='t' OR wallabag_enabled='t' OR notion_enabled='t' OR nunux_keeper_enabled='t' OR espial_enabled='t' OR readwise_enabled='t' OR pocket_enabled='t' OR linkding_enabled='t')
  335. `
  336. if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
  337. result = false
  338. }
  339. return result
  340. }