1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-11-03 04:27:16 -05:00
neonmodem/system/lemmy/lemmy.go

238 lines
4.7 KiB
Go
Raw Normal View History

2022-12-28 22:22:36 -05:00
package lemmy
import (
2022-12-30 00:39:51 -05:00
"context"
"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"
"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"
"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 {
ID int
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
}
func (sys *System) GetID() int {
return sys.ID
}
func (sys *System) SetID(id int) {
sys.ID = id
}
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
}
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
}
func (sys *System) ListPosts() ([]post.Post, error) {
2022-12-30 00:39:51 -05:00
resp, err := sys.client.Posts(context.Background(), types.GetPosts{
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 {
t := "post"
2022-12-30 02:05:48 -05:00
body := i.Post.Body.ValueOr("")
if i.Post.URL.IsValid() {
t = "url"
2022-12-30 02:05:48 -05:00
body = i.Post.URL.ValueOr("")
}
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 00:39:51 -05:00
models = append(models, post.Post{
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,
Type: t,
Pinned: i.Post.Stickied,
Closed: i.Post.Locked,
2022-12-30 02:05:48 -05:00
CreatedAt: createdAt,
LastCommentedAt: lastCommentedAt,
Author: author.Author{
ID: strconv.Itoa(i.Post.CreatorID),
2022-12-30 02:05:48 -05:00
Name: i.Creator.Name,
},
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,
},
SysIDX: sys.ID,
2022-12-30 00:39:51 -05:00
})
}
return models, nil
2022-12-28 22:22:36 -05:00
}
func (sys *System) LoadPost(p *post.Post) error {
pid, err := strconv.Atoi(p.ID)
if err != nil {
return err
}
// cid, err := strconv.Atoi(p.Forum.ID)
// if err != nil {
// return err
// }
2022-12-28 22:22:36 -05:00
resp, err := sys.client.Comments(context.Background(), types.GetComments{
Type: types.NewOptional(types.ListingLocal),
Sort: types.NewOptional(types.CommentSortHot),
// CommunityID: types.NewOptional(cid),
PostID: types.NewOptional(pid),
2022-12-28 22:22:36 -05:00
})
if err != nil {
return err
}
2022-12-28 22:22:36 -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,
},
SysIDX: sys.ID,
})
}
return nil
2022-12-28 22:22:36 -05:00
}
func (sys *System) CreatePost(p *post.Post) error {
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)
return nil
}
func (sys *System) CreateReply(r *reply.Reply) error {
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)
return nil
}