integration.go 14 KB

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