From 1bd66589a2adc67df5df9c108a2f2ecc77dfdf44 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 27 Sep 2017 11:09:09 -0400 Subject: Allow custom post types (#7468) --- model/post.go | 6 ++++-- model/post_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/model/post.go b/model/post.go index d4fff7ef9..523a97c73 100644 --- a/model/post.go +++ b/model/post.go @@ -7,6 +7,7 @@ import ( "encoding/json" "io" "net/http" + "strings" "unicode/utf8" ) @@ -31,6 +32,7 @@ const ( POST_HASHTAGS_MAX_RUNES = 1000 POST_MESSAGE_MAX_RUNES = 4000 POST_PROPS_MAX_RUNES = 8000 + POST_CUSTOM_TYPE_PREFIX = "custom_" ) type Post struct { @@ -162,12 +164,12 @@ func (o *Post) IsValid() *AppError { return NewAppError("Post.IsValid", "model.post.is_valid.hashtags.app_error", nil, "id="+o.Id, http.StatusBadRequest) } - // should be removed once more message types are supported if !(o.Type == POST_DEFAULT || o.Type == POST_JOIN_LEAVE || o.Type == POST_ADD_REMOVE || o.Type == POST_JOIN_CHANNEL || o.Type == POST_LEAVE_CHANNEL || o.Type == POST_REMOVE_FROM_CHANNEL || o.Type == POST_ADD_TO_CHANNEL || o.Type == POST_SLACK_ATTACHMENT || o.Type == POST_HEADER_CHANGE || o.Type == POST_PURPOSE_CHANGE || - o.Type == POST_DISPLAYNAME_CHANGE || o.Type == POST_CHANNEL_DELETED) { + o.Type == POST_DISPLAYNAME_CHANGE || o.Type == POST_CHANNEL_DELETED || + strings.HasPrefix(o.Type, POST_CUSTOM_TYPE_PREFIX)) { return NewAppError("Post.IsValid", "model.post.is_valid.type.app_error", nil, "id="+o.Type, http.StatusBadRequest) } diff --git a/model/post_test.go b/model/post_test.go index 1096ccbd8..ced84f26b 100644 --- a/model/post_test.go +++ b/model/post_test.go @@ -78,6 +78,16 @@ func TestPostIsValid(t *testing.T) { if err := o.IsValid(); err != nil { t.Fatal(err) } + + o.Type = "junk" + if err := o.IsValid(); err == nil { + t.Fatal("should be invalid") + } + + o.Type = POST_CUSTOM_TYPE_PREFIX + "type" + if err := o.IsValid(); err != nil { + t.Fatal(err) + } } func TestPostPreSave(t *testing.T) { -- cgit v1.2.3-1-g7c22