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:
2026-11-10 06:14:40 -07:00
co-authored by Claude Opus 5.5
parent c48ee22f99
commit 4d304cd65b
18 changed files with 391 additions and 96 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
package store
import (
"coremarketing/internal/fileutil"
"encoding/json"
"os"
)
@@ -34,7 +36,7 @@ func (s *DismissedStore) Save() error {
if err != nil {
return err
}
return os.WriteFile(s.path, data, 0644)
return fileutil.WriteFileAtomic(s.path, data, 0600)
}
func (s *DismissedStore) Dismiss(id string) {
+30 -1
View File
@@ -1,6 +1,8 @@
package store
import (
"coremarketing/internal/fileutil"
"encoding/json"
"fmt"
"os"
@@ -13,6 +15,24 @@ type GeneratedPost struct {
Content string `json:"content"`
SourceURL string `json:"source_url,omitempty"`
GeneratedAt time.Time `json:"generated_at"`
// PostedTo lists platforms this post has already been published to, so a
// partial failure can be retried without double-posting elsewhere.
PostedTo []string `json:"posted_to,omitempty"`
}
// Platform identifiers recorded in GeneratedPost.PostedTo.
const (
PlatformX = "x"
PlatformLinkedIn = "linkedin"
)
func (p GeneratedPost) HasPosted(platform string) bool {
for _, done := range p.PostedTo {
if done == platform {
return true
}
}
return false
}
type Store struct {
@@ -41,7 +61,7 @@ func (s *Store) Save() error {
if err != nil {
return err
}
return os.WriteFile(s.path, data, 0644)
return fileutil.WriteFileAtomic(s.path, data, 0600)
}
func (s *Store) Add(appName, content, sourceURL string) {
@@ -63,3 +83,12 @@ func (s *Store) Remove(ids map[string]bool) {
}
s.Posts = kept
}
// MarkPosted records that the post with the given ID was published to platform.
func (s *Store) MarkPosted(id, platform string) {
for i := range s.Posts {
if s.Posts[i].ID == id && !s.Posts[i].HasPosted(platform) {
s.Posts[i].PostedTo = append(s.Posts[i].PostedTo, platform)
}
}
}
+39
View File
@@ -0,0 +1,39 @@
package store
import (
"os"
"path/filepath"
"testing"
)
func TestMarkPostedPersists(t *testing.T) {
path := filepath.Join(t.TempDir(), "posts.json")
s, err := Load(path)
if err != nil {
t.Fatal(err)
}
s.Add("App", "content", "")
id := s.Posts[0].ID
s.MarkPosted(id, PlatformX)
s.MarkPosted(id, PlatformX) // idempotent
if err := s.Save(); err != nil {
t.Fatal(err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if perm := info.Mode().Perm(); perm != 0600 {
t.Errorf("posts file mode = %o, want 600", perm)
}
reloaded, err := Load(path)
if err != nil {
t.Fatal(err)
}
p := reloaded.Posts[0]
if !p.HasPosted(PlatformX) || p.HasPosted(PlatformLinkedIn) || len(p.PostedTo) != 1 {
t.Errorf("PostedTo = %v, want [x]", p.PostedTo)
}
}