1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00

Implemented Forum

This commit is contained in:
マリウス 2022-12-30 23:57:03 -05:00
parent fc51f619b9
commit 9c50ddffdc
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
7 changed files with 143 additions and 25 deletions

6
models/forum/forum.go Normal file
View File

@ -0,0 +1,6 @@
package forum
type Forum struct {
ID string
Name string
}

View File

@ -1,9 +1,11 @@
package post package post
import ( import (
"fmt"
"time" "time"
"github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/author"
"github.com/mrusme/gobbs/models/forum"
"github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/models/reply"
) )
@ -22,6 +24,8 @@ type Post struct {
Author author.Author Author author.Author
Forum forum.Forum
Replies []reply.Reply Replies []reply.Reply
SysIDX int SysIDX int
@ -36,5 +40,10 @@ func (post Post) Title() string {
} }
func (post Post) Description() string { func (post Post) Description() string {
return post.ID return fmt.Sprintf(
"%s in %s on %s",
post.Author.Name,
post.Forum.Name,
post.CreatedAt.Format("Jan 2 2006"),
)
} }

View File

@ -0,0 +1,88 @@
package discourse
import (
"context"
"net/http"
)
const CategoriesBaseURL = "/categories"
type LatestCategoriesResponse struct {
CategoryList struct {
CanCreateCategory bool `json:"can_create_category"`
CanCreateTopic bool `json:"can_create_topic"`
Categories []CategoryModel `json:"categories"`
} `json:"category_list"`
}
type CategoryModel struct {
ID int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
TextColor string `json:"text_color"`
Slug string `json:"slug"`
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
Position int `json:"position"`
Description string `json:"description"`
DescriptionText string `json:"description_text"`
DescriptionExcerpt string `json:"description_excerpt"`
TopicUrl string `json:"topic_url"`
ReadRestricted bool `json:"read_restricted"`
Permission int `json:"permission"`
NotificationLevel int `json:"notification_level"`
CanEdit bool `json:"can_edit"`
TopicTemplate string `json:"topic_template"`
HasChildren bool `json:"has_children"`
SortOrder string `json:"sort_order"`
SortAscending string `json:"sort_ascending"`
ShowSubcategoryList bool `json:"show_subcategory_list"`
NumFeaturedTopics int `json:"num_featured_topics"`
DefaultView string `json:"default_view"`
SubcategoryListStyle string `json:"subcategory_list_style"`
DefaultTopPeriod string `json:"default_top_period"`
DefaultListFilter string `json:"default_list_filter"`
MinimumRequiredTags int `json:"minimum_required_tags"`
NavigateToFirstPostAfterRead bool `json:"navigate_to_first_post_after_read"`
TopicsDay int `json:"topics_day"`
TopicsWeek int `json:"topics_week"`
TopicsMonth int `json:"topics_month"`
TopicsYear int `json:"topics_year"`
TopicsAllTime int `json:"topics_all_time"`
IsUncategorized bool `json:"is_uncategorized"`
SubcategoryIDs []string `json:"subcategory_ids"`
SubcategoryList []CategoryModel `json:"subcategory_list"`
UploadedLogo string `json:"uploaded_logo"`
UploadedLogoDark string `json:"uploaded_logo_dark"`
UploadedBackground string `json:"uploaded_background"`
}
type CategoriesService interface {
List(
ctx context.Context,
) (*LatestCategoriesResponse, error)
}
type CategoryServiceHandler struct {
client *Client
}
// List
func (a *CategoryServiceHandler) List(
ctx context.Context,
) (*LatestCategoriesResponse, error) {
uri := CategoriesBaseURL + ".json"
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
response := new(LatestCategoriesResponse)
if err = a.client.Do(ctx, req, response); err != nil {
return nil, err
}
return response, nil
}

View File

