APIv4 POST /channels/group (#6166)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
be9624e2ad
Коммит
b0e5713680
@@ -17,6 +17,7 @@ func InitChannel() {
|
|||||||
|
|
||||||
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
|
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
|
||||||
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")
|
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")
|
||||||
|
BaseRoutes.Channels.Handle("/group", ApiSessionRequired(createGroupChannel)).Methods("POST")
|
||||||
BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST")
|
BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST")
|
||||||
|
|
||||||
BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
|
BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
|
||||||
@@ -230,6 +231,43 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
userIds := model.ArrayFromJson(r.Body)
|
||||||
|
|
||||||
|
if len(userIds) == 0 {
|
||||||
|
c.SetInvalidParam("user_ids")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, id := range userIds {
|
||||||
|
if len(id) != 26 {
|
||||||
|
c.SetInvalidParam("user_id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if id == c.Session.UserId {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
userIds = append(userIds, c.Session.UserId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_CREATE_GROUP_CHANNEL) {
|
||||||
|
c.SetPermissionError(model.PERMISSION_CREATE_GROUP_CHANNEL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if groupChannel, err := app.CreateGroupChannel(userIds); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
w.Write([]byte(groupChannel.ToJson()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
c.RequireChannelId()
|
c.RequireChannelId()
|
||||||
if c.Err != nil {
|
if c.Err != nil {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package api4
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -367,6 +368,78 @@ func TestCreateDirectChannel(t *testing.T) {
|
|||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateGroupChannel(t *testing.T) {
|
||||||
|
th := Setup().InitBasic().InitSystemAdmin()
|
||||||
|
defer TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
user := th.BasicUser
|
||||||
|
user2 := th.BasicUser2
|
||||||
|
user3 := th.CreateUser()
|
||||||
|
|
||||||
|
userIds := []string{user.Id, user2.Id, user3.Id}
|
||||||
|
|
||||||
|
rgc, resp := Client.CreateGroupChannel(userIds)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
CheckCreatedStatus(t, resp)
|
||||||
|
|
||||||
|
if rgc == nil {
|
||||||
|
t.Fatal("should have created a group channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
if rgc.Type != model.CHANNEL_GROUP {
|
||||||
|
t.Fatal("should have created a channel of group type")
|
||||||
|
}
|
||||||
|
|
||||||
|
m, _ := app.GetChannelMembersPage(rgc.Id, 0, 10)
|
||||||
|
if len(*m) != 3 {
|
||||||
|
t.Fatal("should have 3 channel members")
|
||||||
|
}
|
||||||
|
|
||||||
|
// saving duplicate group channel
|
||||||
|
rgc2, resp := Client.CreateGroupChannel([]string{user3.Id, user2.Id})
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
if rgc.Id != rgc2.Id {
|
||||||
|
t.Fatal("should have returned existing channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
m2, _ := app.GetChannelMembersPage(rgc2.Id, 0, 10)
|
||||||
|
if !reflect.DeepEqual(*m, *m2) {
|
||||||
|
t.Fatal("should be equal")
|
||||||
|
}
|
||||||
|
|
||||||
|
rgc, resp = Client.CreateGroupChannel([]string{user2.Id})
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
user4 := th.CreateUser()
|
||||||
|
user5 := th.CreateUser()
|
||||||
|
user6 := th.CreateUser()
|
||||||
|
user7 := th.CreateUser()
|
||||||
|
user8 := th.CreateUser()
|
||||||
|
user9 := th.CreateUser()
|
||||||
|
|
||||||
|
rgc, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, user4.Id, user5.Id, user6.Id, user7.Id, user8.Id, user9.Id})
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
if rgc != nil {
|
||||||
|
t.Fatal("should return nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, GenerateTestId()})
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"})
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
|
Client.Logout()
|
||||||
|
|
||||||
|
_, resp = Client.CreateGroupChannel(userIds)
|
||||||
|
CheckUnauthorizedStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = th.SystemAdminClient.CreateGroupChannel(userIds)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetChannel(t *testing.T) {
|
func TestGetChannel(t *testing.T) {
|
||||||
th := Setup().InitBasic().InitSystemAdmin()
|
th := Setup().InitBasic().InitSystemAdmin()
|
||||||
defer TearDown()
|
defer TearDown()
|
||||||
|
|||||||
@@ -1181,6 +1181,16 @@ func (c *Client4) CreateDirectChannel(userId1, userId2 string) (*Channel, *Respo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateGroupChannel creates a group message channel based on userIds provided
|
||||||
|
func (c *Client4) CreateGroupChannel(userIds []string) (*Channel, *Response) {
|
||||||
|
if r, err := c.DoApiPost(c.GetChannelsRoute()+"/group", ArrayToJson(userIds)); err != nil {
|
||||||
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
|
} else {
|
||||||
|
defer closeBody(r)
|
||||||
|
return ChannelFromJson(r.Body), BuildResponse(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetChannel returns a channel based on the provided channel id string.
|
// GetChannel returns a channel based on the provided channel id string.
|
||||||
func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response) {
|
func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response) {
|
||||||
if r, err := c.DoApiGet(c.GetChannelRoute(channelId), etag); err != nil {
|
if r, err := c.DoApiGet(c.GetChannelRoute(channelId), etag); err != nil {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user