Yōten: A social tracker for your language learning journey built on the atproto.

feat: pass posthog through oauth flow

brookjeynes.dev f226f8fd d3a43223

verified
+36 -26
+7 -7
internal/server/app.go
··· 52 52 return nil, err 53 53 } 54 54 55 - oauth, err := oauth.New(config) 55 + posthog, err := posthog.NewWithConfig(config.Posthog.ApiKey, posthog.Config{Endpoint: config.Posthog.Endpoint}) 56 56 if err != nil { 57 - return nil, fmt.Errorf("failed to start oauth handler: %w", err) 57 + return nil, fmt.Errorf("failed to create posthog client: %w", err) 58 58 } 59 59 60 60 idResolver := atproto.DefaultResolver() 61 61 62 - cache := cache.New(config.Redis.Addr) 63 - sess := session.New(cache) 64 - 65 - posthog, err := posthog.NewWithConfig(config.Posthog.ApiKey, posthog.Config{Endpoint: config.Posthog.Endpoint}) 62 + oauth, err := oauth.New(config, posthog) 66 63 if err != nil { 67 - return nil, fmt.Errorf("failed to create posthog client: %w", err) 64 + return nil, fmt.Errorf("failed to start oauth handler: %w", err) 68 65 } 66 + 67 + cache := cache.New(config.Redis.Addr) 68 + sess := session.New(cache) 69 69 70 70 wrapper := db.DbWrapper{Execer: d} 71 71 jc, err := consumer.NewJetstreamClient(
+11 -17
internal/server/handlers/login.go
··· 1 1 package handlers 2 2 3 3 import ( 4 + "context" 4 5 "fmt" 5 6 "log" 6 7 "net/http" ··· 58 59 return 59 60 } 60 61 61 - if !h.Config.Core.Dev { 62 - err := h.Posthog.Enqueue(posthog.Capture{ 63 - DistinctId: handle, 64 - Event: ph.UserSignInInitiatedEvent, 65 - }) 66 - if err != nil { 67 - log.Println("failed to enqueue posthog event:", err) 62 + resolved, err := h.IdResolver.ResolveIdent(context.Background(), handle) 63 + if err == nil { 64 + if !h.Config.Core.Dev && resolved.DID.String() != "" { 65 + err := h.Posthog.Enqueue(posthog.Capture{ 66 + DistinctId: string(resolved.DID), 67 + Event: ph.UserSignInInitiatedEvent, 68 + }) 69 + if err != nil { 70 + log.Println("failed to enqueue posthog event:", err) 71 + } 68 72 } 69 73 } 70 74 ··· 72 76 if err != nil { 73 77 http.Error(w, err.Error(), http.StatusInternalServerError) 74 78 return 75 - } 76 - 77 - if !h.Config.Core.Dev { 78 - err := h.Posthog.Enqueue(posthog.Capture{ 79 - DistinctId: handle, 80 - Event: ph.UserSignInSuccessEvent, 81 - }) 82 - if err != nil { 83 - log.Println("failed to enqueue posthog event:", err) 84 - } 85 79 } 86 80 87 81 htmx.HxRedirect(w, redirectURL)
+13
internal/server/oauth/handler.go
··· 7 7 8 8 "github.com/go-chi/chi/v5" 9 9 "github.com/lestrrat-go/jwx/v2/jwk" 10 + "github.com/posthog/posthog-go" 11 + 12 + ph "yoten.app/internal/clients/posthog" 10 13 ) 11 14 12 15 func (o *OAuth) Router() http.Handler { ··· 72 75 if err := o.SaveSession(w, r, sessData); err != nil { 73 76 http.Error(w, err.Error(), http.StatusInternalServerError) 74 77 return 78 + } 79 + 80 + if !o.Config.Core.Dev { 81 + err = o.Posthog.Enqueue(posthog.Capture{ 82 + DistinctId: sessData.AccountDID.String(), 83 + Event: ph.UserSignInSuccessEvent, 84 + }) 85 + if err != nil { 86 + log.Println("failed to enqueue posthog event:", err) 87 + } 75 88 } 76 89 77 90 http.Redirect(w, r, "/", http.StatusFound)
+4 -1
internal/server/oauth/oauth.go
··· 12 12 "github.com/bluesky-social/indigo/atproto/syntax" 13 13 xrpc "github.com/bluesky-social/indigo/xrpc" 14 14 "github.com/gorilla/sessions" 15 + "github.com/posthog/posthog-go" 15 16 16 17 "yoten.app/internal/server/config" 17 18 "yoten.app/internal/types" ··· 22 23 SessionStore *sessions.CookieStore 23 24 Config *config.Config 24 25 JwksUri string 26 + Posthog posthog.Client 25 27 } 26 28 27 - func New(config *config.Config) (*OAuth, error) { 29 + func New(config *config.Config, ph posthog.Client) (*OAuth, error) { 28 30 var oauthConfig oauth.ClientConfig 29 31 var clientUri string 30 32 ··· 53 55 Config: config, 54 56 SessionStore: sessStore, 55 57 JwksUri: jwksUri, 58 + Posthog: ph, 56 59 }, nil 57 60 58 61 }
+1 -1
internal/server/views/partials/notification.templ
··· 48 48 <a class="hover:underline" href={ templ.SafeURL("/" + params.Notification.ActorDid) }> 49 49 &commat;{ params.Notification.ActorBskyHandle } 50 50 </a> replied to your study session 51 - // TODO: Link to comment. 51 + // TODO: Link to comment. 52 52 // <a class="hover:underline" href={ templ.SafeURL("/" + params.Notification.SubjectDid + "/comment/" + params.Notification.SubjectRkey) }> 53 53 // comment 54 54 // </a>