entry.go 2.7 KB

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