entry.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 model // import "miniflux.app/model"
  5. import (
  6. "fmt"
  7. "time"
  8. )
  9. // Entry statuses
  10. const (
  11. EntryStatusUnread = "unread"
  12. EntryStatusRead = "read"
  13. EntryStatusRemoved = "removed"
  14. DefaultSortingOrder = "published_at"
  15. DefaultSortingDirection = "asc"
  16. )
  17. // Entry represents a feed item in the system.
  18. type Entry struct {
  19. ID int64 `json:"id"`
  20. UserID int64 `json:"user_id"`
  21. FeedID int64 `json:"feed_id"`
  22. Status string `json:"status"`
  23. Hash string `json:"hash"`
  24. Title string `json:"title"`
  25. URL string `json:"url"`
  26. CommentsURL string `json:"comments_url"`
  27. Date time.Time `json:"published_at"`
  28. Content string `json:"content"`
  29. Author string `json:"author"`
  30. ShareCode string `json:"share_code"`
  31. Starred bool `json:"starred"`
  32. ReadingTime int `json:"reading_time"`
  33. Enclosures EnclosureList `json:"enclosures,omitempty"`
  34. Feed *Feed `json:"feed,omitempty"`
  35. }
  36. // Entries represents a list of entries.
  37. type Entries []*Entry
  38. // ValidateEntryStatus makes sure the entry status is valid.
  39. func ValidateEntryStatus(status string) error {
  40. switch status {
  41. case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
  42. return nil
  43. }
  44. return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
  45. }
  46. // ValidateEntryOrder makes sure the sorting order is valid.
  47. func ValidateEntryOrder(order string) error {
  48. switch order {
  49. case "id", "status", "changed_at", "published_at", "category_title", "category_id":
  50. return nil
  51. }
  52. return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "category_title", "category_id"`)
  53. }
  54. // ValidateDirection makes sure the sorting direction is valid.
  55. func ValidateDirection(direction string) error {
  56. switch direction {
  57. case "asc", "desc":
  58. return nil
  59. }
  60. return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
  61. }
  62. // ValidateRange makes sure the offset/limit values are valid.
  63. func ValidateRange(offset, limit int) error {
  64. if offset < 0 {
  65. return fmt.Errorf(`Offset value should be >= 0`)
  66. }
  67. if limit < 0 {
  68. return fmt.Errorf(`Limit value should be >= 0`)
  69. }
  70. return nil
  71. }
  72. // OppositeDirection returns the opposite sorting direction.
  73. func OppositeDirection(direction string) string {
  74. if direction == "asc" {
  75. return "desc"
  76. }
  77. return "asc"
  78. }