Skip to content

Commit

Permalink
rm review moderation
Browse files Browse the repository at this point in the history
  • Loading branch information
nnqq committed Nov 13, 2021
1 parent 373dcf3 commit a70af03
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 41 deletions.
9 changes: 1 addition & 8 deletions review/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Create(
UserID: userID,
Text: text,
Positive: positive,
Status: moderation,
Status: ok,
}
_, err := mongo.Reviews.InsertOne(ctx, r)
if err != nil {
Expand Down Expand Up @@ -91,13 +91,6 @@ func DeleteAll(ctx context.Context, userID primitive.ObjectID) error {
return err
}

func CountModeration(ctx context.Context, userID primitive.ObjectID) (int64, error) {
return mongo.Reviews.CountDocuments(ctx, bson.M{
"u": userID,
"s": nil,
})
}

func SetOK(ctx context.Context, reviewID primitive.ObjectID) error {
_, err := mongo.Reviews.UpdateOne(ctx, Review{
ID: reviewID,
Expand Down
41 changes: 8 additions & 33 deletions reviewimpl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/leaq-ru/proto/codegen/go/parser"
"github.com/leaq-ru/proto/codegen/go/user"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"
"time"
Expand Down Expand Up @@ -94,10 +93,7 @@ func (s *server) Create(ctx context.Context, req *parser.CreateRequest) (*emptyp
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

const (
maxLen = 3000
maxModeration = 10
)
const maxLen = 3000
if utf8.RuneCountInString(req.GetText()) > maxLen {
return nil, fmt.Errorf("text too long, max length %d", maxLen)
}
Expand All @@ -112,36 +108,15 @@ func (s *server) Create(ctx context.Context, req *parser.CreateRequest) (*emptyp
return nil, err
}

var userData *user.ShortUser
var eg errgroup.Group
eg.Go(func() error {
u, e := call.User.GetById(ctx, &user.GetByIdRequest{
UserId: userID.Hex(),
})
if e != nil {
logger.Log.Error().Err(e).Send()
return safeerr.InternalServerError
}
userData = u
if u.GetBanReview() {
return errors.New("you not allowed to post reviews")
}
return nil
})
eg.Go(func() error {
countMod, e := review.CountModeration(ctx, userID)
if e != nil {
logger.Log.Error().Err(e).Send()
return safeerr.InternalServerError
}
if countMod >= maxModeration {
return errors.New("too many reviews in moderation, try later")
}
return nil
userData, err := call.User.GetById(ctx, &user.GetByIdRequest{
UserId: userID.Hex(),
})
err = eg.Wait()
if err != nil {
return nil, err
logger.Log.Error().Err(err).Send()
return nil, safeerr.InternalServerError
}
if userData.GetBanReview() {
return nil, errors.New("you not allowed to post reviews")
}

r, err := review.Create(ctx, compID, userID, req.GetText(), req.GetPositive())
Expand Down

0 comments on commit a70af03

Please sign in to comment.