7 changed files with 180 additions and 104 deletions
@ -0,0 +1,52 @@
|
||||
package cfg |
||||
|
||||
import ( |
||||
"os" |
||||
"sort" |
||||
"strconv" |
||||
"strings" |
||||
) |
||||
|
||||
const ( |
||||
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN" |
||||
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS" |
||||
MASTODON_TOOT_FOOTER = "MASTODON_TOOT_FOOTER" |
||||
MASTODON_TOOT_MAX_CHARACTERS = "MASTODON_TOOT_MAX_CHARACTERS" |
||||
MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY" |
||||
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" |
||||
TELEGRAM_CHAT_ID = "TELEGRAM_CHAT_ID" |
||||
TELEGRAM_DEBUG = "TELEGRAM_DEBUG" |
||||
) |
||||
|
||||
// Check the specified Mastodon visibility and return it if valid or return
|
||||
// unlisted if it's not valid.
|
||||
// The specified string will be cheched case unsensitive.
|
||||
func parseMastodonVisibility(s string) string { |
||||
s = strings.ToLower(s) |
||||
// Keep sorted since we search inside.
|
||||
visibilities := []string{"direct", "private", "public", "unlisted"} |
||||
r := sort.SearchStrings(visibilities, s) |
||||
if r < len(visibilities) && visibilities[r] == s { |
||||
return s |
||||
} |
||||
|
||||
return "unlisted" |
||||
} |
||||
|
||||
// Return configured Mastodon visibility for toot.
|
||||
func GetMastodonVisibility() string { |
||||
return parseMastodonVisibility(os.Getenv(MASTODON_TOOT_VISIBILITY)) |
||||
} |
||||
|
||||
// Parse Mastodon max characters and return 500 as default in case of errors.
|
||||
func parseMastodonMaxCharacters(s string) int { |
||||
if n, err := strconv.ParseUint(s, 10, 32); err == nil { |
||||
return int(n) |
||||
} |
||||
|
||||
return 500 |
||||
} |
||||
|
||||
func GetMastodonMaxCharacters() int { |
||||
return parseMastodonMaxCharacters(os.Getenv(MASTODON_TOOT_MAX_CHARACTERS)) |
||||
} |
@ -0,0 +1,30 @@
|
||||
package cfg |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/alecthomas/assert" |
||||
) |
||||
|
||||
func TestParseMastodonVisibility(t *testing.T) { |
||||
assert.Equal(t, parseMastodonVisibility("public"), "public") |
||||
assert.Equal(t, parseMastodonVisibility("direct"), "direct") |
||||
assert.Equal(t, parseMastodonVisibility("unlisted"), "unlisted") |
||||
assert.Equal(t, parseMastodonVisibility("private"), "private") |
||||
|
||||
assert.Equal(t, parseMastodonVisibility("Public"), "public") |
||||
assert.Equal(t, parseMastodonVisibility("diRect"), "direct") |
||||
assert.Equal(t, parseMastodonVisibility("unlisTED"), "unlisted") |
||||
assert.Equal(t, parseMastodonVisibility("PRIVATE"), "private") |
||||
|
||||
assert.Equal(t, parseMastodonVisibility("True"), "unlisted") |
||||
assert.Equal(t, parseMastodonVisibility("eriol"), "unlisted") |
||||
assert.Equal(t, parseMastodonVisibility(""), "unlisted") |
||||
assert.Equal(t, parseMastodonVisibility(" "), "unlisted") |
||||
} |
||||
|
||||
func TestParseMastodonMaxCharacters(t *testing.T) { |
||||
assert.Equal(t, parseMastodonMaxCharacters("42"), 42) |
||||
assert.Equal(t, parseMastodonMaxCharacters("-42"), 500) |
||||
assert.Equal(t, parseMastodonMaxCharacters("hello"), 500) |
||||
} |
@ -0,0 +1,59 @@
|
||||
package mastodon |
||||
|
||||
import ( |
||||
"context" |
||||
"io" |
||||
"log" |
||||
|
||||
mastodonapi "github.com/cking/go-mastodon" |
||||
) |
||||
|
||||
// Post one or more toots.
|
||||
func PostToots(client *mastodonapi.Client, messages []string, visibility string) { |
||||
in_reply_to := "" |
||||
for _, message := range messages { |
||||
status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{ |
||||
Status: message, |
||||
Visibility: visibility, |
||||
InReplyToID: mastodonapi.ID(in_reply_to), |
||||
}) |
||||
if err != nil { |
||||
log.Printf("Could not post status: %v", err) |
||||
continue |
||||
} |
||||
log.Printf("Posted status %s", status.URL) |
||||
in_reply_to = string(status.ID) |
||||
} |
||||
} |
||||
|
||||
// Post a photo on mastodon with caption.
|
||||
func PostPhoto( |
||||
client *mastodonapi.Client, |
||||
file io.ReadCloser, |
||||
caption string, |
||||
maxCharacters int, |
||||
visibility string) { |
||||
attachment, err := client.UploadMediaFromReader( |
||||
context.Background(), file) |
||||
if err != nil { |
||||
log.Printf("Could not upload media: %v", err) |
||||
} |
||||
file.Close() |
||||
log.Printf("Posted attachment %s", attachment.TextURL) |
||||
|
||||
mediaIds := [...]mastodonapi.ID{attachment.ID} |
||||
if len(caption) > maxCharacters { |
||||
caption = caption[:maxCharacters] |
||||
} |
||||
status, err := client.PostStatus(context.Background(), &mastodonapi.Toot{ |
||||
// Write the caption in the toot because it almost probably
|
||||
// doesn't describe the image.
|
||||
Status: caption, |
||||
MediaIDs: mediaIds[:], |
||||
Visibility: visibility, |
||||
}) |
||||
if err != nil { |
||||
log.Printf("Could not post status: %v", err) |
||||
} |
||||
log.Printf("Posted status %s", status.URL) |
||||
} |
Loading…
Reference in new issue