1package posthog_service
2
3import (
4 "context"
5 "log"
6
7 "github.com/posthog/posthog-go"
8 "tangled.sh/tangled.sh/core/appview/db"
9 "tangled.sh/tangled.sh/core/appview/notify"
10)
11
12type posthogNotifier struct {
13 client posthog.Client
14 notify.BaseNotifier
15}
16
17func NewPosthogNotifier(client posthog.Client) notify.Notifier {
18 return &posthogNotifier{
19 client,
20 notify.BaseNotifier{},
21 }
22}
23
24var _ notify.Notifier = &posthogNotifier{}
25
26func (n *posthogNotifier) NewRepo(ctx context.Context, repo *db.Repo) {
27 err := n.client.Enqueue(posthog.Capture{
28 DistinctId: repo.Did,
29 Event: "new_repo",
30 Properties: posthog.Properties{"repo": repo.Name, "repo_at": repo.RepoAt()},
31 })
32 if err != nil {
33 log.Println("failed to enqueue posthog event:", err)
34 }
35}
36
37func (n *posthogNotifier) NewStar(ctx context.Context, star *db.Star) {
38 err := n.client.Enqueue(posthog.Capture{
39 DistinctId: star.StarredByDid,
40 Event: "star",
41 Properties: posthog.Properties{"repo_at": star.RepoAt.String()},
42 })
43 if err != nil {
44 log.Println("failed to enqueue posthog event:", err)
45 }
46}
47
48func (n *posthogNotifier) DeleteStar(ctx context.Context, star *db.Star) {
49 err := n.client.Enqueue(posthog.Capture{
50 DistinctId: star.StarredByDid,
51 Event: "unstar",
52 Properties: posthog.Properties{"repo_at": star.RepoAt.String()},
53 })
54 if err != nil {
55 log.Println("failed to enqueue posthog event:", err)
56 }
57}
58
59func (n *posthogNotifier) NewIssue(ctx context.Context, issue *db.Issue) {
60 err := n.client.Enqueue(posthog.Capture{
61 DistinctId: issue.Did,
62 Event: "new_issue",
63 Properties: posthog.Properties{
64 "repo_at": issue.RepoAt.String(),
65 "issue_id": issue.IssueId,
66 },
67 })
68 if err != nil {
69 log.Println("failed to enqueue posthog event:", err)
70 }
71}
72
73func (n *posthogNotifier) NewPull(ctx context.Context, pull *db.Pull) {
74 err := n.client.Enqueue(posthog.Capture{
75 DistinctId: pull.OwnerDid,
76 Event: "new_pull",
77 Properties: posthog.Properties{
78 "repo_at": pull.RepoAt,
79 "pull_id": pull.PullId,
80 },
81 })
82 if err != nil {
83 log.Println("failed to enqueue posthog event:", err)
84 }
85}
86
87func (n *posthogNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {
88 err := n.client.Enqueue(posthog.Capture{
89 DistinctId: comment.OwnerDid,
90 Event: "new_pull_comment",
91 Properties: posthog.Properties{
92 "repo_at": comment.RepoAt,
93 "pull_id": comment.PullId,
94 },
95 })
96 if err != nil {
97 log.Println("failed to enqueue posthog event:", err)
98 }
99}
100
101func (n *posthogNotifier) NewFollow(ctx context.Context, follow *db.Follow) {
102 err := n.client.Enqueue(posthog.Capture{
103 DistinctId: follow.UserDid,
104 Event: "follow",
105 Properties: posthog.Properties{"subject": follow.SubjectDid},
106 })
107 if err != nil {
108 log.Println("failed to enqueue posthog event:", err)
109 }
110}
111
112func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {
113 err := n.client.Enqueue(posthog.Capture{
114 DistinctId: follow.UserDid,
115 Event: "unfollow",
116 Properties: posthog.Properties{"subject": follow.SubjectDid},
117 })
118 if err != nil {
119 log.Println("failed to enqueue posthog event:", err)
120 }
121}
122
123func (n *posthogNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) {
124 err := n.client.Enqueue(posthog.Capture{
125 DistinctId: profile.Did,
126 Event: "edit_profile",
127 })
128 if err != nil {
129 log.Println("failed to enqueue posthog event:", err)
130 }
131}