From 1909fd66071b6f12c81d6ddbd14c6e146f1c23be Mon Sep 17 00:00:00 2001 From: Allen Lai Date: Thu, 9 Jan 2020 00:42:29 -0800 Subject: [PATCH] [MM-20804] Add "Unarchive Channel" option to the channel info screen (#13349) * add websocket hooks to restoreChannel * add userId to call in api4 * add constant * undo changes in en.json * add constants to en.json * delete code for deleting webhooks * Update i18n/en.json Co-Authored-By: Ben Schumacher * restore channel ordering * delete Props * Update app/channel.go Co-Authored-By: Ben Schumacher * Update app/channel.go Co-Authored-By: Ben Schumacher * change message format * move WS message right after invalidate * manually reorder en.json entry * reorder en.json * undelete to restore * update en.json ordering * update wording * Update model/post.go Co-Authored-By: Saturnino Abril * Update model/websocket_message.go Co-Authored-By: Saturnino Abril * gofmt * undeleted to restored * add test to success case * delete repeated test Co-authored-by: Ben Schumacher Co-authored-by: Saturnino Abril Co-authored-by: mattermod --- api4/channel.go | 2 +- app/channel.go | 32 +++++++++++++++++++++++++++++++- i18n/en.json | 8 ++++++++ model/post.go | 2 ++ model/websocket_message.go | 1 + 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index 76f6c82570..4b6eb66e6a 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -358,7 +358,7 @@ func restoreChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - channel, err = c.App.RestoreChannel(channel) + channel, err = c.App.RestoreChannel(channel, c.App.Session.UserId) if err != nil { c.Err = err return diff --git a/app/channel.go b/app/channel.go index 3387301cf7..180f839cfe 100644 --- a/app/channel.go +++ b/app/channel.go @@ -605,10 +605,40 @@ func (a *App) postChannelPrivacyMessage(user *model.User, channel *model.Channel return nil } -func (a *App) RestoreChannel(channel *model.Channel) (*model.Channel, *model.AppError) { +func (a *App) RestoreChannel(channel *model.Channel, userId string) (*model.Channel, *model.AppError) { + if channel.DeleteAt == 0 { + return nil, model.NewAppError("restoreChannel", "api.channel.restore_channel.restored.app_error", nil, "", http.StatusBadRequest) + } + if err := a.Srv.Store.Channel().Restore(channel.Id, model.GetMillis()); err != nil { return nil, err } + a.InvalidateCacheForChannel(channel) + + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_RESTORED, channel.TeamId, "", "", nil) + message.Add("channel_id", channel.Id) + a.Publish(message) + + user, err := a.Srv.Store.User().Get(userId) + if err != nil { + return nil, err + } + + if user != nil { + T := utils.GetUserTranslations(user.Locale) + + post := &model.Post{ + ChannelId: channel.Id, + Message: T("api.channel.restore_channel.unarchived", map[string]interface{}{"Username": user.Username}), + Type: model.POST_CHANNEL_RESTORED, + UserId: userId, + } + + if _, err := a.CreatePost(post, channel, false); err != nil { + mlog.Error("Failed to post unarchive message", mlog.Err(err)) + } + } + return channel, nil } diff --git a/i18n/en.json b/i18n/en.json index 3a8542dfa1..7f4eec7b1e 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -367,6 +367,14 @@ "id": "api.channel.rename_channel.cant_rename_group_messages.app_error", "translation": "You cannot rename a group message channel" }, + { + "id": "api.channel.restore_channel.restored.app_error", + "translation": "Unable to unarchive channel. The channel is not archived." + }, + { + "id": "api.channel.restore_channel.unarchived", + "translation": "{{.Username}} unarchived the channel." + }, { "id": "api.channel.update_channel.deleted.app_error", "translation": "The channel has been archived or deleted" diff --git a/model/post.go b/model/post.go index 948ac11497..06899d20f5 100644 --- a/model/post.go +++ b/model/post.go @@ -38,6 +38,7 @@ const ( POST_CONVERT_CHANNEL = "system_convert_channel" POST_PURPOSE_CHANGE = "system_purpose_change" POST_CHANNEL_DELETED = "system_channel_deleted" + POST_CHANNEL_RESTORED = "system_channel_restored" POST_EPHEMERAL = "system_ephemeral" POST_CHANGE_CHANNEL_PRIVACY = "system_change_chan_privacy" POST_ADD_BOT_TEAMS_CHANNELS = "add_bot_teams_channels" @@ -264,6 +265,7 @@ func (o *Post) IsValid(maxPostSize int) *AppError { POST_DISPLAYNAME_CHANGE, POST_CONVERT_CHANNEL, POST_CHANNEL_DELETED, + POST_CHANNEL_RESTORED, POST_CHANGE_CHANNEL_PRIVACY, POST_ME, POST_ADD_BOT_TEAMS_CHANNELS: diff --git a/model/websocket_message.go b/model/websocket_message.go index cb0a801d28..915488963f 100644 --- a/model/websocket_message.go +++ b/model/websocket_message.go @@ -18,6 +18,7 @@ const ( WEBSOCKET_EVENT_CHANNEL_CONVERTED = "channel_converted" WEBSOCKET_EVENT_CHANNEL_CREATED = "channel_created" WEBSOCKET_EVENT_CHANNEL_DELETED = "channel_deleted" + WEBSOCKET_EVENT_CHANNEL_RESTORED = "channel_restored" WEBSOCKET_EVENT_CHANNEL_UPDATED = "channel_updated" WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED = "channel_member_updated" WEBSOCKET_EVENT_DIRECT_ADDED = "direct_added"