From 3a7d2af1002884b5cec70a97eaf6983835c26da5 Mon Sep 17 00:00:00 2001 From: BreadMakesYouFat Date: Mon, 26 Jun 2023 15:31:45 -0400 Subject: [PATCH] Limit subscription query (configurable) --- system/lemmy/connect.go | 2 ++ system/lemmy/constant.go | 4 ++++ system/lemmy/lemmy.go | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 system/lemmy/constant.go diff --git a/system/lemmy/connect.go b/system/lemmy/connect.go index 47a386e..5ef81f3 100644 --- a/system/lemmy/connect.go +++ b/system/lemmy/connect.go @@ -50,5 +50,7 @@ func (sys *System) Connect(sysURL string) error { sys.config["url"] = sysURL sys.config["credentials"] = credentials + sys.config["MaxSubscriptions"] = MaxSubscriptions + return nil } diff --git a/system/lemmy/constant.go b/system/lemmy/constant.go new file mode 100644 index 0000000..55d4553 --- /dev/null +++ b/system/lemmy/constant.go @@ -0,0 +1,4 @@ +package lemmy + +// Limit the maximum number of subscription queries +const MaxSubscriptions = 10000 diff --git a/system/lemmy/lemmy.go b/system/lemmy/lemmy.go index f0040ba..055f570 100644 --- a/system/lemmy/lemmy.go +++ b/system/lemmy/lemmy.go @@ -19,6 +19,7 @@ import ( "go.uber.org/zap" ) + type System struct { ID int config map[string]interface{} @@ -178,11 +179,19 @@ func communityFullname(community types.CommunitySafe) (communityName string) { func (sys *System) ListForums() ([]forum.Forum, error) { var models []forum.Forum - for j := 1; j < 100; j++ { + var maxSubscriptions int + if sys.config["MaxSubscriptions"] != nil { + maxSubscriptions = sys.config["MaxSubscriptions"].(int) + } else { + maxSubscriptions = MaxSubscriptions + } + page := 1 + queryLimit := 50 + for page < maxSubscriptions { resp, err := sys.client.Communities(context.Background(), types.ListCommunities{ Type: types.NewOptional(types.ListingTypeSubscribed), - Page: types.NewOptional(int64(j)), - Limit: types.NewOptional(int64(50)), + Page: types.NewOptional(int64(page)), + Limit: types.NewOptional(int64(queryLimit)), }) if err != nil { break @@ -200,6 +209,7 @@ func (sys *System) ListForums() ([]forum.Forum, error) { SysIDX: sys.ID, }) } + page += 1 } return models, nil }