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-30 23:57:03 -05:00
|
|
|
"github.com/mrusme/gobbs/models/forum"
|
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 {
|
2023-01-01 22:51:16 -05:00
|
|
|
ID int
|
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
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:16 -05:00
|
|
|
func (sys *System) GetID() int {
|
|
|
|
return sys.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) SetID(id int) {
|
|
|
|
sys.ID = id
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:16 -05:00
|
|
|
func (sys *System) ListPosts() ([]post.Post, error) {
|
2022-12-30 00:39:51 -05:00
|
|
|
resp, err := sys.client.Posts(context.Background(), types.GetPosts{
|
2022-12-31 00:39:34 -05:00
|
|
|
Type: types.NewOptional(types.ListingSubscribed),
|
|
|
|
Sort: types.NewOptional(types.New),
|
|
|
|
Limit: types.NewOptional(int64(50)),
|
2022-12-30 00:39:51 -05:00
|
|
|
})
|
|
|
|
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
|
|
|
|
2022-12-30 23:57:03 -05:00
|
|
|
Forum: forum.Forum{
|
|
|
|
ID: strconv.Itoa(i.Post.CommunityID),
|
|
|
|
Name: i.Community.Name,
|
|
|
|
},
|
|
|
|
|
2023-01-01 22:51:16 -05:00
|
|
|
SysIDX: sys.ID,
|
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-31 00:39:34 -05:00
|
|
|
// cid, err := strconv.Atoi(p.Forum.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{
|
2022-12-31 00:39:34 -05:00
|
|
|
Type: types.NewOptional(types.ListingLocal),
|
|
|
|
Sort: types.NewOptional(types.CommentSortHot),
|
|
|
|
// CommunityID: types.NewOptional(cid),
|
2022-12-30 02:48:53 -05:00
|
|
|
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,
|
|
|
|
},
|
2023-01-01 22:51:16 -05:00
|
|
|
|
|
|
|
SysIDX: sys.ID,
|
2022-12-30 02:48:53 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
2023-01-01 22:51:16 -05:00
|
|
|
|
|
|
|
func (sys *System) CreatePost(p *post.Post) error {
|
2023-01-02 23:47:30 -05:00
|
|
|
communityID, err := strconv.Atoi(p.Forum.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := sys.client.CreatePost(context.Background(), types.CreatePost{
|
|
|
|
Name: p.Subject,
|
|
|
|
CommunityID: communityID,
|
|
|
|
Body: types.NewOptional(p.Body),
|
|
|
|
NSFW: types.NewOptional(false),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ID = strconv.Itoa(resp.PostView.Post.ID)
|
2023-01-01 22:51:16 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) CreateReply(r *reply.Reply) error {
|
2023-01-02 23:47:30 -05:00
|
|
|
id, err := strconv.Atoi(r.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
inReplyTo, err := strconv.Atoi(r.InReplyTo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := sys.client.CreateComment(context.Background(), types.CreateComment{
|
|
|
|
PostID: inReplyTo,
|
|
|
|
ParentID: types.NewOptional(id),
|
|
|
|
Content: r.Body,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ID = strconv.Itoa(resp.CommentView.Comment.ID)
|
2023-01-01 22:51:16 -05:00
|
|
|
return nil
|
|
|
|
}
|