summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/channel.go41
-rw-r--r--app/channel_test.go15
2 files changed, 56 insertions, 0 deletions
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
diff --git a/app/channel_test.go b/app/channel_test.go
index d44af467d..a414fbb35 100644
--- a/app/channel_test.go
+++ b/app/channel_test.go
@@ -190,6 +190,21 @@ func TestCreateChannelPrivate(t *testing.T) {
assert.Equal(t, privateChannel.Id, histories[0].ChannelId)
}
+func TestUpdateChannelPrivacy(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
+ privateChannel.Type = model.CHANNEL_OPEN
+
+ if publicChannel, err := th.App.UpdateChannelPrivacy(privateChannel, th.BasicUser); err != nil {
+ t.Fatal("Failed to update channel privacy. Error: " + err.Error())
+ } else {
+ assert.Equal(t, publicChannel.Id, privateChannel.Id)
+ assert.Equal(t, publicChannel.Type, model.CHANNEL_OPEN)
+ }
+}
+
func TestCreateGroupChannel(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()