Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix private per-user feed hidden #419

Merged
merged 1 commit into from
Mar 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions services/feed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ func (s *server) PerArticle(ctx context.Context, r *pb.ArticleRequest) (*pb.Feed
return fp, nil
}

func (s *server) checkFollowing(follower_id int64, followed_id int64) (bool, error) {
if follower_id == followed_id {
return true, nil // Users are 'following' themselves.
}
fr := &pb.DbFollowRequest{
RequestType: pb.DbFollowRequest_FIND,
Match: &pb.Follow{
Follower: follower_id,
Followed: followed_id,
State: pb.Follow_ACTIVE,
},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := s.db.Follow(ctx, fr)
if err != nil {
return false, fmt.Errorf("Checking follower of private account failed: %v", err)
}
// True if there's a match.
return len(resp.Results) == 1, nil
}

func (s *server) PerUser(ctx context.Context, r *pb.FeedRequest) (*pb.FeedResponse, error) {
if r.Username == "" {
return nil, fmt.Errorf("feed.PerUser failed: username field empty")
Expand All @@ -179,9 +201,17 @@ func (s *server) PerUser(ctx context.Context, r *pb.FeedRequest) (*pb.FeedRespon
authorId := author.GlobalId

if author.Private != nil && author.Private.Value {
// TODO(devoxel): Lookup accessor here and see if they're allowed
// to view the posts.
return &pb.FeedResponse{Error: pb.FeedResponse_UNAUTHORIZED}, nil
if r.UserGlobalId == nil {
// User not logged in.
return &pb.FeedResponse{Error: pb.FeedResponse_UNAUTHORIZED}, nil
}
following, err := s.checkFollowing(r.UserGlobalId.Value, author.GlobalId)
if err != nil {
return nil, err
} else if !following {
// User not following this private account.
return &pb.FeedResponse{Error: pb.FeedResponse_UNAUTHORIZED}, nil
}
}

pr := &pb.PostsRequest{
Expand Down