-
Notifications
You must be signed in to change notification settings - Fork 1
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
[#325] refactor post reactions #333
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func (m *database) applyReactionsChecks(reactions *collectionWrapper) error { | ||
log.Println("apply reactions checks.....") | ||
|
||
log.Println("reactions checks passed") | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding indexes is a mandatory step. Please add the necessary indexes here. Notice the need of composite indexes as well
primitive.E{Key: "$inc", Value: bson.D{ | ||
primitive.E{Key: "reaction_stats." + reaction, Value: incrementValue}, | ||
}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one!
core/model/post_reactions.go
Outdated
type AggregateReactions struct { | ||
ID string `json:"id" bson:"_id"` | ||
PostID string `json:"post_id" bson:"post_id"` | ||
Reactions Reaction `json:"reactions" bson:"reactions"` | ||
} | ||
|
||
type Reaction struct { | ||
Key *string `json:"k" bson:"k"` | ||
Value *string `json:"v" bson:"v"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right I forgot to clean this up. I will remove. Thank you!
driven/storage/adapter.go
Outdated
//take the count of the total thumbs up reactions | ||
filter := bson.M{"_id": postID} | ||
update := bson.D{ | ||
{"$set", bson.D{{"reaction_stats.thumbs-up", len(list[i].Reactions["thumbs-up"])}}}, | ||
{"$unset", bson.D{{"reactions", ""}}}, | ||
} | ||
|
||
_, err := sa.db.posts.UpdateOne(filter, update, nil) | ||
if err != nil { | ||
fmt.Printf("error migrating reactions %s", err) | ||
return fmt.Errorf("error migrating reactions %s", err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks unfinished and hardcoded. Also I think it should be moved to driven/storage/database.go where prior migrations are implemented.
driven/storage/adapter.go
Outdated
func (sa *Adapter) DeleteUserPostReactions(context TransactionContext, clientID string, userID string) error { | ||
|
||
filter := bson.M{"user_id": userID} | ||
|
||
var res []model.PostReactions | ||
err := sa.db.reactions.Find(filter, &res, nil) | ||
if err != nil { | ||
log.Printf("error deleting reactions for user %s - %s", userID, err.Error()) | ||
return err | ||
} | ||
|
||
_, err = sa.db.reactions.DeleteMany(filter, nil) | ||
if err != nil { | ||
log.Printf("error deleting user reactions to post - %s", err.Error()) | ||
return err | ||
} | ||
|
||
for i := 0; i < len(res); i++ { | ||
for j := 0; j < len(res[i].Reactions); j++ { | ||
err = sa.UpdateReactionStats(res[i].PostID, false, res[i].Reactions[j]) | ||
if err != nil { | ||
return fmt.Errorf("error decrementing reaction stats for post %s with reaction %s for %s: %v", res[i].PostID, res[i].Reactions[j], userID, err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a transaction is used, then all sub operations must be incorporated in the transaction.
opts := options.Update().SetUpsert(true) | ||
_, err := sa.db.reactions.UpdateOne(filter, update, opts) | ||
|
||
res, err := sa.db.posts.UpdateOneWithContext(context, filter, update, nil) | ||
if err != nil { | ||
return fmt.Errorf("error updating post %s with reaction %s for %s: %v", postID, reaction, userID, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment related for transaction wrapping as above.
255e45b
to
1af4fdc
Compare
Description
Please provide a summary of the pull request and the issue it resolves. Please add necessary details, context, dependencies, explanation of when review is needed (see next section), etc.
This PR is refactoring reactions in order to fix #308 as there was no way to delete reactions with current setup. Reactions will now be more robust allowing users to have multiple reactions to a post and no code changes for any new type of reactions.
**Resolves #325 **
Review Time Estimate
Please give your idea of how soon this pull request needs to be reviewed by selecting one of the options below. This can be based on the criticality of the issue at hand and/or other relevant factors.
Type of changes
Please select a relevant option:
Checklist:
Please select all applicable options: