Monorepo for Tangled
tangled.org
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) RenameRepo(ctx context.Context, actor syntax.DID, oldRepo, newRepo *models.Repo) {
39 err := n.client.Enqueue(posthog.Capture{
40 DistinctId: actor.String(),
41 Event: "repo_renamed",
42 Properties: posthog.Properties{
43 "repo_at": newRepo.RepoAt(),
44 "owner": newRepo.Did,
45 "old_name": oldRepo.Name,
46 "new_name": newRepo.Name,
47 },
48 })
49 if err != nil {
50 log.Println("failed to enqueue posthog event:", err)
51 }
52}
53
54func (n *posthogNotifier) NewStar(ctx context.Context, star *models.Star) {
55 err := n.client.Enqueue(posthog.Capture{
56 DistinctId: star.Did,
57 Event: "star",
58 Properties: posthog.Properties{
59 "subject_type": string(star.SubjectType),
60 "subject": star.Subject,
61 },
62 })
63 if err != nil {
64 log.Println("failed to enqueue posthog event:", err)
65 }
66}
67
68func (n *posthogNotifier) DeleteStar(ctx context.Context, star *models.Star) {
69 err := n.client.Enqueue(posthog.Capture{
70 DistinctId: star.Did,
71 Event: "unstar",
72 Properties: posthog.Properties{
73 "subject_type": string(star.SubjectType),
74 "subject": star.Subject,
75 },
76 })
77 if err != nil {
78 log.Println("failed to enqueue posthog event:", err)
79 }
80}
81
82func (n *posthogNotifier) NewIssue(ctx context.Context, issue *models.Issue, mentions []syntax.DID) {
83 err := n.client.Enqueue(posthog.Capture{
84 DistinctId: issue.Did,
85 Event: "new_issue",
86 Properties: posthog.Properties{
87 "repo_did": string(issue.RepoDid),
88 "issue_id": issue.IssueId,
89 "mentions": mentions,
90 },
91 })
92 if err != nil {
93 log.Println("failed to enqueue posthog event:", err)
94 }
95}
96
97func (n *posthogNotifier) NewPull(ctx context.Context, pull *models.Pull) {
98 err := n.client.Enqueue(posthog.Capture{
99 DistinctId: pull.OwnerDid,
100 Event: "new_pull",
101 Properties: posthog.Properties{
102 "repo_did": string(pull.RepoDid),
103 "pull_id": pull.PullId,
104 },
105 })
106 if err != nil {
107 log.Println("failed to enqueue posthog event:", err)
108 }
109}
110
111func (n *posthogNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
112 err := n.client.Enqueue(posthog.Capture{
113 DistinctId: pull.OwnerDid,
114 Event: "pull_closed",
115 Properties: posthog.Properties{
116 "repo_did": string(pull.RepoDid),
117 "pull_id": pull.PullId,
118 },
119 })
120 if err != nil {
121 log.Println("failed to enqueue posthog event:", err)
122 }
123}
124
125func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
126 err := n.client.Enqueue(posthog.Capture{
127 DistinctId: follow.UserDid,
128 Event: "follow",
129 Properties: posthog.Properties{"subject": follow.SubjectDid},
130 })
131 if err != nil {
132 log.Println("failed to enqueue posthog event:", err)
133 }
134}
135
136func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
137 err := n.client.Enqueue(posthog.Capture{
138 DistinctId: follow.UserDid,
139 Event: "unfollow",
140 Properties: posthog.Properties{"subject": follow.SubjectDid},
141 })
142 if err != nil {
143 log.Println("failed to enqueue posthog event:", err)
144 }
145}
146
147func (n *posthogNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
148 err := n.client.Enqueue(posthog.Capture{
149 DistinctId: profile.Did,
150 Event: "edit_profile",
151 })
152 if err != nil {
153 log.Println("failed to enqueue posthog event:", err)
154 }
155}
156
157func (n *posthogNotifier) DeleteString(ctx context.Context, did, rkey string) {
158 err := n.client.Enqueue(posthog.Capture{
159 DistinctId: did,
160 Event: "delete_string",
161 Properties: posthog.Properties{"rkey": rkey},
162 })
163 if err != nil {
164 log.Println("failed to enqueue posthog event:", err)
165 }
166}
167
168func (n *posthogNotifier) EditString(ctx context.Context, string *models.String) {
169 err := n.client.Enqueue(posthog.Capture{
170 DistinctId: string.Did.String(),
171 Event: "edit_string",
172 Properties: posthog.Properties{"rkey": string.Rkey},
173 })
174 if err != nil {
175 log.Println("failed to enqueue posthog event:", err)
176 }
177}
178
179func (n *posthogNotifier) NewString(ctx context.Context, string *models.String) {
180 err := n.client.Enqueue(posthog.Capture{
181 DistinctId: string.Did.String(),
182 Event: "new_string",
183 Properties: posthog.Properties{"rkey": string.Rkey},
184 })
185 if err != nil {
186 log.Println("failed to enqueue posthog event:", err)
187 }
188}
189
190func (n *posthogNotifier) Clone(ctx context.Context, repo *models.Repo) {
191 err := n.client.Enqueue(posthog.Capture{
192 DistinctId: repo.Did,
193 Event: "clone",
194 Properties: posthog.Properties{"repo": repo.Name, "repo_at": repo.RepoAt()},
195 })
196 if err != nil {
197 log.Println("failed to enqueue posthog event:", err)
198 }
199}
200
201func (n *posthogNotifier) NewComment(ctx context.Context, comment *models.Comment, mentions []syntax.DID) {
202 err := n.client.Enqueue(posthog.Capture{
203 DistinctId: comment.Did.String(),
204 Event: "new_comment",
205 Properties: posthog.Properties{
206 "subject_at": comment.Subject.Uri,
207 "mentions": mentions,
208 },
209 })
210 if err != nil {
211 log.Println("failed to enqueue posthog event:", err)
212 }
213}
214
215func (n *posthogNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) {
216 var event string
217 if issue.Open {
218 event = "issue_reopen"
219 } else {
220 event = "issue_closed"
221 }
222 err := n.client.Enqueue(posthog.Capture{
223 DistinctId: issue.Did,
224 Event: event,
225 Properties: posthog.Properties{
226 "repo_did": string(issue.RepoDid),
227 "actor": actor,
228 "issue_id": issue.IssueId,
229 },
230 })
231 if err != nil {
232 log.Println("failed to enqueue posthog event:", err)
233 }
234}
235
236func (n *posthogNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) {
237 var event string
238 switch pull.State {
239 case models.PullClosed:
240 event = "pull_closed"
241 case models.PullOpen:
242 event = "pull_reopen"
243 case models.PullMerged:
244 event = "pull_merged"
245 default:
246 log.Println("posthog: unexpected new PR state:", pull.State)
247 return
248 }
249 err := n.client.Enqueue(posthog.Capture{
250 DistinctId: pull.OwnerDid,
251 Event: event,
252 Properties: posthog.Properties{
253 "repo_did": string(pull.RepoDid),
254 "pull_id": pull.PullId,
255 "actor": actor,
256 },
257 })
258 if err != nil {
259 log.Println("failed to enqueue posthog event:", err)
260 }
261}