aboutsummaryrefslogtreecommitdiff
path: root/backend/notes/app.bsky.feed.post.go
blob: 41a511a2aca8e2815836b61ab5563455a25dcc23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package notes

import (
	"fmt"
	"strings"
	"time"
)

type AppBskyFeedPostNote struct {
	URI          string    `json:"uri"`
	AuthorDID    string    `json:"authorDid"`
	AuthorHandle string    `json:"authorHandle"`
	Text         string    `json:"text"`
	CreatedAt    time.Time `json:"createdAt"`
	ReplyTo      *string   `json:"replyTo,omitempty"`
}

type AppBskyFeedPostRecord struct {
	URI   string `json:"uri"`
	Value struct {
		Text      string `json:"text"`
		CreatedAt string `json:"createdAt"`
		Reply     *struct {
			Parent struct {
				URI string `json:"uri"`
			} `json:"parent"`
		} `json:"reply,omitempty"`
	} `json:"value"`
}

func (bp *AppBskyFeedPostNote) GetID() string {
	return bp.URI
}

func (bp *AppBskyFeedPostNote) GetAuthorDID() string {
	return bp.AuthorDID
}

func (bp *AppBskyFeedPostNote) GetAuthorHandle() string {
	return bp.AuthorHandle
}

func (bp *AppBskyFeedPostNote) GetTimestamp() time.Time {
	return bp.CreatedAt
}

func (bp *AppBskyFeedPostNote) GetReplyTo() *string {
	return bp.ReplyTo
}

func AppBskyFeedPostFromRecord(record AppBskyFeedPostRecord) (*AppBskyFeedPostNote, error) {
	if record.Value.Text == "" {
		return nil, fmt.Errorf("missing required field: text")
	}
	if record.URI == "" {
		return nil, fmt.Errorf("missing required field: uri")
	}

	createdAt, err := time.Parse(time.RFC3339, record.Value.CreatedAt)
	if err != nil {
		return nil, fmt.Errorf("failed to parse timestamp: %w", err)
	}

	var replyTo *string
	if record.Value.Reply != nil {
		replyTo = &record.Value.Reply.Parent.URI
	}

	parts := strings.Split(record.URI, "/") // at://DID/collection/rkey
	did := parts[2]

	return &AppBskyFeedPostNote{
		URI:          record.URI,
		AuthorDID:    did,
		AuthorHandle: "author_handle", // wip
		CreatedAt:    createdAt,
		Text:         record.Value.Text,
		ReplyTo:      replyTo,
	}, nil
}