1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

First commit

This commit is contained in:
Frédéric Guillot 2017-11-19 21:10:04 -08:00
commit 8ffb773f43
2121 changed files with 1118910 additions and 0 deletions

View file

@ -0,0 +1,97 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// CreateCategory is the API handler to create a new category.
func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
category, err := payload.DecodeCategoryPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
category.UserID = ctx.GetUserID()
if err := category.ValidateCategoryCreation(); err != nil {
response.Json().ServerError(err)
return
}
err = c.store.CreateCategory(category)
if err != nil {
response.Json().ServerError(errors.New("Unable to create this category"))
return
}
response.Json().Created(category)
}
// UpdateCategory is the API handler to update a category.
func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
categoryID, err := request.GetIntegerParam("categoryID")
if err != nil {
response.Json().BadRequest(err)
return
}
category, err := payload.DecodeCategoryPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
category.UserID = ctx.GetUserID()
category.ID = categoryID
if err := category.ValidateCategoryModification(); err != nil {
response.Json().BadRequest(err)
return
}
err = c.store.UpdateCategory(category)
if err != nil {
response.Json().ServerError(errors.New("Unable to update this category"))
return
}
response.Json().Created(category)
}
// GetCategories is the API handler to get a list of categories for a given user.
func (c *Controller) GetCategories(ctx *core.Context, request *core.Request, response *core.Response) {
categories, err := c.store.GetCategories(ctx.GetUserID())
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch categories"))
return
}
response.Json().Standard(categories)
}
// RemoveCategory is the API handler to remove a category.
func (c *Controller) RemoveCategory(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
categoryID, err := request.GetIntegerParam("categoryID")
if err != nil {
response.Json().BadRequest(err)
return
}
if !c.store.CategoryExists(userID, categoryID) {
response.Json().NotFound(errors.New("Category not found"))
return
}
if err := c.store.RemoveCategory(userID, categoryID); err != nil {
response.Json().ServerError(errors.New("Unable to remove this category"))
return
}
response.Json().NoContent()
}

View file

@ -0,0 +1,21 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"github.com/miniflux/miniflux2/reader/feed"
"github.com/miniflux/miniflux2/storage"
)
// Controller holds all handlers for the API.
type Controller struct {
store *storage.Storage
feedHandler *feed.Handler
}
// NewController creates a new controller.
func NewController(store *storage.Storage, feedHandler *feed.Handler) *Controller {
return &Controller{store: store, feedHandler: feedHandler}
}

View file

@ -0,0 +1,156 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// GetEntry is the API handler to get a single feed entry.
func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
entryID, err := request.GetIntegerParam("entryID")
if err != nil {
response.Json().BadRequest(err)
return
}
builder := c.store.GetEntryQueryBuilder(userID, ctx.GetUserTimezone())
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this entry from the database"))
return
}
if entry == nil {
response.Json().NotFound(errors.New("Entry not found"))
return
}
response.Json().Standard(entry)
}
// GetFeedEntries is the API handler to get all feed entries.
func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
status := request.GetQueryStringParam("status", "")
if status != "" {
if err := model.ValidateEntryStatus(status); err != nil {
response.Json().BadRequest(err)
return
}
}
order := request.GetQueryStringParam("order", "id")
if err := model.ValidateEntryOrder(order); err != nil {
response.Json().BadRequest(err)
return
}
direction := request.GetQueryStringParam("direction", "desc")
if err := model.ValidateDirection(direction); err != nil {
response.Json().BadRequest(err)
return
}
limit := request.GetQueryIntegerParam("limit", 100)
offset := request.GetQueryIntegerParam("offset", 0)
builder := c.store.GetEntryQueryBuilder(userID, ctx.GetUserTimezone())
builder.WithFeedID(feedID)
builder.WithStatus(status)
builder.WithOrder(model.DefaultSortingOrder)
builder.WithDirection(model.DefaultSortingDirection)
builder.WithOffset(offset)
builder.WithLimit(limit)
entries, err := builder.GetEntries()
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch the list of entries"))
return
}
count, err := builder.CountEntries()
if err != nil {
response.Json().ServerError(errors.New("Unable to count the number of entries"))
return
}
response.Json().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
}
// SetEntryStatus is the API handler to change the status of an entry.
func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
entryID, err := request.GetIntegerParam("entryID")
if err != nil {
response.Json().BadRequest(err)
return
}
status, err := payload.DecodeEntryStatusPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(errors.New("Invalid JSON payload"))
return
}
if err := model.ValidateEntryStatus(status); err != nil {
response.Json().BadRequest(err)
return
}
builder := c.store.GetEntryQueryBuilder(userID, ctx.GetUserTimezone())
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this entry from the database"))
return
}
if entry == nil {
response.Json().NotFound(errors.New("Entry not found"))
return
}
if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil {
response.Json().ServerError(errors.New("Unable to change entry status"))
return
}
entry, err = builder.GetEntry()
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this entry from the database"))
return
}
response.Json().Standard(entry)
}

View file

