everything you need to create an atproto appview
1package types
2
3import (
4 "errors"
5 "time"
6)
7
8const MaxAccounts = 20
9
10var ErrMaxAccountsReached = errors.New("maximum number of linked accounts reached")
11
12type AccountInfo struct {
13 Did string `json:"did"`
14 SessionId string `json:"session_id"`
15 AddedAt int64 `json:"added_at"`
16}
17
18type AccountRegistry struct {
19 Accounts []AccountInfo `json:"accounts"`
20}
21
22func (r *AccountRegistry) AddAccount(did, sessionId string) error {
23 for i, acc := range r.Accounts {
24 if acc.Did == did {
25 r.Accounts[i].SessionId = sessionId
26 return nil
27 }
28 }
29
30 if len(r.Accounts) >= MaxAccounts {
31 return ErrMaxAccountsReached
32 }
33
34 r.Accounts = append(r.Accounts, AccountInfo{
35 Did: did,
36 SessionId: sessionId,
37 AddedAt: time.Now().Unix(),
38 })
39 return nil
40}
41
42func (r *AccountRegistry) RemoveAccount(did string) {
43 filtered := make([]AccountInfo, 0, len(r.Accounts))
44 for _, acc := range r.Accounts {
45 if acc.Did != did {
46 filtered = append(filtered, acc)
47 }
48 }
49 r.Accounts = filtered
50}
51
52func (r *AccountRegistry) FindAccount(did string) *AccountInfo {
53 for i := range r.Accounts {
54 if r.Accounts[i].Did == did {
55 return &r.Accounts[i]
56 }
57 }
58 return nil
59}
60
61func (r *AccountRegistry) OtherAccounts(activeDid string) []AccountInfo {
62 result := make([]AccountInfo, 0, len(r.Accounts))
63 for _, acc := range r.Accounts {
64 if acc.Did != activeDid {
65 result = append(result, acc)
66 }
67 }
68 return result
69}
70
71type MultiAccountUser struct {
72 Active *User
73 Accounts []AccountInfo
74}
75
76func (m *MultiAccountUser) Did() string {
77 if m.Active == nil {
78 return ""
79 }
80 return m.Active.Did
81}
82
83func (m *MultiAccountUser) Pds() string {
84 if m.Active == nil {
85 return ""
86 }
87 return m.Active.Pds
88}
89
90type User struct {
91 Did string
92 Pds string
93}
94
95type AuthReturnInfo struct {
96 ReturnURL string
97 AddAccount bool
98}