Files
pages/internal/server/feeds.go
T
Anya Tokugawa bf9a9c2847
Build and publish container images / images (push) Successful in 10m33s
feat(feeds): inherit FreshRSS folders in cached subscription tree
2026-11-08 13:34:22 +09:00

220 lines
6.2 KiB
Go

package server
import (
"net/http"
"net/url"
"strconv"
"strings"
"github.com/google/uuid"
"pageswiki/internal/store"
)
const feedsAPI = "/_/api/v1/extension/organizer.feeds/"
func feedRoute(p string) bool { return p == "/v/feeds" || strings.HasPrefix(p, feedsAPI) }
func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-store")
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "organizer.feeds") {
http.NotFound(w, r)
return
}
actor, active := s.calendarActor(r)
if actor == "" {
s.authRequired(w, r)
return
}
if r.Method != "GET" && r.Method != "HEAD" && !active {
taskError(w, store.ErrAccessDenied)
return
}
if r.URL.Path == "/v/feeds" {
if r.Method != "GET" && r.Method != "HEAD" {
w.WriteHeader(405)
return
}
if _, e := s.store.FeedSources(r.Context(), domain, actor); e != nil {
taskError(w, e)
return
}
s.renderWithContext(w, r, "feeds.gohtml", map[string]any{"Title": "RSS", "FeedWritable": active})
return
}
key := strings.TrimPrefix(r.URL.Path, feedsAPI)
if s.handleFeedAction(w, r, domain, actor, key) {
return
}
if strings.HasPrefix(key, "opml/") {
s.handleFeedOPML(w, r, domain, actor, key)
return
}
switch {
case key == "catalog" && r.Method == "GET":
catalog, e := s.store.FeedCatalogSnapshot(r.Context(), domain, actor, r.URL.Query().Get("source"))
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, catalog)
return
case key == "note" && r.Method == "POST":
var input struct {
RequestID string `json:"request_id"`
Source string `json:"source"`
ID string `json:"id"`
Path string `json:"path"`
Title string `json:"title"`
Body string `json:"body"`
Confirm bool `json:"confirm_publication"`
}
if !taskJSONLimit(w, r, &input, 128<<10) {
return
}
if !input.Confirm {
taskError(w, store.ErrTaskInvalid)
return
}
for _, capability := range []string{"read", "create", "write"} {
if s.authorizePathRequest(r, capability, input.Path) != nil {
taskError(w, store.ErrAccessDenied)
return
}
}
page, e := s.store.CaptureFeedNote(r.Context(), domain, actor, input.RequestID, input.Source, input.ID, input.Path, input.Title, input.Body)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 201, map[string]string{"page_uuid": page})
return
case key == "sources" && r.Method == "GET":
sources, e := s.store.FeedSources(r.Context(), domain, actor)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"sources": sources})
return
case key == "sources" && r.Method == "POST":
var input struct {
store.FeedSource
Username string `json:"username"`
Password string `json:"password"`
}
if !taskJSON(w, r, &input) {
return
}
if !validCalendarSourceURL(input.URL) || len(input.Username) > 256 || len(input.Password) > 4096 {
taskError(w, store.ErrTaskInvalid)
return
}
if input.Provider == "freshrss" {
u, _ := url.Parse(input.URL)
if u.RawQuery != "" || !strings.HasSuffix(strings.TrimRight(u.Path, "/"), "/greader.php") {
taskError(w, store.ErrTaskInvalid)
return
}
}
if input.UUID == "" {
input.UUID = uuid.NewString()
}
if input.Version == 0 && input.Provider == "freshrss" {
if input.Username == "" || input.Password == "" {
taskError(w, store.ErrTaskInvalid)
return
}
cipher, e := s.store.SealFeedCredential(domain, actor, input.UUID, input.Username, input.Password)
if e != nil {
taskError(w, e)
return
}
input.Credential = cipher
} else if input.Username != "" || input.Password != "" {
taskError(w, store.ErrTaskInvalid)
return
}
v, e := s.store.SaveFeedSource(r.Context(), domain, actor, input.FeedSource)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"source": v})
return
case strings.HasPrefix(key, "sources/") && r.Method == "POST":
parts := strings.Split(key, "/")
if len(parts) != 3 {
http.NotFound(w, r)
return
}
var input struct {
Version int64 `json:"version"`
}
if !taskJSON(w, r, &input) {
return
}
if e := s.store.FeedSourceAction(r.Context(), domain, actor, parts[1], parts[2], input.Version); e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]bool{"ok": true})
return
case key == "items" && r.Method == "GET":
q := r.URL.Query()
before := int64(-1)
if q.Has("before") {
var e error
before, e = strconv.ParseInt(q.Get("before"), 10, 64)
if e != nil || before < 0 {
taskError(w, store.ErrTaskInvalid)
return
}
}
items, e := s.store.FeedItems(r.Context(), domain, actor, q.Get("source"), q.Get("filter"), q.Get("q"), before, q.Get("key"), q.Get("feed"), q.Get("category"), q.Get("category_mode"))
if e != nil {
taskError(w, e)
return
}
more := len(items) > 100
if more {
items = items[:100]
}
writeEventJSON(w, 200, map[string]any{"items": items, "more": more})
return
case key == "item" && r.Method == "GET":
v, e := s.store.FeedItem(r.Context(), domain, actor, r.URL.Query().Get("source"), r.URL.Query().Get("id"))
if e != nil {
taskError(w, e)
return
}
// Defense in depth for restored/older caches, not just freshly fetched data.
v.BodyHTML = feedHTML(v.BodyHTML, v.URL)
writeEventJSON(w, 200, map[string]any{"item": v})
return
case key == "state" && r.Method == "POST":
var input struct {
Source string `json:"source"`
ID string `json:"id"`
Read bool `json:"read"`
Starred bool `json:"starred"`
Version int64 `json:"version"`
}
if !taskJSON(w, r, &input) {
return
}
if e := s.store.SetFeedState(r.Context(), domain, actor, input.Source, input.ID, input.Read, input.Starred, input.Version); e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"version": input.Version + 1})
return
}
http.NotFound(w, r)
}
func init() {
translations["en"]["feeds.title"] = "RSS"
translations["ja"]["feeds.title"] = "RSS"
translations["en"]["feeds.help"] = "Private local feeds and FreshRSS integration with optional read/star updates"
translations["ja"]["feeds.help"] = "個人用の簡易RSSとFreshRSS連携(既読・star変更は任意)"
}