integration.go 13 KB

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