mirror of
https://github.com/nkanaev/yarr.git
synced 2025-07-08 16:00:11 +00:00
fever items (with_ids, since_id)
This commit is contained in:
parent
57d2437e9c
commit
9b9addf3e6
@ -1,11 +1,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/nkanaev/yarr/storage"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
var feverHandlers = map[string]func(rw http.ResponseWriter, req *http.Request){
|
||||
@ -40,8 +41,20 @@ type FeverFeed struct {
|
||||
LastUpdatedOnTime int64 `json:"last_updated_on_time"`
|
||||
}
|
||||
|
||||
type FeverItem struct {
|
||||
ID int64 `json:"id"`
|
||||
FeedID int64 `json:"feed_id"`
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
HTML string `json:"html"`
|
||||
Url string `json:"url"`
|
||||
IsSaved int `json:"is_saved"`
|
||||
IsRead int `json:"is_read"`
|
||||
CreatedOnTime int64 `json:"created_on_time"`
|
||||
}
|
||||
|
||||
type FeverFavicon struct {
|
||||
ID int64 `json:"id"`
|
||||
ID int64 `json:"id"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
@ -184,7 +197,62 @@ func FeverFaviconsHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
func FeverItemsHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
filter := storage.ItemFilter{}
|
||||
query := req.URL.Query()
|
||||
if _, ok := query["with_ids"]; ok {
|
||||
ids := make([]int64, 0)
|
||||
for _, idstr := range strings.Split(query.Get("with_ids"), ",") {
|
||||
if idnum, err := strconv.ParseInt(idstr, 10, 64); err == nil {
|
||||
ids = append(ids, idnum)
|
||||
}
|
||||
}
|
||||
filter.IDs = &ids
|
||||
}
|
||||
|
||||
if _, ok := query["since_id"]; ok {
|
||||
idstr := query.Get("since_id")
|
||||
if idnum, err := strconv.ParseInt(idstr, 10, 64); err == nil {
|
||||
filter.SinceID = &idnum
|
||||
}
|
||||
}
|
||||
|
||||
items := db(req).ListItems(filter, 0, 50, true)
|
||||
|
||||
feverItems := make([]FeverItem, len(items))
|
||||
for i, item := range items {
|
||||
date := item.Date
|
||||
if date == nil {
|
||||
date = item.DateUpdated
|
||||
}
|
||||
time := int64(0)
|
||||
if date != nil {
|
||||
time = date.UnixNano() / 1000_000_000
|
||||
}
|
||||
|
||||
isSaved := 0
|
||||
if item.Status == storage.STARRED {
|
||||
isSaved = 1
|
||||
}
|
||||
isRead := 0
|
||||
if item.Status == storage.READ {
|
||||
isRead = 1
|
||||
}
|
||||
feverItems[i] = FeverItem{
|
||||
ID: item.Id,
|
||||
FeedID: item.FeedId,
|
||||
Title: item.Title,
|
||||
Author: item.Author,
|
||||
HTML: item.Content,
|
||||
Url: item.Link,
|
||||
IsSaved: isSaved,
|
||||
IsRead: isRead,
|
||||
CreatedOnTime: time,
|
||||
}
|
||||
}
|
||||
|
||||
writeFeverJSON(rw, map[string]interface{}{
|
||||
"items": feverItems,
|
||||
})
|
||||
}
|
||||
|
||||
func FeverLinksHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
|
@ -62,6 +62,9 @@ type ItemFilter struct {
|
||||
FeedID *int64
|
||||
Status *ItemStatus
|
||||
Search *string
|
||||
|
||||
IDs *[]int64
|
||||
SinceID *int64
|
||||
}
|
||||
|
||||
func (s *Storage) CreateItems(items []Item) bool {
|
||||
@ -140,20 +143,36 @@ func listQueryPredicate(filter ItemFilter) (string, []interface{}) {
|
||||
args = append(args, strings.Join(terms, " "))
|
||||
}
|
||||
|
||||
if filter.IDs != nil {
|
||||
cond = append(cond, "i.id in ?")
|
||||
args = append(args, filter.IDs)
|
||||
}
|
||||
if filter.SinceID != nil {
|
||||
cond = append(cond, "i.id > ?")
|
||||
args = append(args, filter.SinceID)
|
||||
}
|
||||
|
||||
predicate := "1"
|
||||
if len(cond) > 0 {
|
||||
predicate = strings.Join(cond, " and ")
|
||||
}
|
||||
|
||||
return predicate, args
|
||||
}
|
||||
|
||||
func (s *Storage) ListItems(filter ItemFilter, offset, limit int, newestFirst bool) []Item {
|
||||
predicate, args := listQueryPredicate(filter)
|
||||
result := make([]Item, 0, 0)
|
||||
order := "desc"
|
||||
|
||||
order := "date desc"
|
||||
if !newestFirst {
|
||||
order = "asc"
|
||||
order = "date asc"
|
||||
}
|
||||
|
||||
if filter.IDs != nil || filter.SinceID != nil {
|
||||
order = "i.id asc"
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
select
|
||||
i.id, i.guid, i.feed_id, i.title, i.link, i.description,
|
||||
@ -161,7 +180,7 @@ func (s *Storage) ListItems(filter ItemFilter, offset, limit int, newestFirst bo
|
||||
from items i
|
||||
join feeds f on f.id = i.feed_id
|
||||
where %s
|
||||
order by i.date %s
|
||||
order by %s
|
||||
limit %d offset %d
|
||||
`, predicate, order, limit, offset)
|
||||
rows, err := s.db.Query(query, args...)
|
||||
|
Loading…
x
Reference in New Issue
Block a user