From 9878f83c6309249ebc384a183ce6107991103225 Mon Sep 17 00:00:00 2001 From: lauralani Date: Fri, 7 Jul 2023 11:04:34 +0200 Subject: [PATCH] log new mastodon post url --- app/mastodon.go | 22 ++++++++++++- models/mastodon.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 models/mastodon.go diff --git a/app/mastodon.go b/app/mastodon.go index 5723641..8c2a35b 100644 --- a/app/mastodon.go +++ b/app/mastodon.go @@ -10,6 +10,7 @@ import ( "bytes" "codeberg.org/lauralani/humble-bot/misc" "codeberg.org/lauralani/humble-bot/models" + "encoding/json" "errors" "fmt" "github.com/google/uuid" @@ -24,6 +25,7 @@ func postQueueItemToMastodon(bundle models.QueueItem) error { urlstring := viper.GetString("mastodon.url") + "/api/v1/statuses" endpoint, _ := url.Parse(urlstring) visibility := viper.GetString("mastodon.visibility") + var post models.MastodonPost token := viper.GetString("mastodon.token") idemkey := uuid.New().String() @@ -67,7 +69,25 @@ func postQueueItemToMastodon(bundle models.QueueItem) error { return errors.New("couldn't POST message to Mastodon") } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Error().Str("func", "postQueueItemToMastodon.body.Close()").Msg(err.Error()) + } + }(res.Body) + + body, err := io.ReadAll(res.Body) + if err != nil { + log.Error().Str("func", "postQueueItemToMastodon.body.ReadAll()").Msg(err.Error()) + } + + err = json.Unmarshal(body, &post) + if err != nil { + log.Error().Str("func", "postQueueItemToMastodon.body.Unmarshal()"). + Msgf("can't unmarshal body: %q", err) + } + log.Info(). - Str("url", bundle.URL).Msg("posted new Bundle to Mastodon =)") + Str("bundle-url", bundle.URL).Str("post-url", post.URL).Msg("posted new Bundle =)") return nil } diff --git a/models/mastodon.go b/models/mastodon.go new file mode 100644 index 0000000..5fd22f3 --- /dev/null +++ b/models/mastodon.go @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 Laura Kalb + * The code of this project is available under the MIT license. See the LICENSE file for more info. + * + */ + +package models + +import "time" + +type MastodonPost struct { + ID string `json:"id"` + CreatedAt time.Time `json:"created_at"` + InReplyToID string `json:"in_reply_to_id"` + InReplyToAccountID string `json:"in_reply_to_account_id"` + Sensitive bool `json:"sensitive"` + SpoilerText string `json:"spoiler_text"` + Visibility string `json:"visibility"` + Language string `json:"language"` + URI string `json:"uri"` + URL string `json:"url"` + RepliesCount int `json:"replies_count"` + ReblogsCount int `json:"reblogs_count"` + FavouritesCount int `json:"favourites_count"` + Favourited bool `json:"favourited"` + Reblogged bool `json:"reblogged"` + Muted bool `json:"muted"` + Bookmarked bool `json:"bookmarked"` + Content string `json:"content"` + Reblog any `json:"reblog,omitempty"` + Application struct { + Name string `json:"name"` + Website string `json:"website,omitempty"` + } `json:"application"` + Account struct { + ID string `json:"id"` + Username string `json:"username"` + Acct string `json:"acct"` + DisplayName string `json:"display_name"` + Locked bool `json:"locked"` + Bot bool `json:"bot"` + Discoverable bool `json:"discoverable"` + Group bool `json:"group"` + CreatedAt time.Time `json:"created_at"` + Note string `json:"note"` + URL string `json:"url"` + Avatar string `json:"avatar"` + AvatarStatic string `json:"avatar_static"` + Header string `json:"header"` + HeaderStatic string `json:"header_static"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StatusesCount int `json:"statuses_count"` + LastStatusAt time.Time `json:"last_status_at"` + Emojis []any `json:"emojis,omitempty"` + Fields []struct { + Name string `json:"name"` + Value string `json:"value"` + VerifiedAt any `json:"verified_at,omitempty"` + } `json:"fields"` + } `json:"account"` + MediaAttachments []any `json:"media_attachments,omitempty"` + Mentions []any `json:"mentions,omitempty"` + Tags []any `json:"tags,omitempty"` + Emojis []any `json:"emojis,omitempty"` + Card struct { + URL string `json:"url"` + Title string `json:"title"` + Description string `json:"description"` + Type string `json:"type"` + AuthorName string `json:"author_name"` + AuthorURL string `json:"author_url"` + ProviderName string `json:"provider_name"` + ProviderURL string `json:"provider_url"` + HTML string `json:"html"` + Width int `json:"width"` + Height int `json:"height"` + Image any `json:"image,omitempty"` + EmbedURL string `json:"embed_url"` + } `json:"card"` + Poll any `json:"poll,omitempty"` +}