Didn't work

This commit is contained in:
2024-11-17 18:46:01 -05:00
parent 9781690db7
commit aa421dab8c

40
main.go
View File

@@ -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)
}
}
}()