From 188d0db5716baf4415ad49d406bd425d4b4d2391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Fri, 30 Dec 2022 01:44:44 -0500 Subject: [PATCH] Extended discourse, lemmy implementations --- system/discourse/discourse.go | 23 ++++++++++++++++++++++- system/discourse/topics.go | 14 +++++++++++--- system/lemmy/lemmy.go | 28 +++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index cac8570..dceefdc 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -3,7 +3,9 @@ package discourse import ( "context" "net/http" + "strconv" + "github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/system/adapter" "go.uber.org/zap" @@ -75,9 +77,28 @@ func (sys *System) ListPosts() ([]post.Post, error) { var models []post.Post for _, i := range (*items).TopicList.Topics { + var userName string = "" + for _, u := range (*items).Users { + if u.ID == i.Posters[0].UserID { + userName = u.Name + break + } + } + models = append(models, post.Post{ - ID: string(i.ID), + ID: strconv.Itoa(i.ID), + Subject: i.Title, + + Type: "post", + + Pinned: i.Pinned, + Closed: i.Closed, + + Author: author.Author{ + ID: strconv.Itoa(i.Posters[0].UserID), + Name: userName, + }, }) } diff --git a/system/discourse/topics.go b/system/discourse/topics.go index 883598d..5857d98 100644 --- a/system/discourse/topics.go +++ b/system/discourse/topics.go @@ -26,6 +26,14 @@ type LatestTopicsResponse struct { } `json:"topic_list"` } +type SingleTopicResponse struct { + PostStream struct { + Posts []PostModel `json:"posts"` + } `json:"post_stream"` + + TopicModel +} + type TopicModel struct { ID int `json:"id"` Title string `json:"title"` @@ -71,7 +79,7 @@ type TopicsService interface { Show( ctx context.Context, id string, - ) (*TopicModel, error) + ) (*SingleTopicResponse, error) ListLatest( ctx context.Context, ) (*LatestTopicsResponse, error) @@ -85,7 +93,7 @@ type TopicServiceHandler struct { func (a *TopicServiceHandler) Show( ctx context.Context, id string, -) (*TopicModel, error) { +) (*SingleTopicResponse, error) { uri := TopicsBaseURL + "/t/" + id + ".json" req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil) @@ -93,7 +101,7 @@ func (a *TopicServiceHandler) Show( return nil, err } - response := new(TopicModel) + response := new(SingleTopicResponse) if err = a.client.Do(ctx, req, response); err != nil { return nil, err } diff --git a/system/lemmy/lemmy.go b/system/lemmy/lemmy.go index ba34246..7c75a1c 100644 --- a/system/lemmy/lemmy.go +++ b/system/lemmy/lemmy.go @@ -2,7 +2,9 @@ package lemmy import ( "context" + "strconv" + "github.com/mrusme/gobbs/models/author" "github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/system/adapter" "go.arsenm.dev/go-lemmy" @@ -67,9 +69,33 @@ func (sys *System) ListPosts() ([]post.Post, error) { var models []post.Post for _, i := range resp.Posts { + t := "post" + if i.Post.URL.IsValid() { + t = "url" + } + + var userName string = "" + presp, err := sys.client.PersonDetails(context.Background(), types.GetPersonDetails{ + PersonID: types.NewOptional(i.Post.CreatorID), + }) + if err == nil { + userName = presp.PersonView.Person.Name + } + models = append(models, post.Post{ - ID: string(i.Post.ID), + ID: strconv.Itoa(i.Post.ID), + Subject: i.Post.Name, + + Type: t, + + Pinned: i.Post.Stickied, + Closed: i.Post.Locked, + + Author: author.Author{ + ID: strconv.Itoa(i.Post.CreatorID), + Name: userName, + }, }) }