integration.go 14 KB

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