Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves mrusme/neonmodem#32 Filter for lemmy communities #36

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions system/lemmy/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions system/lemmy/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package lemmy

// Limit the maximum number of subscription queries
const MaxSubscriptions = 10000
83 changes: 63 additions & 20 deletions system/lemmy/lemmy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.uber.org/zap"
)


type System struct {
ID int
config map[string]interface{}
Expand Down Expand Up @@ -154,44 +155,86 @@ func (sys *System) Load() error {
return nil
}

func (sys *System) ListForums() ([]forum.Forum, error) {
resp, err := sys.client.Communities(context.Background(), types.ListCommunities{
Type: types.NewOptional(types.ListingTypeSubscribed),
})
func communityFullname(community types.CommunitySafe) (communityName string) {
url, err := url.Parse(community.ActorID)
if err != nil {
return []forum.Forum{}, err
return community.Name
} else {
return community.Name + "@" + url.Host
}
}

func (sys *System) ListForums() ([]forum.Forum, error) {
var models []forum.Forum
for _, i := range resp.Communities {
models = append(models, forum.Forum{
ID: strconv.Itoa(i.Community.ID),
Name: i.Community.Name,
var maxSubscriptions int
if sys.config["MaxSubscriptions"] != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding another check that makes sure MaxSubscriptions wasn't set to 0? I fear that if someone might mistakenly change that in their config, debugging the issue might be really tricky. And from what I understand, 0 wouldn't make any sense.

maxSubscriptions = sys.config["MaxSubscriptions"].(int)
} else {
maxSubscriptions = MaxSubscriptions
}
page := 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the previous comment, I was assuming paging would start at 0. It seems like we would either want to page from 0 to < maxSubscriptions or from 1 to <= maxSubscriptions. Otherwise the configured value would not factually represent the actual amount of pages, no?

queryLimit := 50
for page < maxSubscriptions {
resp, err := sys.client.Communities(context.Background(), types.ListCommunities{
Type: types.NewOptional(types.ListingTypeSubscribed),
Page: types.NewOptional(int64(page)),
Limit: types.NewOptional(int64(queryLimit)),
})
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: communityFullname(i.Community),

Info: i.Community.Description.ValueOr(i.Community.Title),
Info: i.Community.Description.ValueOr(i.Community.Title),

SysIDX: sys.ID,
})
SysIDX: sys.ID,
})
}
page += 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there was an error or the len of resp.Communities is 0, it seems like the page would always increment. Do you think it might be better to make the for loop an actual for page := 0; page <= maxSubscriptions; page++, to make this more visible? Or is there another reason you decided to go for the page += 1 at the end that I might be overseeing here? The explicit for loop would save two dedicated lines, ehehehe. :-)

}

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
}

var getPosts types.GetPosts
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

if showAll {
getPosts = types.GetPosts{
Type: types.NewOptional(types.ListingTypeSubscribed),
Sort: types.NewOptional(types.SortTypeNew),
Limit: types.NewOptional(int64(50)),
}
} else {
getPosts = types.GetPosts{
Type: types.NewOptional(types.ListingTypeSubscribed),
Sort: types.NewOptional(types.SortTypeNew),
Limit: types.NewOptional(int64(50)),
CommunityID: types.NewOptional(communityID),
}
}

resp, err := sys.client.Posts(context.Background(), getPosts)
if err != nil {
return []post.Post{}, err
}

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 Expand Up @@ -223,7 +266,7 @@ func (sys *System) ListPosts(forumID string) ([]post.Post, error) {

Forum: forum.Forum{
ID: strconv.Itoa(i.Post.CommunityID),
Name: i.Community.Name,
Name: communityFullname(i.Community),

SysIDX: sys.ID,
},
Expand Down