✅ Embed service within activity

This commit is contained in:
OpenWaygate committed 2026-01-23 22:44:53 +08:00
1 parent 9692db93de
commit 2ed87e34ee
2 files changed
+22 -10

No files matched your search

+6 -6
View File
@@ -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
}
}
+16 -4
View File
@@ -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)
}
},