integration.go 16 KB

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