payload.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) {
  137. type payload struct {
  138. EntryIDs []int64 `json:"entry_ids"`
  139. Status string `json:"status"`
  140. }
  141. var p payload
  142. decoder := json.NewDecoder(r)
  143. defer r.Close()
  144. if err := decoder.Decode(&p); err != nil {
  145. return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
  146. }
  147. return p.EntryIDs, p.Status, nil
  148. }
  149. type categoryRequest struct {
  150. Title string `json:"title"`
  151. }
  152. func decodeCategoryRequest(r io.ReadCloser) (*categoryRequest, error) {
  153. var payload categoryRequest
  154. decoder := json.NewDecoder(r)
  155. defer r.Close()
  156. if err := decoder.Decode(&payload); err != nil {
  157. return nil, fmt.Errorf("Unable to decode JSON object: %v", err)
  158. }
  159. return &payload, nil
  160. }