| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package api // import "miniflux.app/v2/internal/api"
- import (
- json_parser "encoding/json"
- "errors"
- "net/http"
- "strconv"
- "time"
- "miniflux.app/v2/internal/config"
- "miniflux.app/v2/internal/http/request"
- "miniflux.app/v2/internal/http/response/json"
- "miniflux.app/v2/internal/integration"
- "miniflux.app/v2/internal/mediaproxy"
- "miniflux.app/v2/internal/model"
- "miniflux.app/v2/internal/reader/processor"
- "miniflux.app/v2/internal/reader/readingtime"
- "miniflux.app/v2/internal/storage"
- "miniflux.app/v2/internal/validator"
- )
- func (h *handler) getEntryFromBuilder(w http.ResponseWriter, r *http.Request, b *storage.EntryQueryBuilder) {
- entry, err := b.GetEntry()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if entry == nil {
- json.NotFound(w, r)
- return
- }
- entry.Content = mediaproxy.RewriteDocumentWithAbsoluteProxyURL(h.router, entry.Content)
- entry.Enclosures.ProxifyEnclosureURL(h.router, config.Opts.MediaProxyMode(), config.Opts.MediaProxyResourceTypes())
- json.OK(w, r, entry)
- }
- func (h *handler) getFeedEntry(w http.ResponseWriter, r *http.Request) {
- feedID := request.RouteInt64Param(r, "feedID")
- entryID := request.RouteInt64Param(r, "entryID")
- builder := h.store.NewEntryQueryBuilder(request.UserID(r))
- builder.WithFeedID(feedID)
- builder.WithEntryID(entryID)
- builder.WithoutStatus(model.EntryStatusRemoved)
- h.getEntryFromBuilder(w, r, builder)
- }
- func (h *handler) getCategoryEntry(w http.ResponseWriter, r *http.Request) {
- categoryID := request.RouteInt64Param(r, "categoryID")
- entryID := request.RouteInt64Param(r, "entryID")
- builder := h.store.NewEntryQueryBuilder(request.UserID(r))
- builder.WithCategoryID(categoryID)
- builder.WithEntryID(entryID)
- builder.WithoutStatus(model.EntryStatusRemoved)
- h.getEntryFromBuilder(w, r, builder)
- }
- func (h *handler) getEntry(w http.ResponseWriter, r *http.Request) {
- entryID := request.RouteInt64Param(r, "entryID")
- builder := h.store.NewEntryQueryBuilder(request.UserID(r))
- builder.WithEntryID(entryID)
- builder.WithoutStatus(model.EntryStatusRemoved)
- h.getEntryFromBuilder(w, r, builder)
- }
- func (h *handler) getFeedEntries(w http.ResponseWriter, r *http.Request) {
- feedID := request.RouteInt64Param(r, "feedID")
- h.findEntries(w, r, feedID, 0)
- }
- func (h *handler) getCategoryEntries(w http.ResponseWriter, r *http.Request) {
- categoryID := request.RouteInt64Param(r, "categoryID")
- h.findEntries(w, r, 0, categoryID)
- }
- func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
- h.findEntries(w, r, 0, 0)
- }
- func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int64, categoryID int64) {
- statuses := request.QueryStringParamList(r, "status")
- for _, status := range statuses {
- if err := validator.ValidateEntryStatus(status); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- }
- order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
- if err := validator.ValidateEntryOrder(order); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
- if err := validator.ValidateDirection(direction); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- limit := request.QueryIntParam(r, "limit", 100)
- offset := request.QueryIntParam(r, "offset", 0)
- if err := validator.ValidateRange(offset, limit); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- userID := request.UserID(r)
- categoryID = request.QueryInt64Param(r, "category_id", categoryID)
- if categoryID > 0 && !h.store.CategoryIDExists(userID, categoryID) {
- json.BadRequest(w, r, errors.New("invalid category ID"))
- return
- }
- feedID = request.QueryInt64Param(r, "feed_id", feedID)
- if feedID > 0 && !h.store.FeedExists(userID, feedID) {
- json.BadRequest(w, r, errors.New("invalid feed ID"))
- return
- }
- tags := request.QueryStringParamList(r, "tags")
- builder := h.store.NewEntryQueryBuilder(userID)
- builder.WithFeedID(feedID)
- builder.WithCategoryID(categoryID)
- builder.WithStatuses(statuses)
- builder.WithSorting(order, direction)
- builder.WithOffset(offset)
- builder.WithLimit(limit)
- builder.WithTags(tags)
- builder.WithEnclosures()
- builder.WithoutStatus(model.EntryStatusRemoved)
- if request.HasQueryParam(r, "globally_visible") {
- globallyVisible := request.QueryBoolParam(r, "globally_visible", true)
- if globallyVisible {
- builder.WithGloballyVisible()
- }
- }
- configureFilters(builder, r)
- entries, err := builder.GetEntries()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- count, err := builder.CountEntries()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- for i := range entries {
- entries[i].Content = mediaproxy.RewriteDocumentWithAbsoluteProxyURL(h.router, entries[i].Content)
- }
- json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
- }
- func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
- var entriesStatusUpdateRequest model.EntriesStatusUpdateRequest
- if err := json_parser.NewDecoder(r.Body).Decode(&entriesStatusUpdateRequest); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- if err := validator.ValidateEntriesStatusUpdateRequest(&entriesStatusUpdateRequest); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- if err := h.store.SetEntriesStatus(request.UserID(r), entriesStatusUpdateRequest.EntryIDs, entriesStatusUpdateRequest.Status); err != nil {
- json.ServerError(w, r, err)
- return
- }
- json.NoContent(w, r)
- }
- func (h *handler) toggleStarred(w http.ResponseWriter, r *http.Request) {
- entryID := request.RouteInt64Param(r, "entryID")
- if err := h.store.ToggleStarred(request.UserID(r), entryID); err != nil {
- json.ServerError(w, r, err)
- return
- }
- json.NoContent(w, r)
- }
- func (h *handler) saveEntry(w http.ResponseWriter, r *http.Request) {
- entryID := request.RouteInt64Param(r, "entryID")
- builder := h.store.NewEntryQueryBuilder(request.UserID(r))
- builder.WithEntryID(entryID)
- builder.WithoutStatus(model.EntryStatusRemoved)
- if !h.store.HasSaveEntry(request.UserID(r)) {
- json.BadRequest(w, r, errors.New("no third-party integration enabled"))
- return
- }
- entry, err := builder.GetEntry()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if entry == nil {
- json.NotFound(w, r)
- return
- }
- settings, err := h.store.Integration(request.UserID(r))
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- go integration.SendEntry(entry, settings)
- json.Accepted(w, r)
- }
- func (h *handler) updateEntry(w http.ResponseWriter, r *http.Request) {
- var entryUpdateRequest model.EntryUpdateRequest
- if err := json_parser.NewDecoder(r.Body).Decode(&entryUpdateRequest); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- if err := validator.ValidateEntryModification(&entryUpdateRequest); err != nil {
- json.BadRequest(w, r, err)
- return
- }
- loggedUserID := request.UserID(r)
- entryID := request.RouteInt64Param(r, "entryID")
- entryBuilder := h.store.NewEntryQueryBuilder(loggedUserID)
- entryBuilder.WithEntryID(entryID)
- entryBuilder.WithoutStatus(model.EntryStatusRemoved)
- entry, err := entryBuilder.GetEntry()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if entry == nil {
- json.NotFound(w, r)
- return
- }
- user, err := h.store.UserByID(loggedUserID)
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if user == nil {
- json.NotFound(w, r)
- return
- }
- entryUpdateRequest.Patch(entry)
- if user.ShowReadingTime {
- entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
- }
- if err := h.store.UpdateEntryTitleAndContent(entry); err != nil {
- json.ServerError(w, r, err)
- return
- }
- json.Created(w, r, entry)
- }
- func (h *handler) fetchContent(w http.ResponseWriter, r *http.Request) {
- loggedUserID := request.UserID(r)
- entryID := request.RouteInt64Param(r, "entryID")
- entryBuilder := h.store.NewEntryQueryBuilder(loggedUserID)
- entryBuilder.WithEntryID(entryID)
- entryBuilder.WithoutStatus(model.EntryStatusRemoved)
- entry, err := entryBuilder.GetEntry()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if entry == nil {
- json.NotFound(w, r)
- return
- }
- user, err := h.store.UserByID(loggedUserID)
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if user == nil {
- json.NotFound(w, r)
- return
- }
- feedBuilder := storage.NewFeedQueryBuilder(h.store, loggedUserID)
- feedBuilder.WithFeedID(entry.FeedID)
- feed, err := feedBuilder.GetFeed()
- if err != nil {
- json.ServerError(w, r, err)
- return
- }
- if feed == nil {
- json.NotFound(w, r)
- return
- }
- if err := processor.ProcessEntryWebPage(feed, entry, user); err != nil {
- json.ServerError(w, r, err)
- return
- }
- shouldUpdateContent := request.QueryBoolParam(r, "update_content", false)
- if shouldUpdateContent {
- if err := h.store.UpdateEntryTitleAndContent(entry); err != nil {
- json.ServerError(w, r, err)
- return
- }
- json.OK(w, r, map[string]any{"content": mediaproxy.RewriteDocumentWithRelativeProxyURL(h.router, entry.Content), "reading_time": entry.ReadingTime})
- return
- }
- json.OK(w, r, map[string]string{"content": entry.Content})
- }
- func (h *handler) flushHistory(w http.ResponseWriter, r *http.Request) {
- loggedUserID := request.UserID(r)
- go h.store.FlushHistory(loggedUserID)
- json.Accepted(w, r)
- }
- func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
- if beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0); beforeEntryID > 0 {
- builder.BeforeEntryID(beforeEntryID)
- }
- if afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0); afterEntryID > 0 {
- builder.AfterEntryID(afterEntryID)
- }
- if beforePublishedTimestamp := request.QueryInt64Param(r, "before", 0); beforePublishedTimestamp > 0 {
- builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
- }
- if afterPublishedTimestamp := request.QueryInt64Param(r, "after", 0); afterPublishedTimestamp > 0 {
- builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
- }
- if beforePublishedTimestamp := request.QueryInt64Param(r, "published_before", 0); beforePublishedTimestamp > 0 {
- builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
- }
- if afterPublishedTimestamp := request.QueryInt64Param(r, "published_after", 0); afterPublishedTimestamp > 0 {
- builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
- }
- if beforeChangedTimestamp := request.QueryInt64Param(r, "changed_before", 0); beforeChangedTimestamp > 0 {
- builder.BeforeChangedDate(time.Unix(beforeChangedTimestamp, 0))
- }
- if afterChangedTimestamp := request.QueryInt64Param(r, "changed_after", 0); afterChangedTimestamp > 0 {
- builder.AfterChangedDate(time.Unix(afterChangedTimestamp, 0))
- }
- if categoryID := request.QueryInt64Param(r, "category_id", 0); categoryID > 0 {
- builder.WithCategoryID(categoryID)
- }
- if request.HasQueryParam(r, "starred") {
- starred, err := strconv.ParseBool(r.URL.Query().Get("starred"))
- if err == nil {
- builder.WithStarred(starred)
- }
- }
- if searchQuery := request.QueryStringParam(r, "search", ""); searchQuery != "" {
- builder.WithSearchQuery(searchQuery)
- }
- }
|