From 9d6a9ff4be15f673e8364a984ccc2b2c3a465b73 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Fri, 19 Jan 2018 00:05:00 +0300 Subject: Post a system message to the affected channel by CLI command (#7877) (#7968) --- app/channel.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/channel.go') diff --git a/app/channel.go b/app/channel.go index 4d7eb7ef3..68963a94a 100644 --- a/app/channel.go +++ b/app/channel.go @@ -343,6 +343,47 @@ func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppE } } +func (a *App) UpdateChannelPrivacy(oldChannel *model.Channel, user *model.User) (*model.Channel, *model.AppError) { + if channel, err := a.UpdateChannel(oldChannel); err != nil { + return channel, err + } else { + if err := a.postChannelPrivacyMessage(user, channel); err != nil { + if channel.Type == model.CHANNEL_OPEN { + channel.Type = model.CHANNEL_PRIVATE + } else { + channel.Type = model.CHANNEL_OPEN + } + // revert to previous channel privacy + a.UpdateChannel(channel) + return channel, err + } + + return channel, nil + } +} + +func (a *App) postChannelPrivacyMessage(user *model.User, channel *model.Channel) *model.AppError { + privacy := (map[string]string{ + model.CHANNEL_OPEN: "private_to_public", + model.CHANNEL_PRIVATE: "public_to_private", + })[channel.Type] + post := &model.Post{ + ChannelId: channel.Id, + Message: fmt.Sprintf(utils.T("api.channel.change_channel_privacy." + privacy)), + Type: model.POST_CHANGE_CHANNEL_PRIVACY, + UserId: user.Id, + Props: model.StringInterface{ + "username": user.Username, + }, + } + + if _, err := a.CreatePost(post, channel, false); err != nil { + return model.NewAppError("postChannelPrivacyMessage", "api.channel.post_channel_privacy_message.error", nil, err.Error(), http.StatusInternalServerError) + } + + return nil +} + func (a *App) RestoreChannel(channel *model.Channel) (*model.Channel, *model.AppError) { if result := <-a.Srv.Store.Channel().Restore(channel.Id, model.GetMillis()); result.Err != nil { return nil, result.Err -- cgit v1.2.3-1-g7c22