2022-12-28 22:22:36 -05:00
|
|
|
package lemmy
|
|
|
|
|
|
|
|
import (
|
2022-12-30 00:39:51 -05:00
|
|
|
"context"
|
2022-12-30 01:44:44 -05:00
|
|
|
"strconv"
|
2022-12-30 02:05:48 -05:00
|
|
|
"time"
|
2022-12-30 00:39:51 -05:00
|
|
|
|
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-28 22:22:36 -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-30 00:39:51 -05:00
|
|
|
"go.arsenm.dev/go-lemmy"
|
|
|
|
"go.arsenm.dev/go-lemmy/types"
|
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:34:26 -05:00
|
|
|
config map[string]interface{}
|
2022-12-29 22:03:38 -05:00
|
|
|
logger *zap.SugaredLogger
|
2022-12-30 00:39:51 -05:00
|
|
|
client *lemmy.Client
|
2022-12-29 14:34:26 -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:39:51 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
url := sys.config["url"]
|
|
|
|
if url == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-30 00:43:32 -05:00
|
|
|
sys.client, err = lemmy.New(url.(string))
|
2022-12-30 00:39:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
credentials := make(map[string]string)
|
|
|
|
for k, v := range (sys.config["credentials"]).(map[string]interface{}) {
|
|
|
|
credentials[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sys.client.Login(context.Background(), types.Login{
|
|
|
|
UsernameOrEmail: credentials["username"],
|
|
|
|
Password: credentials["password"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:22:54 -05:00
|
|
|
func (sys *System) ListPosts(sysIdx int) ([]post.Post, error) {
|
2022-12-30 00:39:51 -05:00
|
|
|
resp, err := sys.client.Posts(context.Background(), types.GetPosts{
|
|
|
|
Type: types.NewOptional(types.ListingLocal),
|
|
|
|
Sort: types.NewOptional(types.New),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return []post.Post{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var models []post.Post
|
|
|
|
for _, i := range resp.Posts {
|
2022-12-30 01:44:44 -05:00
|
|
|
t := "post"
|
2022-12-30 02:05:48 -05:00
|
|
|
body := i.Post.Body.ValueOr("")
|
2022-12-30 01:44:44 -05:00
|
|
|
if i.Post.URL.IsValid() {
|
|
|
|
t = "url"
|
2022-12-30 02:05:48 -05:00
|
|
|
body = i.Post.URL.ValueOr("")
|
2022-12-30 01:44:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 02:05:48 -05:00
|
|
|
createdAt, err := dateparse.ParseAny(i.Post.Published)
|
|
|
|
if err != nil {
|
|
|
|
createdAt = time.Now() // TODO: Errrr
|
|
|
|
}
|
|
|
|
lastCommentedAt, err := dateparse.ParseAny(i.Counts.NewestCommentTime)
|
|
|
|
if err != nil {
|
|
|
|
lastCommentedAt = time.Now() // TODO: Errrrr
|
2022-12-30 01:44:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 00:39:51 -05:00
|
|
|
models = append(models, post.Post{
|
2022-12-30 01:44:44 -05:00
|
|
|
ID: strconv.Itoa(i.Post.ID),
|
|
|
|
|
2022-12-30 00:39:51 -05:00
|
|
|
Subject: i.Post.Name,
|
2022-12-30 02:05:48 -05:00
|
|
|
Body: body,
|
2022-12-30 01:44:44 -05:00
|
|
|
|
|
|
|
Type: t,
|
|
|
|
|
|
|
|
Pinned: i.Post.Stickied,
|
|
|
|
Closed: i.Post.Locked,
|
|
|
|
|
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.Post.CreatorID),
|
2022-12-30 02:05:48 -05:00
|
|
|
Name: i.Creator.Name,
|
2022-12-30 01:44:44 -05:00
|
|
|
},
|
2022-12-30 03:22:54 -05:00
|
|
|
|
|
|
|
SysIDX: sysIdx,
|
2022-12-30 00:39:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return models, nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
func (sys *System) LoadPost(p *post.Post) error {
|
|
|
|
pid, err := strconv.Atoi(p.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
resp, err := sys.client.Comments(context.Background(), types.GetComments{
|
|
|
|
PostID: types.NewOptional(pid),
|
2022-12-28 22:22:36 -05:00
|
|
|
})
|
2022-12-30 02:48:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 02:48:53 -05:00
|
|
|
for _, i := range resp.Comments {
|
|
|
|
createdAt, err := dateparse.ParseAny(i.Comment.Published)
|
|
|
|
if err != nil {
|
|
|
|
createdAt = time.Now() // TODO: Errrrr
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Replies = append(p.Replies, reply.Reply{
|
|
|
|
ID: strconv.Itoa(i.Comment.ID),
|
|
|
|
|
|
|
|
Body: i.Comment.Content,
|
|
|
|
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
|
|
|
|
Author: author.Author{
|
|
|
|
ID: strconv.Itoa(i.Comment.CreatorID),
|
|
|
|
Name: i.Creator.Name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|