integration.go 15 KB

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