From 3762ccfb8380ff559bfe61768331aeabfe94f12c Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Thu, 12 Oct 2023 15:15:07 +0300 Subject: [PATCH] true remote timeline --- mastodon/status.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ service/service.go | 10 +++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/mastodon/status.go b/mastodon/status.go index e148720..105c312 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -8,6 +8,9 @@ import ( "net/http" "net/url" "time" + "encoding/json" + "path" + "strings" ) type StatusPleroma struct { @@ -224,6 +227,51 @@ func (c *Client) GetTimelineHome(ctx context.Context, pg *Pagination) ([]*Status return statuses, nil } +type RemoteTimelineInstance struct { + http.Client +} + +// TrueRemoteTimeline get public timeline from remote Mastodon API compatible instance directly +func (c *Client) TrueRemoteTimeline(ctx context.Context, instance string, pg *Pagination) ([]*Status, error) { + var httpclient RemoteTimelineInstance + var publicstatuses []*Status + params := url.Values{} + params.Set("local", "true") + if pg != nil { + params = pg.setValues(params) + } + u, err := url.Parse("https://" + instance) + if err != nil { + return nil, err + } + u.Path = path.Join(u.Path, "/api/v1/timelines/public") + + req, err := http.NewRequest(http.MethodGet, u.String(), strings.NewReader(params.Encode())) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + resp, err := httpclient.Do(req) + fmt.Println(req) + fmt.Println(resp) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, parseAPIError("bad request", resp) + } + + err = json.NewDecoder(resp.Body).Decode(&publicstatuses) + fmt.Println(resp.Body) + if err != nil { + return nil, err + } + return publicstatuses, nil +} + // GetTimelinePublic return statuses from public timeline. func (c *Client) GetTimelinePublic(ctx context.Context, isLocal bool, instance string, pg *Pagination) ([]*Status, error) { params := url.Values{} diff --git a/service/service.go b/service/service.go index 4643dae..18a3b89 100644 --- a/service/service.go +++ b/service/service.go @@ -149,12 +149,20 @@ func (s *service) TimelinePage(c *client, tType, instance, listId, maxID, title = "Local Timeline" case "remote": if len(instance) > 0 { - statuses, err = c.GetTimelinePublic(c.ctx, false, instance, &pg) + statuses, err = c.GetTimelinePublic(c.ctx, true, instance, &pg) if err != nil { return err } } title = "Remote Timeline" + case "tremote": + if len(instance) > 0 { + statuses, err = c.TrueRemoteTimeline(c.ctx, instance, &pg) + if err != nil { + return err + } + } + title = "True Remote Timeline" case "twkn": statuses, err = c.GetTimelinePublic(c.ctx, false, "", &pg) if err != nil {