From 2ed87e34ee022493cf135537a082d1dfd6d03b40 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Fri, 23 Jan 2026 22:44:53 +0800 Subject: [PATCH] :white_check_mark: Embed service within activity --- pkg/activity/activity.go | 12 ++++++------ pkg/activity/activity_test.go | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/activity/activity.go b/pkg/activity/activity.go index f6e060f..3aedf3d 100644 --- a/pkg/activity/activity.go +++ b/pkg/activity/activity.go @@ -16,11 +16,11 @@ import ( ) var ( - service *youtube.Service errGetActivity = errors.New("failed to get activity") ) type activity struct { + service *youtube.Service ChannelId string `yaml:"channel_id" json:"channel_id"` Home *bool `yaml:"home" json:"home"` MaxResults int64 `yaml:"max_results" json:"max_results"` @@ -44,8 +44,8 @@ func NewActivity(opts ...Option) Activity[youtube.Activity] { opt(a) } - if service == nil { - service = auth.NewY2BService( + if a.service == nil { + a.service = auth.NewY2BService( auth.WithCredential("", pkg.Root.FS()), auth.WithCacheToken("", pkg.Root.FS()), ).GetService() @@ -55,7 +55,7 @@ func NewActivity(opts ...Option) Activity[youtube.Activity] { } func (a *activity) Get(parts []string) ([]*youtube.Activity, error) { - call := service.Activities.List(parts) + call := a.service.Activities.List(parts) if a.ChannelId != "" { call = call.ChannelId(a.ChannelId) } @@ -187,7 +187,7 @@ func WithRegionCode(regionCode string) Option { } func WithService(svc *youtube.Service) Option { - return func(_ *activity) { - service = svc + return func(c *activity) { + c.service = svc } } diff --git a/pkg/activity/activity_test.go b/pkg/activity/activity_test.go index f593e33..d8e91b7 100644 --- a/pkg/activity/activity_test.go +++ b/pkg/activity/activity_test.go @@ -53,7 +53,9 @@ func TestNewActivity(t *testing.T) { { name: "with no options", args: args{ - opts: []Option{}, + opts: []Option{ + WithService(&youtube.Service{}), + }, }, want: &activity{}, }, @@ -63,6 +65,7 @@ func TestNewActivity(t *testing.T) { opts: []Option{ WithHome(nil), WithMine(nil), + WithService(&youtube.Service{}), }, }, want: &activity{}, @@ -73,6 +76,7 @@ func TestNewActivity(t *testing.T) { opts: []Option{ WithHome(&homeFalse), WithMine(&mineFalse), + WithService(&youtube.Service{}), }, }, want: &activity{ @@ -85,6 +89,7 @@ func TestNewActivity(t *testing.T) { args: args{ opts: []Option{ WithMaxResults(0), + WithService(&youtube.Service{}), }, }, want: &activity{ @@ -96,6 +101,7 @@ func TestNewActivity(t *testing.T) { args: args{ opts: []Option{ WithMaxResults(-10), + WithService(&youtube.Service{}), }, }, want: &activity{ @@ -110,6 +116,7 @@ func TestNewActivity(t *testing.T) { WithPublishedAfter(""), WithPublishedBefore(""), WithRegionCode(""), + WithService(&youtube.Service{}), }, }, want: &activity{ @@ -126,6 +133,7 @@ func TestNewActivity(t *testing.T) { WithChannelId("partial-channel"), WithMaxResults(25), WithRegionCode("UK"), + WithService(&youtube.Service{}), }, }, want: &activity{ @@ -139,9 +147,13 @@ func TestNewActivity(t *testing.T) { for _, tt := range tests { t.Run( tt.name, func(t *testing.T) { - if got := NewActivity(tt.args.opts...); !reflect.DeepEqual( - got, tt.want, - ) { + if got := NewActivity(tt.args.opts...); !reflect.DeepEqual(got.(*activity).ChannelId, tt.want.(*activity).ChannelId) || + !reflect.DeepEqual(got.(*activity).Home, tt.want.(*activity).Home) || + !reflect.DeepEqual(got.(*activity).MaxResults, tt.want.(*activity).MaxResults) || + !reflect.DeepEqual(got.(*activity).Mine, tt.want.(*activity).Mine) || + !reflect.DeepEqual(got.(*activity).PublishedAfter, tt.want.(*activity).PublishedAfter) || + !reflect.DeepEqual(got.(*activity).PublishedBefore, tt.want.(*activity).PublishedBefore) || + !reflect.DeepEqual(got.(*activity).RegionCode, tt.want.(*activity).RegionCode) { t.Errorf("NewActivity() = %v, want %v", got, tt.want) } },