Skip to content

Commit

Permalink
Lemmy filter improvements
Browse files Browse the repository at this point in the history
resolves mrusme#32 Filter for lemmy communities
resolves mrusme#37 Keep lemmy results 50 but query specific community
Show all subscribed lemmy communities + re-query for results from that specific community when selected
  • Loading branch information
BreadMakesYouFat authored and BreadMakesYouFat committed Jun 26, 2023
1 parent 72d42ef commit a047a39
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions system/lemmy/lemmy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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),
Info: i.Community.Description.ValueOr(i.Community.Title),

SysIDX: sys.ID,
})
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
Expand All @@ -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("")
Expand Down

0 comments on commit a047a39

Please sign in to comment.