github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/auto_responder.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "github.com/mattermost/mattermost-server/v5/mlog" 8 "github.com/mattermost/mattermost-server/v5/model" 9 ) 10 11 func (a *App) SendAutoResponseIfNecessary(channel *model.Channel, sender *model.User) (bool, *model.AppError) { 12 if channel.Type != model.CHANNEL_DIRECT { 13 return false, nil 14 } 15 16 if sender.IsBot { 17 return false, nil 18 } 19 20 receiverId := channel.GetOtherUserIdForDM(sender.Id) 21 if receiverId == "" { 22 // User direct messaged themself, let them test their auto-responder. 23 receiverId = sender.Id 24 } 25 26 receiver, err := a.GetUser(receiverId) 27 if err != nil { 28 return false, err 29 } 30 31 return a.SendAutoResponse(channel, receiver) 32 } 33 34 func (a *App) SendAutoResponse(channel *model.Channel, receiver *model.User) (bool, *model.AppError) { 35 if receiver == nil || receiver.NotifyProps == nil { 36 return false, nil 37 } 38 39 active := receiver.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true" 40 message := receiver.NotifyProps[model.AUTO_RESPONDER_MESSAGE_NOTIFY_PROP] 41 42 if !active || message == "" { 43 return false, nil 44 } 45 46 autoResponderPost := &model.Post{ 47 ChannelId: channel.Id, 48 Message: message, 49 RootId: "", 50 ParentId: "", 51 Type: model.POST_AUTO_RESPONDER, 52 UserId: receiver.Id, 53 } 54 55 if _, err := a.CreatePost(autoResponderPost, channel, false, false); err != nil { 56 mlog.Error(err.Error()) 57 return false, err 58 } 59 60 return true, nil 61 } 62 63 func (a *App) SetAutoResponderStatus(user *model.User, oldNotifyProps model.StringMap) { 64 active := user.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true" 65 oldActive := oldNotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true" 66 67 autoResponderEnabled := !oldActive && active 68 autoResponderDisabled := oldActive && !active 69 70 if autoResponderEnabled { 71 a.SetStatusOutOfOffice(user.Id) 72 } else if autoResponderDisabled { 73 a.SetStatusOnline(user.Id, true) 74 } 75 } 76 77 func (a *App) DisableAutoResponder(userId string, asAdmin bool) *model.AppError { 78 user, err := a.GetUser(userId) 79 if err != nil { 80 return err 81 } 82 83 active := user.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true" 84 85 if active { 86 patch := &model.UserPatch{} 87 patch.NotifyProps = user.NotifyProps 88 patch.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] = "false" 89 90 _, err := a.PatchUser(userId, patch, asAdmin) 91 if err != nil { 92 return err 93 } 94 } 95 96 return nil 97 }