APIv4 DELETE /users/{user_id}/posts/{post_id}/reactions/name (#6117)
* APIv4 DELETE /users/{user_id}/posts/{post_id}/reactions/name
* updated v3 deleteReaction endpoint
* update parameter of app.DeleteReactionForPost()
* update utils.IsValidAlphaNum, add utils.IsValidAlphaNumHyphenUnderscore, and add related tests
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
e62afeace0
Коммит
ecb10ed62f
@@ -2533,3 +2533,13 @@ func (c *Client4) GetReactions(postId string) ([]*Reaction, *Response) {
|
||||
return ReactionsFromJson(r.Body), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteReaction deletes reaction of a user in a post.
|
||||
func (c *Client4) DeleteReaction(reaction *Reaction) (bool, *Response) {
|
||||
if r, err := c.DoApiDelete(c.GetUserRoute(reaction.UserId) + c.GetPostRoute(reaction.PostId) + fmt.Sprintf("/reactions/%v", reaction.EmojiName)); err != nil {
|
||||
return false, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return CheckStatusOK(r), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (emoji *Emoji) IsValid() *AppError {
|
||||
return NewLocAppError("Emoji.IsValid", "model.emoji.user_id.app_error", nil, "")
|
||||
}
|
||||
|
||||
if len(emoji.Name) == 0 || len(emoji.Name) > 64 {
|
||||
if len(emoji.Name) == 0 || len(emoji.Name) > 64 || !IsValidAlphaNumHyphenUnderscore(emoji.Name, false) {
|
||||
return NewLocAppError("Emoji.IsValid", "model.emoji.name.app_error", nil, "")
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,28 @@ func TestEmojiIsValid(t *testing.T) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
emoji.Name = ""
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
emoji.Name = strings.Repeat("1", 64)
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
emoji.Name = "name-"
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
emoji.Name = "name_"
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
emoji.Name = "name:"
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ func (o *Reaction) IsValid() *AppError {
|
||||
return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId)
|
||||
}
|
||||
|
||||
if len(o.EmojiName) == 0 || len(o.EmojiName) > 64 {
|
||||
if len(o.EmojiName) == 0 || len(o.EmojiName) > 64 || !IsValidAlphaNumHyphenUnderscore(o.EmojiName, false) {
|
||||
return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName)
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,21 @@ func TestReactionIsValid(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reaction.EmojiName = "emoji-"
|
||||
if err := reaction.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reaction.EmojiName = "emoji_"
|
||||
if err := reaction.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reaction.EmojiName = "emoji:"
|
||||
if err := reaction.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reaction.CreateAt = 0
|
||||
if err := reaction.IsValid(); err == nil {
|
||||
t.Fatal("create at should be invalid")
|
||||
|
||||
@@ -233,7 +233,7 @@ func IsReservedTeamName(s string) bool {
|
||||
|
||||
func IsValidTeamName(s string) bool {
|
||||
|
||||
if !IsValidAlphaNum(s, false) {
|
||||
if !IsValidAlphaNum(s) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -297,7 +297,7 @@ var reservedName = []string{
|
||||
|
||||
func IsValidChannelIdentifier(s string) bool {
|
||||
|
||||
if !IsValidAlphaNum(s, true) {
|
||||
if !IsValidAlphaNumHyphenUnderscore(s, true) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -308,22 +308,20 @@ func IsValidChannelIdentifier(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
var validAlphaNumUnderscore = regexp.MustCompile(`^[a-z0-9]+([a-z\-\_0-9]+|(__)?)[a-z0-9]+$`)
|
||||
var validAlphaNum = regexp.MustCompile(`^[a-z0-9]+([a-z\-0-9]+|(__)?)[a-z0-9]+$`)
|
||||
func IsValidAlphaNum(s string) bool {
|
||||
validAlphaNum := regexp.MustCompile(`^[a-z0-9]+([a-z\-0-9]+|(__)?)[a-z0-9]+$`)
|
||||
|
||||
func IsValidAlphaNum(s string, allowUnderscores bool) bool {
|
||||
var match bool
|
||||
if allowUnderscores {
|
||||
match = validAlphaNumUnderscore.MatchString(s)
|
||||
} else {
|
||||
match = validAlphaNum.MatchString(s)
|
||||
return validAlphaNum.MatchString(s)
|
||||
}
|
||||
|
||||
func IsValidAlphaNumHyphenUnderscore(s string, withFormat bool) bool {
|
||||
if withFormat {
|
||||
validAlphaNumHyphenUnderscore := regexp.MustCompile(`^[a-z0-9]+([a-z\-\_0-9]+|(__)?)[a-z0-9]+$`)
|
||||
return validAlphaNumHyphenUnderscore.MatchString(s)
|
||||
}
|
||||
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
validSimpleAlphaNumHyphenUnderscore := regexp.MustCompile(`^[a-zA-Z0-9\-_]+$`)
|
||||
return validSimpleAlphaNumHyphenUnderscore.MatchString(s)
|
||||
}
|
||||
|
||||
func Etag(parts ...interface{}) string {
|
||||
|
||||
@@ -137,3 +137,191 @@ func TestParseHashtags(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidAlphaNum(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input string
|
||||
Result bool
|
||||
}{
|
||||
{
|
||||
Input: "test",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test-name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test--name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test__name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "-",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "__",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test-",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test--",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test__",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test:name",
|
||||
Result: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
actual := IsValidAlphaNum(tc.Input)
|
||||
if actual != tc.Result {
|
||||
t.Fatalf("case: %v\tshould returned: %#v", tc, tc.Result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidAlphaNumHyphenUnderscore(t *testing.T) {
|
||||
casesWithFormat := []struct {
|
||||
Input string
|
||||
Result bool
|
||||
}{
|
||||
{
|
||||
Input: "test",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test-name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test--name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test__name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test_name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test_-name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "-",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "__",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test-",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test--",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test__",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test:name",
|
||||
Result: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range casesWithFormat {
|
||||
actual := IsValidAlphaNumHyphenUnderscore(tc.Input, true)
|
||||
if actual != tc.Result {
|
||||
t.Fatalf("case: %v\tshould returned: %#v", tc, tc.Result)
|
||||
}
|
||||
}
|
||||
|
||||
casesWithoutFormat := []struct {
|
||||
Input string
|
||||
Result bool
|
||||
}{
|
||||
{
|
||||
Input: "test",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test-name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test--name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test__name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test_name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test_-name",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "-",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "_",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test-",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test--",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: "test__",
|
||||
Result: true,
|
||||
},
|
||||
{
|
||||
Input: ".",
|
||||
Result: false,
|
||||
},
|
||||
|
||||
{
|
||||
Input: "test,",
|
||||
Result: false,
|
||||
},
|
||||
{
|
||||
Input: "test:name",
|
||||
Result: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range casesWithoutFormat {
|
||||
actual := IsValidAlphaNumHyphenUnderscore(tc.Input, false)
|
||||
if actual != tc.Result {
|
||||
t.Fatalf("case: '%v'\tshould returned: %#v", tc.Input, tc.Result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user