-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xe Iaso <[email protected]>
- Loading branch information
Showing
34 changed files
with
726 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
jetstreamClient "github.com/bluesky-social/jetstream/pkg/client" | ||
"github.com/bluesky-social/jetstream/pkg/client/schedulers/parallel" | ||
jsModels "github.com/bluesky-social/jetstream/pkg/models" | ||
"github.com/goccy/go-json" | ||
"github.com/nats-io/nats.go" | ||
"within.website/x/internal" | ||
) | ||
|
||
var ( | ||
jetStreamURL = flag.String("jetstream-url", "wss://jetstream2.us-east.bsky.network/subscribe", "Jetstream server to subscribe to") | ||
natsURL = flag.String("nats-url", "nats://localhost:4222", "nats url") | ||
) | ||
|
||
func main() { | ||
internal.HandleStartup() | ||
|
||
slog.Info("starting up", | ||
"jetstream-url", jetStreamURL, | ||
"nats-url", natsURL, | ||
) | ||
|
||
nc, err := nats.Connect(*natsURL) | ||
if err != nil { | ||
slog.Error("can't connect to NATS", "err", err) | ||
os.Exit(1) | ||
} | ||
defer nc.Close() | ||
slog.Info("connected to NATS") | ||
|
||
jsCfg := jetstreamClient.DefaultClientConfig() | ||
jsCfg.WebsocketURL = *jetStreamURL | ||
|
||
jscli, err := jetstreamClient.NewClient( | ||
jsCfg, | ||
slog.With("aspect", "jetstream"), | ||
parallel.NewScheduler( | ||
1, | ||
"amano", | ||
slog.With("aspect", "fan-out"), | ||
handleEvent(nc), | ||
), | ||
) | ||
if err != nil { | ||
slog.Error("can't set up jetstream client", "err", err) | ||
os.Exit(1) | ||
} | ||
defer jscli.Scheduler.Shutdown() | ||
|
||
now := time.Now().UnixNano() | ||
if err := jscli.ConnectAndRead(context.Background(), &now); err != nil { | ||
slog.Error("can't connect to jetstream", "err", err) | ||
os.Exit(1) | ||
} | ||
|
||
slog.Info("connected to jetstream") | ||
|
||
mu := sync.Mutex{} | ||
mu.Lock() | ||
mu.Lock() | ||
} | ||
|
||
func handleEvent(nc *nats.Conn) func(ctx context.Context, ev *jsModels.Event) error { | ||
return func(ctx context.Context, ev *jsModels.Event) error { | ||
switch ev.Kind { | ||
case "account": | ||
data, err := json.Marshal(ev.Account) | ||
if err != nil { | ||
return fmt.Errorf("can't marshal account event: %w", err) | ||
} | ||
|
||
if err := nc.Publish("amano:account", data); err != nil { | ||
return fmt.Errorf("can't publish account event: %w", err) | ||
} | ||
case "identity": | ||
data, err := json.Marshal(ev.Identity) | ||
if err != nil { | ||
return fmt.Errorf("can't marshal identity event: %w", err) | ||
} | ||
|
||
if err := nc.Publish("amano:identity", data); err != nil { | ||
return fmt.Errorf("can't publish identity event: %w", err) | ||
} | ||
case "commit": | ||
subject := fmt.Sprintf("amano:commit:%s", ev.Commit.Collection) | ||
data, err := json.Marshal(ev.Commit) | ||
if err != nil { | ||
return fmt.Errorf("can't marshal commit event: %w", err) | ||
} | ||
|
||
m := nats.NewMsg(subject) | ||
m.Data = data | ||
m.Header.Set("bsky-actor-did", ev.Did) | ||
|
||
if err := nc.PublishMsg(m); err != nil { | ||
return fmt.Errorf("can't publish %q event: %w", subject, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"regexp" | ||
"time" | ||
|
||
comatproto "github.com/bluesky-social/indigo/api/atproto" | ||
bskyData "github.com/bluesky-social/indigo/api/bsky" | ||
jsModels "github.com/bluesky-social/jetstream/pkg/models" | ||
bsky "github.com/danrusei/gobot-bsky" | ||
"github.com/goccy/go-json" | ||
"github.com/nats-io/nats.go" | ||
"within.website/x/internal" | ||
) | ||
|
||
const ( | ||
PostTopic = "amano:commit:app.bsky.feed.post" | ||
) | ||
|
||
var ( | ||
blueskyAuthkey = flag.String("bsky-authkey", "", "Bluesky authkey") | ||
blueskyHandle = flag.String("bsky-handle", "", "Bluesky handle") | ||
blueskyPDS = flag.String("bsky-pds", "https://bsky.social", "Bluesky PDS") | ||
natsURL = flag.String("nats-url", "nats://localhost:4222", "nats url") | ||
|
||
sneakPeakRegex = regexp.MustCompile("(?i)sneak peak") | ||
) | ||
|
||
func main() { | ||
internal.HandleStartup() | ||
|
||
slog.Info("starting up", | ||
"have-bsky-authkey", *blueskyAuthkey != "", | ||
"bsky-handle", *blueskyHandle, | ||
"bsky-pds", *blueskyPDS, | ||
"nats-url", *natsURL, | ||
) | ||
|
||
nc, err := nats.Connect(*natsURL) | ||
if err != nil { | ||
slog.Error("can't connect to NATS", "err", err) | ||
os.Exit(1) | ||
} | ||
defer nc.Close() | ||
slog.Info("connected to NATS") | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
bsAgent, err := bskyAuth(ctx, *blueskyPDS, *blueskyHandle, *blueskyAuthkey) | ||
if err != nil { | ||
slog.Error("can't auth to bluesky", "err", err) | ||
os.Exit(1) | ||
} | ||
|
||
sub, err := nc.SubscribeSync(PostTopic) | ||
if err != nil { | ||
slog.Error("can't subscribe to post feed", "err", err) | ||
os.Exit(1) | ||
} | ||
defer sub.Drain() | ||
|
||
for { | ||
m, err := sub.NextMsg(time.Second) | ||
if err != nil { | ||
slog.Error("can't read message", "err", err) | ||
continue | ||
} | ||
|
||
var commit jsModels.Commit | ||
if err := json.Unmarshal(m.Data, &commit); err != nil { | ||
slog.Error("can't unmarshal commit", "err", err) | ||
continue | ||
} | ||
|
||
if commit.Operation == "delete" { | ||
continue | ||
} | ||
|
||
var post bskyData.FeedPost | ||
if err := json.Unmarshal(commit.Record, &post); err != nil { | ||
slog.Error("can't unmarshal post", "err", err) | ||
continue | ||
} | ||
|
||
if sneakPeakRegex.MatchString(post.Text) { | ||
actorID := m.Header.Get("bsky-actor-did") | ||
slog.Info("found a stealth mountain!", "id", commit.Rev, "actor", actorID) | ||
reply, err := bsky.NewPostBuilder(`I think you mean "sneak peek"`).Build() | ||
if err != nil { | ||
slog.Error("can't build reply post", "err", err) | ||
} | ||
parent := comatproto.RepoStrongRef{ | ||
LexiconTypeID: "app.bsky.feed.post", | ||
Uri: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", actorID, commit.RKey), | ||
Cid: commit.CID, | ||
} | ||
root := parent | ||
|
||
if post.Reply != nil { | ||
root = *post.Reply.Root | ||
} | ||
|
||
reply.Reply = &bskyData.FeedPost_ReplyRef{ | ||
Parent: &parent, | ||
Root: &root, | ||
} | ||
|
||
reply.CreatedAt = time.Now().UTC().Format(time.RFC3339) | ||
|
||
cid, uri, err := bsAgent.PostToFeed(ctx, reply) | ||
if err != nil { | ||
slog.Error("cannot post to feed", "err", err) | ||
continue | ||
} | ||
|
||
slog.Info("posted to bluesky", "bluesky_cid", cid, "bluesky_uri", uri) | ||
} | ||
} | ||
} | ||
|
||
func bskyAuth(ctx context.Context, pds, handle, authkey string) (*bsky.BskyAgent, error) { | ||
bluesky := bsky.NewAgent(ctx, pds, handle, authkey) | ||
if err := bluesky.Connect(ctx); err != nil { | ||
slog.Error("failed to connect to bluesky", "err", err) | ||
return nil, err | ||
} | ||
|
||
go func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
t := time.NewTicker(5 * time.Minute) | ||
defer t.Stop() | ||
for range t.C { | ||
if err := bluesky.Connect(ctx); err != nil { | ||
slog.Error("can't reauth to bluesky", "err", err) | ||
} | ||
} | ||
}() | ||
|
||
if err := bluesky.Connect(ctx); err != nil { | ||
slog.Error("failed to connect to bluesky", "err", err) | ||
return nil, err | ||
} | ||
|
||
return &bluesky, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.