mirror of
https://git.robbyzambito.me/bluesky-nats-proxy/
synced 2025-12-21 08:44:49 +00:00
Didn't work
This commit is contained in:
40
main.go
40
main.go
@@ -2,14 +2,26 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/nats-io/nats.go"
|
"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() {
|
func main() {
|
||||||
// Connect to NATS
|
// Connect to NATS
|
||||||
nc, err := nats.Connect(nats.DefaultURL)
|
nc, err := nats.Connect(nats.DefaultURL)
|
||||||
@@ -34,16 +46,32 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the message is valid JSON
|
var post BlueskyPost
|
||||||
var jsonMsg json.RawMessage
|
if err := json.Unmarshal(message, &post); err != nil {
|
||||||
if err := json.Unmarshal(message, &jsonMsg); err != nil {
|
log.Println("Error unmarshaling JSON:", err)
|
||||||
log.Println("Received non-JSON message, skipping")
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish the JSON message to NATS
|
// Extract user ID from repo field
|
||||||
if err := nc.Publish("bsky.feed.post", message); err != nil {
|
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)
|
log.Println("Error publishing to NATS:", err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Published message to subject: %s", subject)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user