aboutsummaryrefslogtreecommitdiff
path: root/backend/fetchers/app.bsky.go
blob: 069a183904c999dce2a34b596e19ee81c682465f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fetchers

import (
	"encoding/json"
	"fmt"
	"io"
	"llostre/backend/auth"
	"llostre/backend/general"
	"llostre/backend/notes"
	"log/slog"
	"net/http"
)

type ListRecordsResponse struct {
	Records []notes.AppBskyFeedPostRecord `json:"records"`
}

func FetchAppBskyFeedPosts(logger *slog.Logger, did, pdsURL, accessToken string, keypair *auth.DPoPKeyPair, notesCache *Cache) ([]notes.Note, error) {
	url := fmt.Sprintf("%s/xrpc/com.atproto.repo.listRecords?repo=%s&collection=app.bsky.feed.post", pdsURL, did)

	logger.Debug("generating DPoP proof", "url", url)
	dpopProof, err := keypair.GenerateDPoPProof("GET", url, "")
	if err != nil {
		logger.Error("DPoP generation failed", "error", err)
		return nil, err
	}

	logger.Debug("making request to PDS")
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", "Bearer "+accessToken)
	req.Header.Set("DPoP", dpopProof)

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var response ListRecordsResponse
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
		return nil, err
	}

	profile, err := FetchProfile(did, pdsURL, notesCache)
	if err != nil {
		logger.Error("profile fetch failed", "did", did, "error", err)
	} else {
		logger.Debug("fetched profile")
	}

	posts := make([]notes.Note, 0, len(response.Records))
	for _, record := range response.Records {
		bp, err := notes.AppBskyFeedPostFromRecord(record, profile)
		if err != nil {
			logger.Error("failed to convert record", "error", err, "uri", record.URI)
			continue
		}
		notesCache.Set(bp.URI, bp)
		posts = append(posts, *bp)
	}

	return posts, nil
}

func FetchPostByURI(uri, accessToken string, keypair *auth.DPoPKeyPair, cache *Cache) (*notes.Note, error) {
	// check cache
	if cached, found := cache.Get(uri); found {
		return cached, nil
	}

	repo, collection, rkey, err := general.ParseURI(uri)

	// need to resolve DID to PDS
	pds, err := FetchPDS(repo)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("%s/xrpc/com.atproto.repo.getRecord?repo=%s&collection=%s&rkey=%s",
		pds, repo, collection, rkey)

	// fetch with DPoP
	dpopProof, err := keypair.GenerateDPoPProof("GET", url, "")
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", "Bearer "+accessToken)
	req.Header.Set("DPoP", dpopProof)

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		body, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("PDS returned status %d: %s", resp.StatusCode, string(body))
	}

	var result struct {
		URI   string          `json:"uri"`
		Value json.RawMessage `json:"value"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}

	// convert to AppBskyFeedPostRecord
	record := notes.AppBskyFeedPostRecord{
		URI: result.URI,
	}
	if err := json.Unmarshal(result.Value, &record.Value); err != nil {
		return nil, err
	}

	// get author of the note
	profile, found := cache.GetProfile(repo)
	if !found {
		profile, err = FetchProfile(repo, pds, cache)
	}
	if err != nil {
		return nil, err
	}

	note, err := notes.AppBskyFeedPostFromRecord(record, profile)
	if err != nil {
		return nil, err
	}

	// cache it
	cache.Set(note.URI, note)

	return note, nil
}