forked from tangled.org/core
this repo has no description
at master 2.8 kB view raw
1package state 2 3import ( 4 "log" 5 "net/http" 6 "time" 7 8 comatproto "github.com/bluesky-social/indigo/api/atproto" 9 lexutil "github.com/bluesky-social/indigo/lex/util" 10 "tangled.org/core/api/tangled" 11 "tangled.org/core/appview/db" 12 "tangled.org/core/appview/models" 13 "tangled.org/core/appview/pages" 14 "tangled.org/core/tid" 15) 16 17func (s *State) Follow(w http.ResponseWriter, r *http.Request) { 18 currentUser := s.oauth.GetUser(r) 19 20 subject := r.URL.Query().Get("subject") 21 if subject == "" { 22 log.Println("invalid form") 23 return 24 } 25 26 subjectIdent, err := s.idResolver.ResolveIdent(r.Context(), subject) 27 if err != nil { 28 log.Println("failed to follow, invalid did") 29 } 30 31 if currentUser.Did == subjectIdent.DID.String() { 32 log.Println("cant follow or unfollow yourself") 33 return 34 } 35 36 client, err := s.oauth.AuthorizedClient(r) 37 if err != nil { 38 log.Println("failed to authorize client") 39 return 40 } 41 42 switch r.Method { 43 case http.MethodPost: 44 createdAt := time.Now().Format(time.RFC3339) 45 rkey := tid.TID() 46 resp, err := client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 47 Collection: tangled.GraphFollowNSID, 48 Repo: currentUser.Did, 49 Rkey: rkey, 50 Record: &lexutil.LexiconTypeDecoder{ 51 Val: &tangled.GraphFollow{ 52 Subject: subjectIdent.DID.String(), 53 CreatedAt: createdAt, 54 }}, 55 }) 56 if err != nil { 57 log.Println("failed to create atproto record", err) 58 return 59 } 60 61 log.Println("created atproto record: ", resp.Uri) 62 63 follow := &models.Follow{ 64 UserDid: currentUser.Did, 65 SubjectDid: subjectIdent.DID.String(), 66 Rkey: rkey, 67 } 68 69 err = db.AddFollow(s.db, follow) 70 if err != nil { 71 log.Println("failed to follow", err) 72 return 73 } 74 75 s.notifier.NewFollow(r.Context(), follow) 76 77 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 78 UserDid: subjectIdent.DID.String(), 79 FollowStatus: models.IsFollowing, 80 }) 81 82 return 83 case http.MethodDelete: 84 // find the record in the db 85 follow, err := db.GetFollow(s.db, currentUser.Did, subjectIdent.DID.String()) 86 if err != nil { 87 log.Println("failed to get follow relationship") 88 return 89 } 90 91 _, err = client.RepoDeleteRecord(r.Context(), &comatproto.RepoDeleteRecord_Input{ 92 Collection: tangled.GraphFollowNSID, 93 Repo: currentUser.Did, 94 Rkey: follow.Rkey, 95 }) 96 97 if err != nil { 98 log.Println("failed to unfollow") 99 return 100 } 101 102 err = db.DeleteFollowByRkey(s.db, currentUser.Did, follow.Rkey) 103 if err != nil { 104 log.Println("failed to delete follow from DB") 105 // this is not an issue, the firehose event might have already done this 106 } 107 108 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 109 UserDid: subjectIdent.DID.String(), 110 FollowStatus: models.IsNotFollowing, 111 }) 112 113 s.notifier.DeleteFollow(r.Context(), follow) 114 115 return 116 } 117 118}