1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-09 06:20:43 +00:00
This commit is contained in:
BreadMakesYouFat 2023-11-05 10:01:30 -07:00 committed by GitHub
commit f60646f959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 21 deletions

View File

@ -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
system/lemmy/constant.go Normal file
View File

@ -0,0 +1,4 @@
package lemmy
// Limit the maximum number of subscription queries
const MaxSubscriptions = 10000

View File

@ -18,6 +18,7 @@ import (
"go.uber.org/zap"
)
type System struct {
ID int
config map[string]interface{}
@ -154,36 +155,79 @@ 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,
Info: i.Community.Description.ValueOr(i.Community.Title),
SysIDX: sys.ID,
})
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(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),
SysIDX: sys.ID,
})
}
page += 1
}
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
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
}
@ -191,7 +235,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("")
@ -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,
},