MM-10412: Adds deleteBy prop to posts. (#8896)

Этот коммит содержится в:
Martin Kraft
2018-06-01 12:45:46 -04:00
коммит произвёл GitHub
родитель 5992a729c5
Коммит 260d7a0f85
8 изменённых файлов: 39 добавлений и 17 удалений

Просмотреть файл

@@ -264,7 +264,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
if _, err := c.App.DeletePost(c.Params.PostId); err != nil {
if _, err := c.App.DeletePost(c.Params.PostId, c.Session.UserId); err != nil {
c.Err = err
return
}

Просмотреть файл

@@ -158,7 +158,7 @@ func (api *PluginAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError
}
func (api *PluginAPI) DeletePost(postId string) *model.AppError {
_, err := api.app.DeletePost(postId)
_, err := api.app.DeletePost(postId, api.id)
return err
}

Просмотреть файл

@@ -569,14 +569,14 @@ func (a *App) GetPostsAroundPost(postId, channelId string, offset, limit int, be
}
}
func (a *App) DeletePost(postId string) (*model.Post, *model.AppError) {
func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppError) {
if result := <-a.Srv.Store.Post().GetSingle(postId); result.Err != nil {
result.Err.StatusCode = http.StatusBadRequest
return nil, result.Err
} else {
post := result.Data.(*model.Post)
if result := <-a.Srv.Store.Post().Delete(postId, model.GetMillis()); result.Err != nil {
if result := <-a.Srv.Store.Post().Delete(postId, model.GetMillis(), deleteByID); result.Err != nil {
return nil, result.Err
}

Просмотреть файл

@@ -50,6 +50,7 @@ const (
POST_CUSTOM_TYPE_PREFIX = "custom_"
PROPS_ADD_CHANNEL_MEMBER = "add_channel_member"
POST_PROPS_ADDED_USER_ID = "addedUserId"
POST_PROPS_DELETE_BY = "deleteBy"
)
type Post struct {

Просмотреть файл

@@ -398,11 +398,24 @@ func (s *SqlPostStore) GetEtag(channelId string, allowFromCache bool) store.Stor
})
}
func (s *SqlPostStore) Delete(postId string, time int64) store.StoreChannel {
func (s *SqlPostStore) Delete(postId string, time int64, deleteByID string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
_, err := s.GetMaster().Exec("Update Posts SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id OR RootId = :RootId", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": postId, "RootId": postId})
appErr := func(errMsg string) *model.AppError {
return model.NewAppError("SqlPostStore.Delete", "store.sql_post.delete.app_error", nil, "id="+postId+", err="+errMsg, http.StatusInternalServerError)
}
var post model.Post
err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": postId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.Delete", "store.sql_post.delete.app_error", nil, "id="+postId+", err="+err.Error(), http.StatusInternalServerError)
result.Err = appErr(err.Error())
}
post.Props[model.POST_PROPS_DELETE_BY] = deleteByID
_, err = s.GetMaster().Exec("UPDATE Posts SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt, Props = :Props WHERE Id = :Id OR RootId = :RootId", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": postId, "RootId": postId, "Props": model.StringInterfaceToJson(post.Props)})
if err != nil {
result.Err = appErr(err.Error())
}
})
}

Просмотреть файл

@@ -181,7 +181,7 @@ type PostStore interface {
Update(newPost *model.Post, oldPost *model.Post) StoreChannel
Get(id string) StoreChannel
GetSingle(id string) StoreChannel
Delete(postId string, time int64) StoreChannel
Delete(postId string, time int64, deleteByID string) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel
PermanentDeleteByChannel(channelId string) StoreChannel
GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel

Просмотреть файл

@@ -67,12 +67,12 @@ func (_m *PostStore) ClearCaches() {
}
// Delete provides a mock function with given fields: postId, time
func (_m *PostStore) Delete(postId string, time int64) store.StoreChannel {
func (_m *PostStore) Delete(postId string, time int64, deleteByID string) store.StoreChannel {
ret := _m.Called(postId, time)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string, int64) store.StoreChannel); ok {
r0 = rf(postId, time)
if rf, ok := ret.Get(0).(func(string, int64, string) store.StoreChannel); ok {
r0 = rf(postId, time, deleteByID)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)

Просмотреть файл

@@ -247,6 +247,7 @@ func testPostStoreDelete(t *testing.T, ss store.Store) {
o1.ChannelId = model.NewId()
o1.UserId = model.NewId()
o1.Message = "zz" + model.NewId() + "b"
deleteByID := model.NewId()
etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
if strings.Index(etag1, model.CurrentVersion+".") != 0 {
@@ -263,10 +264,17 @@ func testPostStoreDelete(t *testing.T, ss store.Store) {
}
}
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis()); r2.Err != nil {
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), deleteByID); r2.Err != nil {
t.Fatal(r2.Err)
}
r5 := <-ss.Post().GetPostsCreatedAt(o1.ChannelId, o1.CreateAt)
post := r5.Data.([]*model.Post)[0]
actual := post.Props[model.POST_PROPS_DELETE_BY]
if actual != deleteByID {
t.Errorf("Expected (*Post).Props[model.POST_PROPS_DELETE_BY] to be %v but got %v.", deleteByID, actual)
}
if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
t.Log(r3.Data)
t.Fatal("Missing id should have failed")
@@ -293,7 +301,7 @@ func testPostStoreDelete1Level(t *testing.T, ss store.Store) {
o2.RootId = o1.Id
o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis()); r2.Err != nil {
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
t.Fatal(r2.Err)
}
@@ -335,7 +343,7 @@ func testPostStoreDelete2Level(t *testing.T, ss store.Store) {
o4.Message = "zz" + model.NewId() + "b"
o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis()); r2.Err != nil {
if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
t.Fatal(r2.Err)
}
@@ -468,7 +476,7 @@ func testPostStoreGetWithChildren(t *testing.T, ss store.Store) {
}
}
store.Must(ss.Post().Delete(o3.Id, model.GetMillis()))
store.Must(ss.Post().Delete(o3.Id, model.GetMillis(), ""))
if r2 := <-ss.Post().Get(o1.Id); r2.Err != nil {
t.Fatal(r2.Err)
@@ -479,7 +487,7 @@ func testPostStoreGetWithChildren(t *testing.T, ss store.Store) {
}
}
store.Must(ss.Post().Delete(o2.Id, model.GetMillis()))
store.Must(ss.Post().Delete(o2.Id, model.GetMillis(), ""))
if r3 := <-ss.Post().Get(o1.Id); r3.Err != nil {
t.Fatal(r3.Err)
@@ -1589,7 +1597,7 @@ func testPostStoreGetPostsByIds(t *testing.T, ss store.Store) {
t.Fatalf("Expected 3 posts in results. Got %v", len(ro4))
}
store.Must(ss.Post().Delete(ro1.Id, model.GetMillis()))
store.Must(ss.Post().Delete(ro1.Id, model.GetMillis(), ""))
if ro5 := store.Must(ss.Post().GetPostsByIds(postIds)).([]*model.Post); len(ro5) != 2 {
t.Fatalf("Expected 2 posts in results. Got %v", len(ro5))