mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Detect feature level
This commit is contained in:
@ -13,4 +13,8 @@
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/gin-contrib/sessions"
|
||||
revision = "a71ea9167c616cf9f02bc928ed08bc390c8279bc"
|
||||
revision = "a71ea9167c616cf9f02bc928ed08bc390c8279bc"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/mxpv/patreon-go"
|
||||
version = "1.2"
|
@ -1,7 +1,6 @@
|
||||
package feeds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@ -10,13 +9,17 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
maxPageSize = 150
|
||||
)
|
||||
|
||||
type service struct {
|
||||
id id
|
||||
storage storage
|
||||
builders map[api.Provider]builder
|
||||
}
|
||||
|
||||
func (s *service) CreateFeed(ctx context.Context, req *api.CreateFeedRequest) (string, error) {
|
||||
func (s *service) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity) (string, error) {
|
||||
feed, err := parseURL(req.URL)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to create feed for URL: %s", req.URL)
|
||||
@ -35,6 +38,16 @@ func (s *service) CreateFeed(ctx context.Context, req *api.CreateFeedRequest) (s
|
||||
feed.FeatureLevel = api.DefaultFeatures
|
||||
feed.LastAccess = time.Now().UTC()
|
||||
|
||||
if identity.FeatureLevel > 0 {
|
||||
feed.Quality = req.Quality
|
||||
feed.Format = req.Format
|
||||
feed.FeatureLevel = identity.FeatureLevel
|
||||
feed.PageSize = req.PageSize
|
||||
if feed.PageSize > maxPageSize {
|
||||
feed.PageSize = maxPageSize
|
||||
}
|
||||
}
|
||||
|
||||
// Generate short id
|
||||
hashId, err := s.id.Generate(feed)
|
||||
if err != nil {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
@ -26,7 +25,7 @@ const (
|
||||
)
|
||||
|
||||
type feed interface {
|
||||
CreateFeed(ctx context.Context, req *api.CreateFeedRequest) (string, error)
|
||||
CreateFeed(req *api.CreateFeedRequest, identity *api.Identity) (string, error)
|
||||
GetFeed(hashId string) (*itunes.Podcast, error)
|
||||
GetMetadata(hashId string) (*api.Feed, error)
|
||||
}
|
||||
@ -116,18 +115,32 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
|
||||
client := patreon.NewClient(tc)
|
||||
|
||||
// Query user info from Patreon
|
||||
user, err := client.FetchUser(patreon.WithIncludes(patreon.UserDefaultRelations))
|
||||
user, err := client.FetchUser()
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Determine feature level
|
||||
level := api.DefaultFeatures
|
||||
amount := 0
|
||||
for _, item := range user.Included.Items {
|
||||
pledge, ok := item.(*patreon.Pledge)
|
||||
if ok {
|
||||
amount += pledge.Attributes.AmountCents
|
||||
}
|
||||
}
|
||||
|
||||
if amount >= 100 {
|
||||
level = api.ExtendedFeatures
|
||||
}
|
||||
|
||||
identity := &api.Identity{
|
||||
UserId: user.Data.Id,
|
||||
FullName: user.Data.Attributes.FullName,
|
||||
Email: user.Data.Attributes.Email,
|
||||
ProfileURL: user.Data.Attributes.URL,
|
||||
FeatureLevel: api.ExtendedFeatures,
|
||||
FeatureLevel: level,
|
||||
}
|
||||
|
||||
// Serialize identity and return cookies
|
||||
@ -158,7 +171,22 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
hashId, err := feed.CreateFeed(c.Request.Context(), req)
|
||||
s := sessions.Default(c)
|
||||
|
||||
identity := &api.Identity{
|
||||
FeatureLevel: api.DefaultFeatures,
|
||||
}
|
||||
|
||||
buf, ok := s.Get(identitySessionKey).(string)
|
||||
if ok {
|
||||
// We are failed to deserialize Identity structure, do cleanup, force user to login again
|
||||
if err := json.Unmarshal([]byte(buf), identity); err != nil {
|
||||
s.Clear()
|
||||
s.Save()
|
||||
}
|
||||
}
|
||||
|
||||
hashId, err := feed.CreateFeed(req, identity)
|
||||
if err != nil {
|
||||
c.JSON(internalError(err))
|
||||
return
|
||||
|
@ -4,7 +4,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
podcast "github.com/mxpv/podcast"
|
||||
api "github.com/mxpv/podsync/pkg/api"
|
||||
@ -35,8 +34,8 @@ func (_m *Mockfeed) EXPECT() *MockfeedMockRecorder {
|
||||
}
|
||||
|
||||
// CreateFeed mocks base method
|
||||
func (_m *Mockfeed) CreateFeed(ctx context.Context, req *api.CreateFeedRequest) (string, error) {
|
||||
ret := _m.ctrl.Call(_m, "CreateFeed", ctx, req)
|
||||
func (_m *Mockfeed) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity) (string, error) {
|
||||
ret := _m.ctrl.Call(_m, "CreateFeed", req, identity)
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
|
@ -30,7 +30,7 @@ func TestCreateFeed(t *testing.T) {
|
||||
}
|
||||
|
||||
feed := NewMockfeed(ctrl)
|
||||
feed.EXPECT().CreateFeed(gomock.Any(), gomock.Eq(req)).Times(1).Return("456", nil)
|
||||
feed.EXPECT().CreateFeed(gomock.Eq(req), gomock.Any()).Times(1).Return("456", nil)
|
||||
|
||||
srv := httptest.NewServer(MakeHandlers(feed, cfg))
|
||||
defer srv.Close()
|
||||
|
Reference in New Issue
Block a user