Stop committing credentials; fix posting length, partial posts, and data safety
- Untrack config.yaml (now gitignored) and add config.example.yaml with blank credentials; install.sh copies the example and sets mode 600 - Reserve room for the appended source link when asking Claude for a post, count length the way X does, and refuse over-length tweets before posting - Return https://x.com/i/status/<id> for posted tweets - Track which platforms each queued post reached so a partial failure can be retried without double-posting - Write config, state, posts and dismissed files atomically with 0600 - Keep refreshed Twitter tokens in memory if saving them fails, and log the failure instead of writing to the hidden stderr - Exit with a clear message on corrupt data files instead of panicking - Surface save errors in the UI; show status/error messages on home - Add tests for X length counting, the post store, and token updates Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,7 +21,7 @@ const apiBase = "https://api.twitter.com/2"
|
||||
type TokenRefresher func(ctx context.Context) (accessToken, refreshToken string, err error)
|
||||
|
||||
type Client struct {
|
||||
bearerToken string
|
||||
bearerToken string
|
||||
oauth2AccessToken string
|
||||
maxResults int
|
||||
http *http.Client
|
||||
@@ -141,7 +141,7 @@ func (c *Client) postTweet(ctx context.Context, text string) (tweetURL string, u
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
return "https://x.com/" + out.Data.ID, false, nil
|
||||
return "https://x.com/i/status/" + out.Data.ID, false, nil
|
||||
}
|
||||
|
||||
// BuildSearchQuery constructs an X search query from hashtags.
|
||||
@@ -280,3 +280,44 @@ func clamp(v, min, max int) int {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// URLLength is the fixed weight X assigns to any link (t.co wrapping).
|
||||
const URLLength = 23
|
||||
|
||||
// TweetLength returns the length X will count for a post consisting of
|
||||
// content followed by "\n\n" and sourceURL (when non-empty).
|
||||
func TweetLength(content, sourceURL string) int {
|
||||
n := weightedLength(content)
|
||||
if sourceURL != "" {
|
||||
n += 2 + URLLength
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// URLReserve is how many characters to hold back from the post limit so the
|
||||
// source URL appended at posting time still fits.
|
||||
func URLReserve(sourceURL string) int {
|
||||
if sourceURL == "" {
|
||||
return 0
|
||||
}
|
||||
return 2 + URLLength
|
||||
}
|
||||
|
||||
// weightedLength approximates X's character counting: Latin and common
|
||||
// punctuation count as 1, everything else (CJK, emoji, …) counts as 2. It may
|
||||
// overcount multi-codepoint emoji, which errs on the side of fitting.
|
||||
func weightedLength(s string) int {
|
||||
n := 0
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case r <= 0x10FF,
|
||||
r >= 0x2000 && r <= 0x200D,
|
||||
r >= 0x2010 && r <= 0x201F,
|
||||
r >= 0x2032 && r <= 0x2037:
|
||||
n++
|
||||
default:
|
||||
n += 2
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTweetLength(t *testing.T) {
|
||||
cases := []struct {
|
||||
content, url string
|
||||
want int
|
||||
}{
|
||||
{"hello", "", 5},
|
||||
{"hello", "https://example.com/a/very/long/path/that/x/shortens", 5 + 2 + 23},
|
||||
{"café — “quoted”", "", 15},
|
||||
{"🚀", "", 2},
|
||||
{"日本", "", 4},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := TweetLength(c.content, c.url); got != c.want {
|
||||
t.Errorf("TweetLength(%q, %q) = %d, want %d", c.content, c.url, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReservedLimitFits(t *testing.T) {
|
||||
url := "https://x.com/someone/status/123"
|
||||
limit := 280 - URLReserve(url)
|
||||
if got := TweetLength(strings.Repeat("a", limit), url); got != 280 {
|
||||
t.Errorf("post at reserved limit counts as %d, want 280", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user