@ -68,8 +68,9 @@ type Client struct {
credentials map[string]string credentials map[string]string
logger Logger logger Logger
Posts PostsService Posts PostsService
Topics TopicsService Topics TopicsService
Categories CategoriesService
} }
func NewDefaultClientConfig( func NewDefaultClientConfig(
@ -99,6 +100,7 @@ func NewClient(cc *ClientConfig) *Client {
c.Posts = &PostServiceHandler{client: c} c.Posts = &PostServiceHandler{client: c}
c.Topics = &TopicServiceHandler{client: c} c.Topics = &TopicServiceHandler{client: c}
c.Categories = &CategoryServiceHandler{client: c}
return c return c
} }

View File

@ -9,6 +9,7 @@ import (
md "github.com/JohannesKaufmann/html-to-markdown" md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/author"
"github.com/mrusme/gobbs/models/forum"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/system/adapter" "github.com/mrusme/gobbs/system/adapter"
@ -74,6 +75,11 @@ func (sys *System) Load() error {
} }
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) { func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
cats, err := sys.client.Categories.List(context.Background())
if err != nil {
return []post.Post{}, err
}
items, err := sys.client.Topics.ListLatest(context.Background()) items, err := sys.client.Topics.ListLatest(context.Background())
if err != nil { if err != nil {
return []post.Post{}, err return []post.Post{}, err
@ -98,6 +104,21 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
lastCommentedAt = time.Now() // TODO: Errrrr lastCommentedAt = time.Now() // TODO: Errrrr
} }
var forumName string = ""
for _, cat := range cats.CategoryList.Categories {
if cat.ID == i.CategoryID {
forumName = cat.Name
break
}
for _, subcat := range cat.SubcategoryList {
if subcat.ID == i.CategoryID {
forumName = subcat.Name
break
}
}
}
models = append(models, post.Post{ models = append(models, post.Post{
ID: strconv.Itoa(i.ID), ID: strconv.Itoa(i.ID),
@ -116,6 +137,11 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
Name: userName, Name: userName,
}, },
Forum: forum.Forum{
ID: strconv.Itoa(i.CategoryID),
Name: forumName,
},
SysIDX: sysIdx, SysIDX: sysIdx,
}) })
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/author"
"github.com/mrusme/gobbs/models/forum"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/system/adapter" "github.com/mrusme/gobbs/system/adapter"
@ -126,6 +127,11 @@ func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
Name: i.Creator.Name, Name: i.Creator.Name,
}, },
Forum: forum.Forum{
ID: strconv.Itoa(i.Post.CommunityID),
Name: i.Community.Name,
},
SysIDX: sysIdx, SysIDX: sysIdx,
}) })
} }

View File

@ -72,10 +72,9 @@ var (
) )
type KeyMap struct { type KeyMap struct {
Refresh key.Binding Refresh key.Binding
Select key.Binding Select key.Binding
SwitchFocus key.Binding Close key.Binding
Close key.Binding
} }
var DefaultKeyMap = KeyMap{ var DefaultKeyMap = KeyMap{
@ -87,10 +86,6 @@ var DefaultKeyMap = KeyMap{
key.WithKeys("enter"), key.WithKeys("enter"),
key.WithHelp("enter", "select"), key.WithHelp("enter", "select"),
), ),
SwitchFocus: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "switch focus"),
),
Close: key.NewBinding( Close: key.NewBinding(
key.WithKeys("esc"), key.WithKeys("esc"),
key.WithHelp("esc", "close"), key.WithHelp("esc", "close"),
@ -107,9 +102,6 @@ type Model struct {
glam *glamour.TermRenderer glam *glamour.TermRenderer
focused int
focusables [2]tea.Model
viewportOpen bool viewportOpen bool
} }
@ -120,13 +112,9 @@ func (m Model) Init() tea.Cmd {
func NewModel(c *ctx.Ctx) Model { func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
keymap: DefaultKeyMap, keymap: DefaultKeyMap,
focused: 0,
viewportOpen: false, viewportOpen: false,
} }
// m.focusables = append(m.focusables, m.list)
// m.focusables = append(m.focusables, m.viewport)
m.list = list.New(m.items, list.NewDefaultDelegate(), 0, 0) m.list = list.New(m.items, list.NewDefaultDelegate(), 0, 0)
m.list.Title = "Posts" m.list.Title = "Posts"
m.ctx = c m.ctx = c
@ -145,13 +133,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.Loading = true m.ctx.Loading = true
cmds = append(cmds, m.refresh()) cmds = append(cmds, m.refresh())
case key.Matches(msg, m.keymap.SwitchFocus):
m.focused++
if m.focused >= len(m.focusables) {
m.focused = 0
}
// return m, nil
case key.Matches(msg, m.keymap.Select): case key.Matches(msg, m.keymap.Select):
i, ok := m.list.SelectedItem().(post.Post) i, ok := m.list.SelectedItem().(post.Post)
if ok { if ok {