tangled
alpha
login
or
join now
yoten.app
/
yoten
17
fork
atom
Yōten: A social tracker for your language learning journey built on the atproto.
17
fork
atom
overview
issues
pulls
pipelines
feat: pass posthog through oauth flow
brookjeynes.dev
5 months ago
f226f8fd
d3a43223
verified
This commit was signed with the committer's
known signature
.
brookjeynes.dev
SSH Key Fingerprint:
SHA256:N3n3PCBSiXfS6EHlmGdx+LMEruJMj6FS2hqaXyfsw0s=
+36
-26
5 changed files
expand all
collapse all
unified
split
internal
server
app.go
handlers
login.go
oauth
handler.go
oauth.go
views
partials
notification.templ
+7
-7
internal/server/app.go
···
52
52
return nil, err
53
53
}
54
54
55
55
-
oauth, err := oauth.New(config)
55
55
+
posthog, err := posthog.NewWithConfig(config.Posthog.ApiKey, posthog.Config{Endpoint: config.Posthog.Endpoint})
56
56
if err != nil {
57
57
-
return nil, fmt.Errorf("failed to start oauth handler: %w", err)
57
57
+
return nil, fmt.Errorf("failed to create posthog client: %w", err)
58
58
}
59
59
60
60
idResolver := atproto.DefaultResolver()
61
61
62
62
-
cache := cache.New(config.Redis.Addr)
63
63
-
sess := session.New(cache)
64
64
-
65
65
-
posthog, err := posthog.NewWithConfig(config.Posthog.ApiKey, posthog.Config{Endpoint: config.Posthog.Endpoint})
62
62
+
oauth, err := oauth.New(config, posthog)
66
63
if err != nil {
67
67
-
return nil, fmt.Errorf("failed to create posthog client: %w", err)
64
64
+
return nil, fmt.Errorf("failed to start oauth handler: %w", err)
68
65
}
66
66
+
67
67
+
cache := cache.New(config.Redis.Addr)
68
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
4
+
"context"
4
5
"fmt"
5
6
"log"
6
7
"net/http"
···
58
59
return
59
60
}
60
61
61
61
-
if !h.Config.Core.Dev {
62
62
-
err := h.Posthog.Enqueue(posthog.Capture{
63
63
-
DistinctId: handle,
64
64
-
Event: ph.UserSignInInitiatedEvent,
65
65
-
})
66
66
-
if err != nil {
67
67
-
log.Println("failed to enqueue posthog event:", err)
62
62
+
resolved, err := h.IdResolver.ResolveIdent(context.Background(), handle)
63
63
+
if err == nil {
64
64
+
if !h.Config.Core.Dev && resolved.DID.String() != "" {
65
65
+
err := h.Posthog.Enqueue(posthog.Capture{
66
66
+
DistinctId: string(resolved.DID),
67
67
+
Event: ph.UserSignInInitiatedEvent,
68
68
+
})
69
69
+
if err != nil {
70
70
+
log.Println("failed to enqueue posthog event:", err)
71
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
75
-
}
76
76
-
77
77
-
if !h.Config.Core.Dev {
78
78
-
err := h.Posthog.Enqueue(posthog.Capture{
79
79
-
DistinctId: handle,
80
80
-
Event: ph.UserSignInSuccessEvent,
81
81
-
})
82
82
-
if err != nil {
83
83
-
log.Println("failed to enqueue posthog event:", err)
84
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
10
+
"github.com/posthog/posthog-go"
11
11
+
12
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
78
+
}
79
79
+
80
80
+
if !o.Config.Core.Dev {
81
81
+
err = o.Posthog.Enqueue(posthog.Capture{
82
82
+
DistinctId: sessData.AccountDID.String(),
83
83
+
Event: ph.UserSignInSuccessEvent,
84
84
+
})
85
85
+
if err != nil {
86
86
+
log.Println("failed to enqueue posthog event:", err)
87
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
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
26
+
Posthog posthog.Client
25
27
}
26
28
27
27
-
func New(config *config.Config) (*OAuth, error) {
29
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
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
@{ params.Notification.ActorBskyHandle }
50
50
</a> replied to your study session
51
51
-
// TODO: Link to comment.
51
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>