From 0685afd1d197584e78a111934754d7111bb2dbe8 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Tue, 17 Jan 2017 20:45:18 +0000 Subject: PLT-2978: Add channel purpose change system message. (#5094) Completes original patch by David Lu. --- api/channel.go | 5 ++++ api/channel_test.go | 11 ++++++++ app/channel.go | 35 ++++++++++++++++++++++++ i18n/en.json | 20 ++++++++++++++ model/post.go | 3 +- webapp/components/edit_channel_purpose_modal.jsx | 9 ++++-- 6 files changed, 80 insertions(+), 3 deletions(-) diff --git a/api/channel.go b/api/channel.go index 590409921..b8b73bf38 100644 --- a/api/channel.go +++ b/api/channel.go @@ -262,6 +262,7 @@ func updateChannelHeader(c *Context, w http.ResponseWriter, r *http.Request) { } func updateChannelPurpose(c *Context, w http.ResponseWriter, r *http.Request) { + props := model.MapFromJson(r.Body) channelId := props["channel_id"] if len(channelId) != 26 { @@ -292,6 +293,7 @@ func updateChannelPurpose(c *Context, w http.ResponseWriter, r *http.Request) { return } + oldChannelPurpose := channel.Purpose channel.Purpose = channelPurpose app.InvalidateCacheForChannel(channel.Id) @@ -299,6 +301,9 @@ func updateChannelPurpose(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = ucresult.Err return } else { + if err := app.PostUpdateChannelPurposeMessage(c.Session.UserId, channel.Id, c.TeamId, oldChannelPurpose, channelPurpose); err != nil { + l4g.Error(err.Error()) + } c.LogAudit("name=" + channel.Name) w.Write([]byte(channel.ToJson())) } diff --git a/api/channel_test.go b/api/channel_test.go index 1bce8b9fd..119578e8b 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -630,6 +630,17 @@ func TestUpdateChannelPurpose(t *testing.T) { upChannel1 = result.Data.(*model.Channel) } + time.Sleep(100 * time.Millisecond) + + r1 := Client.Must(Client.GetPosts(channel1.Id, 0, 1, "")).Data.(*model.PostList) + if len(r1.Order) != 1 { + t.Fatal("Purpose update system message was not found") + } else if val, ok := r1.Posts[r1.Order[0]].Props["old_purpose"]; !ok || val != "" { + t.Fatal("Props should contain old_header with old purpose value") + } else if val, ok := r1.Posts[r1.Order[0]].Props["new_purpose"]; !ok || val != "new purpose" { + t.Fatal("Props should contain new_header with new purpose value") + } + if upChannel1.Purpose != data["channel_purpose"] { t.Fatal("Failed to update purpose") } diff --git a/app/channel.go b/app/channel.go index 9451ca974..aa84e12be 100644 --- a/app/channel.go +++ b/app/channel.go @@ -316,6 +316,41 @@ func PostUpdateChannelHeaderMessage(userId string, channelId string, teamId stri return nil } +func PostUpdateChannelPurposeMessage(userId string, channelId string, teamId string, oldChannelPurpose string, newChannelPurpose string) *model.AppError { + uc := Srv.Store.User().Get(userId) + + if uresult := <-uc; uresult.Err != nil { + return model.NewLocAppError("PostUpdateChannelPurposeMessage", "app.channel.post_update_channel_purpose_message.retrieve_user.error", nil, uresult.Err.Error()) + } else { + user := uresult.Data.(*model.User) + + var message string + if oldChannelPurpose == "" { + message = fmt.Sprintf(utils.T("app.channel.post_update_channel_purpose_message.updated_to"), user.Username, newChannelPurpose) + } else if newChannelPurpose == "" { + message = fmt.Sprintf(utils.T("app.channel.post_update_channel_purpose_message.removed"), user.Username, oldChannelPurpose) + } else { + message = fmt.Sprintf(utils.T("app.channel.post_update_channel_purpose_message.updated_from"), user.Username, oldChannelPurpose, newChannelPurpose) + } + + post := &model.Post{ + ChannelId: channelId, + Message: message, + Type: model.POST_PURPOSE_CHANGE, + UserId: userId, + Props: model.StringInterface{ + "old_purpose": oldChannelPurpose, + "new_purpose": newChannelPurpose, + }, + } + if _, err := CreatePost(post, teamId, false); err != nil { + return model.NewLocAppError("", "app.channel.post_update_channel_purpose_message.post.error", nil, err.Error()) + } + } + + return nil +} + func PostUpdateChannelDisplayNameMessage(userId string, channelId string, teamId string, oldChannelDisplayName, newChannelDisplayName string) *model.AppError { uc := Srv.Store.User().Get(userId) diff --git a/i18n/en.json b/i18n/en.json index f4b752dc1..84306f0ce 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -331,6 +331,26 @@ "id": "api.channel.post_update_channel_header_message_and_forget.updated_to", "translation": "%s updated the channel header to: %s" }, + { + "id": "app.channel.post_update_channel_purpose_message.post.error", + "translation": "Failed to post channel purpose message" + }, + { + "id": "app.channel.post_update_channel_purpose_message.removed", + "translation": "%s removed the channel purpose (was: %s)" + }, + { + "id": "app.channel.post_update_channel_purpose_message.retrieve_user.error", + "translation": "Failed to retrieve user while trying to save update channel purpose message %v" + }, + { + "id": "app.channel.post_update_channel_purpose_message.updated_from", + "translation": "%s updated the channel purpose from: %s to: %s" + }, + { + "id": "app.channel.post_update_channel_purpose_message.updated_to", + "translation": "%s updated the channel purpose to: %s" + }, { "id": "api.channel.post_user_add_remove_message_and_forget.error", "translation": "Failed to post join/leave message" diff --git a/model/post.go b/model/post.go index 766324b4c..668c4db61 100644 --- a/model/post.go +++ b/model/post.go @@ -18,6 +18,7 @@ const ( POST_ADD_REMOVE = "system_add_remove" POST_HEADER_CHANGE = "system_header_change" POST_DISPLAYNAME_CHANGE = "system_displayname_change" + POST_PURPOSE_CHANGE = "system_purpose_change" POST_CHANNEL_DELETED = "system_channel_deleted" POST_EPHEMERAL = "system_ephemeral" POST_FILEIDS_MAX_RUNES = 150 @@ -120,7 +121,7 @@ func (o *Post) IsValid() *AppError { // 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_SLACK_ATTACHMENT || o.Type == POST_HEADER_CHANGE || + 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) { return NewLocAppError("Post.IsValid", "model.post.is_valid.type.app_error", nil, "id="+o.Type) } diff --git a/webapp/components/edit_channel_purpose_modal.jsx b/webapp/components/edit_channel_purpose_modal.jsx index 7ba2eff2c..21bf013a9 100644 --- a/webapp/components/edit_channel_purpose_modal.jsx +++ b/webapp/components/edit_channel_purpose_modal.jsx @@ -2,7 +2,7 @@ // See License.txt for license information. import PreferenceStore from 'stores/preference_store.jsx'; - +import AppDispatcher from '../dispatcher/app_dispatcher.jsx'; import * as AsyncClient from 'utils/async_client.jsx'; import Client from 'client/web_client.jsx'; import Constants from 'utils/constants.jsx'; @@ -67,10 +67,15 @@ export default class EditChannelPurposeModal extends React.Component { Client.updateChannelPurpose( this.props.channel.id, this.refs.purpose.value.trim(), - () => { + (channel) => { AsyncClient.getChannel(this.props.channel.id); this.handleHide(); + + AppDispatcher.handleServerAction({ + type: Constants.ActionTypes.RECEIVED_CHANNEL, + channel + }); }, (err) => { if (err.id === 'api.context.invalid_param.app_error') { -- cgit v1.2.3-1-g7c22