integration.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "golang.org/x/crypto/bcrypt"
  8. "miniflux.app/v2/internal/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.username, 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.Username, &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. telegram_bot_topic_id,
  135. telegram_bot_disable_web_page_preview,
  136. telegram_bot_disable_notification,
  137. telegram_bot_disable_buttons,
  138. linkace_enabled,
  139. linkace_url,
  140. linkace_api_key,
  141. linkace_tags,
  142. linkace_is_private,
  143. linkace_check_disabled,
  144. linkding_enabled,
  145. linkding_url,
  146. linkding_api_key,
  147. linkding_tags,
  148. linkding_mark_as_unread,
  149. linkwarden_enabled,
  150. linkwarden_url,
  151. linkwarden_api_key,
  152. matrix_bot_enabled,
  153. matrix_bot_user,
  154. matrix_bot_password,
  155. matrix_bot_url,
  156. matrix_bot_chat_id,
  157. apprise_enabled,
  158. apprise_url,
  159. apprise_services_url,
  160. readeck_enabled,
  161. readeck_url,
  162. readeck_api_key,
  163. readeck_labels,
  164. readeck_only_url,
  165. shiori_enabled,
  166. shiori_url,
  167. shiori_username,
  168. shiori_password,
  169. shaarli_enabled,
  170. shaarli_url,
  171. shaarli_api_secret,
  172. webhook_enabled,
  173. webhook_url,
  174. webhook_secret,
  175. rssbridge_enabled,
  176. rssbridge_url,
  177. omnivore_enabled,
  178. omnivore_api_key,
  179. omnivore_url,
  180. raindrop_enabled,
  181. raindrop_token,
  182. raindrop_collection_id,
  183. raindrop_tags,
  184. betula_enabled,
  185. betula_url,
  186. betula_token,
  187. ntfy_enabled,
  188. ntfy_topic,
  189. ntfy_url,
  190. ntfy_api_token,
  191. ntfy_username,
  192. ntfy_password,
  193. ntfy_icon_url,
  194. ntfy_internal_links,
  195. cubox_enabled,
  196. cubox_api_link,
  197. discord_enabled,
  198. discord_webhook_link,
  199. slack_enabled,
  200. slack_webhook_link,
  201. pushover_enabled,
  202. pushover_user,
  203. pushover_token,
  204. pushover_device,
  205. pushover_prefix,
  206. rssbridge_token
  207. FROM
  208. integrations
  209. WHERE
  210. user_id=$1
  211. `
  212. var integration model.Integration
  213. err := s.db.QueryRow(query, userID).Scan(
  214. &integration.UserID,
  215. &integration.PinboardEnabled,
  216. &integration.PinboardToken,
  217. &integration.PinboardTags,
  218. &integration.PinboardMarkAsUnread,
  219. &integration.InstapaperEnabled,
  220. &integration.InstapaperUsername,
  221. &integration.InstapaperPassword,
  222. &integration.FeverEnabled,
  223. &integration.FeverUsername,
  224. &integration.FeverToken,
  225. &integration.GoogleReaderEnabled,
  226. &integration.GoogleReaderUsername,
  227. &integration.GoogleReaderPassword,
  228. &integration.WallabagEnabled,
  229. &integration.WallabagOnlyURL,
  230. &integration.WallabagURL,
  231. &integration.WallabagClientID,
  232. &integration.WallabagClientSecret,
  233. &integration.WallabagUsername,
  234. &integration.WallabagPassword,
  235. &integration.NotionEnabled,
  236. &integration.NotionToken,
  237. &integration.NotionPageID,
  238. &integration.NunuxKeeperEnabled,
  239. &integration.NunuxKeeperURL,
  240. &integration.NunuxKeeperAPIKey,
  241. &integration.EspialEnabled,
  242. &integration.EspialURL,
  243. &integration.EspialAPIKey,
  244. &integration.EspialTags,
  245. &integration.ReadwiseEnabled,
  246. &integration.ReadwiseAPIKey,
  247. &integration.PocketEnabled,
  248. &integration.PocketAccessToken,
  249. &integration.PocketConsumerKey,
  250. &integration.TelegramBotEnabled,
  251. &integration.TelegramBotToken,
  252. &integration.TelegramBotChatID,
  253. &integration.TelegramBotTopicID,
  254. &integration.TelegramBotDisableWebPagePreview,
  255. &integration.TelegramBotDisableNotification,
  256. &integration.TelegramBotDisableButtons,
  257. &integration.LinkAceEnabled,
  258. &integration.LinkAceURL,
  259. &integration.LinkAceAPIKey,
  260. &integration.LinkAceTags,
  261. &integration.LinkAcePrivate,
  262. &integration.LinkAceCheckDisabled,
  263. &integration.LinkdingEnabled,
  264. &integration.LinkdingURL,
  265. &integration.LinkdingAPIKey,
  266. &integration.LinkdingTags,
  267. &integration.LinkdingMarkAsUnread,
  268. &integration.LinkwardenEnabled,
  269. &integration.LinkwardenURL,
  270. &integration.LinkwardenAPIKey,
  271. &integration.MatrixBotEnabled,
  272. &integration.MatrixBotUser,
  273. &integration.MatrixBotPassword,
  274. &integration.MatrixBotURL,
  275. &integration.MatrixBotChatID,
  276. &integration.AppriseEnabled,
  277. &integration.AppriseURL,
  278. &integration.AppriseServicesURL,
  279. &integration.ReadeckEnabled,
  280. &integration.ReadeckURL,
  281. &integration.ReadeckAPIKey,
  282. &integration.ReadeckLabels,
  283. &integration.ReadeckOnlyURL,
  284. &integration.ShioriEnabled,
  285. &integration.ShioriURL,
  286. &integration.ShioriUsername,
  287. &integration.ShioriPassword,
  288. &integration.ShaarliEnabled,
  289. &integration.ShaarliURL,
  290. &integration.ShaarliAPISecret,
  291. &integration.WebhookEnabled,
  292. &integration.WebhookURL,
  293. &integration.WebhookSecret,
  294. &integration.RSSBridgeEnabled,
  295. &integration.RSSBridgeURL,
  296. &integration.OmnivoreEnabled,
  297. &integration.OmnivoreAPIKey,
  298. &integration.OmnivoreURL,
  299. &integration.RaindropEnabled,
  300. &integration.RaindropToken,
  301. &integration.RaindropCollectionID,
  302. &integration.RaindropTags,
  303. &integration.BetulaEnabled,
  304. &integration.BetulaURL,
  305. &integration.BetulaToken,
  306. &integration.NtfyEnabled,
  307. &integration.NtfyTopic,
  308. &integration.NtfyURL,
  309. &integration.NtfyAPIToken,
  310. &integration.NtfyUsername,
  311. &integration.NtfyPassword,
  312. &integration.NtfyIconURL,
  313. &integration.NtfyInternalLinks,
  314. &integration.CuboxEnabled,
  315. &integration.CuboxAPILink,
  316. &integration.DiscordEnabled,
  317. &integration.DiscordWebhookLink,
  318. &integration.SlackEnabled,
  319. &integration.SlackWebhookLink,
  320. &integration.PushoverEnabled,
  321. &integration.PushoverUser,
  322. &integration.PushoverToken,
  323. &integration.PushoverDevice,
  324. &integration.PushoverPrefix,
  325. &integration.RSSBridgeToken,
  326. )
  327. switch {
  328. case err == sql.ErrNoRows:
  329. return &integration, nil
  330. case err != nil:
  331. return &integration, fmt.Errorf(`store: unable to fetch integration row: %v`, err)
  332. default:
  333. return &integration, nil
  334. }
  335. }
  336. // UpdateIntegration saves user integration settings.
  337. func (s *Storage) UpdateIntegration(integration *model.Integration) error {
  338. query := `
  339. UPDATE
  340. integrations
  341. SET
  342. pinboard_enabled=$1,
  343. pinboard_token=$2,
  344. pinboard_tags=$3,
  345. pinboard_mark_as_unread=$4,
  346. instapaper_enabled=$5,
  347. instapaper_username=$6,
  348. instapaper_password=$7,
  349. fever_enabled=$8,
  350. fever_username=$9,
  351. fever_token=$10,
  352. wallabag_enabled=$11,
  353. wallabag_only_url=$12,
  354. wallabag_url=$13,
  355. wallabag_client_id=$14,
  356. wallabag_client_secret=$15,
  357. wallabag_username=$16,
  358. wallabag_password=$17,
  359. nunux_keeper_enabled=$18,
  360. nunux_keeper_url=$19,
  361. nunux_keeper_api_key=$20,
  362. pocket_enabled=$21,
  363. pocket_access_token=$22,
  364. pocket_consumer_key=$23,
  365. googlereader_enabled=$24,
  366. googlereader_username=$25,
  367. googlereader_password=$26,
  368. telegram_bot_enabled=$27,
  369. telegram_bot_token=$28,
  370. telegram_bot_chat_id=$29,
  371. telegram_bot_topic_id=$30,
  372. telegram_bot_disable_web_page_preview=$31,
  373. telegram_bot_disable_notification=$32,
  374. telegram_bot_disable_buttons=$33,
  375. espial_enabled=$34,
  376. espial_url=$35,
  377. espial_api_key=$36,
  378. espial_tags=$37,
  379. linkace_enabled=$38,
  380. linkace_url=$39,
  381. linkace_api_key=$40,
  382. linkace_tags=$41,
  383. linkace_is_private=$42,
  384. linkace_check_disabled=$43,
  385. linkding_enabled=$44,
  386. linkding_url=$45,
  387. linkding_api_key=$46,
  388. linkding_tags=$47,
  389. linkding_mark_as_unread=$48,
  390. matrix_bot_enabled=$49,
  391. matrix_bot_user=$50,
  392. matrix_bot_password=$51,
  393. matrix_bot_url=$52,
  394. matrix_bot_chat_id=$53,
  395. notion_enabled=$54,
  396. notion_token=$55,
  397. notion_page_id=$56,
  398. readwise_enabled=$57,
  399. readwise_api_key=$58,
  400. apprise_enabled=$59,
  401. apprise_url=$60,
  402. apprise_services_url=$61,
  403. readeck_enabled=$62,
  404. readeck_url=$63,
  405. readeck_api_key=$64,
  406. readeck_labels=$65,
  407. readeck_only_url=$66,
  408. shiori_enabled=$67,
  409. shiori_url=$68,
  410. shiori_username=$69,
  411. shiori_password=$70,
  412. shaarli_enabled=$71,
  413. shaarli_url=$72,
  414. shaarli_api_secret=$73,
  415. webhook_enabled=$74,
  416. webhook_url=$75,
  417. webhook_secret=$76,
  418. rssbridge_enabled=$77,
  419. rssbridge_url=$78,
  420. omnivore_enabled=$79,
  421. omnivore_api_key=$80,
  422. omnivore_url=$81,
  423. linkwarden_enabled=$82,
  424. linkwarden_url=$83,
  425. linkwarden_api_key=$84,
  426. raindrop_enabled=$85,
  427. raindrop_token=$86,
  428. raindrop_collection_id=$87,
  429. raindrop_tags=$88,
  430. betula_enabled=$89,
  431. betula_url=$90,
  432. betula_token=$91,
  433. ntfy_enabled=$92,
  434. ntfy_topic=$93,
  435. ntfy_url=$94,
  436. ntfy_api_token=$95,
  437. ntfy_username=$96,
  438. ntfy_password=$97,
  439. ntfy_icon_url=$98,
  440. ntfy_internal_links=$99,
  441. cubox_enabled=$100,
  442. cubox_api_link=$101,
  443. discord_enabled=$102,
  444. discord_webhook_link=$103,
  445. slack_enabled=$104,
  446. slack_webhook_link=$105,
  447. pushover_enabled=$106,
  448. pushover_user=$107,
  449. pushover_token=$108,
  450. pushover_device=$109,
  451. pushover_prefix=$110,
  452. rssbridge_token=$111
  453. WHERE
  454. user_id=$112
  455. `
  456. _, err := s.db.Exec(
  457. query,
  458. integration.PinboardEnabled,
  459. integration.PinboardToken,
  460. integration.PinboardTags,
  461. integration.PinboardMarkAsUnread,
  462. integration.InstapaperEnabled,
  463. integration.InstapaperUsername,
  464. integration.InstapaperPassword,
  465. integration.FeverEnabled,
  466. integration.FeverUsername,
  467. integration.FeverToken,
  468. integration.WallabagEnabled,
  469. integration.WallabagOnlyURL,
  470. integration.WallabagURL,
  471. integration.WallabagClientID,
  472. integration.WallabagClientSecret,
  473. integration.WallabagUsername,
  474. integration.WallabagPassword,
  475. integration.NunuxKeeperEnabled,
  476. integration.NunuxKeeperURL,
  477. integration.NunuxKeeperAPIKey,
  478. integration.PocketEnabled,
  479. integration.PocketAccessToken,
  480. integration.PocketConsumerKey,
  481. integration.GoogleReaderEnabled,
  482. integration.GoogleReaderUsername,
  483. integration.GoogleReaderPassword,
  484. integration.TelegramBotEnabled,
  485. integration.TelegramBotToken,
  486. integration.TelegramBotChatID,
  487. integration.TelegramBotTopicID,
  488. integration.TelegramBotDisableWebPagePreview,
  489. integration.TelegramBotDisableNotification,
  490. integration.TelegramBotDisableButtons,
  491. integration.EspialEnabled,
  492. integration.EspialURL,
  493. integration.EspialAPIKey,
  494. integration.EspialTags,
  495. integration.LinkAceEnabled,
  496. integration.LinkAceURL,
  497. integration.LinkAceAPIKey,
  498. integration.LinkAceTags,
  499. integration.LinkAcePrivate,
  500. integration.LinkAceCheckDisabled,
  501. integration.LinkdingEnabled,
  502. integration.LinkdingURL,
  503. integration.LinkdingAPIKey,
  504. integration.LinkdingTags,
  505. integration.LinkdingMarkAsUnread,
  506. integration.MatrixBotEnabled,
  507. integration.MatrixBotUser,
  508. integration.MatrixBotPassword,
  509. integration.MatrixBotURL,
  510. integration.MatrixBotChatID,
  511. integration.NotionEnabled,
  512. integration.NotionToken,
  513. integration.NotionPageID,
  514. integration.ReadwiseEnabled,
  515. integration.ReadwiseAPIKey,
  516. integration.AppriseEnabled,
  517. integration.AppriseURL,
  518. integration.AppriseServicesURL,
  519. integration.ReadeckEnabled,
  520. integration.ReadeckURL,
  521. integration.ReadeckAPIKey,
  522. integration.ReadeckLabels,
  523. integration.ReadeckOnlyURL,
  524. integration.ShioriEnabled,
  525. integration.ShioriURL,
  526. integration.ShioriUsername,
  527. integration.ShioriPassword,
  528. integration.ShaarliEnabled,
  529. integration.ShaarliURL,
  530. integration.ShaarliAPISecret,
  531. integration.WebhookEnabled,
  532. integration.WebhookURL,
  533. integration.WebhookSecret,
  534. integration.RSSBridgeEnabled,
  535. integration.RSSBridgeURL,
  536. integration.OmnivoreEnabled,
  537. integration.OmnivoreAPIKey,
  538. integration.OmnivoreURL,
  539. integration.LinkwardenEnabled,
  540. integration.LinkwardenURL,
  541. integration.LinkwardenAPIKey,
  542. integration.RaindropEnabled,
  543. integration.RaindropToken,
  544. integration.RaindropCollectionID,
  545. integration.RaindropTags,
  546. integration.BetulaEnabled,
  547. integration.BetulaURL,
  548. integration.BetulaToken,
  549. integration.NtfyEnabled,
  550. integration.NtfyTopic,
  551. integration.NtfyURL,
  552. integration.NtfyAPIToken,
  553. integration.NtfyUsername,
  554. integration.NtfyPassword,
  555. integration.NtfyIconURL,
  556. integration.NtfyInternalLinks,
  557. integration.CuboxEnabled,
  558. integration.CuboxAPILink,
  559. integration.DiscordEnabled,
  560. integration.DiscordWebhookLink,
  561. integration.SlackEnabled,
  562. integration.SlackWebhookLink,
  563. integration.PushoverEnabled,
  564. integration.PushoverUser,
  565. integration.PushoverToken,
  566. integration.PushoverDevice,
  567. integration.PushoverPrefix,
  568. integration.RSSBridgeToken,
  569. integration.UserID,
  570. )
  571. if err != nil {
  572. return fmt.Errorf(`store: unable to update integration record: %v`, err)
  573. }
  574. return nil
  575. }
  576. // HasSaveEntry returns true if the given user can save articles to third-parties.
  577. func (s *Storage) HasSaveEntry(userID int64) (result bool) {
  578. query := `
  579. SELECT
  580. true
  581. FROM
  582. integrations
  583. WHERE
  584. user_id=$1
  585. AND
  586. (
  587. pinboard_enabled='t' OR
  588. instapaper_enabled='t' OR
  589. wallabag_enabled='t' OR
  590. notion_enabled='t' OR
  591. nunux_keeper_enabled='t' OR
  592. espial_enabled='t' OR
  593. readwise_enabled='t' OR
  594. pocket_enabled='t' OR
  595. linkace_enabled='t' OR
  596. linkding_enabled='t' OR
  597. linkwarden_enabled='t' OR
  598. apprise_enabled='t' OR
  599. shiori_enabled='t' OR
  600. readeck_enabled='t' OR
  601. shaarli_enabled='t' OR
  602. webhook_enabled='t' OR
  603. omnivore_enabled='t' OR
  604. raindrop_enabled='t' OR
  605. betula_enabled='t' OR
  606. cubox_enabled='t' OR
  607. discord_enabled='t' OR
  608. slack_enabled='t'
  609. )
  610. `
  611. if err := s.db.QueryRow(query, userID).Scan(&result); err != nil {
  612. result = false
  613. }
  614. return result
  615. }