Files

62 lines
1.6 KiB
Go

package pds
import (
"context"
"fmt"
"log/slog"
"time"
bsky "github.com/bluesky-social/indigo/api/bsky"
)
const (
// StatusPostCollection is the collection name for Bluesky posts
StatusPostCollection = "app.bsky.feed.post"
)
// SetStatus creates a new status post on Bluesky
// status should be "online" or "offline"
// Each call creates a unique post with a TID-based rkey
func (p *HoldPDS) SetStatus(ctx context.Context, status string) error {
// Check if Bluesky posts are enabled
if !p.enableBlueskyPosts {
slog.Debug("Bluesky posts disabled, skipping status post", "status", status)
return nil
}
// Format the post text with emoji indicator
emoji := "🟢"
if status == "offline" {
emoji = "🔴"
}
text := fmt.Sprintf("%s Current status: %s", emoji, status)
// Create the post with a unique TID
return p.createStatusPost(ctx, text)
}
// createStatusPost creates a new status post with a TID-based rkey
func (p *HoldPDS) createStatusPost(ctx context.Context, text string) error {
// Create post struct
now := time.Now()
post := &bsky.FeedPost{
LexiconTypeID: "app.bsky.feed.post",
Text: text,
CreatedAt: now.Format(time.RFC3339),
}
// Use repomgr.CreateRecord to create the post with auto-generated TID
// CreateRecord automatically generates a unique TID using the repo's clock
rkey, recordCID, err := p.repomgr.CreateRecord(ctx, p.uid, StatusPostCollection, post)
if err != nil {
return fmt.Errorf("failed to create status post: %w", err)
}
slog.Info("Created status post",
"collection", StatusPostCollection,
"rkey", rkey,
"cid", recordCID.String(),
"text", text)
return nil
}