integration.go 15 KB

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