integration.go 15 KB

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