1
0
Fork 0

Don't cut caption to max toot chars
continuous-integration/drone/push Build is passing Details

We split the caption as we do for messages and post it in a thread.
This commit is contained in:
Daniele Tricoli 2022-03-13 04:09:06 +01:00
parent 37a52eda8a
commit 38101bd2a5
2 changed files with 24 additions and 18 deletions

View File

@ -61,9 +61,11 @@ the specified Mastodon account.`,
if update.Message.Text != "" { if update.Message.Text != "" {
log.Printf("Text message received. Message id: %d\n", messageID) log.Printf("Text message received. Message id: %d\n", messageID)
text := update.Message.Text messages := utils.SplitTextAtChunk(
messages := utils.SplitTextAtChunk(text, maxChars, tootFooter) update.Message.Text,
mastodon.PostToots(c, messages, tootVisibility) maxChars,
tootFooter)
mastodon.PostToots(c, messages, tootVisibility, "")
} else if update.Message.Photo != nil { } else if update.Message.Photo != nil {
log.Printf("Photo received. Message id: %d\n", messageID) log.Printf("Photo received. Message id: %d\n", messageID)
@ -86,11 +88,14 @@ the specified Mastodon account.`,
continue continue
} }
messages := utils.SplitTextAtChunk(
update.Message.Caption,
maxChars,
tootFooter)
mastodon.PostPhoto( mastodon.PostPhoto(
c, c,
file, file,
update.Message.Caption, messages,
maxChars,
tootVisibility, tootVisibility,
) )
} }

View File

@ -9,29 +9,32 @@ import (
) )
// Post one or more toots. // Post one or more toots.
func PostToots(client *mastodonapi.Client, messages []string, visibility string) { func PostToots(
in_reply_to := "" client *mastodonapi.Client,
messages []string,
visibility string,
inReplyTo string) {
for _, message := range messages { for _, message := range messages {
status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{ status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{
Status: message, Status: message,
Visibility: visibility, Visibility: visibility,
InReplyToID: mastodonapi.ID(in_reply_to), InReplyToID: mastodonapi.ID(inReplyTo),
}) })
if err != nil { if err != nil {
log.Printf("Could not post status: %v", err) log.Printf("Could not post status: %v", err)
continue continue
} }
log.Printf("Posted status %s", status.URL) log.Printf("Posted status %s", status.URL)
in_reply_to = string(status.ID) inReplyTo = string(status.ID)
} }
} }
// Post a photo on mastodon with caption. // Post a photo on mastodon using the caption as text of the toot.
// If the caption is bigger than the max toot size, we post multiple toots.
func PostPhoto( func PostPhoto(
client *mastodonapi.Client, client *mastodonapi.Client,
file io.ReadCloser, file io.ReadCloser,
caption string, messages []string,
maxCharacters int,
visibility string) { visibility string) {
attachment, err := client.UploadMediaFromReader( attachment, err := client.UploadMediaFromReader(
context.Background(), file) context.Background(), file)
@ -42,13 +45,9 @@ func PostPhoto(
log.Printf("Posted attachment %s", attachment.TextURL) log.Printf("Posted attachment %s", attachment.TextURL)
mediaIds := [...]mastodonapi.ID{attachment.ID} mediaIds := [...]mastodonapi.ID{attachment.ID}
if len(caption) > maxCharacters { // 1. Post the photo with the first part of the message.
caption = caption[:maxCharacters]
}
status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{ status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{
// Write the caption in the toot because it almost probably Status: messages[0],
// doesn't describe the image.
Status: caption,
MediaIDs: mediaIds[:], MediaIDs: mediaIds[:],
Visibility: visibility, Visibility: visibility,
}) })
@ -56,4 +55,6 @@ func PostPhoto(
log.Printf("Could not post status: %v", err) log.Printf("Could not post status: %v", err)
} }
log.Printf("Posted status %s", status.URL) log.Printf("Posted status %s", status.URL)
// 2. Post the remaining caption if it exists.
PostToots(client, messages[1:], visibility, string(status.ID))
} }