From a047a392bd8faa9b78f8059d1b6c890864e16f14 Mon Sep 17 00:00:00 2001 From: BreadMakesYouFat Date: Fri, 23 Jun 2023 16:53:54 -0400 Subject: [PATCH] Lemmy filter improvements resolves mrusme/neonmodem#32 Filter for lemmy communities resolves mrusme/neonmodem#37 Keep lemmy results 50 but query specific community Show all subscribed lemmy communities + re-query for results from that specific community when selected --- system/lemmy/lemmy.go | 67 +++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/system/lemmy/lemmy.go b/system/lemmy/lemmy.go index 36bc7ec..21b1a79 100644 --- a/system/lemmy/lemmy.go +++ b/system/lemmy/lemmy.go @@ -155,34 +155,58 @@ func (sys *System) Load() error { } func (sys *System) ListForums() ([]forum.Forum, error) { - resp, err := sys.client.Communities(context.Background(), types.ListCommunities{ - Type: types.NewOptional(types.ListingTypeSubscribed), - }) - if err != nil { - return []forum.Forum{}, err - } - var models []forum.Forum - for _, i := range resp.Communities { - models = append(models, forum.Forum{ - ID: strconv.Itoa(i.Community.ID), - Name: i.Community.Name, - - Info: i.Community.Description.ValueOr(i.Community.Title), - - SysIDX: sys.ID, + for j := 1; j < 100; j++ { + resp, err := sys.client.Communities(context.Background(), types.ListCommunities{ + Type: types.NewOptional(types.ListingTypeSubscribed), + Page: types.NewOptional(int64(j)), + Limit: types.NewOptional(int64(50)), }) - } + if err != nil { + break + } + if len(resp.Communities) == 0 { + break + } + for _, i := range resp.Communities { + models = append(models, forum.Forum{ + ID: strconv.Itoa(i.Community.ID), + Name: i.Community.Name, + Info: i.Community.Description.ValueOr(i.Community.Title), + + SysIDX: sys.ID, + }) + } + } return models, nil } func (sys *System) ListPosts(forumID string) ([]post.Post, error) { - resp, err := sys.client.Posts(context.Background(), types.GetPosts{ - Type: types.NewOptional(types.ListingTypeSubscribed), - Sort: types.NewOptional(types.SortTypeNew), - Limit: types.NewOptional(int64(50)), - }) + var models []post.Post + var showAll bool + var err error + + communityID, err := strconv.Atoi(forumID) + if err != nil { + showAll = true + } + + resp := &types.GetPostsResponse{} + if showAll { + resp, err = sys.client.Posts(context.Background(), types.GetPosts{ + Type: types.NewOptional(types.ListingTypeSubscribed), + Sort: types.NewOptional(types.SortTypeNew), + Limit: types.NewOptional(int64(50)), + }) + } else { + resp, err = sys.client.Posts(context.Background(), types.GetPosts{ + Type: types.NewOptional(types.ListingTypeSubscribed), + Sort: types.NewOptional(types.SortTypeNew), + Limit: types.NewOptional(int64(50)), + CommunityID: types.NewOptional(communityID), + }) + } if err != nil { return []post.Post{}, err @@ -191,7 +215,6 @@ func (sys *System) ListPosts(forumID string) ([]post.Post, error) { cfg := sys.GetConfig() baseURL := cfg["url"].(string) - var models []post.Post for _, i := range resp.Posts { t := "post" body := i.Post.Body.ValueOr("")