package state import ( "log" "net/http" "time" comatproto "github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/atproto/syntax" lexutil "github.com/bluesky-social/indigo/lex/util" "tangled.org/core/api/tangled" "tangled.org/core/appview/db" "tangled.org/core/appview/models" "tangled.org/core/appview/pages" "tangled.org/core/tid" ) func (s *State) Follow(w http.ResponseWriter, r *http.Request) { currentUser := s.oauth.GetMultiAccountUser(r) subject := r.URL.Query().Get("subject") if subject == "" { log.Println("invalid form") return } subjectIdent, err := s.idResolver.ResolveIdent(r.Context(), subject) if err != nil { log.Println("failed to follow, invalid did") return } if currentUser.Active.Did == subjectIdent.DID.String() { log.Println("cant follow or unfollow yourself") return } client, err := s.oauth.AuthorizedClient(r) if err != nil { log.Println("failed to authorize client") return } switch r.Method { case http.MethodPost: follow := models.Follow{ UserDid: currentUser.Active.Did, SubjectDid: subjectIdent.DID.String(), Rkey: tid.TID(), FollowedAt: time.Now(), } tx, err := s.db.BeginTx(r.Context(), nil) if err != nil { s.logger.Error("failed to start transaction", "err", err) return } defer tx.Rollback() if err := db.UpsertFollow(tx, follow); err != nil { s.logger.Error("failed to follow", "err", err) return } record := follow.AsRecord() resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ Collection: tangled.GraphFollowNSID, Repo: currentUser.Active.Did, Rkey: follow.Rkey, Record: &lexutil.LexiconTypeDecoder{ Val: &record, }, }) if err != nil { log.Println("failed to create atproto record", err) return } log.Println("created atproto record: ", resp.Uri) if err := tx.Commit(); err != nil { s.logger.Error("failed to commit transaction", "err", err) // DB op failed but record is created in PDS. Ingester will backfill the missed operation } s.notifier.NewFollow(r.Context(), &follow) followStats, err := db.GetFollowerFollowingCount(s.db, subjectIdent.DID.String()) if err != nil { log.Println("failed to get follow stats", err) } s.pages.FollowFragment(w, pages.FollowFragmentParams{ UserDid: subjectIdent.DID.String(), FollowStatus: models.IsFollowing, FollowersCount: followStats.Followers, }) return case http.MethodDelete: tx, err := s.db.BeginTx(r.Context(), nil) if err != nil { s.logger.Error("failed to start transaction", "err", err) } defer tx.Rollback() follows, err := db.DeleteFollow(tx, syntax.DID(currentUser.Active.Did), subjectIdent.DID) if err != nil { s.logger.Error("failed to delete follows from db", "err", err) return } var writes []*comatproto.RepoApplyWrites_Input_Writes_Elem for _, followAt := range follows { writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ RepoApplyWrites_Delete: &comatproto.RepoApplyWrites_Delete{ Collection: tangled.GraphFollowNSID, Rkey: followAt.RecordKey().String(), }, }) } _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ Repo: currentUser.Active.Did, Writes: writes, }) if err != nil { s.logger.Error("failed to delete follows from PDS", "err", err) return } if err := tx.Commit(); err != nil { s.logger.Error("failed to commit transaction", "err", err) // DB op failed but record is created in PDS. Ingester will backfill the missed operation } s.notifier.DeleteFollow(r.Context(), &models.Follow{ UserDid: currentUser.Active.Did, SubjectDid: subjectIdent.DID.String(), // Rkey // FollowedAt }) followStats, err := db.GetFollowerFollowingCount(s.db, subjectIdent.DID.String()) if err != nil { log.Println("failed to get follow stats", err) } s.pages.FollowFragment(w, pages.FollowFragmentParams{ UserDid: subjectIdent.DID.String(), FollowStatus: models.IsNotFollowing, FollowersCount: followStats.Followers, }) return } }