2022-12-28 22:22:36 -05:00
|
|
|
package discourse
|
|
|
|
|
|
|
|
import (
|
2022-12-29 23:02:19 -05:00
|
|
|
"context"
|
|
|
|
"net/http"
|
2022-12-30 01:44:44 -05:00
|
|
|
"strconv"
|
2022-12-30 02:05:48 -05:00
|
|
|
"time"
|
2022-12-29 23:02:19 -05:00
|
|
|
|
2022-12-30 21:14:00 -05:00
|
|
|
md "github.com/JohannesKaufmann/html-to-markdown"
|
2022-12-30 02:05:48 -05:00
|
|
|
"github.com/araddon/dateparse"
|
2022-12-30 01:44:44 -05:00
|
|
|
"github.com/mrusme/gobbs/models/author"
|
2022-12-30 23:57:03 -05:00
|
|
|
"github.com/mrusme/gobbs/models/forum"
|
2022-12-29 23:02:19 -05:00
|
|
|
"github.com/mrusme/gobbs/models/post"
|
2022-12-30 02:48:53 -05:00
|
|
|
"github.com/mrusme/gobbs/models/reply"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/mrusme/gobbs/system/adapter"
|
2022-12-29 22:03:38 -05:00
|
|
|
"go.uber.org/zap"
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type System struct {
|
2022-12-29 14:35:01 -05:00
|
|
|
config map[string]interface{}
|
2022-12-29 22:03:38 -05:00
|
|
|
logger *zap.SugaredLogger
|
2022-12-30 00:43:32 -05:00
|
|
|
client *Client
|
2022-12-29 14:35:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) GetConfig() map[string]interface{} {
|
|
|
|
return sys.config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) SetConfig(cfg *map[string]interface{}) {
|
|
|
|
sys.config = *cfg
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-29 22:03:38 -05:00
|
|
|
func (sys *System) SetLogger(logger *zap.SugaredLogger) {
|
|
|
|
sys.logger = logger
|
|
|
|
}
|
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
func (sys *System) GetCapabilities() []adapter.Capability {
|
|
|
|
var caps []adapter.Capability
|
|
|
|
|
|
|
|
caps = append(caps, adapter.Capability{
|
|
|
|
ID: "posts",
|
|
|
|
Name: "Posts",
|
|
|
|
})
|
|
|
|
caps = append(caps, adapter.Capability{
|
|
|
|
ID: "groups",
|
|
|
|
Name: "Groups",
|
|
|
|
})
|
|
|
|
caps = append(caps, adapter.Capability{
|
|
|
|
ID: "search",
|
|
|
|
Name: "Search",
|
|
|
|
})
|
|
|
|
|
|
|
|
return caps
|
|
|
|
}
|
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
func (sys *System) Load() error {
|
2022-12-30 00:43:32 -05:00
|
|
|
url := sys.config["url"]
|
|
|
|
if url == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
credentials := make(map[string]string)
|
|
|
|
for k, v := range (sys.config["credentials"]).(map[string]interface{}) {
|
|
|
|
credentials[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.client = NewClient(&ClientConfig{
|
|
|
|
Endpoint: url.(string),
|
|
|
|
Credentials: credentials,
|
|
|
|
HTTPClient: http.DefaultClient,
|
|
|
|
Logger: sys.logger,
|
|
|
|
})
|
2022-12-29 14:35:01 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
|
2022-12-30 23:57:03 -05:00
|
|
|
cats, err := sys.client.Categories.List(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return []post.Post{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-30 00:43:32 -05:00
|
|
|
items, err := sys.client.Topics.ListLatest(context.Background())
|
2022-12-29 23:02:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return []post.Post{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:47:16 -05:00
|
|
|
var models []post.Post
|
|
|
|
for _, i := range (*items).TopicList.Topics {
|
2022-12-30 01:44:44 -05:00
|
|
|
var userName string = ""
|
|
|
|
for _, u := range (*items).Users {
|
|
|
|
if u.ID == i.Posters[0].UserID {
|
|
|
|
userName = u.Name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 02:05:48 -05:00
|
|
|
createdAt, err := dateparse.ParseAny(i.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
createdAt = time.Now() // TODO: Errrr
|
|
|
|
}
|
|
|
|
lastCommentedAt, err := dateparse.ParseAny(i.LastPostedAt)
|
|
|
|
if err != nil {
|
|
|
|
lastCommentedAt = time.Now() // TODO: Errrrr
|
|
|
|
}
|
|
|
|
|
2022-12-30 23:57:03 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:47:16 -05:00
|
|
|
models = append(models, post.Post{
|
2022-12-30 01:44:44 -05:00
|
|
|
ID: strconv.Itoa(i.ID),
|
|
|
|
|
2022-12-29 23:47:16 -05:00
|
|
|
Subject: i.Title,
|
2022-12-30 01:44:44 -05:00
|
|
|
|
|
|
|
Type: "post",
|
|
|
|
|
|
|
|
Pinned: i.Pinned,
|
|
|
|
Closed: i.Closed,
|
|
|
|
|
2022-12-30 02:05:48 -05:00
|
|
|
CreatedAt: createdAt,
|
|
|
|
LastCommentedAt: lastCommentedAt,
|
|
|
|
|
2022-12-30 01:44:44 -05:00
|
|
|
Author: author.Author{
|
|
|
|
ID: strconv.Itoa(i.Posters[0].UserID),
|
|
|
|
Name: userName,
|
|
|
|
},
|
2022-12-30 03:22:54 -05:00
|
|
|
|
2022-12-30 23:57:03 -05:00
|
|
|
Forum: forum.Forum{
|
|
|
|
ID: strconv.Itoa(i.CategoryID),
|
|
|
|
Name: forumName,
|
|
|
|
},
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
SysIDX: sysIdx,
|
2022-12-29 23:02:19 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:47:16 -05:00
|
|
|
return models, nil
|
2022-12-29 23:02:19 -05:00
|
|
|
}
|
2022-12-30 02:48:53 -05:00
|
|
|
|
|
|
|
func (sys *System) LoadPost(p *post.Post) error {
|
|
|
|
item, err := sys.client.Topics.Show(context.Background(), p.ID)
|
|
|
|
if err != nil {
|
2022-12-30 02:58:28 -05:00
|
|
|
return err
|
2022-12-30 02:48:53 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 21:14:00 -05:00
|
|
|
converter := md.NewConverter("", true, nil)
|
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
for idx, i := range item.PostStream.Posts {
|
2022-12-30 21:14:00 -05:00
|
|
|
cookedMd, err := converter.ConvertString(i.Cooked)
|
|
|
|
if err != nil {
|
|
|
|
cookedMd = i.Cooked
|
|
|
|
}
|
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
if idx == 0 {
|
2022-12-30 21:14:00 -05:00
|
|
|
p.Body = cookedMd
|
2022-12-30 02:48:53 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt, err := dateparse.ParseAny(i.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
createdAt = time.Now() // TODO: Errrrrr
|
|
|
|
}
|
|
|
|
p.Replies = append(p.Replies, reply.Reply{
|
|
|
|
ID: strconv.Itoa(i.ID),
|
|
|
|
|
2022-12-30 21:14:00 -05:00
|
|
|
Body: cookedMd,
|
2022-12-30 02:48:53 -05:00
|
|
|
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
|
|
|
|
Author: author.Author{
|
|
|
|
ID: strconv.Itoa(i.UserID),
|
|
|
|
Name: i.Name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|