github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/plugin_api.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"net/http"
    10  	"strings"
    11  
    12  	"github.com/mattermost/mattermost-server/mlog"
    13  	"github.com/mattermost/mattermost-server/model"
    14  )
    15  
    16  type PluginAPI struct {
    17  	id       string
    18  	app      *App
    19  	logger   *mlog.SugarLogger
    20  	manifest *model.Manifest
    21  }
    22  
    23  func NewPluginAPI(a *App, manifest *model.Manifest) *PluginAPI {
    24  	return &PluginAPI{
    25  		id:       manifest.Id,
    26  		manifest: manifest,
    27  		app:      a,
    28  		logger:   a.Log.With(mlog.String("plugin_id", manifest.Id)).Sugar(),
    29  	}
    30  }
    31  
    32  func (api *PluginAPI) LoadPluginConfiguration(dest interface{}) error {
    33  	finalConfig := make(map[string]interface{})
    34  
    35  	// First set final config to defaults
    36  	if api.manifest.SettingsSchema != nil {
    37  		for _, setting := range api.manifest.SettingsSchema.Settings {
    38  			finalConfig[strings.ToLower(setting.Key)] = setting.Default
    39  		}
    40  	}
    41  
    42  	// If we have settings given we override the defaults with them
    43  	for setting, value := range api.app.Config().PluginSettings.Plugins[api.id] {
    44  		finalConfig[strings.ToLower(setting)] = value
    45  	}
    46  
    47  	if pluginSettingsJsonBytes, err := json.Marshal(finalConfig); err != nil {
    48  		api.logger.Error("Error marshaling config for plugin", mlog.Err(err))
    49  		return nil
    50  	} else {
    51  		err := json.Unmarshal(pluginSettingsJsonBytes, dest)
    52  		if err != nil {
    53  			api.logger.Error("Error unmarshaling config for plugin", mlog.Err(err))
    54  		}
    55  		return nil
    56  	}
    57  }
    58  
    59  func (api *PluginAPI) RegisterCommand(command *model.Command) error {
    60  	return api.app.RegisterPluginCommand(api.id, command)
    61  }
    62  
    63  func (api *PluginAPI) UnregisterCommand(teamId, trigger string) error {
    64  	api.app.UnregisterPluginCommand(api.id, teamId, trigger)
    65  	return nil
    66  }
    67  
    68  func (api *PluginAPI) GetSession(sessionId string) (*model.Session, *model.AppError) {
    69  	session, err := api.app.GetSessionById(sessionId)
    70  
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return session, nil
    76  }
    77  
    78  func (api *PluginAPI) GetConfig() *model.Config {
    79  	return api.app.GetConfig()
    80  }
    81  
    82  func (api *PluginAPI) SaveConfig(config *model.Config) *model.AppError {
    83  	return api.app.SaveConfig(config, true)
    84  }
    85  
    86  func (api *PluginAPI) GetServerVersion() string {
    87  	return model.CurrentVersion
    88  }
    89  
    90  func (api *PluginAPI) CreateTeam(team *model.Team) (*model.Team, *model.AppError) {
    91  	return api.app.CreateTeam(team)
    92  }
    93  
    94  func (api *PluginAPI) DeleteTeam(teamId string) *model.AppError {
    95  	return api.app.SoftDeleteTeam(teamId)
    96  }
    97  
    98  func (api *PluginAPI) GetTeams() ([]*model.Team, *model.AppError) {
    99  	return api.app.GetAllTeams()
   100  }
   101  
   102  func (api *PluginAPI) GetTeam(teamId string) (*model.Team, *model.AppError) {
   103  	return api.app.GetTeam(teamId)
   104  }
   105  
   106  func (api *PluginAPI) GetTeamByName(name string) (*model.Team, *model.AppError) {
   107  	return api.app.GetTeamByName(name)
   108  }
   109  
   110  func (api *PluginAPI) UpdateTeam(team *model.Team) (*model.Team, *model.AppError) {
   111  	return api.app.UpdateTeam(team)
   112  }
   113  
   114  func (api *PluginAPI) CreateTeamMember(teamId, userId string) (*model.TeamMember, *model.AppError) {
   115  	return api.app.AddTeamMember(teamId, userId)
   116  }
   117  
   118  func (api *PluginAPI) CreateTeamMembers(teamId string, userIds []string, requestorId string) ([]*model.TeamMember, *model.AppError) {
   119  	return api.app.AddTeamMembers(teamId, userIds, requestorId)
   120  }
   121  
   122  func (api *PluginAPI) DeleteTeamMember(teamId, userId, requestorId string) *model.AppError {
   123  	return api.app.RemoveUserFromTeam(teamId, userId, requestorId)
   124  }
   125  
   126  func (api *PluginAPI) GetTeamMembers(teamId string, offset, limit int) ([]*model.TeamMember, *model.AppError) {
   127  	return api.app.GetTeamMembers(teamId, offset, limit)
   128  }
   129  
   130  func (api *PluginAPI) GetTeamMember(teamId, userId string) (*model.TeamMember, *model.AppError) {
   131  	return api.app.GetTeamMember(teamId, userId)
   132  }
   133  
   134  func (api *PluginAPI) UpdateTeamMemberRoles(teamId, userId, newRoles string) (*model.TeamMember, *model.AppError) {
   135  	return api.app.UpdateTeamMemberRoles(teamId, userId, newRoles)
   136  }
   137  
   138  func (api *PluginAPI) CreateUser(user *model.User) (*model.User, *model.AppError) {
   139  	return api.app.CreateUser(user)
   140  }
   141  
   142  func (api *PluginAPI) DeleteUser(userId string) *model.AppError {
   143  	user, err := api.app.GetUser(userId)
   144  	if err != nil {
   145  		return err
   146  	}
   147  	_, err = api.app.UpdateActive(user, false)
   148  	return err
   149  }
   150  
   151  func (api *PluginAPI) GetUser(userId string) (*model.User, *model.AppError) {
   152  	return api.app.GetUser(userId)
   153  }
   154  
   155  func (api *PluginAPI) GetUserByEmail(email string) (*model.User, *model.AppError) {
   156  	return api.app.GetUserByEmail(email)
   157  }
   158  
   159  func (api *PluginAPI) GetUserByUsername(name string) (*model.User, *model.AppError) {
   160  	return api.app.GetUserByUsername(name)
   161  }
   162  
   163  func (api *PluginAPI) UpdateUser(user *model.User) (*model.User, *model.AppError) {
   164  	return api.app.UpdateUser(user, true)
   165  }
   166  
   167  func (api *PluginAPI) GetUserStatus(userId string) (*model.Status, *model.AppError) {
   168  	return api.app.GetStatus(userId)
   169  }
   170  
   171  func (api *PluginAPI) GetUserStatusesByIds(userIds []string) ([]*model.Status, *model.AppError) {
   172  	return api.app.GetUserStatusesByIds(userIds)
   173  }
   174  
   175  func (api *PluginAPI) UpdateUserStatus(userId, status string) (*model.Status, *model.AppError) {
   176  	switch status {
   177  	case model.STATUS_ONLINE:
   178  		api.app.SetStatusOnline(userId, true)
   179  	case model.STATUS_OFFLINE:
   180  		api.app.SetStatusOffline(userId, true)
   181  	case model.STATUS_AWAY:
   182  		api.app.SetStatusAwayIfNeeded(userId, true)
   183  	case model.STATUS_DND:
   184  		api.app.SetStatusDoNotDisturb(userId)
   185  	default:
   186  		return nil, model.NewAppError("UpdateUserStatus", "plugin.api.update_user_status.bad_status", nil, "unrecognized status", http.StatusBadRequest)
   187  	}
   188  
   189  	return api.app.GetStatus(userId)
   190  }
   191  func (api *PluginAPI) GetLDAPUserAttributes(userId string, attributes []string) (map[string]string, *model.AppError) {
   192  	if api.app.Ldap == nil {
   193  		return nil, model.NewAppError("GetLdapUserAttributes", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented)
   194  	}
   195  
   196  	user, err := api.app.GetUser(userId)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	if user.AuthService != model.USER_AUTH_SERVICE_LDAP || user.AuthData == nil {
   202  		return map[string]string{}, nil
   203  	}
   204  
   205  	return api.app.Ldap.GetUserAttributes(*user.AuthData, attributes)
   206  }
   207  
   208  func (api *PluginAPI) CreateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
   209  	return api.app.CreateChannel(channel, false)
   210  }
   211  
   212  func (api *PluginAPI) DeleteChannel(channelId string) *model.AppError {
   213  	channel, err := api.app.GetChannel(channelId)
   214  	if err != nil {
   215  		return err
   216  	}
   217  	return api.app.DeleteChannel(channel, "")
   218  }
   219  
   220  func (api *PluginAPI) GetPublicChannelsForTeam(teamId string, offset, limit int) (*model.ChannelList, *model.AppError) {
   221  	return api.app.GetPublicChannelsForTeam(teamId, offset, limit)
   222  }
   223  
   224  func (api *PluginAPI) GetChannel(channelId string) (*model.Channel, *model.AppError) {
   225  	return api.app.GetChannel(channelId)
   226  }
   227  
   228  func (api *PluginAPI) GetChannelByName(teamId, name string, includeDeleted bool) (*model.Channel, *model.AppError) {
   229  	return api.app.GetChannelByName(name, teamId, includeDeleted)
   230  }
   231  
   232  func (api *PluginAPI) GetChannelByNameForTeamName(teamName, channelName string, includeDeleted bool) (*model.Channel, *model.AppError) {
   233  	return api.app.GetChannelByNameForTeamName(channelName, teamName, includeDeleted)
   234  }
   235  
   236  func (api *PluginAPI) GetDirectChannel(userId1, userId2 string) (*model.Channel, *model.AppError) {
   237  	return api.app.GetDirectChannel(userId1, userId2)
   238  }
   239  
   240  func (api *PluginAPI) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError) {
   241  	return api.app.CreateGroupChannel(userIds, "")
   242  }
   243  
   244  func (api *PluginAPI) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
   245  	return api.app.UpdateChannel(channel)
   246  }
   247  
   248  func (api *PluginAPI) AddChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) {
   249  	// For now, don't allow overriding these via the plugin API.
   250  	userRequestorId := ""
   251  	postRootId := ""
   252  
   253  	channel, err := api.GetChannel(channelId)
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  
   258  	return api.app.AddChannelMember(userId, channel, userRequestorId, postRootId, false)
   259  }
   260  
   261  func (api *PluginAPI) GetChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) {
   262  	return api.app.GetChannelMember(channelId, userId)
   263  }
   264  
   265  func (api *PluginAPI) UpdateChannelMemberRoles(channelId, userId, newRoles string) (*model.ChannelMember, *model.AppError) {
   266  	return api.app.UpdateChannelMemberRoles(channelId, userId, newRoles)
   267  }
   268  
   269  func (api *PluginAPI) UpdateChannelMemberNotifications(channelId, userId string, notifications map[string]string) (*model.ChannelMember, *model.AppError) {
   270  	return api.app.UpdateChannelMemberNotifyProps(notifications, channelId, userId)
   271  }
   272  
   273  func (api *PluginAPI) DeleteChannelMember(channelId, userId string) *model.AppError {
   274  	return api.app.LeaveChannel(channelId, userId)
   275  }
   276  
   277  func (api *PluginAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError) {
   278  	return api.app.CreatePostMissingChannel(post, true)
   279  }
   280  
   281  func (api *PluginAPI) AddReaction(reaction *model.Reaction) (*model.Reaction, *model.AppError) {
   282  	return api.app.SaveReactionForPost(reaction)
   283  }
   284  
   285  func (api *PluginAPI) RemoveReaction(reaction *model.Reaction) *model.AppError {
   286  	return api.app.DeleteReactionForPost(reaction)
   287  }
   288  
   289  func (api *PluginAPI) GetReactions(postId string) ([]*model.Reaction, *model.AppError) {
   290  	return api.app.GetReactionsForPost(postId)
   291  }
   292  
   293  func (api *PluginAPI) SendEphemeralPost(userId string, post *model.Post) *model.Post {
   294  	return api.app.SendEphemeralPost(userId, post)
   295  }
   296  
   297  func (api *PluginAPI) DeletePost(postId string) *model.AppError {
   298  	_, err := api.app.DeletePost(postId, api.id)
   299  	return err
   300  }
   301  
   302  func (api *PluginAPI) GetPost(postId string) (*model.Post, *model.AppError) {
   303  	return api.app.GetSinglePost(postId)
   304  }
   305  
   306  func (api *PluginAPI) UpdatePost(post *model.Post) (*model.Post, *model.AppError) {
   307  	return api.app.UpdatePost(post, false)
   308  }
   309  
   310  func (api *PluginAPI) CopyFileInfos(userId string, fileIds []string) ([]string, *model.AppError) {
   311  	return api.app.CopyFileInfos(userId, fileIds)
   312  }
   313  
   314  func (api *PluginAPI) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
   315  	return api.app.GetFileInfo(fileId)
   316  }
   317  
   318  func (api *PluginAPI) ReadFile(path string) ([]byte, *model.AppError) {
   319  	return api.app.ReadFile(path)
   320  }
   321  
   322  func (api *PluginAPI) KVSet(key string, value []byte) *model.AppError {
   323  	return api.app.SetPluginKey(api.id, key, value)
   324  }
   325  
   326  func (api *PluginAPI) KVGet(key string) ([]byte, *model.AppError) {
   327  	return api.app.GetPluginKey(api.id, key)
   328  }
   329  
   330  func (api *PluginAPI) KVDelete(key string) *model.AppError {
   331  	return api.app.DeletePluginKey(api.id, key)
   332  }
   333  
   334  func (api *PluginAPI) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) {
   335  	api.app.Publish(&model.WebSocketEvent{
   336  		Event:     fmt.Sprintf("custom_%v_%v", api.id, event),
   337  		Data:      payload,
   338  		Broadcast: broadcast,
   339  	})
   340  }
   341  
   342  func (api *PluginAPI) HasPermissionTo(userId string, permission *model.Permission) bool {
   343  	return api.app.HasPermissionTo(userId, permission)
   344  }
   345  
   346  func (api *PluginAPI) HasPermissionToTeam(userId, teamId string, permission *model.Permission) bool {
   347  	return api.app.HasPermissionToTeam(userId, teamId, permission)
   348  }
   349  
   350  func (api *PluginAPI) HasPermissionToChannel(userId, channelId string, permission *model.Permission) bool {
   351  	return api.app.HasPermissionToChannel(userId, channelId, permission)
   352  }
   353  
   354  func (api *PluginAPI) LogDebug(msg string, keyValuePairs ...interface{}) {
   355  	api.logger.Debug(msg, keyValuePairs...)
   356  }
   357  func (api *PluginAPI) LogInfo(msg string, keyValuePairs ...interface{}) {
   358  	api.logger.Info(msg, keyValuePairs...)
   359  }
   360  func (api *PluginAPI) LogError(msg string, keyValuePairs ...interface{}) {
   361  	api.logger.Error(msg, keyValuePairs...)
   362  }
   363  func (api *PluginAPI) LogWarn(msg string, keyValuePairs ...interface{}) {
   364  	api.logger.Warn(msg, keyValuePairs...)
   365  }