1
0
Fork 1
mirror of https://github.com/0x2E/fusion.git synced 2025-06-09 09:34:58 +09:00

refactor: derive context from user's request

This commit is contained in:
rook1e 2024-03-18 18:17:43 +08:00
parent 450cfcc228
commit 39072600d3
No known key found for this signature in database
GPG key ID: C63289D731719BC0
10 changed files with 52 additions and 51 deletions

View file

@ -6,7 +6,6 @@ import (
"net/http"
"net/url"
"sync"
"time"
"github.com/0x2e/fusion/model"
"github.com/0x2e/fusion/repo"
@ -40,7 +39,7 @@ func NewFeed(feedRepo FeedRepo, itemRepo ItemInFeedRepo) *Feed {
}
}
func (f Feed) All() (*RespFeedAll, error) {
func (f Feed) All(ctx context.Context) (*RespFeedAll, error) {
data, err := f.feedRepo.All()
if err != nil {
return nil, err
@ -63,7 +62,7 @@ func (f Feed) All() (*RespFeedAll, error) {
}, nil
}
func (f Feed) Get(req *ReqFeedGet) (*RespFeedGet, error) {
func (f Feed) Get(ctx context.Context, req *ReqFeedGet) (*RespFeedGet, error) {
data, err := f.feedRepo.Get(req.ID)
if err != nil {
return nil, err
@ -79,7 +78,7 @@ func (f Feed) Get(req *ReqFeedGet) (*RespFeedGet, error) {
}, nil
}
func (f Feed) Create(req *ReqFeedCreate) error {
func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
feeds := make([]*model.Feed, 0, len(req.Feeds))
for _, r := range req.Feeds {
feeds = append(feeds, &model.Feed{
@ -109,7 +108,9 @@ func (f Feed) Create(req *ReqFeedCreate) error {
routinePool <- struct{}{}
wg.Add(1)
go func() {
puller.PullOne(feed.ID)
// NOTE: do not use the incoming ctx, as it will be Done() automatically
// by api timeout middleware
puller.PullOne(context.Background(), feed.ID)
<-routinePool
wg.Done()
}()
@ -118,13 +119,10 @@ func (f Feed) Create(req *ReqFeedCreate) error {
}()
return nil
}
return puller.PullOne(feeds[0].ID)
return puller.PullOne(ctx, feeds[0].ID)
}
func (f Feed) CheckValidity(req *ReqFeedCheckValidity) (*RespFeedCheckValidity, error) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
func (f Feed) CheckValidity(ctx context.Context, req *ReqFeedCheckValidity) (*RespFeedCheckValidity, error) {
validLinks := make([]ValidityItem, 0)
parsed, err := pull.Fetch(ctx, req.Link)
if err == nil && parsed != nil {
@ -153,7 +151,7 @@ func (f Feed) CheckValidity(req *ReqFeedCheckValidity) (*RespFeedCheckValidity,
}, nil
}
func (f Feed) Update(req *ReqFeedUpdate) error {
func (f Feed) Update(ctx context.Context, req *ReqFeedUpdate) error {
data := &model.Feed{
Name: req.Name,
Link: req.Link,
@ -169,7 +167,7 @@ func (f Feed) Update(req *ReqFeedUpdate) error {
return err
}
func (f Feed) Delete(req *ReqFeedDelete) error {
func (f Feed) Delete(ctx context.Context, req *ReqFeedDelete) error {
// FIX: transaction
if err := f.itemRepo.DeleteByFeed(req.ID); err != nil {
return err
@ -177,12 +175,14 @@ func (f Feed) Delete(req *ReqFeedDelete) error {
return f.feedRepo.Delete(req.ID)
}
func (f Feed) Refresh(req *ReqFeedRefresh) error {
func (f Feed) Refresh(ctx context.Context, req *ReqFeedRefresh) error {
pull := pull.NewPuller(repo.NewFeed(repo.DB), repo.NewItem(repo.DB))
if req.ID != nil {
return pull.PullOne(*req.ID)
return pull.PullOne(ctx, *req.ID)
}
if req.All != nil && *req.All {
// NOTE: do not use the incoming ctx, as it will be Done() automatically
// by api timeout middleware
go pull.PullAll(context.Background(), true)
}
return nil

View file

@ -1,6 +1,7 @@
package server
import (
"context"
"errors"
"net/http"
@ -32,7 +33,7 @@ func NewGroup(groupRepo GroupRepo, feedRepo FeedinGroupRepo) *Group {
}
}
func (g Group) All() (*RespGroupAll, error) {
func (g Group) All(ctx context.Context) (*RespGroupAll, error) {
data, err := g.groupRepo.All()
if err != nil {
return nil, err
@ -50,7 +51,7 @@ func (g Group) All() (*RespGroupAll, error) {
}, nil
}
func (g Group) Create(req *ReqGroupCreate) error {
func (g Group) Create(ctx context.Context, req *ReqGroupCreate) error {
newGroup := &model.Group{
Name: req.Name,
}
@ -61,7 +62,7 @@ func (g Group) Create(req *ReqGroupCreate) error {
return err
}
func (g Group) Update(req *ReqGroupUpdate) error {
func (g Group) Update(ctx context.Context, req *ReqGroupUpdate) error {
err := g.groupRepo.Update(req.ID, &model.Group{
Name: req.Name,
})
@ -71,7 +72,7 @@ func (g Group) Update(req *ReqGroupUpdate) error {
return err
}
func (g Group) Delete(req *ReqGroupDelete) error {
func (g Group) Delete(ctx context.Context, req *ReqGroupDelete) error {
if req.ID == 1 {
return errors.New("cannot delete the default group")
}

View file

@ -1,6 +1,8 @@
package server
import (
"context"
"github.com/0x2e/fusion/model"
"github.com/0x2e/fusion/repo"
)
@ -25,7 +27,7 @@ func NewItem(repo ItemRepo) *Item {
}
}
func (i Item) List(req *ReqItemList) (*RespItemList, error) {
func (i Item) List(ctx context.Context, req *ReqItemList) (*RespItemList, error) {
filter := repo.ItemFilter{
Keyword: req.Keyword,
FeedID: req.FeedID,
@ -66,7 +68,7 @@ func (i Item) List(req *ReqItemList) (*RespItemList, error) {
}, nil
}
func (i Item) Get(req *ReqItemGet) (*RespItemGet, error) {
func (i Item) Get(ctx context.Context, req *ReqItemGet) (*RespItemGet, error) {
data, err := i.repo.Get(req.ID)
if err != nil {
return nil, err
@ -89,14 +91,14 @@ func (i Item) Get(req *ReqItemGet) (*RespItemGet, error) {
}, nil
}
func (i Item) Delete(req *ReqItemDelete) error {
func (i Item) Delete(ctx context.Context, req *ReqItemDelete) error {
return i.repo.Delete(req.ID)
}
func (i Item) UpdateUnread(req *ReqItemUpdateUnread) error {
func (i Item) UpdateUnread(ctx context.Context, req *ReqItemUpdateUnread) error {
return i.repo.UpdateUnread(req.IDs, req.Unread)
}
func (i Item) UpdateBookmark(req *ReqItemUpdateBookmark) error {
func (i Item) UpdateBookmark(ctx context.Context, req *ReqItemUpdateBookmark) error {
return i.repo.UpdateBookmark(req.ID, req.Bookmark)
}