diff options
author | Stan Chan <stanchan@users.noreply.github.com> | 2018-04-12 12:02:36 -0700 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2018-04-12 15:02:36 -0400 |
commit | 7826774a14ebf9a3bab11069a8f2ff8947fe931a (patch) | |
tree | 8159c364cb70a640cf737e1abe24751b58b741cc /app/auto_responder.go | |
parent | 8df6d5cc30d22a608c58635449047e421add82ae (diff) | |
download | chat-7826774a14ebf9a3bab11069a8f2ff8947fe931a.tar.gz chat-7826774a14ebf9a3bab11069a8f2ff8947fe931a.tar.bz2 chat-7826774a14ebf9a3bab11069a8f2ff8947fe931a.zip |
Add Auto Responder handler (#8386)
WIP Out Of Office
Return error for status command if user status is OOO
Ignore notifications if Out Of Office
Disable AutoResponder if status is set to online
Add test for AutoResponder
DisableAutoResponse when manually setting status
Remove check on status slash command
return early if user does not exists in SendAutoResponse method
Add proper error handling
Add a newline after error handling
Revert back to err == nil in api4/status.go
Remove a.Go when using a.Publish
Add name consistency with the feature auto responder
Last changes for name consistency, also fix failing test with auto_responder
Fix names of functions in auto responder test
Add ExperimentalEnableAutomaticReplies flag
Auto Responder reply to a post
Diffstat (limited to 'app/auto_responder.go')
-rw-r--r-- | app/auto_responder.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/app/auto_responder.go b/app/auto_responder.go new file mode 100644 index 000000000..23402ecd4 --- /dev/null +++ b/app/auto_responder.go @@ -0,0 +1,70 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + l4g "github.com/alecthomas/log4go" + + "github.com/mattermost/mattermost-server/model" +) + +func (a *App) SendAutoResponse(channel *model.Channel, receiver *model.User, rootId string) { + if receiver == nil || receiver.NotifyProps == nil { + return + } + + active := receiver.NotifyProps["auto_responder_active"] == "true" + message := receiver.NotifyProps["auto_responder_message"] + + if active && message != "" { + autoResponderPost := &model.Post{ + ChannelId: channel.Id, + Message: message, + RootId: rootId, + ParentId: rootId, + Type: model.POST_AUTO_RESPONDER, + UserId: receiver.Id, + } + + if _, err := a.CreatePost(autoResponderPost, channel, false); err != nil { + l4g.Error(err.Error()) + } + } +} + +func (a *App) SetAutoResponderStatus(user *model.User, oldNotifyProps model.StringMap) { + active := user.NotifyProps["auto_responder_active"] == "true" + oldActive := oldNotifyProps["auto_responder_active"] == "true" + + autoResponderEnabled := !oldActive && active + autoResponderDisabled := oldActive && !active + + if autoResponderEnabled { + a.SetStatusOutOfOffice(user.Id) + } else if autoResponderDisabled { + a.SetStatusOnline(user.Id, "", true) + } +} + +func (a *App) DisableAutoResponder(userId string, asAdmin bool) *model.AppError { + user, err := a.GetUser(userId) + if err != nil { + return err + } + + active := user.NotifyProps["auto_responder_active"] == "true" + + if active { + patch := &model.UserPatch{} + patch.NotifyProps = user.NotifyProps + patch.NotifyProps["auto_responder_active"] = "false" + + _, err := a.PatchUser(userId, patch, asAdmin) + if err != nil { + return err + } + } + + return nil +} |