MM-45317: global drafts endpoints and ws events (#20614)
* MM-23881: global drafts endpoints and ws events Adds endpoints: - create/update drafts - delete draft - get drafts Adds WS events: - draft_updated - draft_created - draft_deleted * Ordering and WS event name fixes * Adds PostID to the drafts table In the future the drafts will include edited posts, this commit adds the post id in the combined pkey of the table. * Fixes route for deleting a thread draft * Fixes failed checks * Fixes migrations * Fixes migration * Extract translation strings * Removes PostID since we won't sync editing posts * Fixes tests * Fixes i18n * Update migrations for global drafts * update branch with latest master changes * Add feature flag for global drafts * Set global drafts feature flag default to true * Added support for files in drafts * Fix failing i18n check * Added support for deleting files in drafts * Revert "Added support for deleting files in drafts" This reverts commit 45dfd04a760359de2e8814d652c9ef46daf994f6. * Triggering new test server * Add config setting 'AllowSyncedDrafts' for syncing drafts with server * Triggering new test server * Triggering new test server * Add guard for config setting and add initial tests * Fix i18n and lint errors * Triggering new test server * Add tests for drafts * fix lint issues * Add tests for model/draft * Triggering new test server * Triggering new test server * Trigger new test server * Address PR comments * Change left join to regular join in GetDraftsForUser * Fix broken test Maybe consider adding an inclDeleted field if we want to get deleted drafts in the future * fix translations * Add store tests for drafts * fix test naming * remove comment * update migrations * set feature flag default to false * update migrations Co-authored-by: Mylon Suren <mylonsuren@gmail.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
@@ -42,6 +42,7 @@ const (
|
||||
StatusFail = "FAIL"
|
||||
StatusUnhealthy = "UNHEALTHY"
|
||||
StatusRemove = "REMOVE"
|
||||
ConnectionId = "Connection-Id"
|
||||
|
||||
ClientDir = "client"
|
||||
|
||||
@@ -433,6 +434,10 @@ func (c *Client4) commandMoveRoute(commandId string) string {
|
||||
return fmt.Sprintf(c.commandsRoute()+"/%v/move", commandId)
|
||||
}
|
||||
|
||||
func (c *Client4) draftsRoute() string {
|
||||
return "/drafts"
|
||||
}
|
||||
|
||||
func (c *Client4) emojisRoute() string {
|
||||
return "/emoji"
|
||||
}
|
||||
@@ -6229,6 +6234,59 @@ func (c *Client4) GetChannelPoliciesForUser(userID string, offset, limit int) (*
|
||||
return &channels, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
// Drafts Sections
|
||||
|
||||
// UpsertDraft will create a new draft or update a draft if it already exists
|
||||
func (c *Client4) UpsertDraft(draft *Draft) (*Draft, *Response, error) {
|
||||
buf, err := json.Marshal(draft)
|
||||
if err != nil {
|
||||
return nil, nil, NewAppError("UpsertDraft", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
r, err := c.DoAPIPostBytes(c.draftsRoute(), buf)
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), err
|
||||
}
|
||||
defer closeBody(r)
|
||||
|
||||
var df Draft
|
||||
err = json.NewDecoder(r.Body).Decode(&df)
|
||||
if err != nil {
|
||||
return nil, nil, NewAppError("UpsertDraft", "api.unmarshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
return &df, BuildResponse(r), err
|
||||
}
|
||||
|
||||
// GetDrafts will get all drafts for a user
|
||||
func (c *Client4) GetDrafts(userId, teamId string) ([]*Draft, *Response, error) {
|
||||
r, err := c.DoAPIGet(c.userRoute(userId)+c.teamRoute(teamId)+"/drafts", "")
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), err
|
||||
}
|
||||
defer closeBody(r)
|
||||
var drafts []*Draft
|
||||
err = json.NewDecoder(r.Body).Decode(&drafts)
|
||||
if err != nil {
|
||||
return nil, nil, NewAppError("GetDrafts", "api.unmarshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
return drafts, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
func (c *Client4) DeleteDraft(userId, channelId, rootId string) (*Draft, *Response, error) {
|
||||
r, err := c.DoAPIDelete(c.userRoute(userId) + c.channelRoute(channelId) + "/drafts")
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), err
|
||||
}
|
||||
defer closeBody(r)
|
||||
|
||||
var df *Draft
|
||||
err = json.NewDecoder(r.Body).Decode(&df)
|
||||
if err != nil {
|
||||
return nil, BuildResponse(r), NewAppError("DeleteDraft", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
return df, BuildResponse(r), nil
|
||||
}
|
||||
|
||||
// Commands Section
|
||||
|
||||
// CreateCommand will create a new command if the user have the right permissions.
|
||||
|
||||
@@ -383,6 +383,7 @@ type ServiceSettings struct {
|
||||
CollapsedThreads *string `access:"experimental_features"`
|
||||
ManagedResourcePaths *string `access:"environment_web_server,write_restrictable,cloud_restrictable"`
|
||||
EnableCustomGroups *bool `access:"site_users_and_teams"`
|
||||
AllowSyncedDrafts *bool `access:"site_posts"`
|
||||
}
|
||||
|
||||
func (s *ServiceSettings) SetDefaults(isUpdate bool) {
|
||||
@@ -847,6 +848,10 @@ func (s *ServiceSettings) SetDefaults(isUpdate bool) {
|
||||
if s.PostPriority == nil {
|
||||
s.PostPriority = NewBool(true)
|
||||
}
|
||||
|
||||
if s.AllowSyncedDrafts == nil {
|
||||
s.AllowSyncedDrafts = NewBool(true)
|
||||
}
|
||||
}
|
||||
|
||||
type ClusterSettings struct {
|
||||
|
||||
101
model/draft.go
Обычный файл
101
model/draft.go
Обычный файл
@@ -0,0 +1,101 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Draft struct {
|
||||
CreateAt int64 `json:"create_at"`
|
||||
UpdateAt int64 `json:"update_at"`
|
||||
DeleteAt int64 `json:"delete_at"`
|
||||
UserId string `json:"user_id"`
|
||||
ChannelId string `json:"channel_id"`
|
||||
RootId string `json:"root_id"`
|
||||
|
||||
Message string `json:"message"`
|
||||
|
||||
propsMu sync.RWMutex `db:"-"` // Unexported mutex used to guard Draft.Props.
|
||||
Props StringInterface `json:"props"` // Deprecated: use GetProps()
|
||||
FileIds StringArray `json:"file_ids,omitempty"`
|
||||
Metadata *PostMetadata `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
func (o *Draft) IsValid(maxDraftSize int) *AppError {
|
||||
if o.CreateAt == 0 {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.create_at.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if o.UpdateAt == 0 {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.update_at.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if !IsValidId(o.UserId) {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if !IsValidId(o.ChannelId) {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if !(IsValidId(o.RootId) || o.RootId == "") {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.root_id.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(o.Message) > maxDraftSize {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.msg.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(ArrayToJSON(o.FileIds)) > PostFileidsMaxRunes {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.file_ids.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(StringInterfaceToJSON(o.GetProps())) > PostPropsMaxRunes {
|
||||
return NewAppError("Drafts.IsValid", "model.draft.is_valid.props.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Draft) SetProps(props StringInterface) {
|
||||
o.propsMu.Lock()
|
||||
defer o.propsMu.Unlock()
|
||||
o.Props = props
|
||||
}
|
||||
|
||||
func (o *Draft) GetProps() StringInterface {
|
||||
o.propsMu.RLock()
|
||||
defer o.propsMu.RUnlock()
|
||||
return o.Props
|
||||
}
|
||||
|
||||
func (o *Draft) PreSave() {
|
||||
if o.CreateAt == 0 {
|
||||
o.CreateAt = GetMillis()
|
||||
}
|
||||
|
||||
o.UpdateAt = o.CreateAt
|
||||
o.PreCommit()
|
||||
}
|
||||
|
||||
func (o *Draft) PreCommit() {
|
||||
if o.GetProps() == nil {
|
||||
o.SetProps(make(map[string]interface{}))
|
||||
}
|
||||
|
||||
if o.FileIds == nil {
|
||||
o.FileIds = []string{}
|
||||
}
|
||||
|
||||
// There's a rare bug where the client sends up duplicate FileIds so protect against that
|
||||
o.FileIds = RemoveDuplicateStrings(o.FileIds)
|
||||
}
|
||||
|
||||
func (o *Draft) PreUpdate() {
|
||||
o.UpdateAt = GetMillis()
|
||||
o.PreCommit()
|
||||
}
|
||||
80
model/draft_test.go
Обычный файл
80
model/draft_test.go
Обычный файл
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDraftIsValid(t *testing.T) {
|
||||
o := Draft{}
|
||||
maxDraftSize := 10000
|
||||
|
||||
err := o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.CreateAt = GetMillis()
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.UpdateAt = GetMillis()
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.UserId = NewId()
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.ChannelId = NewId()
|
||||
o.RootId = "123"
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.RootId = ""
|
||||
|
||||
o.Message = strings.Repeat("0", maxDraftSize+1)
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
o.Message = strings.Repeat("0", maxDraftSize)
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.Nil(t, err)
|
||||
|
||||
o.Message = "test"
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.Nil(t, err)
|
||||
|
||||
o.FileIds = StringArray{strings.Repeat("0", maxDraftSize+1)}
|
||||
err = o.IsValid(maxDraftSize)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestDraftPreSave(t *testing.T) {
|
||||
o := Draft{Message: "test"}
|
||||
o.PreSave()
|
||||
|
||||
assert.NotEqual(t, 0, o.CreateAt)
|
||||
|
||||
past := GetMillis() - 1
|
||||
o = Draft{Message: "test", CreateAt: past}
|
||||
o.PreSave()
|
||||
|
||||
assert.LessOrEqual(t, o.CreateAt, past)
|
||||
}
|
||||
|
||||
func TestDraftPreUpdate(t *testing.T) {
|
||||
o := Draft{Message: "test"}
|
||||
o.PreUpdate()
|
||||
|
||||
assert.NotEqual(t, 0, o.UpdateAt)
|
||||
|
||||
past := GetMillis() - 1
|
||||
o = Draft{Message: "test", UpdateAt: past}
|
||||
o.PreSave()
|
||||
|
||||
assert.GreaterOrEqual(t, o.UpdateAt, past)
|
||||
}
|
||||
@@ -79,6 +79,8 @@ type FeatureFlags struct {
|
||||
ReduceOnBoardingTaskList bool
|
||||
|
||||
ThreadsEverywhere bool
|
||||
|
||||
GlobalDrafts bool
|
||||
}
|
||||
|
||||
func (f *FeatureFlags) SetDefaults() {
|
||||
@@ -109,6 +111,7 @@ func (f *FeatureFlags) SetDefaults() {
|
||||
f.AnnualSubscription = false
|
||||
f.ReduceOnBoardingTaskList = false
|
||||
f.ThreadsEverywhere = false
|
||||
f.GlobalDrafts = false
|
||||
}
|
||||
|
||||
func (f *FeatureFlags) Plugins() map[string]string {
|
||||
|
||||
@@ -76,6 +76,9 @@ const (
|
||||
WebsocketEventThreadFollowChanged = "thread_follow_changed"
|
||||
WebsocketEventThreadReadChanged = "thread_read_changed"
|
||||
WebsocketFirstAdminVisitMarketplaceStatusReceived = "first_admin_visit_marketplace_status_received"
|
||||
WebsocketEventDraftCreated = "draft_created"
|
||||
WebsocketEventDraftUpdated = "draft_updated"
|
||||
WebsocketEventDraftDeleted = "draft_deleted"
|
||||
WebsocketEventAcknowledgementAdded = "post_acknowledgement_added"
|
||||
WebsocketEventAcknowledgementRemoved = "post_acknowledgement_removed"
|
||||
)
|
||||
|
||||
Ссылка в новой задаче
Block a user