mirror of
https://git.robbyzambito.me/bluesky-nats-proxy/
synced 2025-12-20 08:14:50 +00:00
Didn't work
This commit is contained in:
40
main.go
40
main.go
@@ -2,14 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type BlueskyPost struct {
|
||||
Op string `json:"op"`
|
||||
Repo string `json:"repo"`
|
||||
Record struct {
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
} `json:"record"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Connect to NATS
|
||||
nc, err := nats.Connect(nats.DefaultURL)
|
||||
@@ -34,16 +46,32 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify that the message is valid JSON
|
||||
var jsonMsg json.RawMessage
|
||||
if err := json.Unmarshal(message, &jsonMsg); err != nil {
|
||||
log.Println("Received non-JSON message, skipping")
|
||||
var post BlueskyPost
|
||||
if err := json.Unmarshal(message, &post); err != nil {
|
||||
log.Println("Error unmarshaling JSON:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Publish the JSON message to NATS
|
||||
if err := nc.Publish("bsky.feed.post", message); err != nil {
|
||||
// Extract user ID from repo field
|
||||
userID := strings.Split(post.Repo, ".")[0]
|
||||
|
||||
// Determine post type (simplified for this example)
|
||||
postType := "text"
|
||||
if strings.Contains(post.Record.Text, "http") {
|
||||
postType = "link"
|
||||
}
|
||||
|
||||
// Create a more detailed NATS subject
|
||||
subject := fmt.Sprintf("bsky.feed.post.user.%s.type.%s.time.%s",
|
||||
userID,
|
||||
postType,
|
||||
post.Record.CreatedAt.Format("20060102"))
|
||||
|
||||
// Publish the JSON message to NATS with the detailed subject
|
||||
if err := nc.Publish(subject, message); err != nil {
|
||||
log.Println("Error publishing to NATS:", err)
|
||||
} else {
|
||||
log.Printf("Published message to subject: %s", subject)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user