summaryrefslogtreecommitdiffstats
path: root/app/auto_responder.go
blob: aa7f243c43a5c35e873bf22aff8f6f0f64213157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"github.com/mattermost/mattermost-server/mlog"
	"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 {
			mlog.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
}