integration.go 13 KB

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