Repository navigation
Expand file tree
/
Copy pathxstore.go
More file actions
146 lines (130 loc) · 3.77 KB
/
Copy pathxstore.go
File metadata and controls
146 lines (130 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
// XAccount links a nostr pubkey (team member in nostr.json) to an X/Twitter
// identity. Two modes:
// - "local": this relay holds the X app consumer credentials and the user's
// OAuth tokens (AccessToken/AccessSecret) and posts directly.
// - "delegate": a shared xpost service holds all X credentials; this relay
// only keeps a per-link LinkToken used to call its /v1/post endpoint.
type XAccount struct {
UserPubkey string `json:"user_pubkey"`
Mode string `json:"mode,omitempty"` // "local" (default) or "delegate"
TwitterUserID string `json:"twitter_user_id"`
ScreenName string `json:"screen_name"`
AccessToken string `json:"access_token,omitempty"`
AccessSecret string `json:"access_secret,omitempty"`
LinkToken string `json:"link_token,omitempty"`
Enabled bool `json:"enabled"`
LinkedAt time.Time `json:"linked_at"`
LastPostedAt *time.Time `json:"last_posted_at,omitempty"`
LastError string `json:"last_error,omitempty"`
// IDs of nostr events already crossposted, capped for dedupe.
PostedIDs []string `json:"posted_ids,omitempty"`
}
// copy returns a deep copy so callers can't mutate store state.
func (a *XAccount) copy() *XAccount {
c := *a
if a.LastPostedAt != nil {
t := *a.LastPostedAt
c.LastPostedAt = &t
}
if a.PostedIDs != nil {
c.PostedIDs = make([]string, len(a.PostedIDs))
copy(c.PostedIDs, a.PostedIDs)
}
return &c
}
const maxPostedIDs = 500
func (a *XAccount) markPosted(eventID string, at time.Time) {
if a.hasPosted(eventID) {
return
}
a.PostedIDs = append(a.PostedIDs, eventID)
if len(a.PostedIDs) > maxPostedIDs {
a.PostedIDs = a.PostedIDs[len(a.PostedIDs)-maxPostedIDs:]
}
a.LastPostedAt = &at
a.LastError = ""
}
func (a *XAccount) hasPosted(eventID string) bool {
for _, id := range a.PostedIDs {
if id == eventID {
return true
}
}
return false
}
// XAccountStore persists linked X accounts to a JSON file, same pattern as
// SchedulerStore. Secret material stays server-side on disk; it is never
// exposed through the API (status endpoint strips it).
type XAccountStore struct {
mu sync.RWMutex
filePath string
accounts map[string]*XAccount
}
func NewXAccountStore(dataDir string) (*XAccountStore, error) {
store := &XAccountStore{
filePath: filepath.Join(dataDir, "x_accounts.json"),
accounts: make(map[string]*XAccount),
}
if err := os.MkdirAll(dataDir, 0755); err != nil {
return nil, err
}
data, err := os.ReadFile(store.filePath)
if err != nil {
if os.IsNotExist(err) {
return store, nil
}
return nil, err
}
if err := json.Unmarshal(data, &store.accounts); err != nil {
return nil, fmt.Errorf("parsing x_accounts.json: %w", err)
}
return store, nil
}
// saveLocked must be called with s.mu held (read or write).
func (s *XAccountStore) saveLocked() error {
data, err := json.MarshalIndent(s.accounts, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.filePath, data, 0600)
}
func (s *XAccountStore) Get(pubkey string) *XAccount {
s.mu.RLock()
defer s.mu.RUnlock()
if acct, ok := s.accounts[pubkey]; ok {
return acct.copy()
}
return nil
}
func (s *XAccountStore) Put(acct *XAccount) error {
s.mu.Lock()
defer s.mu.Unlock()
s.accounts[acct.UserPubkey] = acct
return s.saveLocked()
}
func (s *XAccountStore) Delete(pubkey string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.accounts, pubkey)
return s.saveLocked()
}
// Mutate applies fn to the stored account under lock and persists.
func (s *XAccountStore) Mutate(pubkey string, fn func(*XAccount)) error {
s.mu.Lock()
defer s.mu.Unlock()
acct, ok := s.accounts[pubkey]
if !ok {
return fmt.Errorf("account not found")
}
fn(acct)
return s.saveLocked()
}