add import preferences (-preferences)

This commit is contained in:
ari melody 2025-06-22 19:16:17 +01:00
parent 24db172462
commit 6bde84b7e3
Signed by: ari
GPG key ID: CF99829C92678188
3 changed files with 107 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
@ -15,8 +16,9 @@ func ImportProfile(session *AtprotoSession, pdsUrl string, fromAccount string, d
fmt.Printf("fetching profile record from @%s...\n", fromAccount)
var err error
fromDid := fromAccount
if !strings.HasPrefix(fromAccount, "did:") {
fromAccount, err = ResolveAtprotoHandle(session, pdsUrl, fromAccount)
fromDid, err = ResolveAtprotoHandle(session, pdsUrl, fromAccount)
if err != nil {
return errors.New(fmt.Sprintf("failed to resolve identity: %v\n", err))
}
@ -62,7 +64,7 @@ func ImportProfile(session *AtprotoSession, pdsUrl string, fromAccount string, d
if !dryrun {
// import avatar
avatarBytes, err := GetAtprotoBlob(pdsUrl, fromAccount, profile.Avatar.Ref.Link)
avatarBytes, err := GetAtprotoBlob(pdsUrl, fromDid, profile.Avatar.Ref.Link)
if err != nil { return errors.New(fmt.Sprintf("failed to download avatar: %v\n", err)) }
avatarBlob, err := UploadAtprotoBlob(session, pdsUrl, avatarBytes, profile.Avatar.MimeType)
@ -70,7 +72,7 @@ func ImportProfile(session *AtprotoSession, pdsUrl string, fromAccount string, d
newProfile.Avatar = avatarBlob
// import banner
bannerBytes, err := GetAtprotoBlob(pdsUrl, fromAccount, profile.Banner.Ref.Link)
bannerBytes, err := GetAtprotoBlob(pdsUrl, fromDid, profile.Banner.Ref.Link)
if err != nil { return errors.New(fmt.Sprintf("failed to download banner: %v\n", err)) }
bannerBlob, err := UploadAtprotoBlob(session, pdsUrl, bannerBytes, profile.Banner.MimeType)
@ -159,3 +161,44 @@ func ImportFollows(session *AtprotoSession, pdsUrl string, fromAccount string, d
return nil
}
func ImportPreferences(fromSession *AtprotoSession, toSession *AtprotoSession, pdsUrl string, dryrun bool, verbose bool) error {
fmt.Printf("fetching preferences from @%s...\n", fromSession.Handle)
req, _ := http.NewRequest(http.MethodGet, pdsUrl + "/xrpc/app.bsky.actor.getPreferences", nil)
req.Header.Set("Authorization", "Bearer " + fromSession.AccessJwt)
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("failed to fetch preferences: %v\n", err))
}
// // TODO: BskyGetPreferencesResponse
data := BskyActorPreferences{}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return errors.New(fmt.Sprintf("failed to parse PDS response: %v\n", err))
}
fmt.Printf("fetched %d preferences.\n", len(data.Preferences))
fmt.Printf("importing preferences to @%s...\n", toSession.Handle)
if !dryrun {
reqBodyBytes, _ := json.Marshal(data)
req, _ := http.NewRequest(http.MethodPost, pdsUrl + "/xrpc/app.bsky.actor.putPreferences", bytes.NewReader(reqBodyBytes))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer " + toSession.AccessJwt)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
errBody := ErrorResponse{}
json.NewDecoder(res.Body).Decode(&errBody)
return errors.New(fmt.Sprintf("%s: %s", errBody.Error, errBody.Message))
}
}
fmt.Printf("preferences imported successfully!\n")
return nil
}