intake/core/item.go

64 lines
1.3 KiB
Go

package core
import (
"encoding/json"
"fmt"
"log"
)
type Item struct {
Source string `json:"source"`
Id string `json:"id"`
Created int `json:"created"`
Active bool `json:"active"`
Title string `json:"title"`
Author string `json:"author"`
Body string `json:"body"`
Link string `json:"link"`
Time int `json:"time"`
}
// Whether an item that no longer appears in a fetch can be deleted.
func (item Item) Deletable() bool {
return !item.Active
}
func FormatAsHeadline(item Item) string {
title := item.Title
if title == "" {
title = item.Id
}
return title
}
func FormatAsJson(item Item) string {
data, err := json.Marshal(item)
if err != nil {
log.Fatalf("error: failed to serialize %s/%s: %v", item.Source, item.Id, err)
}
return string(data)
}
func FormatAsShort(item Item) string {
return fmt.Sprintf("%s/%s", item.Source, item.Id)
}
func FormatAs(format string) (func(item Item) string, error) {
switch format {
case "headlines":
return FormatAsHeadline, nil
case "json":
return FormatAsJson, nil
case "short":
return FormatAsShort, nil
default:
return nil, fmt.Errorf("invalid format '%s'", format)
}
}
var AvailableFormats = map[string]string{
"headlines": "Only item titles",
"json": "Full item JSON",
"short": "Item source and id",
}