1package fakedata
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8
9 comatproto "github.com/bluesky-social/indigo/api/atproto"
10 "github.com/bluesky-social/indigo/util"
11 "github.com/bluesky-social/indigo/xrpc"
12
13 "github.com/carlmjohnson/versioninfo"
14)
15
16type AccountCatalog struct {
17 Celebs []AccountContext
18 Regulars []AccountContext
19}
20
21func (ac *AccountCatalog) Combined() []AccountContext {
22 var combined []AccountContext
23 combined = append(combined, ac.Celebs...)
24 combined = append(combined, ac.Regulars...)
25 return combined
26}
27
28type AccountContext struct {
29 // 0-based index; should match index
30 Index int `json:"index"`
31 AccountType string `json:"accountType"`
32 Email string `json:"email"`
33 Password string `json:"password"`
34 Auth xrpc.AuthInfo `json:"auth"`
35}
36
37func ReadAccountCatalog(path string) (*AccountCatalog, error) {
38 catalog := &AccountCatalog{}
39 catFile, err := os.Open(path)
40 if err != nil {
41 return nil, err
42 }
43 defer catFile.Close()
44
45 decoder := json.NewDecoder(catFile)
46 for decoder.More() {
47 var usr AccountContext
48 if err := decoder.Decode(&usr); err != nil {
49 return nil, fmt.Errorf("parse AccountContext: %w", err)
50 }
51 switch usr.AccountType {
52 case "celebrity":
53 catalog.Celebs = append(catalog.Celebs, usr)
54 case "regular":
55 catalog.Regulars = append(catalog.Regulars, usr)
56 default:
57 return nil, fmt.Errorf("unhandled account type: %v", usr.AccountType)
58 }
59 }
60 // validate index numbers
61 for i, u := range catalog.Celebs {
62 if i != u.Index {
63 return nil, fmt.Errorf("account index didn't match: %d != %d (%s)", i, u.Index, u.AccountType)
64 }
65 }
66 for i, u := range catalog.Regulars {
67 if i != u.Index {
68 return nil, fmt.Errorf("account index didn't match: %d != %d (%s)", i, u.Index, u.AccountType)
69 }
70 }
71 log.Info("loaded account catalog", "regular", len(catalog.Regulars), "celebrity", len(catalog.Celebs))
72 return catalog, nil
73}
74
75func AccountXrpcClient(pdsHost string, ac *AccountContext) (*xrpc.Client, error) {
76 httpClient := util.RobustHTTPClient()
77 ua := "IndigoFakerMaker/" + versioninfo.Short()
78 xrpcc := &xrpc.Client{
79 Client: httpClient,
80 Host: pdsHost,
81 Auth: &ac.Auth,
82 UserAgent: &ua,
83 }
84 // use XRPC client to re-auth using user/pass
85 auth, err := comatproto.ServerCreateSession(context.TODO(), xrpcc, &comatproto.ServerCreateSession_Input{
86 Identifier: ac.Auth.Handle,
87 Password: ac.Password,
88 })
89 if err != nil {
90 return nil, err
91 }
92 xrpcc.Auth.AccessJwt = auth.AccessJwt
93 xrpcc.Auth.RefreshJwt = auth.RefreshJwt
94 return xrpcc, nil
95}