-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
auto_responder.go
121 lines (96 loc) · 3.33 KB
/
auto_responder.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"net/http"
"time"
"github.com/mattermost/mattermost-server/v5/app/request"
"github.com/mattermost/mattermost-server/v5/model"
)
// check if there is any auto_response type post in channel by the user in a calender day
func (a *App) checkIfRespondedToday(createdAt int64, channelId, userId string) (bool, error) {
y, m, d := time.Unix(int64(model.GetTimeForMillis(createdAt).Second()), 0).Date()
since := model.GetMillisForTime(time.Date(y, m, d, 0, 0, 0, 0, time.UTC))
return a.Srv().Store.Post().HasAutoResponsePostByUserSince(
model.GetPostsSinceOptions{ChannelId: channelId, Time: since},
userId,
)
}
func (a *App) SendAutoResponseIfNecessary(c *request.Context, channel *model.Channel, sender *model.User, post *model.Post) (bool, *model.AppError) {
if channel.Type != model.CHANNEL_DIRECT {
return false, nil
}
if sender.IsBot {
return false, nil
}
receiverId := channel.GetOtherUserIdForDM(sender.Id)
if receiverId == "" {
// User direct messaged themself, let them test their auto-responder.
receiverId = sender.Id
}
receiver, aErr := a.GetUser(receiverId)
if aErr != nil {
return false, aErr
}
autoResponded, err := a.checkIfRespondedToday(post.CreateAt, post.ChannelId, receiverId)
if err != nil {
return false, model.NewAppError("SendAutoResponseIfNecessary", "app.user.send_auto_response.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if autoResponded {
return false, nil
}
return a.SendAutoResponse(c, channel, receiver, post)
}
func (a *App) SendAutoResponse(c *request.Context, channel *model.Channel, receiver *model.User, post *model.Post) (bool, *model.AppError) {
if receiver == nil || receiver.NotifyProps == nil {
return false, nil
}
active := receiver.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
message := receiver.NotifyProps[model.AUTO_RESPONDER_MESSAGE_NOTIFY_PROP]
if !active || message == "" {
return false, nil
}
rootID := post.Id
if post.RootId != "" {
rootID = post.RootId
}
autoResponderPost := &model.Post{
ChannelId: channel.Id,
Message: message,
RootId: rootID,
Type: model.POST_AUTO_RESPONDER,
UserId: receiver.Id,
}
if _, err := a.CreatePost(c, autoResponderPost, channel, false, false); err != nil {
return false, err
}
return true, nil
}
func (a *App) SetAutoResponderStatus(user *model.User, oldNotifyProps model.StringMap) {
active := user.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
oldActive := oldNotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "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[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
if active {
patch := &model.UserPatch{}
patch.NotifyProps = user.NotifyProps
patch.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] = "false"
_, err := a.PatchUser(userID, patch, asAdmin)
if err != nil {
return err
}
}
return nil
}