This commit is contained in:
parent
85fc508aa3
commit
c9cc325ef7
3 changed files with 77 additions and 51 deletions
|
|
@ -10,6 +10,17 @@ import (
|
|||
"github.com/dghubble/sling"
|
||||
)
|
||||
|
||||
const (
|
||||
// URI parsing indices for at:// URIs split by "/"
|
||||
uriRepoIndex = 2
|
||||
uriCollectionIndex = 3
|
||||
uriRkeyIndex = 4
|
||||
|
||||
// Custom collections
|
||||
PostCollection = "blue.zio.bsky2tg.post"
|
||||
AliasCollection = "blue.zio.bsky2tg.alias"
|
||||
)
|
||||
|
||||
type BlueskyConfig struct {
|
||||
PDSURL string `json:"pds-url"`
|
||||
Repo string `json:"repo"`
|
||||
|
|
@ -72,10 +83,11 @@ type Link struct {
|
|||
}
|
||||
|
||||
type Bluesky struct {
|
||||
Cfg *BlueskyConfig
|
||||
HttpClient *http.Client
|
||||
Logger *log.Logger
|
||||
sling *sling.Sling
|
||||
Cfg *BlueskyConfig
|
||||
HttpClient *http.Client
|
||||
Logger *log.Logger
|
||||
sling *sling.Sling
|
||||
publicSling *sling.Sling
|
||||
}
|
||||
|
||||
func (bluesky *Bluesky) CreateSession(cfg *BlueskyConfig) error {
|
||||
|
|
@ -88,14 +100,14 @@ func (bluesky *Bluesky) CreateSession(cfg *BlueskyConfig) error {
|
|||
}
|
||||
resp := new(BSkySessionResponse)
|
||||
|
||||
bluesky.sling.New().Post("/xrpc/com.atproto.server.createSession").BodyJSON(body).ReceiveSuccess(resp)
|
||||
bluesky.sling.New().Client(bluesky.HttpClient).
|
||||
Post("/xrpc/com.atproto.server.createSession").BodyJSON(body).ReceiveSuccess(resp)
|
||||
if resp.AccessJWT != "" {
|
||||
cfg.AccessJWT = resp.AccessJWT
|
||||
cfg.RefreshJWT = resp.RefreshJWT
|
||||
return nil
|
||||
}
|
||||
|
||||
bluesky.sling.New().Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.AccessJWT))
|
||||
return errors.New("unable to authenticate, check handle/password")
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +173,7 @@ func (bluesky *Bluesky) CommitTelegramResponse(data *TelegramRecord, rkey string
|
|||
Record TelegramRecord `json:"record"`
|
||||
}{
|
||||
Repo: bluesky.Cfg.DID,
|
||||
Collection: "blue.zio.bsky2tg.post",
|
||||
Collection: PostCollection,
|
||||
RKey: rkey,
|
||||
Record: TelegramRecord{
|
||||
ChannelID: data.ChannelID,
|
||||
|
|
@ -193,7 +205,7 @@ func (bluesky *Bluesky) GetTelegramData(rkey string) (*TelegramRecord, string) {
|
|||
RKey string `url:"rkey"`
|
||||
}{
|
||||
Repo: bluesky.Cfg.DID,
|
||||
Collection: "blue.zio.bsky2tg.post",
|
||||
Collection: PostCollection,
|
||||
RKey: rkey,
|
||||
}
|
||||
|
||||
|
|
@ -215,9 +227,9 @@ func (bluesky *Bluesky) GetPost(uri string) *Post {
|
|||
Repo string `url:"repo"`
|
||||
Collection string `url:"collection"`
|
||||
}{
|
||||
RKey: args[4],
|
||||
Repo: args[2],
|
||||
Collection: args[3],
|
||||
RKey: args[uriRkeyIndex],
|
||||
Repo: args[uriRepoIndex],
|
||||
Collection: args[uriCollectionIndex],
|
||||
}
|
||||
bluesky.sling.New().Get("/xrpc/com.atproto.repo.getRecord").QueryStruct(params).ReceiveSuccess(&post)
|
||||
|
||||
|
|
@ -249,7 +261,7 @@ func (bluesky *Bluesky) FetchAliases() []Records {
|
|||
Collection string `url:"collection"`
|
||||
}{
|
||||
Repo: bluesky.Cfg.DID,
|
||||
Collection: "blue.zio.bsky2tg.alias",
|
||||
Collection: AliasCollection,
|
||||
}
|
||||
|
||||
bluesky.sling.New().Get("/xrpc/com.atproto.repo.listRecords").QueryStruct(¶ms).Receive(resp, resp)
|
||||
|
|
|
|||
|
|
@ -13,23 +13,28 @@ import (
|
|||
"github.com/dghubble/sling"
|
||||
)
|
||||
|
||||
const (
|
||||
didWebPrefixLen = len("did:web:")
|
||||
atPrefixLen = len("at://")
|
||||
httpClientTimeout = 3 * time.Second
|
||||
)
|
||||
|
||||
type BSky struct {
|
||||
Bluesky *Bluesky
|
||||
DID string
|
||||
}
|
||||
|
||||
func NewBSky() *BSky {
|
||||
return &BSky{
|
||||
Bluesky: &Bluesky{
|
||||
Cfg: &BlueskyConfig{},
|
||||
HttpClient: &http.Client{},
|
||||
sling: sling.New().Client(&http.Client{Timeout: time.Second * 3}),
|
||||
Cfg: &BlueskyConfig{},
|
||||
HttpClient: &http.Client{},
|
||||
sling: sling.New().Client(&http.Client{Timeout: httpClientTimeout}),
|
||||
publicSling: sling.New().Base("https://public.api.bsky.app/").Client(&http.Client{Timeout: httpClientTimeout}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BSky) ResolveHandle(handle string) (string, error) {
|
||||
httpClient := &http.Client{Timeout: 3 * time.Second}
|
||||
resp := new(BSkySessionResponse)
|
||||
errResp := &struct {
|
||||
Message string `json:"message"`
|
||||
|
|
@ -40,8 +45,7 @@ func (b *BSky) ResolveHandle(handle string) (string, error) {
|
|||
}{
|
||||
Handle: handle,
|
||||
}
|
||||
sling.New().Base("https://public.api.bsky.app/").Client(httpClient).
|
||||
Get("/xrpc/com.atproto.identity.resolveHandle").QueryStruct(params).
|
||||
b.Bluesky.publicSling.New().Get("/xrpc/com.atproto.identity.resolveHandle").QueryStruct(params).
|
||||
Receive(resp, errResp)
|
||||
|
||||
if errResp.Error != "" {
|
||||
|
|
@ -51,54 +55,62 @@ func (b *BSky) ResolveHandle(handle string) (string, error) {
|
|||
return resp.DID, nil
|
||||
}
|
||||
|
||||
func parseDIDURL(did string) (*url.URL, error) {
|
||||
if strings.HasPrefix(did, "did:web:") {
|
||||
return url.Parse("https://" + did[didWebPrefixLen:] + "/.well-known/did.json")
|
||||
} else if strings.HasPrefix(did, "did:plc:") {
|
||||
return url.Parse("https://plc.directory/" + did)
|
||||
}
|
||||
return nil, errors.New("DID is not supported")
|
||||
}
|
||||
|
||||
func (b *BSky) getPDS() error {
|
||||
did, _ := b.ResolveHandle(b.Bluesky.Cfg.Handle)
|
||||
|
||||
var didURL url.URL
|
||||
if strings.HasPrefix(did, "did:web:") {
|
||||
didURL.Host = "https://" + did[8:]
|
||||
didURL.Path = "/.well-known/did.json"
|
||||
} else if strings.HasPrefix(did, "did:plc:") {
|
||||
didURL.Host = "https://plc.directory"
|
||||
didURL.Path = "/" + did
|
||||
} else {
|
||||
return errors.New("DID is not supported")
|
||||
didURL, err := parseDIDURL(did)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
didResp := new(DIDResponse)
|
||||
sling.New().Base(didURL.Host).Get(didURL.Path).ReceiveSuccess(didResp)
|
||||
baseURL := fmt.Sprintf("%s://%s", didURL.Scheme, didURL.Host)
|
||||
sling.New().Base(baseURL).Get(didURL.Path).ReceiveSuccess(didResp)
|
||||
if didResp.ID == "" {
|
||||
return errors.New("unable to resolve DID")
|
||||
}
|
||||
|
||||
b.Bluesky.Cfg.DID = didResp.ID
|
||||
b.Bluesky.Cfg.PDSURL = didResp.Service[0].ServiceEndpoint
|
||||
b.Bluesky.sling.Base(didResp.Service[0].ServiceEndpoint)
|
||||
if len(didResp.Service) == 0 {
|
||||
return errors.New("DID response has no services")
|
||||
}
|
||||
|
||||
pdsURL := didResp.Service[0].ServiceEndpoint
|
||||
if pdsURL == "" {
|
||||
return errors.New("service endpoint is empty")
|
||||
}
|
||||
|
||||
b.Bluesky.Cfg.PDSURL = pdsURL
|
||||
b.Bluesky.sling.Base(pdsURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BSky) GetHandleFromDID(did string) (handle string, err error) {
|
||||
var didURL url.URL
|
||||
if strings.HasPrefix(did, "did:web:") {
|
||||
didURL.Host = "https://" + did[8:]
|
||||
didURL.Path = "/.well-known/did.json"
|
||||
} else if strings.HasPrefix(did, "did:plc:") {
|
||||
didURL.Host = "https://plc.directory"
|
||||
didURL.Path = "/" + did
|
||||
} else {
|
||||
return "", errors.New("DID is not supported")
|
||||
didURL, err := parseDIDURL(did)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
didResp := new(DIDResponse)
|
||||
sling.New().Base(didURL.Host).Get(didURL.Path).ReceiveSuccess(didResp)
|
||||
baseURL := fmt.Sprintf("%s://%s", didURL.Scheme, didURL.Host)
|
||||
sling.New().Base(baseURL).Get(didURL.Path).ReceiveSuccess(didResp)
|
||||
if didResp.ID == "" {
|
||||
return "", errors.New("unable to resolve DID")
|
||||
}
|
||||
|
||||
return didResp.AlsoKnownAs[0][5:], nil
|
||||
return didResp.AlsoKnownAs[0][atPrefixLen:], nil
|
||||
}
|
||||
|
||||
func (b *BSky) GetPDS(handle string) string {
|
||||
func (b *BSky) GetPDS() string {
|
||||
return b.Bluesky.Cfg.PDSURL
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +130,6 @@ func (b *BSky) Auth(authData []string) error {
|
|||
b.Bluesky.Cfg.Cursor = auth.Cursor
|
||||
b.Bluesky.Cfg.AccessJWT = auth.AccessJWT
|
||||
b.Bluesky.Cfg.RefreshJWT = auth.RefreshJWT
|
||||
// b.RefreshSession()
|
||||
b.Bluesky.CheckSessionValid()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue