forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
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 return 30 } 31 32 if currentUser.Did == subjectIdent.DID.String() { 33 log.Println("cant follow or unfollow yourself") 34 return 35 } 36 37 client, err := s.oauth.AuthorizedClient(r) 38 if err != nil { 39 log.Println("failed to authorize client") 40 return 41 } 42 43 switch r.Method { 44 case http.MethodPost: 45 createdAt := time.Now().Format(time.RFC3339) 46 rkey := tid.TID() 47 resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 48 Collection: tangled.GraphFollowNSID, 49 Repo: currentUser.Did, 50 Rkey: rkey, 51 Record: &lexutil.LexiconTypeDecoder{ 52 Val: &tangled.GraphFollow{ 53 Subject: subjectIdent.DID.String(), 54 CreatedAt: createdAt, 55 }}, 56 }) 57 if err != nil { 58 log.Println("failed to create atproto record", err) 59 return 60 } 61 62 log.Println("created atproto record: ", resp.Uri) 63 64 follow := &models.Follow{ 65 UserDid: currentUser.Did, 66 SubjectDid: subjectIdent.DID.String(), 67 Rkey: rkey, 68 } 69 70 err = db.AddFollow(s.db, follow) 71 if err != nil { 72 log.Println("failed to follow", err) 73 return 74 } 75 76 s.notifier.NewFollow(r.Context(), follow) 77 78 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 79 UserDid: subjectIdent.DID.String(), 80 FollowStatus: models.IsFollowing, 81 }) 82 83 return 84 case http.MethodDelete: 85 // find the record in the db 86 follow, err := db.GetFollow(s.db, currentUser.Did, subjectIdent.DID.String()) 87 if err != nil { 88 log.Println("failed to get follow relationship") 89 return 90 } 91 92 _, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{ 93 Collection: tangled.GraphFollowNSID, 94 Repo: currentUser.Did, 95 Rkey: follow.Rkey, 96 }) 97 98 if err != nil { 99 log.Println("failed to unfollow") 100 return 101 } 102 103 err = db.DeleteFollowByRkey(s.db, currentUser.Did, follow.Rkey) 104 if err != nil { 105 log.Println("failed to delete follow from DB") 106 // this is not an issue, the firehose event might have already done this 107 } 108 109 s.pages.FollowFragment(w, pages.FollowFragmentParams{ 110 UserDid: subjectIdent.DID.String(), 111 FollowStatus: models.IsNotFollowing, 112 }) 113 114 s.notifier.DeleteFollow(r.Context(), follow) 115 116 return 117 } 118 119}