MCPcopy Index your code
hub / github.com/Preloading/TwitterAPIBridge / FollowUser

Function FollowUser

bluesky/blueskyapi.go:1360–1410  ·  view source on GitHub ↗
(pds string, token string, targetActor string, my_did string)

Source from the content-addressed store, hash-verified

1358}
1359
1360func FollowUser(pds string, token string, targetActor string, my_did string) (*User, error) {
1361 url := pds + "/xrpc/com.atproto.repo.createRecord"
1362
1363 targetUser, err := GetUserInfoRaw(pds, token, targetActor)
1364 if err != nil {
1365 return nil, errors.New("failed to fetch post")
1366 }
1367
1368 if targetUser.Viewer.Following != nil {
1369 return nil, errors.New("already following user")
1370 }
1371
1372 payload := CreateRecordPayload{
1373 Collection: "app.bsky.graph.follow",
1374 Repo: my_did,
1375 Record: PostInteractionRecord{
1376 Type: "app.bsky.graph.follow",
1377 CreatedAt: time.Now().UTC().Format(time.RFC3339),
1378 Subject: targetUser.DID,
1379 },
1380 }
1381
1382 reqBody, err := json.Marshal(payload)
1383 if err != nil {
1384 return nil, errors.New("failed to marshal payload")
1385 }
1386
1387 resp, err := SendRequest(&token, http.MethodPost, url, bytes.NewReader(reqBody))
1388 if err != nil {
1389 return nil, err
1390 }
1391 defer resp.Body.Close()
1392
1393 if resp.StatusCode != http.StatusOK {
1394 bodyBytes, _ := io.ReadAll(resp.Body)
1395 bodyString := string(bodyBytes)
1396 fmt.Println("Response Status:", resp.StatusCode)
1397 fmt.Println("Response Body:", bodyString)
1398 return nil, errors.New(bodyString) // return response
1399 }
1400
1401 followRes := CreateRecordResult{}
1402 if err := json.NewDecoder(resp.Body).Decode(&followRes); err != nil {
1403 return nil, err
1404 }
1405
1406 targetUser.Viewer.Following = &strings.Split(followRes.URI, "/app.bsky.graph.follow/")[1]
1407 targetUser.FollowersCount++
1408
1409 return targetUser, nil
1410}
1411
1412func UnfollowUser(pds string, token string, targetActor string, my_did string) (*User, error) {
1413 url := pds + "/xrpc/com.atproto.repo.deleteRecord"

Callers

nothing calls this directly

Calls 2

GetUserInfoRawFunction · 0.85
SendRequestFunction · 0.85

Tested by

no test coverage detected