forked from jazware/go-bsky-feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeedrouter.go
More file actions
101 lines (86 loc) · 3 KB
/
feedrouter.go
File metadata and controls
101 lines (86 loc) · 3 KB
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
// Package feedrouter describes the FeedRouter type, which is responsible for generating feeds for a given DID.
// It also describes the Feed interface, which is implemented by the various feed types.
package feedrouter
import (
"context"
"fmt"
appbsky "github.com/bluesky-social/indigo/api/bsky"
did "github.com/whyrusleeping/go-did"
)
type Feed interface {
GetPage(ctx context.Context, feed string, userDID string, limit int64, cursor string) (feedPosts []*appbsky.FeedDefs_SkeletonFeedPost, newCursor *string, err error)
Describe(ctx context.Context) ([]appbsky.FeedDescribeFeedGenerator_Feed, error)
}
type FeedRouter struct {
FeedActorDID did.DID // DID of the Repo the Feed is published under
ServiceEndpoint string // URL of the FeedRouter feedgen
ServiceDID did.DID // DID of the FeedRouter feedgen
DIDDocument did.Document // DID Document of the FeedRouter feedgen
AcceptableURIPrefixes []string // URIs that the FeedRouter is allowed to generate feeds for
FeedMap map[string]Feed // map of FeedName to Feed
Feeds []Feed
}
type NotFoundError struct {
error
}
// NewFeedRouter returns a new FeedRouter
func NewFeedRouter(
ctx context.Context,
feedActorDIDString string,
serviceDIDString string,
acceptableDIDs []string,
serviceEndpoint string,
) (*FeedRouter, error) {
acceptableURIPrefixes := []string{}
for _, did := range acceptableDIDs {
acceptableURIPrefixes = append(acceptableURIPrefixes, "at://"+did+"/app.bsky.feed.generator/")
}
serviceDID, err := did.ParseDID(serviceDIDString)
if err != nil {
return nil, fmt.Errorf("error parsing serviceDID: %w", err)
}
feedActorDID, err := did.ParseDID(feedActorDIDString)
if err != nil {
return nil, fmt.Errorf("error parsing feedActorDID: %w", err)
}
serviceID, err := did.ParseDID("#bsky_fg")
if err != nil {
panic(err)
}
doc := did.Document{
Context: []string{did.CtxDIDv1},
ID: serviceDID,
Service: []did.Service{
{
ID: serviceID,
Type: "BskyFeedGenerator",
ServiceEndpoint: serviceEndpoint,
},
},
}
return &FeedRouter{
FeedMap: map[string]Feed{},
FeedActorDID: feedActorDID,
ServiceDID: serviceDID,
DIDDocument: doc,
AcceptableURIPrefixes: acceptableURIPrefixes,
ServiceEndpoint: serviceEndpoint,
}, nil
}
// AddFeed adds a feed to the FeedRouter
// Feed precedence for overlapping aliases is determined by the order in which
// they are added (first added is highest precedence)
func (fg *FeedRouter) AddFeed(feedAliases []string, feed Feed) {
if fg.FeedMap == nil {
fg.FeedMap = map[string]Feed{}
}
for _, feedAlias := range feedAliases {
// Skip the feed if we already have the alias registered so we don't add it twice
// Feed precedence is determined by the order in which they are added
if _, ok := fg.FeedMap[feedAlias]; ok {
continue
}
fg.FeedMap[feedAlias] = feed
}
fg.Feeds = append(fg.Feeds, feed)
}