integration.go 12 KB

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