@ -0,0 +1,138 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// CreateFeed is the API handler to create a new feed.
func (c *Controller) CreateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL)
if err != nil {
response.Json().ServerError(errors.New("Unable to create this feed"))
return
}
response.Json().Created(feed)
}
// RefreshFeed is the API handler to refresh a feed.
func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
err = c.feedHandler.RefreshFeed(userID, feedID)
if err != nil {
response.Json().ServerError(errors.New("Unable to refresh this feed"))
return
}
response.Json().NoContent()
}
// UpdateFeed is the API handler that is used to update a feed.
func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
newFeed, err := payload.DecodeFeedModificationPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
originalFeed, err := c.store.GetFeedById(userID, feedID)
if err != nil {
response.Json().NotFound(errors.New("Unable to find this feed"))
return
}
if originalFeed == nil {
response.Json().NotFound(errors.New("Feed not found"))
return
}
originalFeed.Merge(newFeed)
if err := c.store.UpdateFeed(originalFeed); err != nil {
response.Json().ServerError(errors.New("Unable to update this feed"))
return
}
response.Json().Created(originalFeed)
}
// GetFeeds is the API handler that get all feeds that belongs to the given user.
func (c *Controller) GetFeeds(ctx *core.Context, request *core.Request, response *core.Response) {
feeds, err := c.store.GetFeeds(ctx.GetUserID())
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch feeds from the database"))
return
}
response.Json().Standard(feeds)
}
// GetFeed is the API handler to get a feed.
func (c *Controller) GetFeed(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
feed, err := c.store.GetFeedById(userID, feedID)
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this feed"))
return
}
if feed == nil {
response.Json().NotFound(errors.New("Feed not found"))
return
}
response.Json().Standard(feed)
}
// RemoveFeed is the API handler to remove a feed.
func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.GetUserID()
feedID, err := request.GetIntegerParam("feedID")
if err != nil {
response.Json().BadRequest(err)
return
}
if !c.store.FeedExists(userID, feedID) {
response.Json().NotFound(errors.New("Feed not found"))
return
}
if err := c.store.RemoveFeed(userID, feedID); err != nil {
response.Json().ServerError(errors.New("Unable to remove this feed"))
return
}
response.Json().NoContent()
}

View file

@ -0,0 +1,35 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"fmt"
"github.com/miniflux/miniflux2/reader/subscription"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// GetSubscriptions is the API handler to find subscriptions.
func (c *Controller) GetSubscriptions(ctx *core.Context, request *core.Request, response *core.Response) {
websiteURL, err := payload.DecodeURLPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
subscriptions, err := subscription.FindSubscriptions(websiteURL)
if err != nil {
response.Json().ServerError(errors.New("Unable to discover subscriptions"))
return
}
if subscriptions == nil {
response.Json().NotFound(fmt.Errorf("No subscription found"))
return
}
response.Json().Standard(subscriptions)
}

View file

@ -0,0 +1,163 @@
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package api
import (
"errors"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
)
// CreateUser is the API handler to create a new user.
func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() {
response.Json().Forbidden()
return
}
user, err := payload.DecodeUserPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
if err := user.ValidateUserCreation(); err != nil {
response.Json().BadRequest(err)
return
}
if c.store.UserExists(user.Username) {
response.Json().BadRequest(errors.New("This user already exists"))
return
}
err = c.store.CreateUser(user)
if err != nil {
response.Json().ServerError(errors.New("Unable to create this user"))
return
}
user.Password = ""
response.Json().Created(user)
}
// UpdateUser is the API handler to update the given user.
func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() {
response.Json().Forbidden()
return
}
userID, err := request.GetIntegerParam("userID")
if err != nil {
response.Json().BadRequest(err)
return
}
user, err := payload.DecodeUserPayload(request.GetBody())
if err != nil {
response.Json().BadRequest(err)
return
}
if err := user.ValidateUserModification(); err != nil {
response.Json().BadRequest(err)
return
}
originalUser, err := c.store.GetUserById(userID)
if err != nil {
response.Json().BadRequest(errors.New("Unable to fetch this user from the database"))
return
}
if originalUser == nil {
response.Json().NotFound(errors.New("User not found"))
return
}
originalUser.Merge(user)
if err = c.store.UpdateUser(originalUser); err != nil {
response.Json().ServerError(errors.New("Unable to update this user"))
return
}
response.Json().Created(originalUser)
}
// GetUsers is the API handler to get the list of users.
func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() {
response.Json().Forbidden()
return
}
users, err := c.store.GetUsers()
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch the list of users"))
return
}
response.Json().Standard(users)
}
// GetUser is the API handler to fetch the given user.
func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() {
response.Json().Forbidden()
return
}
userID, err := request.GetIntegerParam("userID")
if err != nil {
response.Json().BadRequest(err)
return
}
user, err := c.store.GetUserById(userID)
if err != nil {
response.Json().BadRequest(errors.New("Unable to fetch this user from the database"))
return
}
if user == nil {
response.Json().NotFound(errors.New("User not found"))
return
}
response.Json().Standard(user)
}
// RemoveUser is the API handler to remove an existing user.
func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() {
response.Json().Forbidden()
return
}
userID, err := request.GetIntegerParam("userID")
if err != nil {
response.Json().BadRequest(err)
return
}
user, err := c.store.GetUserById(userID)
if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this user from the database"))
return
}
if user == nil {
response.Json().NotFound(errors.New("User not found"))
return
}
if err := c.store.RemoveUser(user.ID); err != nil {
response.Json().BadRequest(errors.New("Unable to remove this user from the database"))
return
}
response.Json().NoContent()
}