1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-07-07 02:54:15 -04:00
neonmodem/system/lemmy/lemmy.go

309 lines
5.8 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"
2023-01-05 11:43:52 -05:00
"encoding/json"
"fmt"
2023-01-04 23:59:37 -05:00
"net/url"
"strconv"
2022-12-30 00:39:51 -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"
"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
}
2023-01-05 00:23:25 -05:00
func (sys *System) GetCapabilities() adapter.Capabilities {
var caps []adapter.Capability
2023-01-05 00:04:20 -05:00
caps = append(caps,
2023-01-05 17:07:09 -05:00
adapter.Capability{
ID: "list:forums",
Name: "List Forums",
},
2023-01-05 00:04:20 -05:00
adapter.Capability{
ID: "list:posts",
Name: "List Posts",
},
// TODO
2023-01-05 13:24:32 -05:00
// adapter.Capability{
// ID: "create:post",
// Name: "Create Post",
// },
adapter.Capability{
ID: "list:replies",
Name: "List Replies",
},
adapter.Capability{
ID: "create:reply",
Name: "Create Reply",
},
2023-01-05 00:04:20 -05:00
)
return caps
}
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()
}
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.ClientLogin(context.Background(), types.Login{
2022-12-30 00:39:51 -05:00
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) {
resp, err := sys.client.ListCommunities(context.Background(), types.ListCommunities{
Type: types.NewOptional(types.ListingTypeSubscribed),
2023-01-05 11:43:52 -05:00
})
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.Community.ID),
Name: i.Community.Name,
2023-01-05 11:43:52 -05:00
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{
Type: types.NewOptional(types.ListingTypeSubscribed),
Sort: types.NewOptional(types.SortTypeNew),
Limit: types.NewOptional(int64(50)),
2022-12-30 00:39:51 -05:00
})
sys.logger.Debug("DEEEEBUUUUUUUGGGGGGGG")
2022-12-30 00:39:51 -05:00
if err != nil {
return []post.Post{}, err
}
cfg := sys.GetConfig()
baseURL := cfg["url"].(string)
2022-12-30 00:39:51 -05:00
var models []post.Post
for _, i := range resp.Posts {
b, _ := json.Marshal(i)
sys.logger.Debug(string(b))
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("")
}
createdAt := i.Post.Published.Time
lastCommentedAt := i.Counts.NewestCommentTime.Time
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,
2023-01-05 11:43:52 -05:00
SysIDX: sys.ID,
2022-12-30 23:57:03 -05:00
},
URL: fmt.Sprintf("%s/post/%d", baseURL, i.Post.ID),
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.Post(context.Background(), types.GetPost{
ID: 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 := i.Comment.Published.Time
p.Replies = append(p.Replies, reply.Reply{
ID: strconv.Itoa(i.Comment.ID),
InReplyTo: p.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
}
var create types.CreateComment
if r.InReplyTo != "" {
// Reply to a reply of a post
InReplyTo, err := strconv.Atoi(r.InReplyTo)
if err != nil {
return err
}
create = types.CreateComment{
PostID: InReplyTo,
ParentID: types.NewOptional(ID),
Content: r.Body,
}
} else {
// Reply to a post
create = types.CreateComment{
PostID: ID,
Content: r.Body,
}
}
resp, err := sys.client.CreateComment(context.Background(), create)
if err != nil {
return err
}
r.ID = strconv.Itoa(resp.CommentView.Comment.ID)
return nil
}