add update channel endpoint for APIv4 (#5624)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
e9c6cc269b
Коммит
b299bc8999
@@ -21,6 +21,7 @@ func InitChannel() {
|
|||||||
BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
|
BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
|
||||||
|
|
||||||
BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
|
BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
|
||||||
|
BaseRoutes.Channel.Handle("", ApiSessionRequired(updateChannel)).Methods("PUT")
|
||||||
BaseRoutes.ChannelByName.Handle("", ApiSessionRequired(getChannelByName)).Methods("GET")
|
BaseRoutes.ChannelByName.Handle("", ApiSessionRequired(getChannelByName)).Methods("GET")
|
||||||
BaseRoutes.ChannelByNameForTeamName.Handle("", ApiSessionRequired(getChannelByNameForTeamName)).Methods("GET")
|
BaseRoutes.ChannelByNameForTeamName.Handle("", ApiSessionRequired(getChannelByNameForTeamName)).Methods("GET")
|
||||||
|
|
||||||
@@ -59,6 +60,94 @@ func createChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
c.RequireChannelId()
|
||||||
|
if c.Err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
channel := model.ChannelFromJson(r.Body)
|
||||||
|
|
||||||
|
if channel == nil {
|
||||||
|
c.SetInvalidParam("channel")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var oldChannel *model.Channel
|
||||||
|
var err *model.AppError
|
||||||
|
if oldChannel, err = app.GetChannel(channel.Id); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = app.GetChannelMember(channel.Id, c.Session.UserId); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !CanManageChannel(c, channel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldChannel.DeleteAt > 0 {
|
||||||
|
c.Err = model.NewLocAppError("updateChannel", "api.channel.update_channel.deleted.app_error", nil, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldChannel.Name == model.DEFAULT_CHANNEL {
|
||||||
|
if (len(channel.Name) > 0 && channel.Name != oldChannel.Name) || (len(channel.Type) > 0 && channel.Type != oldChannel.Type) {
|
||||||
|
c.Err = model.NewLocAppError("updateChannel", "api.channel.update_channel.tried.app_error", map[string]interface{}{"Channel": model.DEFAULT_CHANNEL}, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
oldChannel.Header = channel.Header
|
||||||
|
oldChannel.Purpose = channel.Purpose
|
||||||
|
|
||||||
|
oldChannelDisplayName := oldChannel.DisplayName
|
||||||
|
|
||||||
|
if len(channel.DisplayName) > 0 {
|
||||||
|
oldChannel.DisplayName = channel.DisplayName
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(channel.Name) > 0 {
|
||||||
|
oldChannel.Name = channel.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(channel.Type) > 0 {
|
||||||
|
oldChannel.Type = channel.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := app.UpdateChannel(oldChannel); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
if oldChannelDisplayName != channel.DisplayName {
|
||||||
|
if err := app.PostUpdateChannelDisplayNameMessage(c.Session.UserId, channel.Id, c.Params.TeamId, oldChannelDisplayName, channel.DisplayName); err != nil {
|
||||||
|
l4g.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.LogAudit("name=" + channel.Name)
|
||||||
|
w.Write([]byte(oldChannel.ToJson()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CanManageChannel(c *Context, channel *model.Channel) bool {
|
||||||
|
if channel.Type == model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) {
|
||||||
|
c.SetPermissionError(model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if channel.Type == model.CHANNEL_PRIVATE && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES) {
|
||||||
|
c.SetPermissionError(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
userIds := model.ArrayFromJson(r.Body)
|
userIds := model.ArrayFromJson(r.Body)
|
||||||
allowed := false
|
allowed := false
|
||||||
|
|||||||
@@ -171,6 +171,78 @@ func TestCreateChannel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateChannel(t *testing.T) {
|
||||||
|
th := Setup().InitBasic().InitSystemAdmin()
|
||||||
|
defer TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
team := th.BasicTeam
|
||||||
|
|
||||||
|
channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||||
|
private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
|
||||||
|
|
||||||
|
channel, resp := Client.CreateChannel(channel)
|
||||||
|
private, resp = Client.CreateChannel(private)
|
||||||
|
|
||||||
|
//Update a open channel
|
||||||
|
channel.DisplayName = "My new display name"
|
||||||
|
channel.Header = "My fancy header"
|
||||||
|
channel.Purpose = "Mattermost ftw!"
|
||||||
|
|
||||||
|
newChannel, resp := Client.UpdateChannel(channel)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
if newChannel.DisplayName != channel.DisplayName {
|
||||||
|
t.Fatal("Update failed for DisplayName")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newChannel.Header != channel.Header {
|
||||||
|
t.Fatal("Update failed for Header")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newChannel.Purpose != channel.Purpose {
|
||||||
|
t.Fatal("Update failed for Purpose")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Update a private channel
|
||||||
|
private.DisplayName = "My new display name for private channel"
|
||||||
|
private.Header = "My fancy private header"
|
||||||
|
private.Purpose = "Mattermost ftw! in private mode"
|
||||||
|
|
||||||
|
newPrivateChannel, resp := Client.UpdateChannel(private)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
if newPrivateChannel.DisplayName != private.DisplayName {
|
||||||
|
t.Fatal("Update failed for DisplayName in private channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newPrivateChannel.Header != private.Header {
|
||||||
|
t.Fatal("Update failed for Header in private channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newPrivateChannel.Purpose != private.Purpose {
|
||||||
|
t.Fatal("Update failed for Purpose in private channel")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Non existing channel
|
||||||
|
channel1 := &model.Channel{DisplayName: "Test API Name for apiv4", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||||
|
_, resp = Client.UpdateChannel(channel1)
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
|
||||||
|
//Try to update with not logged user
|
||||||
|
Client.Logout()
|
||||||
|
_, resp = Client.UpdateChannel(channel)
|
||||||
|
CheckUnauthorizedStatus(t, resp)
|
||||||
|
|
||||||
|
//Try to update using another user
|
||||||
|
user := th.CreateUser()
|
||||||
|
Client.Login(user.Email, user.Password)
|
||||||
|
|
||||||
|
channel.DisplayName = "Should not update"
|
||||||
|
_, resp = Client.UpdateChannel(channel)
|
||||||
|
CheckNotFoundStatus(t, resp)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateDirectChannel(t *testing.T) {
|
func TestCreateDirectChannel(t *testing.T) {
|
||||||
th := Setup().InitBasic().InitSystemAdmin()
|
th := Setup().InitBasic().InitSystemAdmin()
|
||||||
defer TearDown()
|
defer TearDown()
|
||||||
|
|||||||
@@ -741,6 +741,16 @@ func (c *Client4) CreateChannel(channel *Channel) (*Channel, *Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateChannel update a channel based on the provided channel struct.
|
||||||
|
func (c *Client4) UpdateChannel(channel *Channel) (*Channel, *Response) {
|
||||||
|
if r, err := c.DoApiPut(c.GetChannelRoute(channel.Id), channel.ToJson()); err != nil {
|
||||||
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
|
} else {
|
||||||
|
defer closeBody(r)
|
||||||
|
return ChannelFromJson(r.Body), BuildResponse(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CreateDirectChannel creates a direct message channel based on the two user
|
// CreateDirectChannel creates a direct message channel based on the two user
|
||||||
// ids provided.
|
// ids provided.
|
||||||
func (c *Client4) CreateDirectChannel(userId1, userId2 string) (*Channel, *Response) {
|
func (c *Client4) CreateDirectChannel(userId1, userId2 string) (*Channel, *Response) {
|
||||||
|
|||||||
@@ -314,7 +314,7 @@ func (us SqlChannelStore) InvalidateChannel(id string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (us SqlChannelStore) InvalidateChannelByName(teamId, name string) {
|
func (us SqlChannelStore) InvalidateChannelByName(teamId, name string) {
|
||||||
channelCache.Remove(teamId + name)
|
channelByNameCache.Remove(teamId + name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) Get(id string, allowFromCache bool) StoreChannel {
|
func (s SqlChannelStore) Get(id string, allowFromCache bool) StoreChannel {
|
||||||
@@ -684,7 +684,6 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.GetReplica().SelectOne(&channel, query, map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil {
|
if err := s.GetReplica().SelectOne(&channel, query, map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.GetByName", MISSING_CHANNEL_ERROR, nil, "teamId="+teamId+", "+"name="+name+", "+err.Error())
|
result.Err = model.NewLocAppError("SqlChannelStore.GetByName", MISSING_CHANNEL_ERROR, nil, "teamId="+teamId+", "+"name="+name+", "+err.Error())
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user