entry.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. Starred bool `json:"starred"`
  31. Enclosures EnclosureList `json:"enclosures,omitempty"`
  32. Feed *Feed `json:"feed,omitempty"`
  33. Category *Category `json:"category,omitempty"`
  34. }
  35. // Entries represents a list of entries.
  36. type Entries []*Entry
  37. // ValidateEntryStatus makes sure the entry status is valid.
  38. func ValidateEntryStatus(status string) error {
  39. switch status {
  40. case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
  41. return nil
  42. }
  43. return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
  44. }
  45. // ValidateEntryOrder makes sure the sorting order is valid.
  46. func ValidateEntryOrder(order string) error {
  47. switch order {
  48. case "id", "status", "published_at", "category_title", "category_id":
  49. return nil
  50. }
  51. return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "published_at", "category_title", "category_id"`)
  52. }
  53. // ValidateDirection makes sure the sorting direction is valid.
  54. func ValidateDirection(direction string) error {
  55. switch direction {
  56. case "asc", "desc":
  57. return nil
  58. }
  59. return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
  60. }
  61. // ValidateRange makes sure the offset/limit values are valid.
  62. func ValidateRange(offset, limit int) error {
  63. if offset < 0 {
  64. return fmt.Errorf(`Offset value should be >= 0`)
  65. }
  66. if limit < 0 {
  67. return fmt.Errorf(`Limit value should be >= 0`)
  68. }
  69. return nil
  70. }
  71. // OppositeDirection returns the opposite sorting direction.
  72. func OppositeDirection(direction string) string {
  73. if direction == "asc" {
  74. return "desc"
  75. }
  76. return "asc"
  77. }