github.com/adacta-ru/mattermost-server@v5.11.1+incompatible/app/bot.go (about)

     1  // Copyright (c) 2016-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  // CreateBot creates the given bot and corresponding user.
    12  func (a *App) CreateBot(bot *model.Bot) (*model.Bot, *model.AppError) {
    13  	result := <-a.Srv.Store.User().Save(model.UserFromBot(bot))
    14  	if result.Err != nil {
    15  		return nil, result.Err
    16  	}
    17  	bot.UserId = result.Data.(*model.User).Id
    18  
    19  	result = <-a.Srv.Store.Bot().Save(bot)
    20  	if result.Err != nil {
    21  		<-a.Srv.Store.User().PermanentDelete(bot.UserId)
    22  		return nil, result.Err
    23  	}
    24  
    25  	return result.Data.(*model.Bot), nil
    26  }
    27  
    28  // PatchBot applies the given patch to the bot and corresponding user.
    29  func (a *App) PatchBot(botUserId string, botPatch *model.BotPatch) (*model.Bot, *model.AppError) {
    30  	bot, err := a.GetBot(botUserId, true)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	bot.Patch(botPatch)
    36  
    37  	result := <-a.Srv.Store.User().Get(botUserId)
    38  	if result.Err != nil {
    39  		return nil, result.Err
    40  	}
    41  	user := result.Data.(*model.User)
    42  
    43  	patchedUser := model.UserFromBot(bot)
    44  	user.Id = patchedUser.Id
    45  	user.Username = patchedUser.Username
    46  	user.Email = patchedUser.Email
    47  	user.FirstName = patchedUser.FirstName
    48  	if result = <-a.Srv.Store.User().Update(user, true); result.Err != nil {
    49  		return nil, result.Err
    50  	}
    51  
    52  	result = <-a.Srv.Store.Bot().Update(bot)
    53  	if result.Err != nil {
    54  		return nil, result.Err
    55  	}
    56  
    57  	return result.Data.(*model.Bot), nil
    58  }
    59  
    60  // GetBot returns the given bot.
    61  func (a *App) GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) {
    62  	result := <-a.Srv.Store.Bot().Get(botUserId, includeDeleted)
    63  	if result.Err != nil {
    64  		return nil, result.Err
    65  	}
    66  
    67  	return result.Data.(*model.Bot), nil
    68  }
    69  
    70  // GetBots returns the requested page of bots.
    71  func (a *App) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError) {
    72  	result := <-a.Srv.Store.Bot().GetAll(options)
    73  	if result.Err != nil {
    74  		return nil, result.Err
    75  	}
    76  
    77  	return result.Data.([]*model.Bot), nil
    78  }
    79  
    80  // UpdateBotActive marks a bot as active or inactive, along with its corresponding user.
    81  func (a *App) UpdateBotActive(botUserId string, active bool) (*model.Bot, *model.AppError) {
    82  	result := <-a.Srv.Store.User().Get(botUserId)
    83  	if result.Err != nil {
    84  		return nil, result.Err
    85  	}
    86  	user := result.Data.(*model.User)
    87  
    88  	if _, err := a.UpdateActive(user, active); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	result = <-a.Srv.Store.Bot().Get(botUserId, true)
    93  	if result.Err != nil {
    94  		return nil, result.Err
    95  	}
    96  	bot := result.Data.(*model.Bot)
    97  
    98  	changed := true
    99  	if active && bot.DeleteAt != 0 {
   100  		bot.DeleteAt = 0
   101  	} else if !active && bot.DeleteAt == 0 {
   102  		bot.DeleteAt = model.GetMillis()
   103  	} else {
   104  		changed = false
   105  	}
   106  
   107  	if changed {
   108  		result := <-a.Srv.Store.Bot().Update(bot)
   109  		if result.Err != nil {
   110  			return nil, result.Err
   111  		}
   112  		bot = result.Data.(*model.Bot)
   113  	}
   114  
   115  	return bot, nil
   116  }
   117  
   118  // PermanentDeleteBot permanently deletes a bot and its corresponding user.
   119  func (a *App) PermanentDeleteBot(botUserId string) *model.AppError {
   120  	if result := <-a.Srv.Store.Bot().PermanentDelete(botUserId); result.Err != nil {
   121  		return result.Err
   122  	}
   123  
   124  	if result := <-a.Srv.Store.User().PermanentDelete(botUserId); result.Err != nil {
   125  		return result.Err
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  // UpdateBotOwner changes a bot's owner to the given value
   132  func (a *App) UpdateBotOwner(botUserId, newOwnerId string) (*model.Bot, *model.AppError) {
   133  	result := <-a.Srv.Store.Bot().Get(botUserId, true)
   134  	if result.Err != nil {
   135  		return nil, result.Err
   136  	}
   137  	bot := result.Data.(*model.Bot)
   138  
   139  	bot.OwnerId = newOwnerId
   140  
   141  	if result = <-a.Srv.Store.Bot().Update(bot); result.Err != nil {
   142  		return nil, result.Err
   143  	}
   144  
   145  	return result.Data.(*model.Bot), nil
   146  }
   147  
   148  // disableUserBots disables all bots owned by the given user
   149  func (a *App) disableUserBots(userId string) *model.AppError {
   150  	perPage := 20
   151  	for {
   152  		options := &model.BotGetOptions{
   153  			OwnerId:        userId,
   154  			IncludeDeleted: false,
   155  			OnlyOrphaned:   false,
   156  			Page:           0,
   157  			PerPage:        perPage,
   158  		}
   159  		userBots, err := a.GetBots(options)
   160  		if err != nil {
   161  			return err
   162  		}
   163  
   164  		for _, bot := range userBots {
   165  			_, err := a.UpdateBotActive(bot.UserId, false)
   166  			if err != nil {
   167  				mlog.Error("Unable to deactivate bot.", mlog.String("bot_user_id", bot.UserId), mlog.Err(err))
   168  			}
   169  		}
   170  
   171  		// Get next set of bots if we got the max number of bots
   172  		if len(userBots) == perPage {
   173  			options.Page += 1
   174  			continue
   175  		}
   176  		break
   177  	}
   178  
   179  	return nil
   180  }