tangled
alpha
login
or
join now
moll.dev
/
core
forked from
tangled.org/core
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
attempt to fix 400 from token refresh
oppi.li
11 months ago
985f3926
ec0558e5
+119
-104
4 changed files
expand all
collapse all
unified
split
appview
auth
auth.go
state
follow.go
middleware.go
state.go
+1
-1
appview/auth/auth.go
···
140
140
clientSession.Values[appview.SessionPds] = pdsEndpoint
141
141
clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt()
142
142
clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt()
143
143
-
clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Hour).Format(time.RFC3339)
143
143
+
clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Minute * 15).Format(time.RFC3339)
144
144
clientSession.Values[appview.SessionAuthenticated] = true
145
145
return clientSession.Save(r, w)
146
146
}
+112
appview/state/follow.go
···
1
1
+
package state
2
2
+
3
3
+
import (
4
4
+
"fmt"
5
5
+
"log"
6
6
+
"net/http"
7
7
+
"time"
8
8
+
9
9
+
comatproto "github.com/bluesky-social/indigo/api/atproto"
10
10
+
lexutil "github.com/bluesky-social/indigo/lex/util"
11
11
+
tangled "github.com/sotangled/tangled/api/tangled"
12
12
+
)
13
13
+
14
14
+
func (s *State) Follow(w http.ResponseWriter, r *http.Request) {
15
15
+
currentUser := s.auth.GetUser(r)
16
16
+
17
17
+
subject := r.URL.Query().Get("subject")
18
18
+
if subject == "" {
19
19
+
log.Println("invalid form")
20
20
+
return
21
21
+
}
22
22
+
23
23
+
subjectIdent, err := s.resolver.ResolveIdent(r.Context(), subject)
24
24
+
if err != nil {
25
25
+
log.Println("failed to follow, invalid did")
26
26
+
}
27
27
+
28
28
+
if currentUser.Did == subjectIdent.DID.String() {
29
29
+
log.Println("cant follow or unfollow yourself")
30
30
+
return
31
31
+
}
32
32
+
33
33
+
client, _ := s.auth.AuthorizedClient(r)
34
34
+
35
35
+
switch r.Method {
36
36
+
case http.MethodPost:
37
37
+
createdAt := time.Now().Format(time.RFC3339)
38
38
+
rkey := s.TID()
39
39
+
resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{
40
40
+
Collection: tangled.GraphFollowNSID,
41
41
+
Repo: currentUser.Did,
42
42
+
Rkey: rkey,
43
43
+
Record: &lexutil.LexiconTypeDecoder{
44
44
+
Val: &tangled.GraphFollow{
45
45
+
Subject: subjectIdent.DID.String(),
46
46
+
CreatedAt: createdAt,
47
47
+
}},
48
48
+
})
49
49
+
if err != nil {
50
50
+
log.Println("failed to create atproto record", err)
51
51
+
return
52
52
+
}
53
53
+
54
54
+
err = s.db.AddFollow(currentUser.Did, subjectIdent.DID.String(), rkey)
55
55
+
if err != nil {
56
56
+
log.Println("failed to follow", err)
57
57
+
return
58
58
+
}
59
59
+
60
60
+
log.Println("created atproto record: ", resp.Uri)
61
61
+
62
62
+
w.Write([]byte(fmt.Sprintf(`
63
63
+
<button id="followBtn"
64
64
+
class="btn mt-2"
65
65
+
hx-delete="/follow?subject=%s"
66
66
+
hx-trigger="click"
67
67
+
hx-target="#followBtn"
68
68
+
hx-swap="outerHTML">
69
69
+
Unfollow
70
70
+
</button>
71
71
+
`, subjectIdent.DID.String())))
72
72
+
73
73
+
return
74
74
+
case http.MethodDelete:
75
75
+
// find the record in the db
76
76
+
follow, err := s.db.GetFollow(currentUser.Did, subjectIdent.DID.String())
77
77
+
if err != nil {
78
78
+
log.Println("failed to get follow relationship")
79
79
+
return
80
80
+
}
81
81
+
82
82
+
_, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{
83
83
+
Collection: tangled.GraphFollowNSID,
84
84
+
Repo: currentUser.Did,
85
85
+
Rkey: follow.RKey,
86
86
+
})
87
87
+
88
88
+
if err != nil {
89
89
+
log.Println("failed to unfollow")
90
90
+
return
91
91
+
}
92
92
+
93
93
+
err = s.db.DeleteFollow(currentUser.Did, subjectIdent.DID.String())
94
94
+
if err != nil {
95
95
+
log.Println("failed to delete follow from DB")
96
96
+
// this is not an issue, the firehose event might have already done this
97
97
+
}
98
98
+
99
99
+
w.Write([]byte(fmt.Sprintf(`
100
100
+
<button id="followBtn"
101
101
+
class="btn mt-2"
102
102
+
hx-post="/follow?subject=%s"
103
103
+
hx-trigger="click"
104
104
+
hx-target="#followBtn"
105
105
+
hx-swap="outerHTML">
106
106
+
Follow
107
107
+
</button>
108
108
+
`, subjectIdent.DID.String())))
109
109
+
return
110
110
+
}
111
111
+
112
112
+
}
+2
-1
appview/state/middleware.go
···
54
54
}
55
55
atSession, err := comatproto.ServerRefreshSession(r.Context(), &client)
56
56
if err != nil {
57
57
-
log.Println(err)
57
57
+
log.Println("failed to refresh session", err)
58
58
+
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
58
59
return
59
60
}
60
61
+4
-102
appview/state/state.go
···
682
682
})
683
683
}
684
684
685
685
-
func (s *State) Follow(w http.ResponseWriter, r *http.Request) {
686
686
-
currentUser := s.auth.GetUser(r)
687
687
-
688
688
-
subject := r.URL.Query().Get("subject")
689
689
-
if subject == "" {
690
690
-
log.Println("invalid form")
691
691
-
return
692
692
-
}
693
693
-
694
694
-
subjectIdent, err := s.resolver.ResolveIdent(r.Context(), subject)
695
695
-
if err != nil {
696
696
-
log.Println("failed to follow, invalid did")
697
697
-
}
698
698
-
699
699
-
if currentUser.Did == subjectIdent.DID.String() {
700
700
-
log.Println("cant follow or unfollow yourself")
701
701
-
return
702
702
-
}
703
703
-
704
704
-
client, _ := s.auth.AuthorizedClient(r)
705
705
-
706
706
-
switch r.Method {
707
707
-
case http.MethodPost:
708
708
-
createdAt := time.Now().Format(time.RFC3339)
709
709
-
rkey := s.TID()
710
710
-
resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{
711
711
-
Collection: tangled.GraphFollowNSID,
712
712
-
Repo: currentUser.Did,
713
713
-
Rkey: rkey,
714
714
-
Record: &lexutil.LexiconTypeDecoder{
715
715
-
Val: &tangled.GraphFollow{
716
716
-
Subject: subjectIdent.DID.String(),
717
717
-
CreatedAt: createdAt,
718
718
-
}},
719
719
-
})
720
720
-
if err != nil {
721
721
-
log.Println("failed to create atproto record", err)
722
722
-
return
723
723
-
}
724
724
-
725
725
-
err = s.db.AddFollow(currentUser.Did, subjectIdent.DID.String(), rkey)
726
726
-
if err != nil {
727
727
-
log.Println("failed to follow", err)
728
728
-
return
729
729
-
}
730
730
-
731
731
-
log.Println("created atproto record: ", resp.Uri)
732
732
-
733
733
-
w.Write([]byte(fmt.Sprintf(`
734
734
-
<button id="followBtn"
735
735
-
class="btn mt-2"
736
736
-
hx-delete="/follow?subject=%s"
737
737
-
hx-trigger="click"
738
738
-
hx-target="#followBtn"
739
739
-
hx-swap="outerHTML">
740
740
-
Unfollow
741
741
-
</button>
742
742
-
`, subjectIdent.DID.String())))
743
743
-
744
744
-
return
745
745
-
case http.MethodDelete:
746
746
-
// find the record in the db
747
747
-
follow, err := s.db.GetFollow(currentUser.Did, subjectIdent.DID.String())
748
748
-
if err != nil {
749
749
-
log.Println("failed to get follow relationship")
750
750
-
return
751
751
-
}
752
752
-
753
753
-
_, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{
754
754
-
Collection: tangled.GraphFollowNSID,
755
755
-
Repo: currentUser.Did,
756
756
-
Rkey: follow.RKey,
757
757
-
})
758
758
-
759
759
-
if err != nil {
760
760
-
log.Println("failed to unfollow")
761
761
-
return
762
762
-
}
763
763
-
764
764
-
err = s.db.DeleteFollow(currentUser.Did, subjectIdent.DID.String())
765
765
-
if err != nil {
766
766
-
log.Println("failed to delete follow from DB")
767
767
-
// this is not an issue, the firehose event might have already done this
768
768
-
}
769
769
-
770
770
-
w.Write([]byte(fmt.Sprintf(`
771
771
-
<button id="followBtn"
772
772
-
class="btn mt-2"
773
773
-
hx-post="/follow?subject=%s"
774
774
-
hx-trigger="click"
775
775
-
hx-target="#followBtn"
776
776
-
hx-swap="outerHTML">
777
777
-
Follow
778
778
-
</button>
779
779
-
`, subjectIdent.DID.String())))
780
780
-
return
781
781
-
}
782
782
-
783
783
-
}
784
784
-
785
685
func (s *State) Router() http.Handler {
786
686
router := chi.NewRouter()
787
687
···
861
761
862
762
r.Get("/logout", s.Logout)
863
763
864
864
-
r.Get("/login", s.Login)
865
865
-
r.Post("/login", s.Login)
764
764
+
r.Route("/login", func(r chi.Router) {
765
765
+
r.Get("/", s.Login)
766
766
+
r.Post("/", s.Login)
767
767
+
})
866
768
867
769
r.Route("/knots", func(r chi.Router) {
868
770
r.Use(AuthMiddleware(s))