2022-12-28 22:22:36 -05:00
|
|
|
package lemmy
|
|
|
|
|
|
|
|
import (
|
2022-12-30 00:39:51 -05:00
|
|
|
"context"
|
2023-01-05 11:43:52 -05:00
|
|
|
"encoding/json"
|
2023-01-04 21:41:41 -05:00
|
|
|
"fmt"
|
2023-01-04 23:59:37 -05:00
|
|
|
"net/url"
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-05 00:23:25 -05:00
|
|
|
func (sys *System) GetCapabilities() adapter.Capabilities {
|
2022-12-30 02:48:53 -05:00
|
|
|
var caps []adapter.Capability
|
|
|
|
|
2023-01-05 00:04:20 -05:00
|
|
|
caps = append(caps,
|
2023-01-05 13:24:32 -05:00
|
|
|
// TODO: https://github.com/Arsen6331/go-lemmy/issues/2
|
|
|
|
// adapter.Capability{
|
|
|
|
// ID: "list:forums",
|
|
|
|
// Name: "List Forums",
|
|
|
|
// },
|
2023-01-05 00:04:20 -05:00
|
|
|
adapter.Capability{
|
|
|
|
ID: "list:posts",
|
|
|
|
Name: "List Posts",
|
|
|
|
},
|
2023-01-05 13:24:32 -05:00
|
|
|
// TODO: Not possible without list:forums
|
|
|
|
// adapter.Capability{
|
|
|
|
// ID: "create:post",
|
|
|
|
// Name: "Create Post",
|
|
|
|
// },
|
|
|
|
// TODO: https://github.com/Arsen6331/go-lemmy/issues/1
|
|
|
|
// adapter.Capability{
|
|
|
|
// ID: "list:replies",
|
|
|
|
// Name: "List Replies",
|
|
|
|
// },
|
|
|
|
// TODO: Maybe possible but kind of pointless without list:replies
|
|
|
|
// adapter.Capability{
|
|
|
|
// ID: "create:reply",
|
|
|
|
// Name: "Create Reply",
|
|
|
|
// },
|
2023-01-05 00:04:20 -05:00
|
|
|
)
|
2022-12-30 02:48:53 -05:00
|
|
|
|
|
|
|
return caps
|
|
|
|
}
|
|
|
|
|
2023-01-04 21:41:41 -05:00
|
|
|
func (sys *System) FilterValue() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"Lemmy %s",
|
|
|
|
sys.config["url"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) Title() string {
|
2023-01-04 23:59:37 -05:00
|
|
|
sysUrl := sys.config["url"].(string)
|
|
|
|
u, err := url.Parse(sysUrl)
|
|
|
|
if err != nil {
|
|
|
|
return sysUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Hostname()
|
2023-01-04 21:41:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) Description() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"Lemmy",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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-05 11:43:52 -05:00
|
|
|
func (sys *System) ListForums() ([]forum.Forum, error) {
|
2023-01-05 12:12:14 -05:00
|
|
|
return []forum.Forum{}, nil
|
|
|
|
// Not possible to list forums atm
|
|
|
|
|
2023-01-05 11:43:52 -05:00
|
|
|
resp, err := sys.client.Communities(context.Background(), types.ListCommunities{
|
|
|
|
Type: types.NewOptional(types.ListingSubscribed),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return []forum.Forum{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var models []forum.Forum
|
|
|
|
for _, i := range resp.Communities {
|
|
|
|
sys.logger.Debugf("FORUM:")
|
|
|
|
b, _ := json.Marshal(i)
|
|
|
|
sys.logger.Debug(string(b))
|
|
|
|
models = append(models, forum.Forum{
|
|
|
|
ID: strconv.Itoa(i.CommunitySafe.ID),
|
|
|
|
Name: i.CommunitySafe.Name,
|
|
|
|
|
|
|
|
SysIDX: sys.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return models, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *System) ListPosts(forumID string) ([]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
|
|
|
|
}
|
|
|
|
|
2023-01-05 15:26:27 -05:00
|
|
|
cfg := sys.GetConfig()
|
|
|
|
baseURL := cfg["url"].(string)
|
|
|
|
|
2022-12-30 00:39:51 -05:00
|
|
|
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-05 11:43:52 -05:00
|
|
|
|
|
|
|
SysIDX: sys.ID,
|
2022-12-30 23:57:03 -05:00
|
|
|
},
|
|
|
|
|
2023-01-05 15:26:27 -05:00
|
|
|
URL: fmt.Sprintf("%s/post/%d", baseURL, i.Post.ID),
|
|
|
|
|
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
|
|
|
|
}
|