payload.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. FetchViaProxy bool `json:"fetch_via_proxy"`
  47. ScraperRules string `json:"scraper_rules"`
  48. RewriteRules string `json:"rewrite_rules"`
  49. BlocklistRules string `json:"blocklist_rules"`
  50. KeeplistRules string `json:"keeplist_rules"`
  51. }
  52. func decodeFeedCreationRequest(r io.ReadCloser) (*feedCreationRequest, error) {
  53. defer r.Close()
  54. var fc feedCreationRequest
  55. decoder := json.NewDecoder(r)
  56. if err := decoder.Decode(&fc); err != nil {
  57. return nil, fmt.Errorf("Invalid JSON payload: %v", err)
  58. }
  59. return &fc, nil
  60. }
  61. type feedModificationRequest struct {
  62. FeedURL *string `json:"feed_url"`
  63. SiteURL *string `json:"site_url"`
  64. Title *string `json:"title"`
  65. ScraperRules *string `json:"scraper_rules"`
  66. RewriteRules *string `json:"rewrite_rules"`
  67. BlocklistRules *string `json:"blocklist_rules"`
  68. KeeplistRules *string `json:"keeplist_rules"`
  69. Crawler *bool `json:"crawler"`
  70. UserAgent *string `json:"user_agent"`
  71. Username *string `json:"username"`
  72. Password *string `json:"password"`
  73. CategoryID *int64 `json:"category_id"`
  74. Disabled *bool `json:"disabled"`
  75. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  76. FetchViaProxy *bool `json:"fetch_via_proxy"`
  77. }
  78. func (f *feedModificationRequest) Update(feed *model.Feed) {
  79. if f.FeedURL != nil && *f.FeedURL != "" {
  80. feed.FeedURL = *f.FeedURL
  81. }
  82. if f.SiteURL != nil && *f.SiteURL != "" {
  83. feed.SiteURL = *f.SiteURL
  84. }
  85. if f.Title != nil && *f.Title != "" {
  86. feed.Title = *f.Title
  87. }
  88. if f.ScraperRules != nil {
  89. feed.ScraperRules = *f.ScraperRules
  90. }
  91. if f.RewriteRules != nil {
  92. feed.RewriteRules = *f.RewriteRules
  93. }
  94. if f.KeeplistRules != nil {
  95. feed.KeeplistRules = *f.KeeplistRules
  96. }
  97. if f.BlocklistRules != nil {
  98. feed.BlocklistRules = *f.BlocklistRules
  99. }
  100. if f.Crawler != nil {
  101. feed.Crawler = *f.Crawler
  102. }
  103. if f.UserAgent != nil {
  104. feed.UserAgent = *f.UserAgent
  105. }
  106. if f.Username != nil {
  107. feed.Username = *f.Username
  108. }
  109. if f.Password != nil {
  110. feed.Password = *f.Password
  111. }
  112. if f.CategoryID != nil && *f.CategoryID > 0 {
  113. feed.Category.ID = *f.CategoryID
  114. }
  115. if f.Disabled != nil {
  116. feed.Disabled = *f.Disabled
  117. }
  118. if f.IgnoreHTTPCache != nil {
  119. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  120. }
  121. if f.FetchViaProxy != nil {
  122. feed.FetchViaProxy = *f.FetchViaProxy
  123. }
  124. }
  125. func decodeFeedModificationRequest(r io.ReadCloser) (*feedModificationRequest, error) {
  126. defer r.Close()
  127. var feed feedModificationRequest
  128. decoder := json.NewDecoder(r)
  129. if err := decoder.Decode(&feed); err != nil {
  130. return nil, fmt.Errorf("Unable to decode feed modification JSON object: %v", err)
  131. }
  132. return &feed, nil
  133. }
  134. func decodeUserCreationRequest(r io.ReadCloser) (*model.User, error) {
  135. defer r.Close()
  136. var user model.User
  137. decoder := json.NewDecoder(r)
  138. if err := decoder.Decode(&user); err != nil {
  139. return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
  140. }
  141. return &user, nil
  142. }
  143. type userModificationRequest struct {
  144. Username *string `json:"username"`
  145. Password *string `json:"password"`
  146. IsAdmin *bool `json:"is_admin"`
  147. Theme *string `json:"theme"`
  148. Language *string `json:"language"`
  149. Timezone *string `json:"timezone"`
  150. EntryDirection *string `json:"entry_sorting_direction"`
  151. EntriesPerPage *int `json:"entries_per_page"`
  152. }
  153. func (u *userModificationRequest) Update(user *model.User) {
  154. if u.Username != nil {
  155. user.Username = *u.Username
  156. }
  157. if u.Password != nil {
  158. user.Password = *u.Password
  159. }
  160. if u.IsAdmin != nil {
  161. user.IsAdmin = *u.IsAdmin
  162. }
  163. if u.Theme != nil {
  164. user.Theme = *u.Theme
  165. }
  166. if u.Language != nil {
  167. user.Language = *u.Language
  168. }
  169. if u.Timezone != nil {
  170. user.Timezone = *u.Timezone
  171. }
  172. if u.EntryDirection != nil {
  173. user.EntryDirection = *u.EntryDirection
  174. }
  175. if u.EntriesPerPage != nil {
  176. user.EntriesPerPage = *u.EntriesPerPage
  177. }
  178. }
  179. func decodeUserModificationRequest(r io.ReadCloser) (*userModificationRequest, error) {
  180. defer r.Close()
  181. var user userModificationRequest
  182. decoder := json.NewDecoder(r)
  183. if err := decoder.Decode(&user); err != nil {
  184. return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
  185. }
  186. return &user, nil
  187. }
  188. func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) {
  189. type payload struct {
  190. EntryIDs []int64 `json:"entry_ids"`
  191. Status string `json:"status"`
  192. }
  193. var p payload
  194. decoder := json.NewDecoder(r)
  195. defer r.Close()
  196. if err := decoder.Decode(&p); err != nil {
  197. return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
  198. }
  199. return p.EntryIDs, p.Status, nil
  200. }
  201. type categoryRequest struct {
  202. Title string `json:"title"`
  203. }
  204. func decodeCategoryRequest(r io.ReadCloser) (*categoryRequest, error) {
  205. var payload categoryRequest
  206. decoder := json.NewDecoder(r)
  207. defer r.Close()
  208. if err := decoder.Decode(&payload); err != nil {
  209. return nil, fmt.Errorf("Unable to decode JSON object: %v", err)
  210. }
  211. return &payload, nil
  212. }