Skip to content

Commit

Permalink
Implemented mrusme#3, nested categories for Discourse
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Jan 11, 2023
1 parent ba46179 commit 3fabca8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions system/discourse/api/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (a *CategoryServiceHandler) List(
return nil, err
}

q := req.URL.Query()
q.Add("include_subcategories", "true")
req.URL.RawQuery = q.Encode()

response := new(LatestCategoriesResponse)
if err = a.client.Do(ctx, req, response); err != nil {
return nil, err
Expand Down
32 changes: 25 additions & 7 deletions system/discourse/discourse.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,36 @@ func (sys *System) ListForums() ([]forum.Forum, error) {
return []forum.Forum{}, err
}

for _, cat := range cats.CategoryList.Categories {
models = append(models, forum.Forum{
ID: strconv.Itoa(cat.ID),
Name: cat.Name,
models = sys.recurseForums(&cats.CategoryList.Categories, nil)

Info: cat.Description,
return models, nil
}

func (sys *System) recurseForums(cats *[]api.CategoryModel, parent *forum.Forum) []forum.Forum {
var models []forum.Forum

sys.logger.Debugf("recursing categories: %d\n", len(*cats))
for i := 0; i < len(*cats); i++ {
sys.logger.Debugf("adding category: %s\n", (*cats)[i].Name)

var name = (*cats)[i].Name
if parent != nil {
name = fmt.Sprintf("%s / %s", parent.Name, name)
}
f := forum.Forum{
ID: strconv.Itoa((*cats)[i].ID),
Name: name,

Info: (*cats)[i].Description,

SysIDX: sys.ID,
})
}
models = append(models, f)

models = append(models, sys.recurseForums(&(*cats)[i].SubcategoryList, &f)...)
}

return models, nil
return models
}

func (sys *System) ListPosts(forumID string) ([]post.Post, error) {
Expand Down

0 comments on commit 3fabca8

Please sign in to comment.