This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / appview / notify / posthog / notifier.go
7.3 kB 276 lines
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) NewPullComment(ctx context.Context, comment *models.PullComment, mentions []syntax.DID) { 112 err := n.client.Enqueue(posthog.Capture{ 113 DistinctId: comment.OwnerDid, 114 Event: "new_pull_comment", 115 Properties: posthog.Properties{ 116 "repo_did": comment.RepoDid, 117 "pull_id": comment.PullId, 118 "mentions": mentions, 119 }, 120 }) 121 if err != nil { 122 log.Println("failed to enqueue posthog event:", err) 123 } 124} 125 126func (n *posthogNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { 127 err := n.client.Enqueue(posthog.Capture{ 128 DistinctId: pull.OwnerDid, 129 Event: "pull_closed", 130 Properties: posthog.Properties{ 131 "repo_did": string(pull.RepoDid), 132 "pull_id": pull.PullId, 133 }, 134 }) 135 if err != nil { 136 log.Println("failed to enqueue posthog event:", err) 137 } 138} 139 140func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 141 err := n.client.Enqueue(posthog.Capture{ 142 DistinctId: follow.UserDid, 143 Event: "follow", 144 Properties: posthog.Properties{"subject": follow.SubjectDid}, 145 }) 146 if err != nil { 147 log.Println("failed to enqueue posthog event:", err) 148 } 149} 150 151func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 152 err := n.client.Enqueue(posthog.Capture{ 153 DistinctId: follow.UserDid, 154 Event: "unfollow", 155 Properties: posthog.Properties{"subject": follow.SubjectDid}, 156 }) 157 if err != nil { 158 log.Println("failed to enqueue posthog event:", err) 159 } 160} 161 162func (n *posthogNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 163 err := n.client.Enqueue(posthog.Capture{ 164 DistinctId: profile.Did, 165 Event: "edit_profile", 166 }) 167 if err != nil { 168 log.Println("failed to enqueue posthog event:", err) 169 } 170} 171 172func (n *posthogNotifier) DeleteString(ctx context.Context, did, rkey string) { 173 err := n.client.Enqueue(posthog.Capture{ 174 DistinctId: did, 175 Event: "delete_string", 176 Properties: posthog.Properties{"rkey": rkey}, 177 }) 178 if err != nil { 179 log.Println("failed to enqueue posthog event:", err) 180 } 181} 182 183func (n *posthogNotifier) EditString(ctx context.Context, string *models.String) { 184 err := n.client.Enqueue(posthog.Capture{ 185 DistinctId: string.Did.String(), 186 Event: "edit_string", 187 Properties: posthog.Properties{"rkey": string.Rkey}, 188 }) 189 if err != nil { 190 log.Println("failed to enqueue posthog event:", err) 191 } 192} 193 194func (n *posthogNotifier) NewString(ctx context.Context, string *models.String) { 195 err := n.client.Enqueue(posthog.Capture{ 196 DistinctId: string.Did.String(), 197 Event: "new_string", 198 Properties: posthog.Properties{"rkey": string.Rkey}, 199 }) 200 if err != nil { 201 log.Println("failed to enqueue posthog event:", err) 202 } 203} 204 205func (n *posthogNotifier) Clone(ctx context.Context, repo *models.Repo) { 206 err := n.client.Enqueue(posthog.Capture{ 207 DistinctId: repo.Did, 208 Event: "clone", 209 Properties: posthog.Properties{"repo": repo.Name, "repo_at": repo.RepoAt()}, 210 }) 211 if err != nil { 212 log.Println("failed to enqueue posthog event:", err) 213 } 214} 215 216func (n *posthogNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment, mentions []syntax.DID) { 217 err := n.client.Enqueue(posthog.Capture{ 218 DistinctId: comment.Did, 219 Event: "new_issue_comment", 220 Properties: posthog.Properties{ 221 "issue_at": comment.IssueAt, 222 "mentions": mentions, 223 }, 224 }) 225 if err != nil { 226 log.Println("failed to enqueue posthog event:", err) 227 } 228} 229 230func (n *posthogNotifier) NewIssueState(ctx context.Context, actor syntax.DID, issue *models.Issue) { 231 var event string 232 if issue.Open { 233 event = "issue_reopen" 234 } else { 235 event = "issue_closed" 236 } 237 err := n.client.Enqueue(posthog.Capture{ 238 DistinctId: issue.Did, 239 Event: event, 240 Properties: posthog.Properties{ 241 "repo_did": string(issue.RepoDid), 242 "actor": actor, 243 "issue_id": issue.IssueId, 244 }, 245 }) 246 if err != nil { 247 log.Println("failed to enqueue posthog event:", err) 248 } 249} 250 251func (n *posthogNotifier) NewPullState(ctx context.Context, actor syntax.DID, pull *models.Pull) { 252 var event string 253 switch pull.State { 254 case models.PullClosed: 255 event = "pull_closed" 256 case models.PullOpen: 257 event = "pull_reopen" 258 case models.PullMerged: 259 event = "pull_merged" 260 default: 261 log.Println("posthog: unexpected new PR state:", pull.State) 262 return 263 } 264 err := n.client.Enqueue(posthog.Capture{ 265 DistinctId: pull.OwnerDid, 266 Event: event, 267 Properties: posthog.Properties{ 268 "repo_did": string(pull.RepoDid), 269 "pull_id": pull.PullId, 270 "actor": actor, 271 }, 272 }) 273 if err != nil { 274 log.Println("failed to enqueue posthog event:", err) 275 } 276}