payload.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package api // import "miniflux.app/api"
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "miniflux.app/model"
  10. )
  11. type feedIconResponse struct {
  12. ID int64 `json:"id"`
  13. MimeType string `json:"mime_type"`
  14. Data string `json:"data"`
  15. }
  16. type entriesResponse struct {
  17. Total int `json:"total"`
  18. Entries model.Entries `json:"entries"`
  19. }
  20. type subscriptionDiscoveryRequest struct {
  21. URL string `json:"url"`
  22. UserAgent string `json:"user_agent"`
  23. Username string `json:"username"`
  24. Password string `json:"password"`
  25. FetchViaProxy bool `json:"fetch_via_proxy"`
  26. }
  27. func decodeSubscriptionDiscoveryRequest(r io.ReadCloser) (*subscriptionDiscoveryRequest, error) {
  28. defer r.Close()
  29. var s subscriptionDiscoveryRequest
  30. decoder := json.NewDecoder(r)
  31. if err := decoder.Decode(&s); err != nil {
  32. return nil, fmt.Errorf("invalid JSON payload: %v", err)
  33. }
  34. return &s, nil
  35. }
  36. type feedCreationResponse struct {
  37. FeedID int64 `json:"feed_id"`
  38. }
  39. type feedCreationRequest struct {
  40. FeedURL string `json:"feed_url"`
  41. CategoryID int64 `json:"category_id"`
  42. UserAgent string `json:"user_agent"`
  43. Username string `json:"username"`
  44. Password string `json:"password"`
  45. Crawler bool `json:"crawler"`
  46. Disabled bool `json:"disabled"`
  47. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  48. FetchViaProxy bool `json:"fetch_via_proxy"`
  49. ScraperRules string `json:"scraper_rules"`
  50. RewriteRules string `json:"rewrite_rules"`
  51. BlocklistRules string `json:"blocklist_rules"`
  52. KeeplistRules string `json:"keeplist_rules"`
  53. }
  54. func decodeFeedCreationRequest(r io.ReadCloser) (*feedCreationRequest, error) {
  55. defer r.Close()
  56. var fc feedCreationRequest
  57. decoder := json.NewDecoder(r)
  58. if err := decoder.Decode(&fc); err != nil {
  59. return nil, fmt.Errorf("Invalid JSON payload: %v", err)
  60. }
  61. return &fc, nil
  62. }
  63. type feedModificationRequest struct {
  64. FeedURL *string `json:"feed_url"`
  65. SiteURL *string `json:"site_url"`
  66. Title *string `json:"title"`
  67. ScraperRules *string `json:"scraper_rules"`
  68. RewriteRules *string `json:"rewrite_rules"`
  69. BlocklistRules *string `json:"blocklist_rules"`
  70. KeeplistRules *string `json:"keeplist_rules"`
  71. Crawler *bool `json:"crawler"`
  72. UserAgent *string `json:"user_agent"`
  73. Username *string `json:"username"`
  74. Password *string `json:"password"`
  75. CategoryID *int64 `json:"category_id"`
  76. Disabled *bool `json:"disabled"`
  77. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  78. FetchViaProxy *bool `json:"fetch_via_proxy"`
  79. }
  80. func (f *feedModificationRequest) Update(feed *model.Feed) {
  81. if f.FeedURL != nil && *f.FeedURL != "" {
  82. feed.FeedURL = *f.FeedURL
  83. }
  84. if f.SiteURL != nil && *f.SiteURL != "" {
  85. feed.SiteURL = *f.SiteURL
  86. }
  87. if f.Title != nil && *f.Title != "" {
  88. feed.Title = *f.Title
  89. }
  90. if f.ScraperRules != nil {
  91. feed.ScraperRules = *f.ScraperRules
  92. }
  93. if f.RewriteRules != nil {
  94. feed.RewriteRules = *f.RewriteRules
  95. }
  96. if f.KeeplistRules != nil {
  97. feed.KeeplistRules = *f.KeeplistRules
  98. }
  99. if f.BlocklistRules != nil {
  100. feed.BlocklistRules = *f.BlocklistRules
  101. }
  102. if f.Crawler != nil {
  103. feed.Crawler = *f.Crawler
  104. }
  105. if f.UserAgent != nil {
  106. feed.UserAgent = *f.UserAgent
  107. }
  108. if f.Username != nil {
  109. feed.Username = *f.Username
  110. }
  111. if f.Password != nil {
  112. feed.Password = *f.Password
  113. }
  114. if f.CategoryID != nil && *f.CategoryID > 0 {
  115. feed.Category.ID = *f.CategoryID
  116. }
  117. if f.Disabled != nil {
  118. feed.Disabled = *f.Disabled
  119. }
  120. if f.IgnoreHTTPCache != nil {
  121. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  122. }
  123. if f.FetchViaProxy != nil {
  124. feed.FetchViaProxy = *f.FetchViaProxy
  125. }
  126. }
  127. func decodeFeedModificationRequest(r io.ReadCloser) (*feedModificationRequest, error) {
  128. defer r.Close()
  129. var feed feedModificationRequest
  130. decoder := json.NewDecoder(r)
  131. if err := decoder.Decode(&feed); err != nil {
  132. return nil, fmt.Errorf("Unable to decode feed modification JSON object: %v", err)
  133. }
  134. return &feed, nil
  135. }
  136. type userCreationRequest struct {
  137. Username string `json:"username"`
  138. Password string `json:"password"`
  139. IsAdmin bool `json:"is_admin"`
  140. GoogleID string `json:"google_id"`
  141. OpenIDConnectID string `json:"openid_connect_id"`
  142. }
  143. func decodeUserCreationRequest(r io.ReadCloser) (*userCreationRequest, error) {
  144. defer r.Close()
  145. var request userCreationRequest
  146. decoder := json.NewDecoder(r)
  147. if err := decoder.Decode(&request); err != nil {
  148. return nil, fmt.Errorf("Unable to decode user creation JSON object: %v", err)
  149. }
  150. return &request, nil
  151. }
  152. type userModificationRequest struct {
  153. Username *string `json:"username"`
  154. Password *string `json:"password"`
  155. IsAdmin *bool `json:"is_admin"`
  156. Theme *string `json:"theme"`
  157. Language *string `json:"language"`
  158. Timezone *string `json:"timezone"`
  159. EntryDirection *string `json:"entry_sorting_direction"`
  160. Stylesheet *string `json:"stylesheet"`
  161. GoogleID *string `json:"google_id"`
  162. OpenIDConnectID *string `json:"openid_connect_id"`
  163. EntriesPerPage *int `json:"entries_per_page"`
  164. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  165. ShowReadingTime *bool `json:"show_reading_time"`
  166. EntrySwipe *bool `json:"entry_swipe"`
  167. }
  168. func (u *userModificationRequest) Update(user *model.User) {
  169. if u.Username != nil {
  170. user.Username = *u.Username
  171. }
  172. if u.Password != nil {
  173. user.Password = *u.Password
  174. }
  175. if u.IsAdmin != nil {
  176. user.IsAdmin = *u.IsAdmin
  177. }
  178. if u.Theme != nil {
  179. user.Theme = *u.Theme
  180. }
  181. if u.Language != nil {
  182. user.Language = *u.Language
  183. }
  184. if u.Timezone != nil {
  185. user.Timezone = *u.Timezone
  186. }
  187. if u.EntryDirection != nil {
  188. user.EntryDirection = *u.EntryDirection
  189. }
  190. if u.Stylesheet != nil {
  191. user.Stylesheet = *u.Stylesheet
  192. }
  193. if u.GoogleID != nil {
  194. user.GoogleID = *u.GoogleID
  195. }
  196. if u.OpenIDConnectID != nil {
  197. user.OpenIDConnectID = *u.OpenIDConnectID
  198. }
  199. if u.EntriesPerPage != nil {
  200. user.EntriesPerPage = *u.EntriesPerPage
  201. }
  202. if u.KeyboardShortcuts != nil {
  203. user.KeyboardShortcuts = *u.KeyboardShortcuts
  204. }
  205. if u.ShowReadingTime != nil {
  206. user.ShowReadingTime = *u.ShowReadingTime
  207. }
  208. if u.EntrySwipe != nil {
  209. user.EntrySwipe = *u.EntrySwipe
  210. }
  211. }
  212. func decodeUserModificationRequest(r io.ReadCloser) (*userModificationRequest, error) {
  213. defer r.Close()
  214. var request userModificationRequest
  215. decoder := json.NewDecoder(r)
  216. if err := decoder.Decode(&request); err != nil {
  217. return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
  218. }
  219. return &request, nil
  220. }
  221. func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) {
  222. type payload struct {
  223. EntryIDs []int64 `json:"entry_ids"`
  224. Status string `json:"status"`
  225. }
  226. var p payload
  227. decoder := json.NewDecoder(r)
  228. defer r.Close()
  229. if err := decoder.Decode(&p); err != nil {
  230. return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
  231. }
  232. return p.EntryIDs, p.Status, nil
  233. }
  234. type categoryRequest struct {
  235. Title string `json:"title"`
  236. }
  237. func decodeCategoryRequest(r io.ReadCloser) (*categoryRequest, error) {
  238. var payload categoryRequest
  239. decoder := json.NewDecoder(r)
  240. defer r.Close()
  241. if err := decoder.Decode(&payload); err != nil {
  242. return nil, fmt.Errorf("Unable to decode JSON object: %v", err)
  243. }
  244. return &payload, nil
  245. }