miniflux/model/entry.go

94 lines
2.6 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 06:51:50 +02:00
package model // import "miniflux.app/model"
2017-11-20 06:10:04 +01:00
import (
"fmt"
"time"
)
2017-11-22 00:46:59 +01:00
// Entry statuses
2017-11-20 06:10:04 +01:00
const (
EntryStatusUnread = "unread"
EntryStatusRead = "read"
EntryStatusRemoved = "removed"
DefaultSortingOrder = "published_at"
2017-12-03 02:04:01 +01:00
DefaultSortingDirection = "asc"
2017-11-20 06:10:04 +01:00
)
2017-11-22 00:46:59 +01:00
// Entry represents a feed item in the system.
2017-11-20 06:10:04 +01:00
type Entry struct {
2018-04-07 22:50:45 +02:00
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
FeedID int64 `json:"feed_id"`
Status string `json:"status"`
Hash string `json:"hash"`
Title string `json:"title"`
URL string `json:"url"`
CommentsURL string `json:"comments_url"`
2018-04-07 22:50:45 +02:00
Date time.Time `json:"published_at"`
Content string `json:"content"`
Author string `json:"author"`
ShareCode string `json:"share_code"`
2018-04-07 22:50:45 +02:00
Starred bool `json:"starred"`
Enclosures EnclosureList `json:"enclosures,omitempty"`
Feed *Feed `json:"feed,omitempty"`
2017-11-20 06:10:04 +01:00
}
2017-11-22 00:46:59 +01:00
// Entries represents a list of entries.
2017-11-20 06:10:04 +01:00
type Entries []*Entry
2017-11-22 00:46:59 +01:00
// ValidateEntryStatus makes sure the entry status is valid.
2017-11-20 06:10:04 +01:00
func ValidateEntryStatus(status string) error {
switch status {
case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
return nil
}
return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, EntryStatusRemoved)
}
2017-11-22 00:46:59 +01:00
// ValidateEntryOrder makes sure the sorting order is valid.
2017-11-20 06:10:04 +01:00
func ValidateEntryOrder(order string) error {
switch order {
case "id", "status", "changed_at", "published_at", "category_title", "category_id":
2017-11-20 06:10:04 +01:00
return nil
}
return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "category_title", "category_id"`)
2017-11-20 06:10:04 +01:00
}
2017-11-22 00:46:59 +01:00
// ValidateDirection makes sure the sorting direction is valid.
2017-11-20 06:10:04 +01:00
func ValidateDirection(direction string) error {
switch direction {
case "asc", "desc":
return nil
}
return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
}
2017-11-27 00:07:59 +01:00
// ValidateRange makes sure the offset/limit values are valid.
func ValidateRange(offset, limit int) error {
if offset < 0 {
return fmt.Errorf(`Offset value should be >= 0`)
}
if limit < 0 {
return fmt.Errorf(`Limit value should be >= 0`)
}
return nil
}
2017-12-03 02:04:01 +01:00
// OppositeDirection returns the opposite sorting direction.
func OppositeDirection(direction string) string {
2017-11-20 06:10:04 +01:00
if direction == "asc" {
return "desc"
}
return "asc"
}