From dd2dc8b76d7492d3c36e9351a8f29f842a5024e5 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Mon, 21 Jul 2025 01:02:56 +0900 Subject: [PATCH] appview: wrap posthog under unified notifier Change-Id: pslnqmxvulmpoktlsummvlwpqsxnyrwq - Make `db.New*()` methods to accept complete model object instead of individual fields - Remove `posthog` field from *most* Service structs. Oauth still has one as an edge-case - Add more notifier methods to replace posthog logics Signed-off-by: Seongmin Lee --- appview/db/follow.go | 4 +- appview/db/star.go | 9 +- appview/ingester.go | 13 ++- appview/issues/issues.go | 19 ++--- appview/notify/merged_notifier.go | 48 +++++++++-- appview/notify/notifier.go | 36 +++++++- appview/pipelines/pipelines.go | 4 - appview/pulls/pulls.go | 43 ++++------ appview/repo/repo.go | 8 +- appview/state/follow.go | 35 +++----- appview/state/posthog.go | 131 ++++++++++++++++++++++++++++++ appview/state/profile.go | 11 +-- appview/state/router.go | 8 +- appview/state/star.go | 36 +++----- appview/state/state.go | 12 +-- 15 files changed, 278 insertions(+), 139 deletions(-) create mode 100644 appview/state/posthog.go diff --git a/appview/db/follow.go b/appview/db/follow.go index 7cceb64..cc7df7f 100644 --- a/appview/db/follow.go +++ b/appview/db/follow.go @@ -12,9 +12,9 @@ type Follow struct { Rkey string } -func AddFollow(e Execer, userDid, subjectDid, rkey string) error { +func AddFollow(e Execer, follow *Follow) error { query := `insert or ignore into follows (user_did, subject_did, rkey) values (?, ?, ?)` - _, err := e.Exec(query, userDid, subjectDid, rkey) + _, err := e.Exec(query, follow.UserDid, follow.SubjectDid, follow.Rkey) return err } diff --git a/appview/db/star.go b/appview/db/star.go index 0680dbd..cb8b0b2 100644 --- a/appview/db/star.go +++ b/appview/db/star.go @@ -31,9 +31,14 @@ func (star *Star) ResolveRepo(e Execer) error { return nil } -func AddStar(e Execer, starredByDid string, repoAt syntax.ATURI, rkey string) error { +func AddStar(e Execer, star *Star) error { query := `insert or ignore into stars (starred_by_did, repo_at, rkey) values (?, ?, ?)` - _, err := e.Exec(query, starredByDid, repoAt, rkey) + _, err := e.Exec( + query, + star.StarredByDid, + star.RepoAt.String(), + star.Rkey, + ) return err } diff --git a/appview/ingester.go b/appview/ingester.go index 6677bdb..c8b54d8 100644 --- a/appview/ingester.go +++ b/appview/ingester.go @@ -100,7 +100,11 @@ func (i *Ingester) ingestStar(e *models.Event) error { l.Error("invalid record", "err", err) return err } - err = db.AddStar(i.Db, did, subjectUri, e.Commit.RKey) + err = db.AddStar(i.Db, &db.Star{ + StarredByDid: did, + RepoAt: subjectUri, + Rkey: e.Commit.RKey, + }) case models.CommitOperationDelete: err = db.DeleteStarByRkey(i.Db, did, e.Commit.RKey) } @@ -129,8 +133,11 @@ func (i *Ingester) ingestFollow(e *models.Event) error { return err } - subjectDid := record.Subject - err = db.AddFollow(i.Db, did, subjectDid, e.Commit.RKey) + err = db.AddFollow(i.Db, &db.Follow{ + UserDid: did, + SubjectDid: record.Subject, + Rkey: e.Commit.RKey, + }) case models.CommitOperationDelete: err = db.DeleteFollowByRkey(i.Db, did, e.Commit.RKey) } diff --git a/appview/issues/issues.go b/appview/issues/issues.go index cc1b816..c65f0d2 100644 --- a/appview/issues/issues.go +++ b/appview/issues/issues.go @@ -14,13 +14,13 @@ import ( "github.com/bluesky-social/indigo/atproto/syntax" lexutil "github.com/bluesky-social/indigo/lex/util" "github.com/go-chi/chi/v5" - "github.com/posthog/posthog-go" "tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/appview" "tangled.sh/tangled.sh/core/appview/config" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/idresolver" + "tangled.sh/tangled.sh/core/appview/notify" "tangled.sh/tangled.sh/core/appview/oauth" "tangled.sh/tangled.sh/core/appview/pages" "tangled.sh/tangled.sh/core/appview/pagination" @@ -34,7 +34,7 @@ type Issues struct { idResolver *idresolver.Resolver db *db.DB config *config.Config - posthog posthog.Client + notifier notify.Notifier } func New( @@ -44,7 +44,7 @@ func New( idResolver *idresolver.Resolver, db *db.DB, config *config.Config, - posthog posthog.Client, + notifier notify.Notifier, ) *Issues { return &Issues{ oauth: oauth, @@ -53,7 +53,7 @@ func New( idResolver: idResolver, db: db, config: config, - posthog: posthog, + notifier: notifier, } } @@ -750,16 +750,7 @@ func (rp *Issues) NewIssue(w http.ResponseWriter, r *http.Request) { return } - if !rp.config.Core.Dev { - err = rp.posthog.Enqueue(posthog.Capture{ - DistinctId: user.Did, - Event: "new_issue", - Properties: posthog.Properties{"repo_at": f.RepoAt.String(), "issue_id": issue.IssueId}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + rp.notifier.NewIssue(r.Context(), issue) rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", f.OwnerSlashRepo(), issue.IssueId)) return diff --git a/appview/notify/merged_notifier.go b/appview/notify/merged_notifier.go index 901cda8..5d48514 100644 --- a/appview/notify/merged_notifier.go +++ b/appview/notify/merged_notifier.go @@ -11,27 +11,63 @@ type mergedNotifier struct { } func NewMergedNotifier(notifiers ...Notifier) Notifier { - return &mergedNotifier{ - notifiers, - } + return &mergedNotifier{notifiers} } var _ Notifier = &mergedNotifier{} +func (m *mergedNotifier) NewRepo(ctx context.Context, repo *db.Repo) { + for _, notifier := range m.notifiers { + notifier.NewRepo(ctx, repo) + } +} + +func (m *mergedNotifier) NewStar(ctx context.Context, star *db.Star) { + for _, notifier := range m.notifiers { + notifier.NewStar(ctx, star) + } +} +func (m *mergedNotifier) DeleteStar(ctx context.Context, star *db.Star) { + for _, notifier := range m.notifiers { + notifier.DeleteStar(ctx, star) + } +} + func (m *mergedNotifier) NewIssue(ctx context.Context, issue *db.Issue) { for _, notifier := range m.notifiers { notifier.NewIssue(ctx, issue) } } - -func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment db.Comment) { +func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment *db.Comment) { for _, notifier := range m.notifiers { notifier.NewIssueComment(ctx, comment) } } -func (m *mergedNotifier) NewPullComment(ctx context.Context, comment db.PullComment) { +func (m *mergedNotifier) NewFollow(ctx context.Context, follow *db.Follow) { + for _, notifier := range m.notifiers { + notifier.NewFollow(ctx, follow) + } +} +func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) { + for _, notifier := range m.notifiers { + notifier.DeleteFollow(ctx, follow) + } +} + +func (m *mergedNotifier) NewPull(ctx context.Context, pull *db.Pull) { + for _, notifier := range m.notifiers { + notifier.NewPull(ctx, pull) + } +} +func (m *mergedNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) { for _, notifier := range m.notifiers { notifier.NewPullComment(ctx, comment) } } + +func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) { + for _, notifier := range m.notifiers { + notifier.UpdateProfile(ctx, profile) + } +} diff --git a/appview/notify/notifier.go b/appview/notify/notifier.go index 5eff4d3..6aa253c 100644 --- a/appview/notify/notifier.go +++ b/appview/notify/notifier.go @@ -7,8 +7,40 @@ import ( ) type Notifier interface { + NewRepo(ctx context.Context, repo *db.Repo) + + NewStar(ctx context.Context, star *db.Star) + DeleteStar(ctx context.Context, star *db.Star) + NewIssue(ctx context.Context, issue *db.Issue) - NewIssueComment(ctx context.Context, comment db.Comment) + NewIssueComment(ctx context.Context, comment *db.Comment) + + NewFollow(ctx context.Context, follow *db.Follow) + DeleteFollow(ctx context.Context, follow *db.Follow) - NewPullComment(ctx context.Context, comment db.PullComment) + NewPull(ctx context.Context, pull *db.Pull) + NewPullComment(ctx context.Context, comment *db.PullComment) + + UpdateProfile(ctx context.Context, profile *db.Profile) } + +// BaseNotifier is a listener that does nothing +type BaseNotifier struct{} + +var _ Notifier = &BaseNotifier{} + +func (m *BaseNotifier) NewRepo(ctx context.Context, repo *db.Repo) {} + +func (m *BaseNotifier) NewStar(ctx context.Context, star *db.Star) {} +func (m *BaseNotifier) DeleteStar(ctx context.Context, star *db.Star) {} + +func (m *BaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) {} +func (m *BaseNotifier) NewIssueComment(ctx context.Context, comment *db.Comment) {} + +func (m *BaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) {} +func (m *BaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {} + +func (m *BaseNotifier) NewPull(ctx context.Context, pull *db.Pull) {} +func (m *BaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {} + +func (m *BaseNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) {} diff --git a/appview/pipelines/pipelines.go b/appview/pipelines/pipelines.go index 4073967..e650813 100644 --- a/appview/pipelines/pipelines.go +++ b/appview/pipelines/pipelines.go @@ -22,7 +22,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/gorilla/websocket" - "github.com/posthog/posthog-go" ) type Pipelines struct { @@ -34,7 +33,6 @@ type Pipelines struct { spindlestream *eventconsumer.Consumer db *db.DB enforcer *rbac.Enforcer - posthog posthog.Client logger *slog.Logger } @@ -46,7 +44,6 @@ func New( idResolver *idresolver.Resolver, db *db.DB, config *config.Config, - posthog posthog.Client, enforcer *rbac.Enforcer, ) *Pipelines { logger := log.New("pipelines") @@ -58,7 +55,6 @@ func New( config: config, spindlestream: spindlestream, db: db, - posthog: posthog, enforcer: enforcer, logger: logger, } diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index 907d2dc..64229d3 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -18,6 +18,7 @@ import ( "tangled.sh/tangled.sh/core/appview/config" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/idresolver" + "tangled.sh/tangled.sh/core/appview/notify" "tangled.sh/tangled.sh/core/appview/oauth" "tangled.sh/tangled.sh/core/appview/pages" "tangled.sh/tangled.sh/core/appview/reporesolver" @@ -31,7 +32,6 @@ import ( lexutil "github.com/bluesky-social/indigo/lex/util" "github.com/go-chi/chi/v5" "github.com/google/uuid" - "github.com/posthog/posthog-go" ) type Pulls struct { @@ -41,7 +41,7 @@ type Pulls struct { idResolver *idresolver.Resolver db *db.DB config *config.Config - posthog posthog.Client + notifier notify.Notifier } func New( @@ -51,7 +51,7 @@ func New( resolver *idresolver.Resolver, db *db.DB, config *config.Config, - posthog posthog.Client, + notifier notify.Notifier, ) *Pulls { return &Pulls{ oauth: oauth, @@ -60,7 +60,7 @@ func New( idResolver: resolver, db: db, config: config, - posthog: posthog, + notifier: notifier, } } @@ -689,15 +689,17 @@ func (s *Pulls) PullComment(w http.ResponseWriter, r *http.Request) { return } - // Create the pull comment in the database with the commentAt field - commentId, err := db.NewPullComment(tx, &db.PullComment{ + comment := &db.PullComment{ OwnerDid: user.Did, RepoAt: f.RepoAt.String(), PullId: pull.PullId, Body: body, CommentAt: atResp.Uri, SubmissionId: pull.Submissions[roundNumber].ID, - }) + } + + // Create the pull comment in the database with the commentAt field + commentId, err := db.NewPullComment(tx, comment) if err != nil { log.Println("failed to create pull comment", err) s.pages.Notice(w, "pull-comment", "Failed to create comment.") @@ -711,16 +713,7 @@ func (s *Pulls) PullComment(w http.ResponseWriter, r *http.Request) { return } - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: user.Did, - Event: "new_pull_comment", - Properties: posthog.Properties{"repo_at": f.RepoAt.String(), "pull_id": pull.PullId}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + s.notifier.NewPullComment(r.Context(), comment) s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d#comment-%d", f.OwnerSlashRepo(), pull.PullId, commentId)) return @@ -1054,7 +1047,7 @@ func (s *Pulls) createPullRequest( Patch: patch, SourceRev: sourceRev, } - err = db.NewPull(tx, &db.Pull{ + pull := &db.Pull{ Title: title, Body: body, TargetBranch: targetBranch, @@ -1065,7 +1058,8 @@ func (s *Pulls) createPullRequest( &initialSubmission, }, PullSource: pullSource, - }) + } + err = db.NewPull(tx, pull) if err != nil { log.Println("failed to create pull request", err) s.pages.Notice(w, "pull", "Failed to create pull request. Try again later.") @@ -1105,16 +1099,7 @@ func (s *Pulls) createPullRequest( return } - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: user.Did, - Event: "new_pull", - Properties: posthog.Properties{"repo_at": f.RepoAt.String(), "pull_id": pullId}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + s.notifier.NewPull(r.Context(), pull) s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pullId)) } diff --git a/appview/repo/repo.go b/appview/repo/repo.go index 31436b2..b06aef5 100644 --- a/appview/repo/repo.go +++ b/appview/repo/repo.go @@ -23,6 +23,7 @@ import ( "tangled.sh/tangled.sh/core/appview/config" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/idresolver" + "tangled.sh/tangled.sh/core/appview/notify" "tangled.sh/tangled.sh/core/appview/oauth" "tangled.sh/tangled.sh/core/appview/pages" "tangled.sh/tangled.sh/core/appview/pages/markup" @@ -36,7 +37,6 @@ import ( securejoin "github.com/cyphar/filepath-securejoin" "github.com/go-chi/chi/v5" "github.com/go-git/go-git/v5/plumbing" - "github.com/posthog/posthog-go" comatproto "github.com/bluesky-social/indigo/api/atproto" lexutil "github.com/bluesky-social/indigo/lex/util" @@ -51,7 +51,7 @@ type Repo struct { spindlestream *eventconsumer.Consumer db *db.DB enforcer *rbac.Enforcer - posthog posthog.Client + notifier notify.Notifier } func New( @@ -62,7 +62,7 @@ func New( idResolver *idresolver.Resolver, db *db.DB, config *config.Config, - posthog posthog.Client, + notifier notify.Notifier, enforcer *rbac.Enforcer, ) *Repo { return &Repo{oauth: oauth, @@ -72,7 +72,7 @@ func New( config: config, spindlestream: spindlestream, db: db, - posthog: posthog, + notifier: notifier, enforcer: enforcer, } } diff --git a/appview/state/follow.go b/appview/state/follow.go index 4c1a176..97941f6 100644 --- a/appview/state/follow.go +++ b/appview/state/follow.go @@ -7,7 +7,6 @@ import ( comatproto "github.com/bluesky-social/indigo/api/atproto" lexutil "github.com/bluesky-social/indigo/lex/util" - "github.com/posthog/posthog-go" "tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/appview" "tangled.sh/tangled.sh/core/appview/db" @@ -58,30 +57,27 @@ func (s *State) Follow(w http.ResponseWriter, r *http.Request) { return } - err = db.AddFollow(s.db, currentUser.Did, subjectIdent.DID.String(), rkey) + log.Println("created atproto record: ", resp.Uri) + + follow := &db.Follow{ + UserDid: currentUser.Did, + SubjectDid: subjectIdent.DID.String(), + Rkey: rkey, + } + + err = db.AddFollow(s.db, follow) if err != nil { log.Println("failed to follow", err) return } - log.Println("created atproto record: ", resp.Uri) + s.notifier.NewFollow(r.Context(), follow) s.pages.FollowFragment(w, pages.FollowFragmentParams{ UserDid: subjectIdent.DID.String(), FollowStatus: db.IsFollowing, }) - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: currentUser.Did, - Event: "follow", - Properties: posthog.Properties{"subject": subjectIdent.DID.String()}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } - return case http.MethodDelete: // find the record in the db @@ -113,16 +109,7 @@ func (s *State) Follow(w http.ResponseWriter, r *http.Request) { FollowStatus: db.IsNotFollowing, }) - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: currentUser.Did, - Event: "unfollow", - Properties: posthog.Properties{"subject": subjectIdent.DID.String()}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + s.notifier.DeleteFollow(r.Context(), follow) return } diff --git a/appview/state/posthog.go b/appview/state/posthog.go new file mode 100644 index 0000000..c171015 --- /dev/null +++ b/appview/state/posthog.go @@ -0,0 +1,131 @@ +package state + +import ( + "context" + "log" + + "github.com/posthog/posthog-go" + "tangled.sh/tangled.sh/core/appview/db" + "tangled.sh/tangled.sh/core/appview/notify" +) + +type PosthogNotifier struct { + client posthog.Client + notify.BaseNotifier +} + +func NewPosthogNotifier(client posthog.Client) notify.Notifier { + return &PosthogNotifier{ + client, + notify.BaseNotifier{}, + } +} + +var _ notify.Notifier = &PosthogNotifier{} + +func (n *PosthogNotifier) NewRepo(ctx context.Context, repo *db.Repo) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: repo.Did, + Event: "new_repo", + Properties: posthog.Properties{"repo": repo.Name, "repo_at": repo.AtUri}, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) NewStar(ctx context.Context, star *db.Star) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: star.StarredByDid, + Event: "star", + Properties: posthog.Properties{"repo_at": star.RepoAt.String()}, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) DeleteStar(ctx context.Context, star *db.Star) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: star.StarredByDid, + Event: "unstar", + Properties: posthog.Properties{"repo_at": star.RepoAt.String()}, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) NewIssue(ctx context.Context, issue *db.Issue) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: issue.OwnerDid, + Event: "new_issue", + Properties: posthog.Properties{ + "repo_at": issue.RepoAt.String(), + "issue_id": issue.IssueId, + }, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) NewPull(ctx context.Context, pull *db.Pull) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: pull.OwnerDid, + Event: "new_pull", + Properties: posthog.Properties{ + "repo_at": pull.RepoAt, + "pull_id": pull.PullId, + }, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: comment.OwnerDid, + Event: "new_pull_comment", + Properties: posthog.Properties{ + "repo_at": comment.RepoAt, + "pull_id": comment.PullId, + }, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) NewFollow(ctx context.Context, follow *db.Follow) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: follow.UserDid, + Event: "follow", + Properties: posthog.Properties{"subject": follow.SubjectDid}, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: follow.UserDid, + Event: "unfollow", + Properties: posthog.Properties{"subject": follow.SubjectDid}, + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} + +func (n *PosthogNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) { + err := n.client.Enqueue(posthog.Capture{ + DistinctId: profile.Did, + Event: "edit_profile", + }) + if err != nil { + log.Println("failed to enqueue posthog event:", err) + } +} diff --git a/appview/state/profile.go b/appview/state/profile.go index 66f28b1..ed480e3 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -16,7 +16,6 @@ import ( "github.com/bluesky-social/indigo/atproto/syntax" lexutil "github.com/bluesky-social/indigo/lex/util" "github.com/go-chi/chi/v5" - "github.com/posthog/posthog-go" "tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/pages" @@ -369,15 +368,7 @@ func (s *State) updateProfile(profile *db.Profile, w http.ResponseWriter, r *htt return } - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: user.Did, - Event: "edit_profile", - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + s.notifier.UpdateProfile(r.Context(), profile) s.pages.HxRedirect(w, "/"+user.Did) return diff --git a/appview/state/router.go b/appview/state/router.go index 826c3be..d164a0b 100644 --- a/appview/state/router.go +++ b/appview/state/router.go @@ -198,21 +198,21 @@ func (s *State) KnotsRouter(mw *middleware.Middleware) http.Handler { } func (s *State) IssuesRouter(mw *middleware.Middleware) http.Handler { - issues := issues.New(s.oauth, s.repoResolver, s.pages, s.idResolver, s.db, s.config, s.posthog) + issues := issues.New(s.oauth, s.repoResolver, s.pages, s.idResolver, s.db, s.config, s.notifier) return issues.Router(mw) } func (s *State) PullsRouter(mw *middleware.Middleware) http.Handler { - pulls := pulls.New(s.oauth, s.repoResolver, s.pages, s.idResolver, s.db, s.config, s.posthog) + pulls := pulls.New(s.oauth, s.repoResolver, s.pages, s.idResolver, s.db, s.config, s.notifier) return pulls.Router(mw) } func (s *State) RepoRouter(mw *middleware.Middleware) http.Handler { - repo := repo.New(s.oauth, s.repoResolver, s.pages, s.spindlestream, s.idResolver, s.db, s.config, s.posthog, s.enforcer) + repo := repo.New(s.oauth, s.repoResolver, s.pages, s.spindlestream, s.idResolver, s.db, s.config, s.notifier, s.enforcer) return repo.Router(mw) } func (s *State) PipelinesRouter(mw *middleware.Middleware) http.Handler { - pipes := pipelines.New(s.oauth, s.repoResolver, s.pages, s.spindlestream, s.idResolver, s.db, s.config, s.posthog, s.enforcer) + pipes := pipelines.New(s.oauth, s.repoResolver, s.pages, s.spindlestream, s.idResolver, s.db, s.config, s.enforcer) return pipes.Router(mw) } diff --git a/appview/state/star.go b/appview/state/star.go index 8a2b3a8..561de8c 100644 --- a/appview/state/star.go +++ b/appview/state/star.go @@ -8,7 +8,6 @@ import ( comatproto "github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/atproto/syntax" lexutil "github.com/bluesky-social/indigo/lex/util" - "github.com/posthog/posthog-go" "tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/appview" "tangled.sh/tangled.sh/core/appview/db" @@ -54,8 +53,15 @@ func (s *State) Star(w http.ResponseWriter, r *http.Request) { log.Println("failed to create atproto record", err) return } + log.Println("created atproto record: ", resp.Uri) + + star := &db.Star{ + StarredByDid: currentUser.Did, + RepoAt: subjectUri, + Rkey: rkey, + } - err = db.AddStar(s.db, currentUser.Did, subjectUri, rkey) + err = db.AddStar(s.db, star) if err != nil { log.Println("failed to star", err) return @@ -66,7 +72,7 @@ func (s *State) Star(w http.ResponseWriter, r *http.Request) { log.Println("failed to get star count for ", subjectUri) } - log.Println("created atproto record: ", resp.Uri) + s.notifier.NewStar(r.Context(), star) s.pages.RepoActionsFragment(w, pages.RepoActionsFragmentParams{ IsStarred: true, @@ -76,17 +82,6 @@ func (s *State) Star(w http.ResponseWriter, r *http.Request) { }, }) - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: currentUser.Did, - Event: "star", - Properties: posthog.Properties{"repo_at": subjectUri.String()}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } - return case http.MethodDelete: // find the record in the db @@ -119,6 +114,8 @@ func (s *State) Star(w http.ResponseWriter, r *http.Request) { return } + s.notifier.DeleteStar(r.Context(), star) + s.pages.RepoActionsFragment(w, pages.RepoActionsFragmentParams{ IsStarred: false, RepoAt: subjectUri, @@ -127,17 +124,6 @@ func (s *State) Star(w http.ResponseWriter, r *http.Request) { }, }) - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: currentUser.Did, - Event: "unstar", - Properties: posthog.Properties{"repo_at": subjectUri.String()}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } - return } diff --git a/appview/state/state.go b/appview/state/state.go index da4f7e8..62e6ea7 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -134,6 +134,7 @@ func Make(ctx context.Context, config *config.Config) (*State, error) { spindlestream.Start(ctx) notifier := notify.NewMergedNotifier( + NewPosthogNotifier(posthog), ) state := &State{ @@ -766,16 +767,7 @@ func (s *State) NewRepo(w http.ResponseWriter, r *http.Request) { return } - if !s.config.Core.Dev { - err = s.posthog.Enqueue(posthog.Capture{ - DistinctId: user.Did, - Event: "new_repo", - Properties: posthog.Properties{"repo": repoName, "repo_at": repo.AtUri}, - }) - if err != nil { - log.Println("failed to enqueue posthog event:", err) - } - } + s.notifier.NewRepo(r.Context(), repo) s.pages.HxLocation(w, fmt.Sprintf("/@%s/%s", user.Handle, repoName)) return -- 2.43.0