github.com/vnforks/kid@v5.11.1+incompatible/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/mlog"
     8  	"github.com/mattermost/mattermost-server/model"
     9  )
    10  
    11  func (a *App) SendAutoResponse(channel *model.Channel, receiver *model.User) {
    12  	if receiver == nil || receiver.NotifyProps == nil {
    13  		return
    14  	}
    15  
    16  	active := receiver.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
    17  	message := receiver.NotifyProps[model.AUTO_RESPONDER_MESSAGE_NOTIFY_PROP]
    18  
    19  	if active && message != "" {
    20  		autoResponderPost := &model.Post{
    21  			ChannelId: channel.Id,
    22  			Message:   message,
    23  			RootId:    "",
    24  			ParentId:  "",
    25  			Type:      model.POST_AUTO_RESPONDER,
    26  			UserId:    receiver.Id,
    27  		}
    28  
    29  		if _, err := a.CreatePost(autoResponderPost, channel, false); err != nil {
    30  			mlog.Error(err.Error())
    31  		}
    32  	}
    33  }
    34  
    35  func (a *App) SetAutoResponderStatus(user *model.User, oldNotifyProps model.StringMap) {
    36  	active := user.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
    37  	oldActive := oldNotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
    38  
    39  	autoResponderEnabled := !oldActive && active
    40  	autoResponderDisabled := oldActive && !active
    41  
    42  	if autoResponderEnabled {
    43  		a.SetStatusOutOfOffice(user.Id)
    44  	} else if autoResponderDisabled {
    45  		a.SetStatusOnline(user.Id, true)
    46  	}
    47  }
    48  
    49  func (a *App) DisableAutoResponder(userId string, asAdmin bool) *model.AppError {
    50  	user, err := a.GetUser(userId)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	active := user.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] == "true"
    56  
    57  	if active {
    58  		patch := &model.UserPatch{}
    59  		patch.NotifyProps = user.NotifyProps
    60  		patch.NotifyProps[model.AUTO_RESPONDER_ACTIVE_NOTIFY_PROP] = "false"
    61  
    62  		_, err := a.PatchUser(userId, patch, asAdmin)
    63  		if err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	return nil
    69  }