-
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
Open
bsdworkin
wants to merge
6
commits into
develop
Choose a base branch
from
325-refactor-post-reactions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+324
−16
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
501e94a
refactored reactions
bsdworkin a3db7ee
added in aggregation pipeline when deleting a user
bsdworkin 6c5d936
refactor and migration changes
bsdworkin 8d7064a
uncommented code needed
bsdworkin 710edd9
added in transactions and fixed migration
bsdworkin 1af4fdc
Merge branch 'develop' into 325-refactor-post-reactions
bsdworkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2022 Board of Trustees of the University of Illinois. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package model | ||
|
||
// PostReactions are the reations to a specific post | ||
type PostReactions struct { | ||
ID string `json:"id" bson:"_id"` | ||
PostID string `json:"post_id" bson:"post_id"` | ||
UserID string `json:"user_id" bson:"user_id"` | ||
Reactions []string `json:"reactions" bson:"reactions"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,54 @@ func (sa *Adapter) Start() error { | |
return errors.New("error caching managed group configs") | ||
} | ||
|
||
//TODO delete after deployement | ||
err = sa.PerformTransaction(func(context TransactionContext) error { | ||
|
||
filter := bson.D{} | ||
var list []*model.Post | ||
err = sa.db.posts.Find(filter, &list, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < len(list); i++ { | ||
//iterate through post reactions and create new Reaction Document | ||
postID := list[i].ID | ||
for _, userID := range list[i].Reactions["thumbs-up"] { | ||
reactionArray := []string{"thumbs-up"} | ||
reactions := model.PostReactions{ | ||
PostID: *postID, | ||
UserID: userID, | ||
Reactions: reactionArray, | ||
} | ||
|
||
_, err := sa.db.reactions.InsertOne(reactions) | ||
if err != nil { | ||
fmt.Printf("error migrating reactions %s", err) | ||
return fmt.Errorf("error migrating reactions %s", err) | ||
} | ||
} | ||
|
||
//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) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
|
||
}) | ||
//TODO end of deletion | ||
|
||
return err | ||
} | ||
|
||
|
@@ -405,7 +453,14 @@ func (sa *Adapter) DeleteUser(clientID string, userID string) error { | |
} | ||
} | ||
|
||
// delete the user | ||
//delete any reactions on posts | ||
err = sa.DeleteUserPostReactions(sessionContext, clientID, userID) | ||
if err != nil { | ||
log.Printf("error deleting user reactions - %s", err.Error()) | ||
return err | ||
} | ||
|
||
//delete the user | ||
filter := bson.D{ | ||
primitive.E{Key: "_id", Value: userID}, | ||
primitive.E{Key: "client_id", Value: clientID}, | ||
|
@@ -420,6 +475,36 @@ func (sa *Adapter) DeleteUser(clientID string, userID string) error { | |
}) | ||
} | ||
|
||
// DeleteUserPostReactions updates and removes all user post reactions across all existing groups | ||
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(context, 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 | ||
} | ||
|
||
// CreateGroup creates a group. Returns the id of the created group | ||
func (sa *Adapter) CreateGroup(clientID string, current *model.User, group *model.Group, defaultMemberships []model.GroupMembership) (*string, *utils.GroupError) { | ||
insertedID := uuid.NewString() | ||
|
@@ -1432,29 +1517,85 @@ func (sa *Adapter) ReportPostAsAbuse(clientID string, userID string, group *mode | |
|
||
// ReactToPost React to a post | ||
func (sa *Adapter) ReactToPost(context TransactionContext, userID string, postID string, reaction string, on bool) error { | ||
filter := bson.D{primitive.E{Key: "_id", Value: postID}} | ||
filter := bson.D{primitive.E{Key: "post_id", Value: postID}} | ||
|
||
updateOperation := "$pull" | ||
if on { | ||
updateOperation = "$push" | ||
} | ||
update := bson.D{ | ||
primitive.E{Key: updateOperation, Value: bson.D{ | ||
primitive.E{Key: "reactions." + reaction, Value: userID}, | ||
}}, | ||
|
||
update := bson.M{ | ||
"$set": bson.M{ | ||
"post_id": postID, | ||
"user_id": userID, | ||
}, | ||
updateOperation: bson.M{ | ||
"reactions": reaction, | ||
}, | ||
} | ||
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) | ||
} | ||
if res.ModifiedCount != 1 { | ||
return fmt.Errorf("updated %d posts with reaction %s for %s, but expected 1", res.ModifiedCount, reaction, userID) | ||
|
||
err = sa.UpdateReactionStats(context, postID, on, reaction) | ||
if err != nil { | ||
return fmt.Errorf("error updating reaction stats for post %s with reaction %s for %s: %v", postID, reaction, userID, err) | ||
} | ||
return nil | ||
} | ||
|
||
// UpdateReactionStats increments or decrements reaction counts | ||
func (sa *Adapter) UpdateReactionStats(context TransactionContext, postID string, on bool, reaction string) error { | ||
filter := bson.D{primitive.E{Key: "_id", Value: postID}} | ||
|
||
incrementValue := -1 | ||
if on { | ||
incrementValue = 1 | ||
} | ||
|
||
update := bson.D{ | ||
primitive.E{Key: "$inc", Value: bson.D{ | ||
primitive.E{Key: "reaction_stats." + reaction, Value: incrementValue}, | ||
}}, | ||
Comment on lines
+1560
to
+1562
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one! |
||
} | ||
|
||
upsert := true | ||
opts := options.UpdateOptions{Upsert: &upsert} | ||
|
||
_, err := sa.db.posts.UpdateOneWithContext(context, filter, update, &opts) | ||
if err != nil { | ||
return fmt.Errorf("error updating reaction stats for post %s with reaction %s: %v", postID, reaction, err) | ||
|
||
} | ||
return nil | ||
} | ||
|
||
// FindsReactionsByPost finds reactions based on post id | ||
func (sa *Adapter) FindReactionsByPost(postID string) ([]model.PostReactions, error) { | ||
filter := bson.M{"post_id": postID} | ||
var results []model.PostReactions | ||
err := sa.db.reactions.Find(filter, &results, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("error storage.Adapter.FindReactionStats - %s", err) | ||
} | ||
return results, nil | ||
} | ||
|
||
// FindReactions gets reactions based on postID | ||
func (sa *Adapter) FindReactions(context TransactionContext, postID string, userID string) (model.PostReactions, error) { | ||
filter := bson.M{"post_id": postID, "user_id": userID} | ||
var res model.PostReactions | ||
err := sa.db.reactions.FindOneWithContext(context, filter, &res, nil) | ||
if err != nil { | ||
return res, fmt.Errorf("error finding post reactions %s: %v", postID, err) | ||
} | ||
|
||
return res, err | ||
} | ||
|
||
// DeletePost Deletes a post | ||
func (sa *Adapter) DeletePost(ctx TransactionContext, clientID string, userID string, groupID string, postID string, force bool) error { | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.