Skip to content

[#325] refactor post reactions#333

Closed
bsdworkin wants to merge 6 commits intodevelopfrom
325-refactor-post-reactions
Closed

[#325] refactor post reactions#333
bsdworkin wants to merge 6 commits intodevelopfrom
325-refactor-post-reactions

Conversation

@bsdworkin
Copy link
Copy Markdown
Collaborator

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.

  • Immediately
  • Within a week
  • When possible

Type of changes

Please select a relevant option:

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).
  • Other (any another change that does not fall in one of the above categories.)

Checklist:

Please select all applicable options:

  • I have signed the Rokwire Contributor License Agreement (CLA). (Any contributor who is not an employee of the University of Illinois whose official duties include contributing to the Rokwire software, or who is not paid by the Rokwire project, needs to sign the CLA before their contribution can be accepted.)
  • I have updated the CHANGELOG.
  • I have read the Contributor Guidelines.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • My change requires updating the documentation.
  • I have made necessary changes to the documentation.
  • I have added tests related to my changes.
  • My changes generate no new warnings.
  • New and existing unit tests pass locally with my changes.
  • Any dependent changes have been merged and published in downstream modules.

Copy link
Copy Markdown
Contributor

@mdryankov mdryankov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bsdworkin, Please look at my comments and initiate updates accordingly.

Thank you!

CC @shurwit

Comment on lines +692 to +698
func (m *database) applyReactionsChecks(reactions *collectionWrapper) error {
log.Println("apply reactions checks.....")

log.Println("reactions checks passed")
return nil
}

Copy link
Copy Markdown
Contributor

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

Comment thread driven/storage/adapter.go
Comment on lines +1554 to +1562
primitive.E{Key: "$inc", Value: bson.D{
primitive.E{Key: "reaction_stats." + reaction, Value: incrementValue},
}},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

Comment thread core/model/post_reactions.go Outdated
Comment on lines +25 to +34
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"`
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unused.

Copy link
Copy Markdown
Collaborator Author

@bsdworkin bsdworkin Jan 16, 2023

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!

Comment thread driven/storage/adapter.go Outdated
Comment on lines +97 to +108
//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)
}
Copy link
Copy Markdown
Contributor

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.

Comment thread driven/storage/adapter.go
Comment on lines +473 to +507
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
}

Copy link
Copy Markdown
Contributor

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.

Comment thread driven/storage/adapter.go
Comment on lines +1530 to 1541
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)
}
Copy link
Copy Markdown
Contributor

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.

@bsdworkin bsdworkin force-pushed the 325-refactor-post-reactions branch from 255e45b to 1af4fdc Compare January 16, 2023 18:19
@mladen-inabyte
Copy link
Copy Markdown
Collaborator

Closing this due to outdated changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Refactor post reactions [BUG] Reactions not cleared when user is deleted

3 participants