From 80d83ae77c85de5666dc88458e2888f66c49cc90 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 22 Aug 2016 20:36:01 -0400 Subject: [PATCH] Prevent flagging of deleted posts and don't show deleted posts in flagged post list (#3846) --- api/post.go | 8 +++++ i18n/en.json | 4 +++ store/sql_post_store.go | 2 +- store/sql_post_store_test.go | 23 ++++++++++++ .../post_view/components/post_info.jsx | 35 +++++++++++-------- webapp/components/rhs_comment.jsx | 35 +++++++++++-------- 6 files changed, 78 insertions(+), 29 deletions(-) diff --git a/api/post.go b/api/post.go index fa42e6f88c..b4c70fab2c 100644 --- a/api/post.go +++ b/api/post.go @@ -1332,6 +1332,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { go Publish(message) go DeletePostFiles(c.TeamId, post) + go DeleteFlaggedPost(c.Session.UserId, post) result := make(map[string]string) result["id"] = postId @@ -1339,6 +1340,13 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func DeleteFlaggedPost(userId string, post *model.Post) { + if result := <-Srv.Store.Preference().Delete(userId, model.PREFERENCE_CATEGORY_FLAGGED_POST, post.Id); result.Err != nil { + l4g.Warn(utils.T("api.post.delete_flagged_post.app_error.warn"), result.Err) + return + } +} + func DeletePostFiles(teamId string, post *model.Post) { if len(post.Filenames) == 0 { return diff --git a/i18n/en.json b/i18n/en.json index f0f09fd1b7..00686e1af5 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1097,6 +1097,10 @@ "id": "api.post.create_post.bad_filename.error", "translation": "Bad filename discarded, filename=%v" }, + { + "id": "api.post.delete_flagged_post.app_error.warn", + "translation": "Unable to delete flagged post preference when deleting post, err=%v" + }, { "id": "api.post.create_post.channel_root_id.app_error", "translation": "Invalid ChannelId for RootId parameter" diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 07192b4a6d..b844bfbca9 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -149,7 +149,7 @@ func (s SqlPostStore) GetFlaggedPosts(userId string, offset int, limit int) Stor pl := &model.PostList{} var posts []*model.Post - if _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE Id IN (SELECT Name FROM Preferences WHERE UserId = :UserId AND Category = :Category) ORDER BY CreateAt ASC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Offset": offset, "Limit": limit}); err != nil { + if _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE Id IN (SELECT Name FROM Preferences WHERE UserId = :UserId AND Category = :Category) AND DeleteAt = 0 ORDER BY CreateAt ASC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Offset": offset, "Limit": limit}); err != nil { result.Err = model.NewLocAppError("SqlPostStore.GetFlaggedPosts", "store.sql_post.get_flagged_posts.app_error", nil, err.Error()) } else { for _, post := range posts { diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index d8f8c2e6be..594e923be4 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -923,6 +923,14 @@ func TestPostStoreGetFlaggedPosts(t *testing.T) { o2 = (<-store.Post().Save(o2)).Data.(*model.Post) time.Sleep(2 * time.Millisecond) + o3 := &model.Post{} + o3.ChannelId = o1.ChannelId + o3.UserId = model.NewId() + o3.Message = "a" + model.NewId() + "b" + o3.DeleteAt = 1 + o3 = (<-store.Post().Save(o3)).Data.(*model.Post) + time.Sleep(2 * time.Millisecond) + r1 := (<-store.Post().GetFlaggedPosts(o1.ChannelId, 0, 2)).Data.(*model.PostList) if len(r1.Order) != 0 { @@ -968,4 +976,19 @@ func TestPostStoreGetFlaggedPosts(t *testing.T) { if len(r4.Order) != 2 { t.Fatal("should have 2 posts") } + + preferences = model.Preferences{ + { + UserId: o1.UserId, + Category: model.PREFERENCE_CATEGORY_FLAGGED_POST, + Name: o3.Id, + Value: "true", + }, + } + + Must(store.Preference().Save(&preferences)) + + if len(r4.Order) != 2 { + t.Fatal("should have 2 posts") + } } diff --git a/webapp/components/post_view/components/post_info.jsx b/webapp/components/post_view/components/post_info.jsx index d48d97ba1f..e95afe3ed7 100644 --- a/webapp/components/post_view/components/post_info.jsx +++ b/webapp/components/post_view/components/post_info.jsx @@ -336,6 +336,26 @@ export default class PostInfo extends React.Component { flagFunc = this.flagPost; } + let flagTrigger; + if (!Utils.isPostEphemeral(post)) { + flagTrigger = ( + + + {flag} + + + ); + } + return ( diff --git a/webapp/components/rhs_comment.jsx b/webapp/components/rhs_comment.jsx index 92182e27a9..05df1ac5fd 100644 --- a/webapp/components/rhs_comment.jsx +++ b/webapp/components/rhs_comment.jsx @@ -330,6 +330,26 @@ export default class RhsComment extends React.Component { flagFunc = this.flagPost; } + let flagTrigger; + if (!Utils.isPostEphemeral(post)) { + flagTrigger = ( + + + {flag} + + + ); + } + return (
@@ -352,20 +372,7 @@ export default class RhsComment extends React.Component { minute='2-digit' /> - - - {flag} - - + {flagTrigger}
  • {dropdown}