diff --git a/appview/auth/auth.go b/appview/auth/auth.go index 0c3df86..f935ff8 100644 --- a/appview/auth/auth.go +++ b/appview/auth/auth.go @@ -140,7 +140,7 @@ func (a *Auth) StoreSession(r *http.Request, w http.ResponseWriter, atSessionish clientSession.Values[appview.SessionPds] = pdsEndpoint clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt() clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt() - clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Hour).Format(time.RFC3339) + clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Minute * 15).Format(time.RFC3339) clientSession.Values[appview.SessionAuthenticated] = true return clientSession.Save(r, w) } diff --git a/appview/state/follow.go b/appview/state/follow.go new file mode 100644 index 0000000..ccd1799 --- /dev/null +++ b/appview/state/follow.go @@ -0,0 +1,112 @@ +package state + +import ( + "fmt" + "log" + "net/http" + "time" + + comatproto "github.com/bluesky-social/indigo/api/atproto" + lexutil "github.com/bluesky-social/indigo/lex/util" + tangled "github.com/sotangled/tangled/api/tangled" +) + +func (s *State) Follow(w http.ResponseWriter, r *http.Request) { + currentUser := s.auth.GetUser(r) + + subject := r.URL.Query().Get("subject") + if subject == "" { + log.Println("invalid form") + return + } + + subjectIdent, err := s.resolver.ResolveIdent(r.Context(), subject) + if err != nil { + log.Println("failed to follow, invalid did") + } + + if currentUser.Did == subjectIdent.DID.String() { + log.Println("cant follow or unfollow yourself") + return + } + + client, _ := s.auth.AuthorizedClient(r) + + switch r.Method { + case http.MethodPost: + createdAt := time.Now().Format(time.RFC3339) + rkey := s.TID() + resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ + Collection: tangled.GraphFollowNSID, + Repo: currentUser.Did, + Rkey: rkey, + Record: &lexutil.LexiconTypeDecoder{ + Val: &tangled.GraphFollow{ + Subject: subjectIdent.DID.String(), + CreatedAt: createdAt, + }}, + }) + if err != nil { + log.Println("failed to create atproto record", err) + return + } + + err = s.db.AddFollow(currentUser.Did, subjectIdent.DID.String(), rkey) + if err != nil { + log.Println("failed to follow", err) + return + } + + log.Println("created atproto record: ", resp.Uri) + + w.Write([]byte(fmt.Sprintf(` + + `, subjectIdent.DID.String()))) + + return + case http.MethodDelete: + // find the record in the db + follow, err := s.db.GetFollow(currentUser.Did, subjectIdent.DID.String()) + if err != nil { + log.Println("failed to get follow relationship") + return + } + + _, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{ + Collection: tangled.GraphFollowNSID, + Repo: currentUser.Did, + Rkey: follow.RKey, + }) + + if err != nil { + log.Println("failed to unfollow") + return + } + + err = s.db.DeleteFollow(currentUser.Did, subjectIdent.DID.String()) + if err != nil { + log.Println("failed to delete follow from DB") + // this is not an issue, the firehose event might have already done this + } + + w.Write([]byte(fmt.Sprintf(` + + `, subjectIdent.DID.String()))) + return + } + +} diff --git a/appview/state/middleware.go b/appview/state/middleware.go index 226bacf..61187ff 100644 --- a/appview/state/middleware.go +++ b/appview/state/middleware.go @@ -54,7 +54,8 @@ func AuthMiddleware(s *State) Middleware { } atSession, err := comatproto.ServerRefreshSession(r.Context(), &client) if err != nil { - log.Println(err) + log.Println("failed to refresh session", err) + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) return } diff --git a/appview/state/state.go b/appview/state/state.go index 8e61214..a8fdc8d 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -682,106 +682,6 @@ func (s *State) ProfilePage(w http.ResponseWriter, r *http.Request) { }) } -func (s *State) Follow(w http.ResponseWriter, r *http.Request) { - currentUser := s.auth.GetUser(r) - - subject := r.URL.Query().Get("subject") - if subject == "" { - log.Println("invalid form") - return - } - - subjectIdent, err := s.resolver.ResolveIdent(r.Context(), subject) - if err != nil { - log.Println("failed to follow, invalid did") - } - - if currentUser.Did == subjectIdent.DID.String() { - log.Println("cant follow or unfollow yourself") - return - } - - client, _ := s.auth.AuthorizedClient(r) - - switch r.Method { - case http.MethodPost: - createdAt := time.Now().Format(time.RFC3339) - rkey := s.TID() - resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ - Collection: tangled.GraphFollowNSID, - Repo: currentUser.Did, - Rkey: rkey, - Record: &lexutil.LexiconTypeDecoder{ - Val: &tangled.GraphFollow{ - Subject: subjectIdent.DID.String(), - CreatedAt: createdAt, - }}, - }) - if err != nil { - log.Println("failed to create atproto record", err) - return - } - - err = s.db.AddFollow(currentUser.Did, subjectIdent.DID.String(), rkey) - if err != nil { - log.Println("failed to follow", err) - return - } - - log.Println("created atproto record: ", resp.Uri) - - w.Write([]byte(fmt.Sprintf(` - - `, subjectIdent.DID.String()))) - - return - case http.MethodDelete: - // find the record in the db - follow, err := s.db.GetFollow(currentUser.Did, subjectIdent.DID.String()) - if err != nil { - log.Println("failed to get follow relationship") - return - } - - _, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{ - Collection: tangled.GraphFollowNSID, - Repo: currentUser.Did, - Rkey: follow.RKey, - }) - - if err != nil { - log.Println("failed to unfollow") - return - } - - err = s.db.DeleteFollow(currentUser.Did, subjectIdent.DID.String()) - if err != nil { - log.Println("failed to delete follow from DB") - // this is not an issue, the firehose event might have already done this - } - - w.Write([]byte(fmt.Sprintf(` - - `, subjectIdent.DID.String()))) - return - } - -} - func (s *State) Router() http.Handler { router := chi.NewRouter() @@ -861,8 +761,10 @@ func (s *State) StandardRouter() http.Handler { r.Get("/logout", s.Logout) - r.Get("/login", s.Login) - r.Post("/login", s.Login) + r.Route("/login", func(r chi.Router) { + r.Get("/", s.Login) + r.Post("/", s.Login) + }) r.Route("/knots", func(r chi.Router) { r.Use(AuthMiddleware(s))