integration.go 12 KB

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