this repo has no description
1package posthog
2
3import (
4 "context"
5 "log"
6
7 "github.com/bluesky-social/indigo/atproto/syntax"
8 "github.com/posthog/posthog-go"
9 "tangled.org/core/appview/models"
10 "tangled.org/core/appview/notify"
11)
12
13type posthogNotifier struct {
14 client posthog.Client
15 notify.BaseNotifier
16}
17
18func NewPosthogNotifier(client posthog.Client) notify.Notifier {
19 return &posthogNotifier{
20 client,
21 notify.BaseNotifier{},
22 }
23}
24
25var _ notify.Notifier = &posthogNotifier{}
26
27func (n *posthogNotifier) NewRepo(ctx context.Context, repo *models.Repo) {
28 err := n.client.Enqueue(posthog.Capture{
29 DistinctId: repo.Did,
30 Event: "new_repo",
31 Properties: posthog.Properties{"repo": repo.Name, "repo_at": repo.RepoAt()},
32 })
33 if err != nil {
34 log.Println("failed to enqueue posthog event:", err)
35 }
36}
37
38func (n *posthogNotifier) NewStar(ctx context.Context, star *models.Star) {
39 err := n.client.Enqueue(posthog.Capture{
40 DistinctId: star.StarredByDid,
41 Event: "star",
42 Properties: posthog.Properties{"repo_at": star.RepoAt.String()},
43 })
44 if err != nil {
45 log.Println("failed to enqueue posthog event:", err)
46 }
47}
48
49func (n *posthogNotifier) DeleteStar(ctx context.Context, star *models.Star) {
50 err := n.client.Enqueue(posthog.Capture{
51 DistinctId: star.StarredByDid,
52 Event: "unstar",
53 Properties: posthog.Properties{"repo_at": star.RepoAt.String()},
54 })
55 if err != nil {
56 log.Println("failed to enqueue posthog event:", err)
57 }
58}
59
60func (n *posthogNotifier) NewIssue(ctx context.Context, issue *models.Issue) {
61 err := n.client.Enqueue(posthog.Capture{
62 DistinctId: issue.Did,
63 Event: "new_issue",
64 Properties: posthog.Properties{
65 "repo_at": issue.RepoAt.String(),
66 "issue_id": issue.IssueId,
67 },
68 })
69 if err != nil {
70 log.Println("failed to enqueue posthog event:", err)
71 }
72}
73
74func (n *posthogNotifier) NewPull(ctx context.Context, pull *models.Pull) {
75 err := n.client.Enqueue(posthog.Capture{
76 DistinctId: pull.OwnerDid,
77 Event: "new_pull",
78 Properties: posthog.Properties{
79 "repo_at": pull.RepoAt,
80 "pull_id": pull.PullId,
81 },
82 })
83 if err != nil {
84 log.Println("failed to enqueue posthog event:", err)
85 }
86}
87
88func (n *posthogNotifier) NewPullComment(ctx context.Context, comment *models.PullComment) {
89 err := n.client.Enqueue(posthog.Capture{
90 DistinctId: comment.OwnerDid,
91 Event: "new_pull_comment",
92 Properties: posthog.Properties{
93 "repo_at": comment.RepoAt,
94 "pull_id": comment.PullId,
95 },
96 })
97 if err != nil {
98 log.Println("failed to enqueue posthog event:", err)
99 }
100}
101
102func (n *posthogNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
103 err := n.client.Enqueue(posthog.Capture{
104 DistinctId: pull.OwnerDid,
105 Event: "pull_closed",
106 Properties: posthog.Properties{
107 "repo_at": pull.RepoAt,
108 "pull_id": pull.PullId,
109 },
110 })
111 if err != nil {
112 log.Println("failed to enqueue posthog event:", err)
113 }
114}
115
116func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
117 err := n.client.Enqueue(posthog.Capture{
118 DistinctId: follow.UserDid,
119 Event: "follow",
120 Properties: posthog.Properties{"subject": follow.SubjectDid},
121 })
122 if err != nil {
123 log.Println("failed to enqueue posthog event:", err)
124 }
125}
126
127func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
128 err := n.client.Enqueue(posthog.Capture{
129 DistinctId: follow.UserDid,
130 Event: "unfollow",
131 Properties: posthog.Properties{"subject": follow.SubjectDid},
132 })
133 if err != nil {
134 log.Println("failed to enqueue posthog event:", err)
135 }
136}
137
138func (n *posthogNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
139 err := n.client.Enqueue(posthog.Capture{
140 DistinctId: profile.Did,
141 Event: "edit_profile",
142 })
143 if err != nil {
144 log.Println("failed to enqueue posthog event:", err)
145 }
146}
147
148func (n *posthogNotifier) DeleteString(ctx context.Context, did, rkey string) {
149 err := n.client.Enqueue(posthog.Capture{
150 DistinctId: did,
151 Event: "delete_string",
152 Properties: posthog.Properties{"rkey": rkey},
153 })
154 if err != nil {
155 log.Println("failed to enqueue posthog event:", err)
156 }
157}
158
159func (n *posthogNotifier) EditString(ctx context.Context, string *models.String) {
160 err := n.client.Enqueue(posthog.Capture{
161 DistinctId: string.Did.String(),
162 Event: "edit_string",
163 Properties: posthog.Properties{"rkey": string.Rkey},
164 })
165 if err != nil {
166 log.Println("failed to enqueue posthog event:", err)
167 }
168}
169
170func (n *posthogNotifier) NewString(ctx context.Context, string *models.String) {
171 err := n.client.Enqueue(posthog.Capture{
172 DistinctId: string.Did.String(),
173 Event: "new_string",
174 Properties: posthog.Properties{"rkey": string.Rkey},
175 })
176 if err != nil {
177 log.Println("failed to enqueue posthog event:", err)
178 }
179}
180
181func (n *posthogNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) {
182 err := n.client.Enqueue(posthog.Capture{
183 DistinctId: comment.Did,
184 Event: "new_issue_comment",
185 Properties: posthog.Properties{
186 "issue_at": comment.IssueAt,
187 },
188 })
189 if err != nil {
190 log.Println("failed to enqueue posthog event:", err)
191 }
192}
193
194func (n *posthogNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) {
195 var event string
196 if issue.Open {
197 event = "issue_reopen"
198 } else {
199 event = "issue_closed"
200 }
201 err := n.client.Enqueue(posthog.Capture{
202 DistinctId: issue.Did,
203 Event: event,
204 Properties: posthog.Properties{
205 "repo_at": issue.RepoAt.String(),
206 "actor": actor,
207 "issue_id": issue.IssueId,
208 },
209 })
210 if err != nil {
211 log.Println("failed to enqueue posthog event:", err)
212 }
213}
214
215func (n *posthogNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) {
216 var event string
217 switch pull.State {
218 case models.PullClosed:
219 event = "pull_closed"
220 case models.PullOpen:
221 event = "pull_reopen"
222 case models.PullMerged:
223 event = "pull_merged"
224 default:
225 log.Println("posthog: unexpected new PR state:", pull.State)
226 return
227 }
228 err := n.client.Enqueue(posthog.Capture{
229 DistinctId: pull.OwnerDid,
230 Event: event,
231 Properties: posthog.Properties{
232 "repo_at": pull.RepoAt,
233 "pull_id": pull.PullId,
234 "actor": actor,
235 },
236 })
237 if err != nil {
238 log.Println("failed to enqueue posthog event:", err)
239 }
240}