-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"go.uber.org/zap" | ||
) | ||
|
||
|
||
type System struct { | ||
ID int | ||
config map[string]interface{} | ||
|
@@ -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 { | ||
maxSubscriptions = sys.config["MaxSubscriptions"].(int) | ||
} else { | ||
maxSubscriptions = MaxSubscriptions | ||
} | ||
page := 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for the previous comment, I was assuming paging would start at |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless there was an error or the |
||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("") | ||
|
@@ -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, | ||
}, | ||
|
There was a problem hiding this comment.
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 to0
? 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.