1package posthog
2
3import (
4 "context"
5 "log"
6
7 "github.com/posthog/posthog-go"
8 "tangled.org/core/appview/models"
9 "tangled.org/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 *models.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 *models.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 *models.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 *models.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 *models.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 *models.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) NewPullClosed(ctx context.Context, pull *models.Pull) {
102 err := n.client.Enqueue(posthog.Capture{
103 DistinctId: pull.OwnerDid,
104 Event: "pull_closed",
105 Properties: posthog.Properties{
106 "repo_at": pull.RepoAt,
107 "pull_id": pull.PullId,
108 },
109 })
110 if err != nil {
111 log.Println("failed to enqueue posthog event:", err)
112 }
113}
114
115func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
116 err := n.client.Enqueue(posthog.Capture{
117 DistinctId: follow.UserDid,
118 Event: "follow",
119 Properties: posthog.Properties{"subject": follow.SubjectDid},
120 })
121 if err != nil {
122 log.Println("failed to enqueue posthog event:", err)
123 }
124}
125
126func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
127 err := n.client.Enqueue(posthog.Capture{
128 DistinctId: follow.UserDid,
129 Event: "unfollow",
130 Properties: posthog.Properties{"subject": follow.SubjectDid},
131 })
132 if err != nil {
133 log.Println("failed to enqueue posthog event:", err)
134 }
135}
136
137func (n *posthogNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
138 err := n.client.Enqueue(posthog.Capture{
139 DistinctId: profile.Did,
140 Event: "edit_profile",
141 })
142 if err != nil {
143 log.Println("failed to enqueue posthog event:", err)
144 }
145}
146
147func (n *posthogNotifier) DeleteString(ctx context.Context, did, rkey string) {
148 err := n.client.Enqueue(posthog.Capture{
149 DistinctId: did,
150 Event: "delete_string",
151 Properties: posthog.Properties{"rkey": rkey},
152 })
153 if err != nil {
154 log.Println("failed to enqueue posthog event:", err)
155 }
156}
157
158func (n *posthogNotifier) EditString(ctx context.Context, string *models.String) {
159 err := n.client.Enqueue(posthog.Capture{
160 DistinctId: string.Did.String(),
161 Event: "edit_string",
162 Properties: posthog.Properties{"rkey": string.Rkey},
163 })
164 if err != nil {
165 log.Println("failed to enqueue posthog event:", err)
166 }
167}
168
169func (n *posthogNotifier) NewString(ctx context.Context, string *models.String) {
170 err := n.client.Enqueue(posthog.Capture{
171 DistinctId: string.Did.String(),
172 Event: "new_string",
173 Properties: posthog.Properties{"rkey": string.Rkey},
174 })
175 if err != nil {
176 log.Println("failed to enqueue posthog event:", err)
177 }
178}
179
180func (n *posthogNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) {
181 err := n.client.Enqueue(posthog.Capture{
182 DistinctId: comment.Did,
183 Event: "new_issue_comment",
184 Properties: posthog.Properties{
185 "issue_at": comment.IssueAt,
186 },
187 })
188 if err != nil {
189 log.Println("failed to enqueue posthog event:", err)
190 }
191}
192
193func (n *posthogNotifier) NewIssueState(ctx context.Context, issue *models.Issue) {
194 var event string
195 if issue.Open {
196 event = "issue_reopen"
197 } else {
198 event = "issue_closed"
199 }
200 err := n.client.Enqueue(posthog.Capture{
201 DistinctId: issue.Did,
202 Event: event,
203 Properties: posthog.Properties{
204 "repo_at": issue.RepoAt.String(),
205 "issue_id": issue.IssueId,
206 },
207 })
208 if err != nil {
209 log.Println("failed to enqueue posthog event:", err)
210 }
211}
212
213func (n *posthogNotifier) NewPullState(ctx context.Context, pull *models.Pull) {
214 var event string
215 switch pull.State {
216 case models.PullClosed:
217 event = "pull_closed"
218 case models.PullOpen:
219 event = "pull_reopen"
220 case models.PullMerged:
221 event = "pull_merged"
222 default:
223 log.Println("posthog: unexpected new PR state:", pull.State)
224 return
225 }
226 err := n.client.Enqueue(posthog.Capture{
227 DistinctId: pull.OwnerDid,
228 Event: event,
229 Properties: posthog.Properties{
230 "repo_at": pull.RepoAt,
231 "pull_id": pull.PullId,
232 },
233 })
234 if err != nil {
235 log.Println("failed to enqueue posthog event:", err)
236 }
237}