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