mirror of
https://github.com/nkanaev/yarr.git
synced 2025-07-09 00:10:09 +00:00
fever items (with_ids, since_id)
This commit is contained in:
parent
57d2437e9c
commit
9b9addf3e6
@ -1,11 +1,12 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nkanaev/yarr/storage"
|
"github.com/nkanaev/yarr/storage"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"encoding/base64"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var feverHandlers = map[string]func(rw http.ResponseWriter, req *http.Request){
|
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"`
|
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 {
|
type FeverFavicon struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,7 +197,62 @@ func FeverFaviconsHandler(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FeverItemsHandler(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) {
|
func FeverLinksHandler(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -62,6 +62,9 @@ type ItemFilter struct {
|
|||||||
FeedID *int64
|
FeedID *int64
|
||||||
Status *ItemStatus
|
Status *ItemStatus
|
||||||
Search *string
|
Search *string
|
||||||
|
|
||||||
|
IDs *[]int64
|
||||||
|
SinceID *int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) CreateItems(items []Item) bool {
|
func (s *Storage) CreateItems(items []Item) bool {
|
||||||
@ -140,20 +143,36 @@ func listQueryPredicate(filter ItemFilter) (string, []interface{}) {
|
|||||||
args = append(args, strings.Join(terms, " "))
|
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"
|
predicate := "1"
|
||||||
if len(cond) > 0 {
|
if len(cond) > 0 {
|
||||||
predicate = strings.Join(cond, " and ")
|
predicate = strings.Join(cond, " and ")
|
||||||
}
|
}
|
||||||
|
|
||||||
return predicate, args
|
return predicate, args
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) ListItems(filter ItemFilter, offset, limit int, newestFirst bool) []Item {
|
func (s *Storage) ListItems(filter ItemFilter, offset, limit int, newestFirst bool) []Item {
|
||||||
predicate, args := listQueryPredicate(filter)
|
predicate, args := listQueryPredicate(filter)
|
||||||
result := make([]Item, 0, 0)
|
result := make([]Item, 0, 0)
|
||||||
order := "desc"
|
|
||||||
|
order := "date desc"
|
||||||
if !newestFirst {
|
if !newestFirst {
|
||||||
order = "asc"
|
order = "date asc"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filter.IDs != nil || filter.SinceID != nil {
|
||||||
|
order = "i.id asc"
|
||||||
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`
|
query := fmt.Sprintf(`
|
||||||
select
|
select
|
||||||
i.id, i.guid, i.feed_id, i.title, i.link, i.description,
|
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
|
from items i
|
||||||
join feeds f on f.id = i.feed_id
|
join feeds f on f.id = i.feed_id
|
||||||
where %s
|
where %s
|
||||||
order by i.date %s
|
order by %s
|
||||||
limit %d offset %d
|
limit %d offset %d
|
||||||
`, predicate, order, limit, offset)
|
`, predicate, order, limit, offset)
|
||||||
rows, err := s.db.Query(query, args...)
|
rows, err := s.db.Query(query, args...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user